14 #ifndef LLVM_CLANG_SEMA_SCOPEINFO_H 15 #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;
88 llvm::TinyPtrVector<const Stmt*>
Stmts;
92 : PD(PD), Loc(Loc), Stmts(Stmts) {}
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;
510 Cap_ByCopy, Cap_ByRef, Cap_Block, Cap_VLA
539 unsigned CapturesThis : 1;
543 unsigned ODRUsed : 1;
547 unsigned NonODRUsed : 1;
551 unsigned Invalid : 1;
557 : CapturedVar(Var), Loc(Loc), EllipsisLoc(EllipsisLoc),
558 CaptureType(CaptureType),
559 Kind(Block ? Cap_Block : ByRef ? Cap_ByRef : Cap_ByCopy),
560 Nested(IsNested), CapturesThis(
false), ODRUsed(
false),
561 NonODRUsed(
false), Invalid(Invalid) {}
565 QualType CaptureType,
const bool ByCopy,
bool Invalid)
566 : Loc(Loc), CaptureType(CaptureType),
567 Kind(ByCopy ? Cap_ByCopy : Cap_ByRef), Nested(IsNested),
574 : CapturedVLA(VLA), Loc(Loc), CaptureType(CaptureType),
Kind(Cap_VLA),
575 Nested(IsNested), CapturesThis(
false), ODRUsed(
false),
580 return !isThisCapture() && !isVLATypeCapture();
593 bool isInitCapture()
const;
605 assert(isVariableCapture());
610 assert(isVLATypeCapture());
633 ImpCap_None, ImpCap_LambdaByval, ImpCap_LambdaByref, ImpCap_Block,
634 ImpCap_CapturedRegion
647 unsigned CXXThisCaptureIndex = 0;
654 bool HasImplicitReturnType =
false;
662 QualType CaptureType,
bool Invalid) {
663 Captures.push_back(
Capture(Var, isBlock, isByref, isNested, Loc,
664 EllipsisLoc, CaptureType, Invalid));
665 CaptureMap[Var] = Captures.size();
671 false, Loc, CaptureType));
682 assert(isCXXThisCaptured() &&
"this has not been captured");
683 return Captures[CXXThisCaptureIndex - 1];
688 return CaptureMap.count(Var);
697 assert(isCaptured(Var) &&
"Variable has not been captured");
698 return Captures[CaptureMap[Var] - 1];
702 llvm::DenseMap<VarDecl*, unsigned>::const_iterator Known
703 = CaptureMap.find(Var);
704 assert(Known != CaptureMap.end() &&
"Variable has not been captured");
705 return Captures[Known->second - 1];
709 return FSI->
Kind == SK_Block || FSI->
Kind == SK_Lambda
710 || FSI->
Kind == SK_CapturedRegion;
729 TheScope(BlockScope) {
736 return FSI->
Kind == SK_Block;
764 TheCapturedDecl(CD), TheRecordDecl(RD), TheScope(S),
765 ContextParam(Context), CapRegionKind(K), OpenMPLevel(OpenMPLevel) {
766 Kind = SK_CapturedRegion;
773 switch (CapRegionKind) {
775 return "default captured statement";
777 return "Objective-C @finally statement";
779 return "OpenMP region";
781 llvm_unreachable(
"Invalid captured region kind!");
785 return FSI->
Kind == SK_CapturedRegion;
806 unsigned NumExplicitCaptures = 0;
809 bool Mutable =
false;
812 bool ExplicitParams =
false;
818 bool ContainsUnexpandedParameterPack =
false;
822 unsigned AutoTemplateParameterDepth = 0;
827 unsigned NumExplicitTemplateParams = 0;
886 NumExplicitCaptures = Captures.size();
890 return FSI->
Kind == SK_Lambda;
896 return !TemplateParams.empty() || GLTemplateParameterList;
917 assert(isa<DeclRefExpr>(VarExpr) || isa<MemberExpr>(VarExpr) ||
918 isa<FunctionParmPackExpr>(VarExpr));
919 PotentiallyCapturingExprs.push_back(VarExpr);
923 PotentialThisCaptureLocation = Loc;
927 return PotentialThisCaptureLocation.
isValid();
970 assert(isa<DeclRefExpr>(CapturingVarExpr) ||
971 isa<MemberExpr>(CapturingVarExpr) ||
972 isa<FunctionParmPackExpr>(CapturingVarExpr));
973 NonODRUsedCapturingExprs.insert(CapturingVarExpr);
976 assert(isa<DeclRefExpr>(CapturingVarExpr) ||
977 isa<MemberExpr>(CapturingVarExpr) ||
978 isa<FunctionParmPackExpr>(CapturingVarExpr));
979 return NonODRUsedCapturingExprs.count(CapturingVarExpr);
982 PotentiallyCapturingExprs.erase(
984 PotentiallyCapturingExprs.end(), E),
985 PotentiallyCapturingExprs.end());
988 PotentiallyCapturingExprs.clear();
992 return PotentiallyCapturingExprs.size();
996 return getNumPotentialVariableCaptures() ||
997 PotentialThisCaptureLocation.
isValid();
1000 void visitPotentialCaptures(
1001 llvm::function_ref<
void(
VarDecl *,
Expr *)> Callback)
const;
1004 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy()
1005 :
Base(
nullptr,
false) {}
1008 FunctionScopeInfo::WeakObjectProfileTy::getSentinel() {
1010 Result.Base.setInt(
true);
1014 template <
typename ExprT>
1027 CXXThisCaptureIndex = Captures.size();
1034 #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.
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.
llvm::TinyPtrVector< const Stmt * > Stmts
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...
Defines the clang::Expr interface and subclasses for C++ expressions.
void setHasBranchProtectedScope()
void addVLATypeCapture(SourceLocation Loc, const VariableArrayType *VLAType, QualType CaptureType)
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
CompoundScopeInfo(bool IsStmtExpr)
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
WeakUseTy(const Expr *Use, bool IsRead)
const VariableArrayType * getCapturedVLAType() const
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.
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.
Represents 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)
const VariableArrayType * CapturedVLA
If Kind == Cap_VLA, the captured type.
unsigned short CapRegionKind
The kind of captured region.
SourceLocation getLocation() const
Retrieve the location at which this variable was captured.
SmallVector< NamedDecl *, 4 > TemplateParams
Store the list of the template parameters for a generic lambda.
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.
Capture(IsThisCapture, bool IsNested, SourceLocation Loc, QualType CaptureType, const bool ByCopy, bool Invalid)
SourceRange ExplicitTemplateParamsRange
Source range covering the explicit template parameter list (if it exists).
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.
Capture(VarDecl *Var, bool Block, bool ByRef, bool IsNested, SourceLocation Loc, SourceLocation EllipsisLoc, QualType CaptureType, bool Invalid)
bool isGenericLambda() const
Is this scope known to be for a generic lambda? (This will be false until we parse a template paramet...
Dataflow Directional Tag Classes.
PossiblyUnreachableDiag(const PartialDiagnostic &PD, SourceLocation Loc, ArrayRef< const Stmt *> Stmts)
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.
void addThisCapture(bool isNested, SourceLocation Loc, QualType CaptureType, bool ByCopy)
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)
VarDecl * CapturedVar
Otherwise, the captured variable (if any).
Capture(IsVLACapture, const VariableArrayType *VLA, bool IsNested, SourceLocation Loc, QualType CaptureType)
Defines the clang::SourceLocation class and associated facilities.
void addCapture(VarDecl *Var, bool isBlock, bool isByref, bool isNested, SourceLocation Loc, SourceLocation EllipsisLoc, QualType CaptureType, bool Invalid)
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()
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.
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
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.
bool HasBranchIntoScope
Whether this function contains any switches or direct gotos.
bool isPlainFunction() const
void setHasFallthroughStmt()
bool isBlockCapture() const
void setCoroutineSuspends(Stmt *Initial, Stmt *Final)