15 #ifndef LLVM_CLANG_SEMA_SCOPEINFO_H 16 #define LLVM_CLANG_SEMA_SCOPEINFO_H 25 #include "llvm/ADT/DenseMap.h" 26 #include "llvm/ADT/DenseMapInfo.h" 27 #include "llvm/ADT/MapVector.h" 28 #include "llvm/ADT/PointerIntPair.h" 29 #include "llvm/ADT/SmallPtrSet.h" 30 #include "llvm/ADT/SmallSet.h" 31 #include "llvm/ADT/SmallVector.h" 32 #include "llvm/ADT/StringRef.h" 33 #include "llvm/ADT/StringSwitch.h" 34 #include "llvm/ADT/TinyPtrVector.h" 35 #include "llvm/Support/Casting.h" 36 #include "llvm/Support/ErrorHandling.h" 47 class ImplicitParamDecl;
49 class ObjCIvarRefExpr;
50 class ObjCMessageExpr;
51 class ObjCPropertyDecl;
52 class ObjCPropertyRefExpr;
59 class TemplateParameterList;
60 class TemplateTypeParmDecl;
80 HasEmptyLoopBodies =
true;
92 : PD(PD), Loc(Loc), stmt(stmt) {}
181 using SwitchInfo = llvm::PointerIntPair<SwitchStmt*, 1, bool>;
207 llvm::SmallPtrSet<const BlockDecl *, 1>
Blocks;
251 using BaseInfoTy = llvm::PointerIntPair<const NamedDecl *, 1, bool>;
261 static BaseInfoTy getBaseInfo(
const Expr *BaseE);
287 return Base.getInt();
291 return Base == Other.Base && Property == Other.Property;
305 return WeakObjectProfileTy::getSentinel();
309 using Pair = std::pair<BaseInfoTy, const NamedDecl *>;
329 llvm::PointerIntPair<const Expr *, 1, bool> Rep;
339 return Rep == Other.Rep;
366 :
Kind(SK_Function), HasBranchProtectedScope(
false),
367 HasBranchIntoScope(
false), HasIndirectGoto(
false),
368 HasDroppedStmt(
false), HasOMPDeclareReductionCombiner(
false),
369 HasFallthroughStmt(
false), HasPotentialAvailabilityViolations(
false),
370 ObjCShouldCallSuper(
false), ObjCIsDesignatedInit(
false),
371 ObjCWarnForNoDesignatedInitChain(
false), ObjCIsSecondaryInit(
false),
372 ObjCWarnForNoInitDelegation(
false), NeedsCoroutineSuspends(
true),
380 template <
typename ExprT>
381 inline void recordUseOfWeak(
const ExprT *E,
bool IsRead =
true);
390 void markSafeWeakUse(
const Expr *E);
393 return WeakObjectUses;
397 HasBranchIntoScope =
true;
401 HasBranchProtectedScope =
true;
405 HasIndirectGoto =
true;
409 HasDroppedStmt =
true;
413 HasOMPDeclareReductionCombiner =
true;
417 HasFallthroughStmt =
true;
421 setHasBranchProtectedScope();
422 FirstCXXTryLoc = TryLoc;
426 setHasBranchProtectedScope();
427 FirstSEHTryLoc = TryLoc;
431 return !HasDroppedStmt &&
433 (HasBranchProtectedScope && HasBranchIntoScope));
443 ByrefBlockVars.push_back(VD);
449 assert(FirstCoroutineStmtLoc.
isInvalid() &&
450 "first coroutine statement location already set");
451 FirstCoroutineStmtLoc = Loc;
452 FirstCoroutineStmtKind = llvm::StringSwitch<unsigned char>(Keyword)
453 .Case(
"co_return", 0)
455 .Case(
"co_yield", 2);
459 assert(FirstCoroutineStmtLoc.
isValid()
460 &&
"no coroutine statement available");
461 switch (FirstCoroutineStmtKind) {
462 case 0:
return "co_return";
463 case 1:
return "co_await";
464 case 2:
return "co_yield";
466 llvm_unreachable(
"FirstCoroutineStmtKind has an invalid value");
471 assert((!value || CoroutineSuspends.first ==
nullptr) &&
472 "we already have valid suspend points");
473 NeedsCoroutineSuspends = value;
477 return !NeedsCoroutineSuspends && CoroutineSuspends.first ==
nullptr;
481 assert(Initial && Final &&
"suspend points cannot be null");
482 assert(CoroutineSuspends.first ==
nullptr &&
"suspend points already set");
483 NeedsCoroutineSuspends =
false;
484 CoroutineSuspends.first = Initial;
485 CoroutineSuspends.second = Final;
508 Cap_ByCopy, Cap_ByRef, Cap_Block, Cap_VLA
511 IsNestedCapture = 0x1,
517 llvm::PointerIntPair<VarDecl*, 2> VarAndNestedAndThis;
523 llvm::PointerIntPair<void *, 2, CaptureKind> InitExprAndCaptureKind;
537 bool ODRUsed =
false;
541 bool NonODRUsed =
false;
547 : VarAndNestedAndThis(Var, IsNested ? IsNestedCapture : 0),
548 InitExprAndCaptureKind(
549 Cpy, !Var ? Cap_VLA : Block ? Cap_Block : ByRef ? Cap_ByRef
551 Loc(Loc), EllipsisLoc(EllipsisLoc), CaptureType(CaptureType) {}
556 : VarAndNestedAndThis(
557 nullptr, (IsThisCaptured | (IsNested ? IsNestedCapture : 0))),
558 InitExprAndCaptureKind(Cpy, ByCopy ? Cap_ByCopy : Cap_ByRef),
559 Loc(Loc), CaptureType(CaptureType) {}
562 return VarAndNestedAndThis.getInt() & IsThisCaptured;
566 return !isThisCapture() && !isVLATypeCapture();
570 return InitExprAndCaptureKind.getInt() == Cap_ByCopy;
574 return InitExprAndCaptureKind.getInt() == Cap_ByRef;
578 return InitExprAndCaptureKind.getInt() == Cap_Block;
582 return InitExprAndCaptureKind.getInt() == Cap_VLA;
586 return VarAndNestedAndThis.getInt() & IsNestedCapture;
591 void markUsed(
bool IsODRUse) { (IsODRUse ? ODRUsed : NonODRUsed) =
true; }
594 assert(isVariableCapture());
595 return VarAndNestedAndThis.getPointer();
609 assert(!isThisCapture());
614 assert(!isVLATypeCapture() &&
"no init expression for type capture");
615 return static_cast<Expr *
>(InitExprAndCaptureKind.getPointer());
625 ImpCap_None, ImpCap_LambdaByval, ImpCap_LambdaByref, ImpCap_Block,
626 ImpCap_CapturedRegion
639 unsigned CXXThisCaptureIndex = 0;
646 bool HasImplicitReturnType =
false;
655 Captures.push_back(
Capture(Var, isBlock, isByref, isNested, Loc,
656 EllipsisLoc, CaptureType, Cpy));
657 CaptureMap[Var] = Captures.size();
661 Captures.push_back(
Capture(
nullptr,
false,
671 Expr *Cpy,
bool ByCopy);
678 assert(isCXXThisCaptured() &&
"this has not been captured");
679 return Captures[CXXThisCaptureIndex - 1];
684 return CaptureMap.count(Var);
693 assert(isCaptured(Var) &&
"Variable has not been captured");
694 return Captures[CaptureMap[Var] - 1];
698 llvm::DenseMap<VarDecl*, unsigned>::const_iterator Known
699 = CaptureMap.find(Var);
700 assert(Known != CaptureMap.end() &&
"Variable has not been captured");
701 return Captures[Known->second - 1];
705 return FSI->
Kind == SK_Block || FSI->
Kind == SK_Lambda
706 || FSI->
Kind == SK_CapturedRegion;
725 TheScope(BlockScope) {
732 return FSI->
Kind == SK_Block;
760 TheCapturedDecl(CD), TheRecordDecl(RD), TheScope(S),
761 ContextParam(Context), CapRegionKind(K), OpenMPLevel(OpenMPLevel) {
762 Kind = SK_CapturedRegion;
769 switch (CapRegionKind) {
771 return "default captured statement";
773 return "Objective-C @finally statement";
775 return "OpenMP region";
777 llvm_unreachable(
"Invalid captured region kind!");
781 return FSI->
Kind == SK_CapturedRegion;
802 unsigned NumExplicitCaptures = 0;
805 bool Mutable =
false;
808 bool ExplicitParams =
false;
814 bool ContainsUnexpandedParameterPack =
false;
818 unsigned AutoTemplateParameterDepth = 0;
874 NumExplicitCaptures = Captures.size();
878 return FSI->
Kind == SK_Lambda;
884 return !AutoTemplateParams.empty() || GLTemplateParameterList;
905 assert(isa<DeclRefExpr>(VarExpr) || isa<MemberExpr>(VarExpr));
906 PotentiallyCapturingExprs.push_back(VarExpr);
910 PotentialThisCaptureLocation = Loc;
914 return PotentialThisCaptureLocation.
isValid();
957 assert(isa<DeclRefExpr>(CapturingVarExpr)
958 || isa<MemberExpr>(CapturingVarExpr));
959 NonODRUsedCapturingExprs.insert(CapturingVarExpr);
962 assert(isa<DeclRefExpr>(CapturingVarExpr)
963 || isa<MemberExpr>(CapturingVarExpr));
964 return NonODRUsedCapturingExprs.count(CapturingVarExpr);
967 PotentiallyCapturingExprs.erase(
968 std::remove(PotentiallyCapturingExprs.begin(),
969 PotentiallyCapturingExprs.end(), E),
970 PotentiallyCapturingExprs.end());
973 PotentiallyCapturingExprs.clear();
977 return PotentiallyCapturingExprs.size();
981 return getNumPotentialVariableCaptures() ||
982 PotentialThisCaptureLocation.
isValid();
987 void getPotentialVariableCapture(
unsigned Idx,
VarDecl *&VD,
Expr *&E)
const;
990 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy()
991 :
Base(
nullptr,
false) {}
994 FunctionScopeInfo::WeakObjectProfileTy::getSentinel() {
996 Result.Base.setInt(
true);
1000 template <
typename ExprT>
1010 const bool ByCopy) {
1013 CXXThisCaptureIndex = Captures.size();
1020 #endif // LLVM_CLANG_SEMA_SCOPEINFO_H ObjCPropertyRefExpr - A dot-syntax expression to access an ObjC property.
void clearPotentialCaptures()
SourceRange IntroducerRange
Source range covering the lambda introducer [...].
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
A (possibly-)qualified type.
void addThisCapture(bool isNested, SourceLocation Loc, Expr *Cpy, bool ByCopy)
const NamedDecl * getProperty() const
bool HasFallthroughStmt
Whether there is a fallthrough statement in this function.
bool HasEmptyLoopBodies
Whether this compound stamement contains `for' or `while' loops with empty bodies.
void setNeedsCoroutineSuspends(bool value=true)
Stmt - This represents one statement.
QualType ReturnType
ReturnType - The target type of return statements in this context, or null if unknown.
C Language Family Type Representation.
bool NeedsScopeChecking() const
QualType getCaptureType() const
Retrieve the capture type for this capture, which is effectively the type of the non-static data memb...
bool hasPotentialCaptures() const
bool operator==(const WeakUseTy &Other) const
bool isCopyCapture() const
const WeakObjectUseMap & getWeakObjectUses() const
static WeakObjectProfileTy getTombstoneKey()
StringRef getFirstCoroutineStmtKeyword() const
static unsigned getHashValue(const WeakObjectProfileTy &Val)
bool isVariableExprMarkedAsNonODRUsed(Expr *CapturingVarExpr) const
std::pair< Stmt *, Stmt * > CoroutineSuspends
The initial and final coroutine suspend points.
static bool classof(const FunctionScopeInfo *FSI)
Retains information about a function, method, or block that is currently being parsed.
Represents a variable declaration or definition.
static bool classof(const FunctionScopeInfo *FSI)
bool hasPotentialThisCapture() const
static bool classof(const FunctionScopeInfo *FSI)
RAII class that determines when any errors have occurred between the time the instance was created an...
Stores a list of template parameters for a TemplateDecl and its derived classes.
llvm::SmallVector< ShadowedOuterDecl, 4 > ShadowingDecls
llvm::PointerIntPair< SwitchStmt *, 1, bool > SwitchInfo
A SwitchStmt, along with a flag indicating if its list of case statements is incomplete (because we d...
bool NeedsCoroutineSuspends
True only when this function has not already built, or attempted to build, the initial and final coro...
void setHasBranchProtectedScope()
CapturedDecl * TheCapturedDecl
The CapturedDecl for this statement.
bool HasDroppedStmt
Whether a statement was dropped because it was invalid.
llvm::SmallMapVector< ParmVarDecl *, Stmt *, 4 > CoroutineParameterMoves
A mapping between the coroutine function parameters that were moved to the coroutine frame...
Represents a struct/union/class.
unsigned char FirstCoroutineStmtKind
An enumeration represeting the kind of the first coroutine statement in the function.
void setHasIndirectGoto()
void setHasOMPDeclareReductionCombiner()
void addByrefBlockVar(VarDecl *VD)
ScopeKind Kind
What kind of scope we are describing.
Scope * TheScope
This is the enclosing scope of the captured region.
llvm::SmallSet< Expr *, 8 > NonODRUsedCapturingExprs
Contains all variable-referring-expressions that refer to local variables that are usable as constant...
llvm::SmallPtrSet< const BlockDecl *, 1 > Blocks
The set of blocks that are introduced in this function.
SourceLocation FirstSEHTryLoc
First SEH '__try' statement in the current function.
void setHasBranchIntoScope()
bool operator==(const WeakObjectProfileTy &Other) const
DiagnosticErrorTrap ErrorTrap
Used to determine if errors occurred in this function or block.
bool isThisCapture() const
Expr * getInitExpr() const
CompoundScopeInfo(bool IsStmtExpr)
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
WeakUseTy(const Expr *Use, bool IsRead)
Concrete class used by the front-end to report problems and issues.
QualType FunctionType
BlockType - The function type of the block, if one was given.
void finishedExplicitCaptures()
Note when all explicit captures have been added.
bool IsStmtExpr
Whether this compound statement corresponds to a GNU statement expression.
static WeakObjectProfileTy getEmptyKey()
Scope - A scope is a transient data structure that is used while parsing the program.
FunctionScopeInfo(DiagnosticsEngine &Diag)
Represents the body of a CapturedStmt, and serves as its DeclContext.
PossiblyUnreachableDiag(const PartialDiagnostic &PD, SourceLocation Loc, const Stmt *stmt)
SmallVector< TemplateTypeParmDecl *, 4 > AutoTemplateParams
Store the list of the auto parameters for a generic lambda.
Contains information about the compound statement currently being parsed.
SourceLocation FirstCXXTryLoc
First C++ 'try' statement in the current function.
CleanupInfo Cleanup
Whether any of the capture expressions requires cleanups.
ImplicitCaptureStyle ImpCaptureStyle
void addPotentialCapture(Expr *VarExpr)
Add a variable that might potentially be captured by the lambda and therefore the enclosing lambdas...
void markVariableExprAsNonODRUsed(Expr *CapturingVarExpr)
Mark a variable's reference in a lambda as non-odr using.
SmallVector< ReturnStmt *, 4 > Returns
The list of return statements that occur within the function or block, if there is any chance of appl...
Retains information about a captured region.
void recordUseOfWeak(const ExprT *E, bool IsRead=true)
Record that a weak object was accessed.
SourceLocation getEllipsisLoc() const
Retrieve the source location of the ellipsis, whose presence indicates that the capture is a pack exp...
SourceLocation PotentialThisCaptureLocation
CapturedRegionScopeInfo(DiagnosticsEngine &Diag, Scope *S, CapturedDecl *CD, RecordDecl *RD, ImplicitParamDecl *Context, CapturedRegionKind K, unsigned OpenMPLevel)
bool isVariableCapture() const
Retains information about a block that is currently being parsed.
Pepresents a block literal declaration, which is like an unnamed FunctionDecl.
This represents one expression.
void removePotentialCapture(Expr *E)
SmallVector< SwitchInfo, 8 > SwitchStack
SwitchStack - This is the current set of active switch statements in the block.
bool HasBranchProtectedScope
Whether this function contains a VLA, @try, try, C++ initializer, or anything else that can't be jump...
llvm::DenseMap< unsigned, SourceRange > ExplicitCaptureRanges
A map of explicit capture indices to their introducer source ranges.
bool isCaptured(VarDecl *Var) const
Determine whether the given variable has been captured.
const Expr * getUseExpr() const
Capture & getCapture(VarDecl *Var)
Retrieve the capture of the given variable, if it has been captured already.
An expression that sends a message to the given Objective-C object or class.
void setHasCXXTry(SourceLocation TryLoc)
unsigned short CapRegionKind
The kind of captured region.
SourceLocation getLocation() const
Retrieve the location at which this variable was captured.
CapturingScopeInfo(DiagnosticsEngine &Diag, ImplicitCaptureStyle Style)
VarDecl * getVariable() const
void setHasSEHTry(SourceLocation TryLoc)
bool HasPotentialAvailabilityViolations
Whether we make reference to a declaration that could be unavailable.
Encodes a location in the source.
bool ObjCWarnForNoDesignatedInitChain
This starts true for a method marked as designated initializer and will be set to false if there is a...
Represents a static or instance method of a struct/union/class.
bool isVLATypeCapture() const
SmallVector< Capture, 4 > Captures
Captures - The captures.
SourceLocation FirstCoroutineStmtLoc
First coroutine statement in the current function.
Represents one property declaration in an Objective-C interface.
void addPotentialThisCapture(SourceLocation Loc)
bool ObjCIsDesignatedInit
True when this is a method marked as a designated initializer.
bool ObjCShouldCallSuper
A flag that is set when parsing a method that must call super's implementation, such as -dealloc...
SourceLocation CaptureDefaultLoc
Source location of the '&' or '=' specifying the default capture type, if any.
const VarDecl * ShadowedDecl
bool HasOMPDeclareReductionCombiner
True if current scope is for OpenMP declare reduction combiner.
llvm::SmallVector< Expr *, 4 > PotentiallyCapturingExprs
Contains all variable-referring-expressions (i.e.
bool isGenericLambda() const
Is this scope known to be for a generic lambda? (This will be false until we parse the first 'auto'-t...
Dataflow Directional Tag Classes.
bool isValid() const
Return true if this is a valid SourceLocation object.
bool ObjCIsSecondaryInit
True when this is an initializer method not marked as a designated initializer within a class that ha...
bool HasIndirectGoto
Whether this function contains any indirect gotos.
const Capture & getCapture(VarDecl *Var) const
StringRef getRegionName() const
A descriptive name for the kind of captured region this is.
Represents a simple identification of a weak object.
bool ObjCWarnForNoInitDelegation
This starts true for a secondary initializer method and will be set to false if there is an invocatio...
const NamedDecl * getBase() const
BlockScopeInfo(DiagnosticsEngine &Diag, Scope *BlockScope, BlockDecl *Block)
unsigned short OpenMPLevel
LambdaScopeInfo(DiagnosticsEngine &Diag)
SourceLocation FirstReturnLoc
First 'return' statement in the current function.
Contains all of the variables defined in this lambda that shadow variables that were defined in paren...
RecordDecl * TheRecordDecl
The captured record type.
bool isReferenceCapture() const
void setFirstCoroutineStmt(SourceLocation Loc, StringRef Keyword)
Implements a partial diagnostic that can be emitted anwyhere in a DiagnosticBuilder stream...
llvm::DenseMap< VarDecl *, unsigned > CaptureMap
CaptureMap - A map of captured variables to (index+1) into Captures.
unsigned getNumPotentialVariableCaptures() const
Capture & getCXXThisCapture()
Retrieve the capture of C++ 'this', if it has been captured.
ObjCIvarRefExpr - A reference to an ObjC instance variable.
static bool isEqual(const WeakObjectProfileTy &LHS, const WeakObjectProfileTy &RHS)
Defines the clang::SourceLocation class and associated facilities.
SmallVector< PossiblyUnreachableDiag, 4 > PossiblyUnreachableDiags
A list of PartialDiagnostics created but delayed within the current function scope.
Represents a C++ struct/union/class.
void markUsed(bool IsODRUse)
void setHasEmptyLoopBodies()
Capture(IsThisCapture, bool IsNested, SourceLocation Loc, QualType CaptureType, Expr *Cpy, const bool ByCopy)
static bool classof(const FunctionScopeInfo *FSI)
llvm::SmallPtrSet< const ParmVarDecl *, 8 > ModifiedNonNullParams
A list of parameters which have the nonnull attribute and are modified in the function.
bool isCXXThisCaptured() const
Determine whether the C++ 'this' is captured.
void addVLATypeCapture(SourceLocation Loc, QualType CaptureType)
Represents a single use of a weak object.
Scope * TheScope
TheScope - This is the scope for the block itself, which contains arguments etc.
void addBlock(const BlockDecl *BD)
llvm::SmallDenseMap< WeakObjectProfileTy, WeakUseVector, 8, WeakObjectProfileTy::DenseMapInfo > WeakObjectUseMap
Used to collect all uses of weak objects in a function body.
A reference to a declared variable, function, enum, etc.
CapturedRegionKind
The different kinds of captured statement.
bool isNonODRUsed() const
void addCapture(VarDecl *Var, bool isBlock, bool isByref, bool isNested, SourceLocation Loc, SourceLocation EllipsisLoc, QualType CaptureType, Expr *Cpy)
bool hasInvalidCoroutineSuspends() const
ImplicitParamDecl * ContextParam
The implicit parameter for the captured variables.
SmallVector< CompoundScopeInfo, 4 > CompoundScopes
The stack of currently active compound stamement scopes in the function.
bool isExactProfile() const
Returns true if the object base specifies a known object in memory, rather than, say, an instance variable or property of another object.
A trivial tuple used to represent a source range.
llvm::TinyPtrVector< VarDecl * > ByrefBlockVars
The set of __block variables that are introduced in this function.
This represents a decl that may have a name.
Represents a C array with a specified size that is not an integer-constant-expression.
Capture(VarDecl *Var, bool Block, bool ByRef, bool IsNested, SourceLocation Loc, SourceLocation EllipsisLoc, QualType CaptureType, Expr *Cpy)
bool HasBranchIntoScope
Whether this function contains any switches or direct gotos.
void setHasFallthroughStmt()
bool isBlockCapture() const
void setCoroutineSuspends(Stmt *Initial, Stmt *Final)