-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[clang] Introduce SemaObjC
#89086
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[clang] Introduce SemaObjC
#89086
Conversation
@llvm/pr-subscribers-clang-modules @llvm/pr-subscribers-clang Author: Vlad Serebrennikov (Endilll) ChangesThis is continuation of efforts to split I split formatting changes into a separate commit to help reviewing the actual changes. Patch is 646.23 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/89086.diff 38 Files Affected:
diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h
index 23b268126de4e0..9e30b5f74a3dd6 100644
--- a/clang/include/clang/Parse/Parser.h
+++ b/clang/include/clang/Parse/Parser.h
@@ -18,6 +18,7 @@
#include "clang/Lex/CodeCompletionHandler.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Sema/Sema.h"
+#include "clang/Sema/SemaObjC.h"
#include "clang/Sema/SemaOpenMP.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Frontend/OpenMP/OMPContext.h"
@@ -421,8 +422,8 @@ class Parser : public CodeCompletionHandler {
/// True if we are within an Objective-C container while parsing C-like decls.
///
/// This is necessary because Sema thinks we have left the container
- /// to parse the C-like decls, meaning Actions.getObjCDeclContext() will
- /// be NULL.
+ /// to parse the C-like decls, meaning Actions.ObjC().getObjCDeclContext()
+ /// will be NULL.
bool ParsingInObjCContainer;
/// Whether to skip parsing of function bodies.
@@ -473,7 +474,7 @@ class Parser : public CodeCompletionHandler {
}
ObjCContainerDecl *getObjCDeclContext() const {
- return Actions.getObjCDeclContext();
+ return Actions.ObjC().getObjCDeclContext();
}
// Type forwarding. All of these are statically 'void*', but they may all be
@@ -1059,11 +1060,11 @@ class Parser : public CodeCompletionHandler {
: P(p), DC(p.getObjCDeclContext()),
WithinObjCContainer(P.ParsingInObjCContainer, DC != nullptr) {
if (DC)
- P.Actions.ActOnObjCTemporaryExitContainerContext(DC);
+ P.Actions.ObjC().ActOnObjCTemporaryExitContainerContext(DC);
}
~ObjCDeclContextSwitch() {
if (DC)
- P.Actions.ActOnObjCReenterContainerContext(DC);
+ P.Actions.ObjC().ActOnObjCReenterContainerContext(DC);
}
};
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 1e89dfc58d92b1..936fbffd3891e2 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -152,18 +152,9 @@ typedef ArrayRef<std::pair<IdentifierInfo *, SourceLocation>> ModuleIdPath;
class ModuleLoader;
class MultiLevelTemplateArgumentList;
class NamedDecl;
-class ObjCCategoryDecl;
-class ObjCCategoryImplDecl;
-class ObjCCompatibleAliasDecl;
-class ObjCContainerDecl;
-class ObjCImplDecl;
class ObjCImplementationDecl;
class ObjCInterfaceDecl;
-class ObjCIvarDecl;
-template <class T> class ObjCList;
-class ObjCMessageExpr;
class ObjCMethodDecl;
-class ObjCPropertyDecl;
class ObjCProtocolDecl;
struct OverloadCandidate;
enum class OverloadCandidateParamOrder : char;
@@ -178,6 +169,7 @@ class PseudoObjectExpr;
class QualType;
class SemaCUDA;
class SemaHLSL;
+class SemaObjC;
class SemaOpenACC;
class SemaOpenMP;
class SemaSYCL;
@@ -489,12 +481,9 @@ class Sema final : public SemaBase {
// 29. C++ Variadic Templates (SemaTemplateVariadic.cpp)
// 30. Constraints and Concepts (SemaConcept.cpp)
// 31. Types (SemaType.cpp)
- // 32. ObjC Declarations (SemaDeclObjC.cpp)
- // 33. ObjC Expressions (SemaExprObjC.cpp)
- // 34. ObjC @property and @synthesize (SemaObjCProperty.cpp)
- // 35. Code Completion (SemaCodeComplete.cpp)
- // 36. FixIt Helpers (SemaFixItUtils.cpp)
- // 37. Name Lookup for RISC-V Vector Intrinsic (SemaRISCVVectorLookup.cpp)
+ // 32. Code Completion (SemaCodeComplete.cpp)
+ // 33. FixIt Helpers (SemaFixItUtils.cpp)
+ // 34. Name Lookup for RISC-V Vector Intrinsic (SemaRISCVVectorLookup.cpp)
/// \name Semantic Analysis
/// Implementations are in Sema.cpp
@@ -1005,6 +994,11 @@ class Sema final : public SemaBase {
return *HLSLPtr;
}
+ SemaObjC &ObjC() {
+ assert(ObjCPtr);
+ return *ObjCPtr;
+ }
+
SemaOpenACC &OpenACC() {
assert(OpenACCPtr);
return *OpenACCPtr;
@@ -1020,6 +1014,9 @@ class Sema final : public SemaBase {
return *SYCLPtr;
}
+ /// Source of additional semantic information.
+ IntrusiveRefCntPtr<ExternalSemaSource> ExternalSource;
+
protected:
friend class Parser;
friend class InitializationSequence;
@@ -1034,9 +1031,6 @@ class Sema final : public SemaBase {
Sema(const Sema &) = delete;
void operator=(const Sema &) = delete;
- /// Source of additional semantic information.
- IntrusiveRefCntPtr<ExternalSemaSource> ExternalSource;
-
/// The handler for the FileChanged preprocessor events.
///
/// Used for diagnostics that implement custom semantic analysis for #include
@@ -1052,6 +1046,7 @@ class Sema final : public SemaBase {
std::unique_ptr<SemaCUDA> CUDAPtr;
std::unique_ptr<SemaHLSL> HLSLPtr;
+ std::unique_ptr<SemaObjC> ObjCPtr;
std::unique_ptr<SemaOpenACC> OpenACCPtr;
std::unique_ptr<SemaOpenMP> OpenMPPtr;
std::unique_ptr<SemaSYCL> SYCLPtr;
@@ -1634,11 +1629,6 @@ class Sema final : public SemaBase {
void ActOnPragmaUnused(const Token &Identifier, Scope *curScope,
SourceLocation PragmaLoc);
- /// AddCFAuditedAttribute - Check whether we're currently within
- /// '\#pragma clang arc_cf_code_audited' and, if so, consider adding
- /// the appropriate attribute.
- void AddCFAuditedAttribute(Decl *D);
-
void ActOnPragmaAttributeAttribute(ParsedAttr &Attribute,
SourceLocation PragmaLoc,
attr::ParsedSubjectMatchRuleSet Rules);
@@ -1978,12 +1968,6 @@ class Sema final : public SemaBase {
void CheckCastAlign(Expr *Op, QualType T, SourceRange TRange);
- /// checkRetainCycles - Check whether an Objective-C message send
- /// might create an obvious retain cycle.
- void checkRetainCycles(ObjCMessageExpr *msg);
- void checkRetainCycles(Expr *receiver, Expr *argument);
- void checkRetainCycles(VarDecl *Var, Expr *Init);
-
/// checkUnsafeAssigns - Check whether +1 expr is being assigned
/// to weak/__unsafe_unretained type.
bool checkUnsafeAssigns(SourceLocation Loc, QualType LHS, Expr *RHS);
@@ -2030,14 +2014,20 @@ class Sema final : public SemaBase {
bool CheckHLSLBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall);
+ void checkCall(NamedDecl *FDecl, const FunctionProtoType *Proto,
+ const Expr *ThisArg, ArrayRef<const Expr *> Args,
+ bool IsMemberFunction, SourceLocation Loc, SourceRange Range,
+ VariadicCallType CallType);
+
+ void CheckTCBEnforcement(const SourceLocation CallExprLoc,
+ const NamedDecl *Callee);
+
private:
void CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
const ArraySubscriptExpr *ASE = nullptr,
bool AllowOnePastEnd = true, bool IndexNegated = false);
void CheckArrayAccess(const Expr *E);
- bool CheckObjCMethodCall(ObjCMethodDecl *Method, SourceLocation loc,
- ArrayRef<const Expr *> Args);
bool CheckPointerCall(NamedDecl *NDecl, CallExpr *TheCall,
const FunctionProtoType *Proto);
bool CheckOtherCall(CallExpr *TheCall, const FunctionProtoType *Proto);
@@ -2050,12 +2040,6 @@ class Sema final : public SemaBase {
void CheckArgAlignment(SourceLocation Loc, NamedDecl *FDecl,
StringRef ParamName, QualType ArgTy, QualType ParamTy);
- void checkCall(NamedDecl *FDecl, const FunctionProtoType *Proto,
- const Expr *ThisArg, ArrayRef<const Expr *> Args,
- bool IsMemberFunction, SourceLocation Loc, SourceRange Range,
- VariadicCallType CallType);
-
- bool CheckObjCString(Expr *Arg);
ExprResult CheckOSLogFormatStringArg(Expr *Arg);
ExprResult CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
@@ -2228,12 +2212,6 @@ class Sema final : public SemaBase {
void CheckBitFieldInitialization(SourceLocation InitLoc, FieldDecl *Field,
Expr *Init);
- /// Check whether receiver is mutable ObjC container which
- /// attempts to add itself into the container
- void CheckObjCCircularContainer(ObjCMessageExpr *Message);
-
- void CheckTCBEnforcement(const SourceLocation CallExprLoc,
- const NamedDecl *Callee);
/// A map from magic value to type information.
std::unique_ptr<llvm::DenseMap<TypeTagMagicValue, TypeTagData>>
@@ -2636,7 +2614,7 @@ class Sema final : public SemaBase {
SmallVector<VarDecl *, 4> ExternalDeclarations;
/// Generally null except when we temporarily switch decl contexts,
- /// like in \see ActOnObjCTemporaryExitContainerContext.
+ /// like in \see SemaObjC::ActOnObjCTemporaryExitContainerContext.
DeclContext *OriginalLexicalContext;
/// Is the module scope we are in a C++ Header Unit?
@@ -2985,9 +2963,6 @@ class Sema final : public SemaBase {
SourceLocation ExplicitThisLoc = {});
ParmVarDecl *BuildParmVarDeclForTypedef(DeclContext *DC, SourceLocation Loc,
QualType T);
- QualType AdjustParameterTypeForObjCAutoRefCount(QualType T,
- SourceLocation NameLoc,
- TypeSourceInfo *TSInfo);
ParmVarDecl *CheckParameter(DeclContext *DC, SourceLocation StartLoc,
SourceLocation NameLoc,
const IdentifierInfo *Name, QualType T,
@@ -3224,8 +3199,6 @@ class Sema final : public SemaBase {
void ActOnLastBitfield(SourceLocation DeclStart,
SmallVectorImpl<Decl *> &AllIvarDecls);
- Decl *ActOnIvar(Scope *S, SourceLocation DeclStart, Declarator &D,
- Expr *BitWidth, tok::ObjCKeywordKind visibility);
// This is used for both record definitions and ObjC interface declarations.
void ActOnFields(Scope *S, SourceLocation RecLoc, Decl *TagDecl,
@@ -3247,8 +3220,6 @@ class Sema final : public SemaBase {
/// Invoked when we enter a tag definition that we're skipping.
SkippedDefinitionContext ActOnTagStartSkippedDefinition(Scope *S, Decl *TD);
- void ActOnObjCContainerStartDefinition(ObjCContainerDecl *IDecl);
-
/// ActOnStartCXXMemberDeclarations - Invoked when we have parsed a
/// C++ record definition's base-specifiers clause and are starting its
/// member declarations.
@@ -3265,15 +3236,6 @@ class Sema final : public SemaBase {
void ActOnTagFinishSkippedDefinition(SkippedDefinitionContext Context);
- void ActOnObjCContainerFinishDefinition();
-
- /// Invoked when we must temporarily exit the objective-c container
- /// scope for parsing/looking-up C constructs.
- ///
- /// Must be followed by a call to \see ActOnObjCReenterContainerContext
- void ActOnObjCTemporaryExitContainerContext(ObjCContainerDecl *ObjCCtx);
- void ActOnObjCReenterContainerContext(ObjCContainerDecl *ObjCCtx);
-
/// ActOnTagDefinitionError - Invoked when there was an unrecoverable
/// error parsing the definition of a tag.
void ActOnTagDefinitionError(Scope *S, Decl *TagDecl);
@@ -3400,10 +3362,6 @@ class Sema final : public SemaBase {
/// variable.
void DiagnoseUnusedButSetDecl(const VarDecl *VD, DiagReceiverTy DiagReceiver);
- ObjCInterfaceDecl *getObjCInterfaceDecl(const IdentifierInfo *&Id,
- SourceLocation IdLoc,
- bool TypoCorrection = false);
-
Scope *getNonFieldDeclScope(Scope *S);
FunctionDecl *CreateBuiltin(IdentifierInfo *II, QualType Type, unsigned ID,
@@ -3425,8 +3383,6 @@ class Sema final : public SemaBase {
/// Look for a locally scoped extern "C" declaration by the given name.
NamedDecl *findLocallyScopedExternCDecl(DeclarationName Name);
- bool inferObjCARCLifetime(ValueDecl *decl);
-
void deduceOpenCLAddressSpace(ValueDecl *decl);
static bool adjustContextForLocalExternDecl(DeclContext *&DC);
@@ -3498,8 +3454,6 @@ class Sema final : public SemaBase {
SourceLocation WeakNameLoc,
SourceLocation AliasNameLoc);
- ObjCContainerDecl *getObjCDeclContext() const;
-
/// Status of the function emission on the CUDA/HIP/OpenMP host/device attrs.
enum class FunctionEmissionStatus {
Emitted,
@@ -4338,8 +4292,6 @@ class Sema final : public SemaBase {
CXXConstructorDecl *Constructor, bool AnyErrors,
ArrayRef<CXXCtorInitializer *> Initializers = std::nullopt);
- void SetIvarInitializers(ObjCImplementationDecl *ObjCImplementation);
-
/// MarkBaseAndMemberDestructorsReferenced - Given a record decl,
/// mark all the non-trivial destructors of its members and bases as
/// referenced.
@@ -5389,14 +5341,6 @@ class Sema final : public SemaBase {
DeclContext *LookupCtx = nullptr,
TypoExpr **Out = nullptr);
- DeclResult LookupIvarInObjCMethod(LookupResult &Lookup, Scope *S,
- IdentifierInfo *II);
- ExprResult BuildIvarRefExpr(Scope *S, SourceLocation Loc, ObjCIvarDecl *IV);
-
- ExprResult LookupInObjCMethod(LookupResult &LookUp, Scope *S,
- IdentifierInfo *II,
- bool AllowBuiltinCreation = false);
-
/// If \p D cannot be odr-used in the current expression evaluation context,
/// return a reason explaining why. Otherwise, return NOUR_None.
NonOdrUseReason getNonOdrUseReasonInCurrentContext(ValueDecl *D);
@@ -5714,19 +5658,6 @@ class Sema final : public SemaBase {
ArrayRef<Expr *> SubExprs,
QualType T = QualType());
- // Note that LK_String is intentionally after the other literals, as
- // this is used for diagnostics logic.
- enum ObjCLiteralKind {
- LK_Array,
- LK_Dictionary,
- LK_Numeric,
- LK_Boxed,
- LK_String,
- LK_Block,
- LK_None
- };
- ObjCLiteralKind CheckLiteralKind(Expr *FromE);
-
ExprResult PerformObjectMemberConversion(Expr *From,
NestedNameSpecifier *Qualifier,
NamedDecl *FoundDecl,
@@ -5762,14 +5693,6 @@ class Sema final : public SemaBase {
bool IsInvalidSMECallConversion(QualType FromType, QualType ToType);
- const DeclContext *getCurObjCLexicalContext() const {
- const DeclContext *DC = getCurLexicalContext();
- // A category implicitly has the attribute of the interface.
- if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(DC))
- DC = CatD->getClassInterface();
- return DC;
- }
-
/// Abstract base class used for diagnosing integer constant
/// expression violations.
class VerifyICEDiagnoser {
@@ -5907,9 +5830,6 @@ class Sema final : public SemaBase {
ExprResult &Cond, ExprResult &LHS, ExprResult &RHS, ExprValueKind &VK,
ExprObjectKind &OK, SourceLocation QuestionLoc);
- QualType FindCompositeObjCPointerType(ExprResult &LHS, ExprResult &RHS,
- SourceLocation QuestionLoc);
-
bool DiagnoseConditionalForNull(const Expr *LHSExpr, const Expr *RHSExpr,
SourceLocation QuestionLoc);
@@ -6257,9 +6177,6 @@ class Sema final : public SemaBase {
Expr *LHSExpr, ExprResult &RHS, SourceLocation Loc, QualType CompoundType,
BinaryOperatorKind Opc);
- bool CheckConversionToObjCLiteral(QualType DstType, Expr *&SrcExpr,
- bool Diagnose = true);
-
/// To be used for checking whether the arguments being passed to
/// function exceeds the number of parameters expected for it.
static bool TooManyArguments(size_t NumParams, size_t NumArgs,
@@ -6589,13 +6506,6 @@ class Sema final : public SemaBase {
/// ActOnCXXBoolLiteral - Parse {true,false} literals.
ExprResult ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind);
- /// ActOnObjCBoolLiteral - Parse {__objc_yes,__objc_no} literals.
- ExprResult ActOnObjCBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind);
-
- ExprResult
- ActOnObjCAvailabilityCheckExpr(llvm::ArrayRef<AvailabilitySpec> AvailSpecs,
- SourceLocation AtLoc, SourceLocation RParen);
-
/// ActOnCXXNullPtrLiteral - Parse 'nullptr'.
ExprResult ActOnCXXNullPtrLiteral(SourceLocation Loc);
@@ -7466,9 +7376,6 @@ class Sema final : public SemaBase {
bool LookupParsedName(LookupResult &R, Scope *S, CXXScopeSpec *SS,
bool AllowBuiltinCreation = false,
bool EnteringContext = false);
- ObjCProtocolDecl *LookupProtocol(
- IdentifierInfo *II, SourceLocation IdLoc,
- RedeclarationKind Redecl = RedeclarationKind::NotForRedeclaration);
bool LookupInSuper(LookupResult &R, CXXRecordDecl *Class);
void LookupOverloadedOperatorName(OverloadedOperatorKind Op, Scope *S,
@@ -7497,6 +7404,20 @@ class Sema final : public SemaBase {
/// visible at the specified location.
void makeMergedDefinitionVisible(NamedDecl *ND);
+ /// Check ODR hashes for C/ObjC when merging types from modules.
+ /// Differently from C++, actually parse the body and reject in case
+ /// of a mismatch.
+ template <typename T,
+ typename = std::enable_if_t<std::is_base_of<NamedDecl, T>::value>>
+ bool ActOnDuplicateODRHashDefinition(T *Duplicate, T *Previous) {
+ if (Duplicate->getODRHash() != Previous->getODRHash())
+ return false;
+
+ // Make the previous decl visible.
+ makeMergedDefinitionVisible(Previous);
+ return true;
+ }
+
/// Get the set of additional modules that should be checked during
/// name lookup. A module and its imports become visible when instanting a
/// template defined within it.
@@ -7962,8 +7883,6 @@ class Sema final : public SemaBase {
bool &IncompatibleObjC);
bool isObjCPointerConversion(QualType FromType, QualType ToType,
QualType &ConvertedType, bool &IncompatibleObjC);
- bool isObjCWritebackConversion(QualType FromType, QualType ToType,
- QualType &ConvertedType);
bool IsBlockPointerConversion(QualType FromType, QualType ToType,
QualType &ConvertedType);
@@ -8425,7 +8344,6 @@ class Sema final : public SemaBase {
DeclAccessPair FoundDecl,
FunctionDecl *Fn);
-private:
/// - Returns a selector which best matches given argument list or
/// nullptr if none could be found
ObjCMethodDecl *SelectBestMethod(Selector Sel, MultiExprArg Args,
@@ -8446,10 +8364,6 @@ class Sema final : public SemaBase {
public:
void maybeExtendBlockObject(ExprResult &E);
- CastKind PrepareCastToObjCObjectPointer(ExprResult &E);
-
- enum ObjCSubscriptKind { OS_Array, OS_Dictionary, OS_Error };
- ObjCSubscriptKind CheckSubscriptingKind(Expr *FromE);
ExprResult checkPseudoObjectIncDec(Scope *S, SourceLocation OpLoc,
UnaryOperatorKind Opcode, Expr *Op);
@@ -8557,13 +8471,6 @@ class Sema final : public SemaBase {
StmtResult ActOnForEachLValueExpr(Expr *E);
- ExprResult CheckObjCForCollectionOperand(SourceLocation forLoc,
- Expr *collection);
- StmtResult ActOnObjCForCollectionStmt(SourceLocation ForColLoc, Stmt *First,
- Expr *collection,
- SourceLocation RParenLoc);
- StmtResult FinishObjCForCollectionStmt(Stmt *ForCollection, Stmt *Body);
-
enum BuildForRangeKind {
/// Initial building of a for-range statement.
BFRK_Build,
@@ -8630,24 +8537,6 @@ class Sema final : public SemaBase {
NamedReturnInfo &NRInfo,
bool SupressSimplerImplicitMoves);
- StmtResult ActOnObjCAtCatchStmt(SourceLocation AtLoc, SourceLocation RParen,
- Decl *Parm, Stmt *Body);
-
- StmtResult ActOnObjCAtFinallyStmt(SourceLocation AtLoc, Stmt *Body);
-
- StmtResult ActOnObjCAtTryStmt(SourceLocation AtLoc, Stmt *Try,
- MultiStmtArg Catch, Stmt *Finally);
-
- StmtResult BuildObjCAtThrowStmt(SourceLocation AtLoc, Expr *Throw);
- StmtResult ActOnObjCAtThrowStmt(SourceLocation AtLoc, Expr *Throw,
- Scope *CurS...
[truncated]
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
Removing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! CC @rjmccall for any last-minute concerns.
This is continuation of efforts to split
Sema
up, following the example of OpenMP, OpenACC, etc. Context can be found in #82217 and #84184.I split formatting changes into a separate commit to help reviewing the actual changes.