blob: eae5a328bfa2c92e697275c8a206bb1225a2acd3 [file] [log] [blame]
John McCall8fb0d9d2011-05-01 22:35:37 +00001//===- Scope.cpp - Lexical scope information --------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Scope class, which is used for recording
11// information about a lexical scope.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Sema/Scope.h"
Nick Lewyckyd78f92f2014-05-03 00:41:18 +000016#include "clang/AST/Decl.h"
David Majnemer2206bf52014-03-05 08:57:59 +000017#include "llvm/Support/raw_ostream.h"
John McCall8fb0d9d2011-05-01 22:35:37 +000018
19using namespace clang;
20
Akira Hatanaka10aced82016-04-29 02:24:14 +000021void Scope::setFlags(Scope *parent, unsigned flags) {
John McCall8fb0d9d2011-05-01 22:35:37 +000022 AnyParent = parent;
23 Flags = flags;
Richard Smith1002d102012-02-17 01:35:32 +000024
25 if (parent && !(flags & FnScope)) {
26 BreakParent = parent->BreakParent;
27 ContinueParent = parent->ContinueParent;
28 } else {
29 // Control scopes do not contain the contents of nested function scopes for
30 // control flow purposes.
Craig Topperc3ec1492014-05-26 06:22:03 +000031 BreakParent = ContinueParent = nullptr;
Richard Smith1002d102012-02-17 01:35:32 +000032 }
33
John McCall8fb0d9d2011-05-01 22:35:37 +000034 if (parent) {
35 Depth = parent->Depth + 1;
36 PrototypeDepth = parent->PrototypeDepth;
37 PrototypeIndex = 0;
38 FnParent = parent->FnParent;
John McCall8fb0d9d2011-05-01 22:35:37 +000039 BlockParent = parent->BlockParent;
40 TemplateParamParent = parent->TemplateParamParent;
David Majnemera7f8c462015-03-19 21:54:30 +000041 MSLastManglingParent = parent->MSLastManglingParent;
42 MSCurManglingNumber = getMSLastManglingNumber();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +000043 if ((Flags & (FnScope | ClassScope | BlockScope | TemplateParamScope |
44 FunctionPrototypeScope | AtCatchScope | ObjCMethodScope)) ==
45 0)
46 Flags |= parent->getFlags() & OpenMPSimdDirectiveScope;
John McCall8fb0d9d2011-05-01 22:35:37 +000047 } else {
48 Depth = 0;
49 PrototypeDepth = 0;
50 PrototypeIndex = 0;
David Majnemera7f8c462015-03-19 21:54:30 +000051 MSLastManglingParent = FnParent = BlockParent = nullptr;
Craig Topperc3ec1492014-05-26 06:22:03 +000052 TemplateParamParent = nullptr;
David Majnemera7f8c462015-03-19 21:54:30 +000053 MSLastManglingNumber = 1;
54 MSCurManglingNumber = 1;
John McCall8fb0d9d2011-05-01 22:35:37 +000055 }
56
57 // If this scope is a function or contains breaks/continues, remember it.
58 if (flags & FnScope) FnParent = this;
David Majnemer2206bf52014-03-05 08:57:59 +000059 // The MS mangler uses the number of scopes that can hold declarations as
60 // part of an external name.
61 if (Flags & (ClassScope | FnScope)) {
David Majnemera7f8c462015-03-19 21:54:30 +000062 MSLastManglingNumber = getMSLastManglingNumber();
63 MSLastManglingParent = this;
64 MSCurManglingNumber = 1;
David Majnemer2206bf52014-03-05 08:57:59 +000065 }
John McCall8fb0d9d2011-05-01 22:35:37 +000066 if (flags & BreakScope) BreakParent = this;
67 if (flags & ContinueScope) ContinueParent = this;
John McCall8fb0d9d2011-05-01 22:35:37 +000068 if (flags & BlockScope) BlockParent = this;
69 if (flags & TemplateParamScope) TemplateParamParent = this;
70
71 // If this is a prototype scope, record that.
72 if (flags & FunctionPrototypeScope) PrototypeDepth++;
Hans Wennborgfe781452014-06-17 00:00:18 +000073
David Majnemer2206bf52014-03-05 08:57:59 +000074 if (flags & DeclScope) {
75 if (flags & FunctionPrototypeScope)
76 ; // Prototype scopes are uninteresting.
77 else if ((flags & ClassScope) && getParent()->isClassScope())
78 ; // Nested class scopes aren't ambiguous.
79 else if ((flags & ClassScope) && getParent()->getFlags() == DeclScope)
80 ; // Classes inside of namespaces aren't ambiguous.
Hans Wennborgfe781452014-06-17 00:00:18 +000081 else if ((flags & EnumScope))
82 ; // Don't increment for enum scopes.
David Majnemer2206bf52014-03-05 08:57:59 +000083 else
David Majnemera7f8c462015-03-19 21:54:30 +000084 incrementMSManglingNumber();
David Majnemer2206bf52014-03-05 08:57:59 +000085 }
Akira Hatanaka10aced82016-04-29 02:24:14 +000086}
87
88void Scope::Init(Scope *parent, unsigned flags) {
89 setFlags(parent, flags);
John McCall8fb0d9d2011-05-01 22:35:37 +000090
91 DeclsInScope.clear();
92 UsingDirectives.clear();
Craig Topperc3ec1492014-05-26 06:22:03 +000093 Entity = nullptr;
John McCall8fb0d9d2011-05-01 22:35:37 +000094 ErrorTrap.reset();
Taiju Tsuiki3be68e12018-06-19 05:35:30 +000095 NRVO.setPointerAndInt(nullptr, 0);
John McCall8fb0d9d2011-05-01 22:35:37 +000096}
James Molloy6f8780b2012-02-29 10:24:19 +000097
98bool Scope::containedInPrototypeScope() const {
99 const Scope *S = this;
100 while (S) {
101 if (S->isFunctionPrototypeScope())
102 return true;
103 S = S->getParent();
104 }
105 return false;
106}
Serge Pavlov09f99242014-01-23 15:05:00 +0000107
108void Scope::AddFlags(unsigned FlagsToSet) {
109 assert((FlagsToSet & ~(BreakScope | ContinueScope)) == 0 &&
110 "Unsupported scope flags");
111 if (FlagsToSet & BreakScope) {
112 assert((Flags & BreakScope) == 0 && "Already set");
113 BreakParent = this;
114 }
115 if (FlagsToSet & ContinueScope) {
116 assert((Flags & ContinueScope) == 0 && "Already set");
117 ContinueParent = this;
118 }
119 Flags |= FlagsToSet;
120}
121
Taiju Tsuiki3be68e12018-06-19 05:35:30 +0000122void Scope::mergeNRVOIntoParent() {
123 if (VarDecl *Candidate = NRVO.getPointer()) {
124 if (isDeclScope(Candidate))
125 Candidate->setNRVOVariable(true);
Nick Lewyckyd78f92f2014-05-03 00:41:18 +0000126 }
127
Taiju Tsuiki3be68e12018-06-19 05:35:30 +0000128 if (getEntity())
129 return;
130
131 if (NRVO.getInt())
132 getParent()->setNoNRVO();
133 else if (NRVO.getPointer())
134 getParent()->addNRVOCandidate(NRVO.getPointer());
Nick Lewyckyd78f92f2014-05-03 00:41:18 +0000135}
136
Yaron Kerencdae9412016-01-29 19:38:18 +0000137LLVM_DUMP_METHOD void Scope::dump() const { dumpImpl(llvm::errs()); }
David Majnemer2206bf52014-03-05 08:57:59 +0000138
139void Scope::dumpImpl(raw_ostream &OS) const {
140 unsigned Flags = getFlags();
141 bool HasFlags = Flags != 0;
142
143 if (HasFlags)
144 OS << "Flags: ";
145
Richard Trieudf5ec072018-01-18 04:28:56 +0000146 std::pair<unsigned, const char *> FlagInfo[] = {
147 {FnScope, "FnScope"},
148 {BreakScope, "BreakScope"},
149 {ContinueScope, "ContinueScope"},
150 {DeclScope, "DeclScope"},
151 {ControlScope, "ControlScope"},
152 {ClassScope, "ClassScope"},
153 {BlockScope, "BlockScope"},
154 {TemplateParamScope, "TemplateParamScope"},
155 {FunctionPrototypeScope, "FunctionPrototypeScope"},
156 {FunctionDeclarationScope, "FunctionDeclarationScope"},
157 {AtCatchScope, "AtCatchScope"},
158 {ObjCMethodScope, "ObjCMethodScope"},
159 {SwitchScope, "SwitchScope"},
160 {TryScope, "TryScope"},
161 {FnTryCatchScope, "FnTryCatchScope"},
162 {OpenMPDirectiveScope, "OpenMPDirectiveScope"},
163 {OpenMPLoopDirectiveScope, "OpenMPLoopDirectiveScope"},
164 {OpenMPSimdDirectiveScope, "OpenMPSimdDirectiveScope"},
165 {EnumScope, "EnumScope"},
166 {SEHTryScope, "SEHTryScope"},
167 {SEHExceptScope, "SEHExceptScope"},
168 {SEHFilterScope, "SEHFilterScope"},
169 {CompoundStmtScope, "CompoundStmtScope"},
170 {ClassInheritanceScope, "ClassInheritanceScope"}};
David Majnemer2206bf52014-03-05 08:57:59 +0000171
Richard Trieudf5ec072018-01-18 04:28:56 +0000172 for (auto Info : FlagInfo) {
173 if (Flags & Info.first) {
174 OS << Info.second;
175 Flags &= ~Info.first;
176 if (Flags)
177 OS << " | ";
178 }
David Majnemer2206bf52014-03-05 08:57:59 +0000179 }
Richard Trieudf5ec072018-01-18 04:28:56 +0000180
181 assert(Flags == 0 && "Unknown scope flags");
182
David Majnemer2206bf52014-03-05 08:57:59 +0000183 if (HasFlags)
184 OS << '\n';
185
186 if (const Scope *Parent = getParent())
187 OS << "Parent: (clang::Scope*)" << Parent << '\n';
188
189 OS << "Depth: " << Depth << '\n';
David Majnemera7f8c462015-03-19 21:54:30 +0000190 OS << "MSLastManglingNumber: " << getMSLastManglingNumber() << '\n';
191 OS << "MSCurManglingNumber: " << getMSCurManglingNumber() << '\n';
David Majnemer2206bf52014-03-05 08:57:59 +0000192 if (const DeclContext *DC = getEntity())
193 OS << "Entity : (clang::DeclContext*)" << DC << '\n';
Taiju Tsuiki3be68e12018-06-19 05:35:30 +0000194
195 if (NRVO.getInt())
196 OS << "NRVO not allowed\n";
197 else if (NRVO.getPointer())
198 OS << "NRVO candidate : (clang::VarDecl*)" << NRVO.getPointer() << '\n';
David Majnemer2206bf52014-03-05 08:57:59 +0000199}