clang 17.0.0git
Scope.cpp
Go to the documentation of this file.
1//===- Scope.cpp - Lexical scope information --------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Scope class, which is used for recording
10// information about a lexical scope.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Sema/Scope.h"
15#include "clang/AST/Decl.h"
16#include "llvm/Support/raw_ostream.h"
17
18using namespace clang;
19
20void Scope::setFlags(Scope *parent, unsigned flags) {
21 AnyParent = parent;
22 Flags = flags;
23
24 if (parent && !(flags & FnScope)) {
25 BreakParent = parent->BreakParent;
26 ContinueParent = parent->ContinueParent;
27 } else {
28 // Control scopes do not contain the contents of nested function scopes for
29 // control flow purposes.
30 BreakParent = ContinueParent = nullptr;
31 }
32
33 if (parent) {
34 Depth = parent->Depth + 1;
35 PrototypeDepth = parent->PrototypeDepth;
36 PrototypeIndex = 0;
37 FnParent = parent->FnParent;
38 BlockParent = parent->BlockParent;
39 TemplateParamParent = parent->TemplateParamParent;
40 MSLastManglingParent = parent->MSLastManglingParent;
41 MSCurManglingNumber = getMSLastManglingNumber();
44 0)
45 Flags |= parent->getFlags() & OpenMPSimdDirectiveScope;
46 // transmit the parent's 'order' flag, if exists
47 if (parent->getFlags() & OpenMPOrderClauseScope)
49 } else {
50 Depth = 0;
51 PrototypeDepth = 0;
52 PrototypeIndex = 0;
53 MSLastManglingParent = FnParent = BlockParent = nullptr;
54 TemplateParamParent = nullptr;
55 MSLastManglingNumber = 1;
56 MSCurManglingNumber = 1;
57 }
58
59 // If this scope is a function or contains breaks/continues, remember it.
60 if (flags & FnScope) FnParent = this;
61 // The MS mangler uses the number of scopes that can hold declarations as
62 // part of an external name.
63 if (Flags & (ClassScope | FnScope)) {
64 MSLastManglingNumber = getMSLastManglingNumber();
65 MSLastManglingParent = this;
66 MSCurManglingNumber = 1;
67 }
68 if (flags & BreakScope) BreakParent = this;
69 if (flags & ContinueScope) ContinueParent = this;
70 if (flags & BlockScope) BlockParent = this;
71 if (flags & TemplateParamScope) TemplateParamParent = this;
72
73 // If this is a prototype scope, record that. Lambdas have an extra prototype
74 // scope that doesn't add any depth.
75 if (flags & FunctionPrototypeScope && !(flags & LambdaScope))
76 PrototypeDepth++;
77
78 if (flags & DeclScope) {
79 if (flags & FunctionPrototypeScope)
80 ; // Prototype scopes are uninteresting.
81 else if ((flags & ClassScope) && getParent()->isClassScope())
82 ; // Nested class scopes aren't ambiguous.
83 else if ((flags & ClassScope) && getParent()->getFlags() == DeclScope)
84 ; // Classes inside of namespaces aren't ambiguous.
85 else if ((flags & EnumScope))
86 ; // Don't increment for enum scopes.
87 else
89 }
90}
91
92void Scope::Init(Scope *parent, unsigned flags) {
93 setFlags(parent, flags);
94
95 DeclsInScope.clear();
96 UsingDirectives.clear();
97 Entity = nullptr;
98 ErrorTrap.reset();
99 NRVO = std::nullopt;
100}
101
103 const Scope *S = this;
104 while (S) {
105 if (S->isFunctionPrototypeScope())
106 return true;
107 S = S->getParent();
108 }
109 return false;
110}
111
112void Scope::AddFlags(unsigned FlagsToSet) {
113 assert((FlagsToSet & ~(BreakScope | ContinueScope)) == 0 &&
114 "Unsupported scope flags");
115 if (FlagsToSet & BreakScope) {
116 assert((Flags & BreakScope) == 0 && "Already set");
117 BreakParent = this;
118 }
119 if (FlagsToSet & ContinueScope) {
120 assert((Flags & ContinueScope) == 0 && "Already set");
121 ContinueParent = this;
122 }
123 Flags |= FlagsToSet;
124}
125
126// The algorithm for updating NRVO candidate is as follows:
127// 1. All previous candidates become invalid because a new NRVO candidate is
128// obtained. Therefore, we need to clear return slots for other
129// variables defined before the current return statement in the current
130// scope and in outer scopes.
131// 2. Store the new candidate if its return slot is available. Otherwise,
132// there is no NRVO candidate so far.
134 auto UpdateReturnSlotsInScopeForVD = [VD](Scope *S) -> bool {
135 bool IsReturnSlotFound = S->ReturnSlots.contains(VD);
136
137 // We found a candidate variable that can be put into a return slot.
138 // Clear the set, because other variables cannot occupy a return
139 // slot in the same scope.
140 S->ReturnSlots.clear();
141
142 if (IsReturnSlotFound)
143 S->ReturnSlots.insert(VD);
144
145 return IsReturnSlotFound;
146 };
147
148 bool CanBePutInReturnSlot = false;
149
150 for (auto *S = this; S; S = S->getParent()) {
151 CanBePutInReturnSlot |= UpdateReturnSlotsInScopeForVD(S);
152
153 if (S->getEntity())
154 break;
155 }
156
157 // Consider the variable as NRVO candidate if the return slot is available
158 // for it in the current scope, or if it can be available in outer scopes.
159 NRVO = CanBePutInReturnSlot ? VD : nullptr;
160}
161
163 // There is no NRVO candidate in the current scope.
164 if (!NRVO.has_value())
165 return;
166
167 if (*NRVO && isDeclScope(*NRVO))
168 (*NRVO)->setNRVOVariable(true);
169
170 // It's necessary to propagate NRVO candidate to the parent scope for cases
171 // when the parent scope doesn't contain a return statement.
172 // For example:
173 // X foo(bool b) {
174 // X x;
175 // if (b)
176 // return x;
177 // exit(0);
178 // }
179 // Also, we need to propagate nullptr value that means NRVO is not
180 // allowed in this scope.
181 // For example:
182 // X foo(bool b) {
183 // X x;
184 // if (b)
185 // return x;
186 // else
187 // return X(); // NRVO is not allowed
188 // }
189 if (!getEntity())
190 getParent()->NRVO = *NRVO;
191}
192
193LLVM_DUMP_METHOD void Scope::dump() const { dumpImpl(llvm::errs()); }
194
195void Scope::dumpImpl(raw_ostream &OS) const {
196 unsigned Flags = getFlags();
197 bool HasFlags = Flags != 0;
198
199 if (HasFlags)
200 OS << "Flags: ";
201
202 std::pair<unsigned, const char *> FlagInfo[] = {
203 {FnScope, "FnScope"},
204 {BreakScope, "BreakScope"},
205 {ContinueScope, "ContinueScope"},
206 {DeclScope, "DeclScope"},
207 {ControlScope, "ControlScope"},
208 {ClassScope, "ClassScope"},
209 {BlockScope, "BlockScope"},
210 {TemplateParamScope, "TemplateParamScope"},
211 {FunctionPrototypeScope, "FunctionPrototypeScope"},
212 {FunctionDeclarationScope, "FunctionDeclarationScope"},
213 {AtCatchScope, "AtCatchScope"},
214 {ObjCMethodScope, "ObjCMethodScope"},
215 {SwitchScope, "SwitchScope"},
216 {TryScope, "TryScope"},
217 {FnTryCatchScope, "FnTryCatchScope"},
218 {OpenMPDirectiveScope, "OpenMPDirectiveScope"},
219 {OpenMPLoopDirectiveScope, "OpenMPLoopDirectiveScope"},
220 {OpenMPSimdDirectiveScope, "OpenMPSimdDirectiveScope"},
221 {EnumScope, "EnumScope"},
222 {SEHTryScope, "SEHTryScope"},
223 {SEHExceptScope, "SEHExceptScope"},
224 {SEHFilterScope, "SEHFilterScope"},
225 {CompoundStmtScope, "CompoundStmtScope"},
226 {ClassInheritanceScope, "ClassInheritanceScope"},
227 {CatchScope, "CatchScope"},
228 };
229
230 for (auto Info : FlagInfo) {
231 if (Flags & Info.first) {
232 OS << Info.second;
233 Flags &= ~Info.first;
234 if (Flags)
235 OS << " | ";
236 }
237 }
238
239 assert(Flags == 0 && "Unknown scope flags");
240
241 if (HasFlags)
242 OS << '\n';
243
244 if (const Scope *Parent = getParent())
245 OS << "Parent: (clang::Scope*)" << Parent << '\n';
246
247 OS << "Depth: " << Depth << '\n';
248 OS << "MSLastManglingNumber: " << getMSLastManglingNumber() << '\n';
249 OS << "MSCurManglingNumber: " << getMSCurManglingNumber() << '\n';
250 if (const DeclContext *DC = getEntity())
251 OS << "Entity : (clang::DeclContext*)" << DC << '\n';
252
253 if (!NRVO)
254 OS << "there is no NRVO candidate\n";
255 else if (*NRVO)
256 OS << "NRVO candidate : (clang::VarDecl*)" << *NRVO << '\n';
257 else
258 OS << "NRVO is not allowed\n";
259}
NodeId Parent
Definition: ASTDiff.cpp:191
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1402
void reset()
Set to initial state of "no errors occurred".
Definition: Diagnostic.h:1094
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:41
void Init(Scope *parent, unsigned flags)
Init - This is used by the parser to implement scope caching.
Definition: Scope.cpp:92
void AddFlags(unsigned Flags)
Sets up the specified scope flags and adjusts the scope state variables accordingly.
Definition: Scope.cpp:112
bool isClassScope() const
isClassScope - Return true if this scope is a class/struct/union scope.
Definition: Scope.h:392
void incrementMSManglingNumber()
Definition: Scope.h:336
unsigned getFlags() const
getFlags - Return the flags for this scope.
Definition: Scope.h:246
bool isDeclScope(const Decl *D) const
isDeclScope - Return true if this is the scope that the specified decl is declared in.
Definition: Scope.h:362
unsigned getMSLastManglingNumber() const
Definition: Scope.h:350
void dump() const
Definition: Scope.cpp:193
DeclContext * getEntity() const
Get the entity corresponding to this scope.
Definition: Scope.h:365
unsigned getMSCurManglingNumber() const
Definition: Scope.h:356
bool containedInPrototypeScope() const
containedInPrototypeScope - Return true if this or a parent scope is a FunctionPrototypeScope.
Definition: Scope.cpp:102
const Scope * getParent() const
getParent - Return the scope that this is nested in.
Definition: Scope.h:254
void updateNRVOCandidate(VarDecl *VD)
Definition: Scope.cpp:133
void dumpImpl(raw_ostream &OS) const
Definition: Scope.cpp:195
void applyNRVO()
Definition: Scope.cpp:162
@ OpenMPDirectiveScope
This is the scope of OpenMP executable directive.
Definition: Scope.h:108
@ FunctionPrototypeScope
This is a scope that corresponds to the parameters within a function prototype.
Definition: Scope.h:82
@ OpenMPOrderClauseScope
This is a scope of some OpenMP directive with order clause which specifies concurrent.
Definition: Scope.h:147
@ LambdaScope
This is the scope for a lambda, after the lambda introducer.
Definition: Scope.h:152
@ BlockScope
This is a scope that corresponds to a block/closure object.
Definition: Scope.h:72
@ SEHTryScope
This scope corresponds to an SEH try.
Definition: Scope.h:122
@ ContinueScope
This is a while, do, for, which can have continue statements embedded into it.
Definition: Scope.h:56
@ ControlScope
The controlling scope in a if/switch/while/for statement.
Definition: Scope.h:63
@ ClassInheritanceScope
We are between inheritance colon and the real class/struct definition scope.
Definition: Scope.h:135
@ AtCatchScope
This is a scope that corresponds to the Objective-C @catch statement.
Definition: Scope.h:92
@ TemplateParamScope
This is a scope that corresponds to the template parameters of a C++ template.
Definition: Scope.h:78
@ SEHFilterScope
We are currently in the filter expression of an SEH except block.
Definition: Scope.h:128
@ SwitchScope
This is a scope that corresponds to a switch statement.
Definition: Scope.h:99
@ BreakScope
This is a while, do, switch, for, etc that can have break statements embedded into it.
Definition: Scope.h:52
@ CatchScope
This is the scope of a C++ catch statement.
Definition: Scope.h:138
@ CompoundStmtScope
This is a compound statement scope.
Definition: Scope.h:131
@ FnTryCatchScope
This is the scope for a function-level C++ try or catch scope.
Definition: Scope.h:105
@ SEHExceptScope
This scope corresponds to an SEH except.
Definition: Scope.h:125
@ ClassScope
The scope of a struct/union/class definition.
Definition: Scope.h:66
@ TryScope
This is the scope of a C++ try statement.
Definition: Scope.h:102
@ OpenMPSimdDirectiveScope
This is the scope of some OpenMP simd directive.
Definition: Scope.h:116
@ FunctionDeclarationScope
This is a scope that corresponds to the parameters within a function prototype for a function declara...
Definition: Scope.h:88
@ FnScope
This indicates that the scope corresponds to a function, which means that labels are set here.
Definition: Scope.h:48
@ ObjCMethodScope
This scope corresponds to an Objective-C method body.
Definition: Scope.h:96
@ EnumScope
This scope corresponds to an enum.
Definition: Scope.h:119
@ OpenMPLoopDirectiveScope
This is the scope of some OpenMP loop directive.
Definition: Scope.h:111
@ DeclScope
This is a scope that can contain a declaration.
Definition: Scope.h:60
Represents a variable declaration or definition.
Definition: Decl.h:913