14 #ifndef LLVM_CLANG_SEMA_SCOPEINFO_H 15 #define LLVM_CLANG_SEMA_SCOPEINFO_H 24 #include "llvm/ADT/DenseMap.h" 25 #include "llvm/ADT/DenseMapInfo.h" 26 #include "llvm/ADT/MapVector.h" 27 #include "llvm/ADT/PointerIntPair.h" 28 #include "llvm/ADT/SmallPtrSet.h" 29 #include "llvm/ADT/SmallSet.h" 30 #include "llvm/ADT/SmallVector.h" 31 #include "llvm/ADT/StringRef.h" 32 #include "llvm/ADT/StringSwitch.h" 33 #include "llvm/ADT/TinyPtrVector.h" 34 #include "llvm/Support/Casting.h" 35 #include "llvm/Support/ErrorHandling.h" 46 class ImplicitParamDecl;
48 class ObjCIvarRefExpr;
49 class ObjCMessageExpr;
50 class ObjCPropertyDecl;
51 class ObjCPropertyRefExpr;
58 class TemplateParameterList;
59 class TemplateTypeParmDecl;
79 HasEmptyLoopBodies =
true;
91 : PD(PD), Loc(Loc), stmt(stmt) {}
180 using SwitchInfo = llvm::PointerIntPair<SwitchStmt*, 1, bool>;
206 llvm::SmallPtrSet<const BlockDecl *, 1>
Blocks;
250 using BaseInfoTy = llvm::PointerIntPair<const NamedDecl *, 1, bool>;
260 static BaseInfoTy getBaseInfo(
const Expr *BaseE);
286 return Base.getInt();
290 return Base == Other.Base && Property == Other.Property;
304 return WeakObjectProfileTy::getSentinel();
308 using Pair = std::pair<BaseInfoTy, const NamedDecl *>;
328 llvm::PointerIntPair<const Expr *, 1, bool> Rep;
338 return Rep == Other.Rep;
365 :
Kind(SK_Function), HasBranchProtectedScope(
false),
366 HasBranchIntoScope(
false), HasIndirectGoto(
false),
367 HasDroppedStmt(
false), HasOMPDeclareReductionCombiner(
false),
368 HasFallthroughStmt(
false), HasPotentialAvailabilityViolations(
false),
369 ObjCShouldCallSuper(
false), ObjCIsDesignatedInit(
false),
370 ObjCWarnForNoDesignatedInitChain(
false), ObjCIsSecondaryInit(
false),
371 ObjCWarnForNoInitDelegation(
false), NeedsCoroutineSuspends(
true),
379 template <
typename ExprT>
380 inline void recordUseOfWeak(
const ExprT *E,
bool IsRead =
true);
389 void markSafeWeakUse(
const Expr *E);
392 return WeakObjectUses;
396 HasBranchIntoScope =
true;
400 HasBranchProtectedScope =
true;
404 HasIndirectGoto =
true;
408 HasDroppedStmt =
true;
412 HasOMPDeclareReductionCombiner =
true;
416 HasFallthroughStmt =
true;
420 setHasBranchProtectedScope();
421 FirstCXXTryLoc = TryLoc;
425 setHasBranchProtectedScope();
426 FirstSEHTryLoc = TryLoc;
430 return !HasDroppedStmt &&
432 (HasBranchProtectedScope && HasBranchIntoScope));
442 ByrefBlockVars.push_back(VD);
448 assert(FirstCoroutineStmtLoc.
isInvalid() &&
449 "first coroutine statement location already set");
450 FirstCoroutineStmtLoc = Loc;
451 FirstCoroutineStmtKind = llvm::StringSwitch<unsigned char>(Keyword)
452 .Case(
"co_return", 0)
454 .Case(
"co_yield", 2);
458 assert(FirstCoroutineStmtLoc.
isValid()
459 &&
"no coroutine statement available");
460 switch (FirstCoroutineStmtKind) {
461 case 0:
return "co_return";
462 case 1:
return "co_await";
463 case 2:
return "co_yield";
465 llvm_unreachable(
"FirstCoroutineStmtKind has an invalid value");
470 assert((!value || CoroutineSuspends.first ==
nullptr) &&
471 "we already have valid suspend points");
472 NeedsCoroutineSuspends = value;
476 return !NeedsCoroutineSuspends && CoroutineSuspends.first ==
nullptr;
480 assert(Initial && Final &&
"suspend points cannot be null");
481 assert(CoroutineSuspends.first ==
nullptr &&
"suspend points already set");
482 NeedsCoroutineSuspends =
false;
483 CoroutineSuspends.first = Initial;
484 CoroutineSuspends.second = Final;
507 Cap_ByCopy, Cap_ByRef, Cap_Block, Cap_VLA
510 IsNestedCapture = 0x1,
516 llvm::PointerIntPair<VarDecl*, 2> VarAndNestedAndThis;
522 llvm::PointerIntPair<void *, 2, CaptureKind> InitExprAndCaptureKind;
536 bool ODRUsed =
false;
540 bool NonODRUsed =
false;
546 : VarAndNestedAndThis(Var, IsNested ? IsNestedCapture : 0),
547 InitExprAndCaptureKind(
548 Cpy, !Var ? Cap_VLA : Block ? Cap_Block : ByRef ? Cap_ByRef
550 Loc(Loc), EllipsisLoc(EllipsisLoc), CaptureType(CaptureType) {}
555 : VarAndNestedAndThis(
556 nullptr, (IsThisCaptured | (IsNested ? IsNestedCapture : 0))),
557 InitExprAndCaptureKind(Cpy, ByCopy ? Cap_ByCopy : Cap_ByRef),
558 Loc(Loc), CaptureType(CaptureType) {}
561 return VarAndNestedAndThis.getInt() & IsThisCaptured;
565 return !isThisCapture() && !isVLATypeCapture();
569 return InitExprAndCaptureKind.getInt() == Cap_ByCopy;
573 return InitExprAndCaptureKind.getInt() == Cap_ByRef;
577 return InitExprAndCaptureKind.getInt() == Cap_Block;
581 return InitExprAndCaptureKind.getInt() == Cap_VLA;
585 return VarAndNestedAndThis.getInt() & IsNestedCapture;
590 void markUsed(
bool IsODRUse) { (IsODRUse ? ODRUsed : NonODRUsed) =
true; }
593 assert(isVariableCapture());
594 return VarAndNestedAndThis.getPointer();
608 assert(!isThisCapture());
613 assert(!isVLATypeCapture() &&
"no init expression for type capture");
614 return static_cast<Expr *
>(InitExprAndCaptureKind.getPointer());
624 ImpCap_None, ImpCap_LambdaByval, ImpCap_LambdaByref, ImpCap_Block,
625 ImpCap_CapturedRegion
638 unsigned CXXThisCaptureIndex = 0;
645 bool HasImplicitReturnType =
false;
654 Captures.push_back(
Capture(Var, isBlock, isByref, isNested, Loc,
655 EllipsisLoc, CaptureType, Cpy));
656 CaptureMap[Var] = Captures.size();
660 Captures.push_back(
Capture(
nullptr,
false,
670 Expr *Cpy,
bool ByCopy);
677 assert(isCXXThisCaptured() &&
"this has not been captured");
678 return Captures[CXXThisCaptureIndex - 1];
683 return CaptureMap.count(Var);
692 assert(isCaptured(Var) &&
"Variable has not been captured");
693 return Captures[CaptureMap[Var] - 1];
697 llvm::DenseMap<VarDecl*, unsigned>::const_iterator Known
698 = CaptureMap.find(Var);
699 assert(Known != CaptureMap.end() &&
"Variable has not been captured");
700 return Captures[Known->second - 1];
704 return FSI->
Kind == SK_Block || FSI->
Kind == SK_Lambda
705 || FSI->
Kind == SK_CapturedRegion;
724 TheScope(BlockScope) {
731 return FSI->
Kind == SK_Block;
759 TheCapturedDecl(CD), TheRecordDecl(RD), TheScope(S),
760 ContextParam(Context), CapRegionKind(K), OpenMPLevel(OpenMPLevel) {
761 Kind = SK_CapturedRegion;
768 switch (CapRegionKind) {
770 return "default captured statement";
772 return "Objective-C @finally statement";
774 return "OpenMP region";
776 llvm_unreachable(
"Invalid captured region kind!");
780 return FSI->
Kind == SK_CapturedRegion;
801 unsigned NumExplicitCaptures = 0;
804 bool Mutable =
false;
807 bool ExplicitParams =
false;
813 bool ContainsUnexpandedParameterPack =
false;
817 unsigned AutoTemplateParameterDepth = 0;
873 NumExplicitCaptures = Captures.size();
877 return FSI->
Kind == SK_Lambda;
883 return !AutoTemplateParams.empty() || GLTemplateParameterList;
904 assert(isa<DeclRefExpr>(VarExpr) || isa<MemberExpr>(VarExpr));
905 PotentiallyCapturingExprs.push_back(VarExpr);
909 PotentialThisCaptureLocation = Loc;
913 return PotentialThisCaptureLocation.
isValid();
956 assert(isa<DeclRefExpr>(CapturingVarExpr)
957 || isa<MemberExpr>(CapturingVarExpr));
958 NonODRUsedCapturingExprs.insert(CapturingVarExpr);
961 assert(isa<DeclRefExpr>(CapturingVarExpr)
962 || isa<MemberExpr>(CapturingVarExpr));
963 return NonODRUsedCapturingExprs.count(CapturingVarExpr);
966 PotentiallyCapturingExprs.erase(
967 std::remove(PotentiallyCapturingExprs.begin(),
968 PotentiallyCapturingExprs.end(), E),
969 PotentiallyCapturingExprs.end());
972 PotentiallyCapturingExprs.clear();
976 return PotentiallyCapturingExprs.size();
980 return getNumPotentialVariableCaptures() ||
981 PotentialThisCaptureLocation.
isValid();
986 void getPotentialVariableCapture(
unsigned Idx,
VarDecl *&VD,
Expr *&E)
const;
989 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy()
990 :
Base(
nullptr,
false) {}
993 FunctionScopeInfo::WeakObjectProfileTy::getSentinel() {
995 Result.Base.setInt(
true);
999 template <
typename ExprT>
1009 const bool ByCopy) {
1012 CXXThisCaptureIndex = Captures.size();
1019 #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.
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)
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)