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/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>;
244 using BaseInfoTy = llvm::PointerIntPair<const NamedDecl *, 1, bool>;
254 static BaseInfoTy getBaseInfo(
const Expr *BaseE);
280 return Base.getInt();
284 return Base == Other.Base && Property == Other.Property;
298 return WeakObjectProfileTy::getSentinel();
302 using Pair = std::pair<BaseInfoTy, const NamedDecl *>;
322 llvm::PointerIntPair<const Expr *, 1, bool> Rep;
332 return Rep == Other.Rep;
359 :
Kind(SK_Function), HasBranchProtectedScope(
false),
360 HasBranchIntoScope(
false), HasIndirectGoto(
false),
361 HasDroppedStmt(
false), HasOMPDeclareReductionCombiner(
false),
362 HasFallthroughStmt(
false), HasPotentialAvailabilityViolations(
false),
363 ObjCShouldCallSuper(
false), ObjCIsDesignatedInit(
false),
364 ObjCWarnForNoDesignatedInitChain(
false), ObjCIsSecondaryInit(
false),
365 ObjCWarnForNoInitDelegation(
false), NeedsCoroutineSuspends(
true),
373 template <
typename ExprT>
374 inline void recordUseOfWeak(
const ExprT *E,
bool IsRead =
true);
383 void markSafeWeakUse(
const Expr *E);
386 return WeakObjectUses;
390 HasBranchIntoScope =
true;
394 HasBranchProtectedScope =
true;
398 HasIndirectGoto =
true;
402 HasDroppedStmt =
true;
406 HasOMPDeclareReductionCombiner =
true;
410 HasFallthroughStmt =
true;
414 setHasBranchProtectedScope();
415 FirstCXXTryLoc = TryLoc;
419 setHasBranchProtectedScope();
420 FirstSEHTryLoc = TryLoc;
424 return !HasDroppedStmt &&
426 (HasBranchProtectedScope && HasBranchIntoScope));
432 assert(FirstCoroutineStmtLoc.
isInvalid() &&
433 "first coroutine statement location already set");
434 FirstCoroutineStmtLoc = Loc;
435 FirstCoroutineStmtKind = llvm::StringSwitch<unsigned char>(Keyword)
436 .Case(
"co_return", 0)
438 .Case(
"co_yield", 2);
442 assert(FirstCoroutineStmtLoc.
isValid()
443 &&
"no coroutine statement available");
444 switch (FirstCoroutineStmtKind) {
445 case 0:
return "co_return";
446 case 1:
return "co_await";
447 case 2:
return "co_yield";
449 llvm_unreachable(
"FirstCoroutineStmtKind has an invalid value");
454 assert((!value || CoroutineSuspends.first ==
nullptr) &&
455 "we already have valid suspend points");
456 NeedsCoroutineSuspends = value;
460 return !NeedsCoroutineSuspends && CoroutineSuspends.first ==
nullptr;
464 assert(Initial && Final &&
"suspend points cannot be null");
465 assert(CoroutineSuspends.first ==
nullptr &&
"suspend points already set");
466 NeedsCoroutineSuspends =
false;
467 CoroutineSuspends.first = Initial;
468 CoroutineSuspends.second = Final;
491 Cap_ByCopy, Cap_ByRef, Cap_Block, Cap_VLA
494 IsNestedCapture = 0x1,
500 llvm::PointerIntPair<VarDecl*, 2> VarAndNestedAndThis;
506 llvm::PointerIntPair<void *, 2, CaptureKind> InitExprAndCaptureKind;
520 bool ODRUsed =
false;
524 bool NonODRUsed =
false;
530 : VarAndNestedAndThis(Var, IsNested ? IsNestedCapture : 0),
531 InitExprAndCaptureKind(
532 Cpy, !Var ? Cap_VLA : Block ? Cap_Block : ByRef ? Cap_ByRef
534 Loc(Loc), EllipsisLoc(EllipsisLoc), CaptureType(CaptureType) {}
539 : VarAndNestedAndThis(
540 nullptr, (IsThisCaptured | (IsNested ? IsNestedCapture : 0))),
541 InitExprAndCaptureKind(Cpy, ByCopy ? Cap_ByCopy : Cap_ByRef),
542 Loc(Loc), CaptureType(CaptureType) {}
545 return VarAndNestedAndThis.getInt() & IsThisCaptured;
549 return !isThisCapture() && !isVLATypeCapture();
553 return InitExprAndCaptureKind.getInt() == Cap_ByCopy;
557 return InitExprAndCaptureKind.getInt() == Cap_ByRef;
561 return InitExprAndCaptureKind.getInt() == Cap_Block;
565 return InitExprAndCaptureKind.getInt() == Cap_VLA;
569 return VarAndNestedAndThis.getInt() & IsNestedCapture;
574 void markUsed(
bool IsODRUse) { (IsODRUse ? ODRUsed : NonODRUsed) =
true; }
577 assert(isVariableCapture());
578 return VarAndNestedAndThis.getPointer();
592 assert(!isThisCapture());
597 assert(!isVLATypeCapture() &&
"no init expression for type capture");
598 return static_cast<Expr *
>(InitExprAndCaptureKind.getPointer());
608 ImpCap_None, ImpCap_LambdaByval, ImpCap_LambdaByref, ImpCap_Block,
609 ImpCap_CapturedRegion
622 unsigned CXXThisCaptureIndex = 0;
629 bool HasImplicitReturnType =
false;
638 Captures.push_back(
Capture(Var, isBlock, isByref, isNested, Loc,
639 EllipsisLoc, CaptureType, Cpy));
640 CaptureMap[Var] = Captures.size();
644 Captures.push_back(
Capture(
nullptr,
false,
654 Expr *Cpy,
bool ByCopy);
661 assert(isCXXThisCaptured() &&
"this has not been captured");
662 return Captures[CXXThisCaptureIndex - 1];
667 return CaptureMap.count(Var);
676 assert(isCaptured(Var) &&
"Variable has not been captured");
677 return Captures[CaptureMap[Var] - 1];
681 llvm::DenseMap<VarDecl*, unsigned>::const_iterator Known
682 = CaptureMap.find(Var);
683 assert(Known != CaptureMap.end() &&
"Variable has not been captured");
684 return Captures[Known->second - 1];
688 return FSI->
Kind == SK_Block || FSI->
Kind == SK_Lambda
689 || FSI->
Kind == SK_CapturedRegion;
708 TheScope(BlockScope) {
715 return FSI->
Kind == SK_Block;
743 TheCapturedDecl(CD), TheRecordDecl(RD), TheScope(S),
744 ContextParam(Context), CapRegionKind(K), OpenMPLevel(OpenMPLevel) {
745 Kind = SK_CapturedRegion;
752 switch (CapRegionKind) {
754 return "default captured statement";
756 return "Objective-C @finally statement";
758 return "OpenMP region";
760 llvm_unreachable(
"Invalid captured region kind!");
764 return FSI->
Kind == SK_CapturedRegion;
785 unsigned NumExplicitCaptures = 0;
788 bool Mutable =
false;
791 bool ExplicitParams =
false;
797 bool ContainsUnexpandedParameterPack =
false;
801 unsigned AutoTemplateParameterDepth = 0;
857 NumExplicitCaptures = Captures.size();
861 return FSI->
Kind == SK_Lambda;
867 return !AutoTemplateParams.empty() || GLTemplateParameterList;
888 assert(isa<DeclRefExpr>(VarExpr) || isa<MemberExpr>(VarExpr));
889 PotentiallyCapturingExprs.push_back(VarExpr);
893 PotentialThisCaptureLocation = Loc;
897 return PotentialThisCaptureLocation.
isValid();
940 assert(isa<DeclRefExpr>(CapturingVarExpr)
941 || isa<MemberExpr>(CapturingVarExpr));
942 NonODRUsedCapturingExprs.insert(CapturingVarExpr);
945 assert(isa<DeclRefExpr>(CapturingVarExpr)
946 || isa<MemberExpr>(CapturingVarExpr));
947 return NonODRUsedCapturingExprs.count(CapturingVarExpr);
950 PotentiallyCapturingExprs.erase(
951 std::remove(PotentiallyCapturingExprs.begin(),
952 PotentiallyCapturingExprs.end(), E),
953 PotentiallyCapturingExprs.end());
956 PotentiallyCapturingExprs.clear();
960 return PotentiallyCapturingExprs.size();
964 return getNumPotentialVariableCaptures() ||
965 PotentialThisCaptureLocation.
isValid();
970 void getPotentialVariableCapture(
unsigned Idx,
VarDecl *&VD,
Expr *&E)
const;
973 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy()
974 :
Base(
nullptr,
false) {}
977 FunctionScopeInfo::WeakObjectProfileTy::getSentinel() {
979 Result.Base.setInt(
true);
983 template <
typename ExprT>
996 CXXThisCaptureIndex = Captures.size();
1003 #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()
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...
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.
Expr - 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.
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.
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)