blob: 860b7c66ec60838997c05cd21a50053ff3e25921 [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
21void Scope::Init(Scope *parent, unsigned flags) {
22 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 Majnemer2206bf52014-03-05 08:57:59 +000041 MSLocalManglingParent = parent->MSLocalManglingParent;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +000042 if ((Flags & (FnScope | ClassScope | BlockScope | TemplateParamScope |
43 FunctionPrototypeScope | AtCatchScope | ObjCMethodScope)) ==
44 0)
45 Flags |= parent->getFlags() & OpenMPSimdDirectiveScope;
John McCall8fb0d9d2011-05-01 22:35:37 +000046 } else {
47 Depth = 0;
48 PrototypeDepth = 0;
49 PrototypeIndex = 0;
Craig Topperc3ec1492014-05-26 06:22:03 +000050 MSLocalManglingParent = FnParent = BlockParent = nullptr;
51 TemplateParamParent = nullptr;
David Majnemer2206bf52014-03-05 08:57:59 +000052 MSLocalManglingNumber = 1;
John McCall8fb0d9d2011-05-01 22:35:37 +000053 }
54
55 // If this scope is a function or contains breaks/continues, remember it.
56 if (flags & FnScope) FnParent = this;
David Majnemer2206bf52014-03-05 08:57:59 +000057 // The MS mangler uses the number of scopes that can hold declarations as
58 // part of an external name.
59 if (Flags & (ClassScope | FnScope)) {
60 MSLocalManglingNumber = getMSLocalManglingNumber();
61 MSLocalManglingParent = this;
62 }
John McCall8fb0d9d2011-05-01 22:35:37 +000063 if (flags & BreakScope) BreakParent = this;
64 if (flags & ContinueScope) ContinueParent = this;
John McCall8fb0d9d2011-05-01 22:35:37 +000065 if (flags & BlockScope) BlockParent = this;
66 if (flags & TemplateParamScope) TemplateParamParent = this;
67
68 // If this is a prototype scope, record that.
69 if (flags & FunctionPrototypeScope) PrototypeDepth++;
David Majnemer2206bf52014-03-05 08:57:59 +000070 if (flags & DeclScope) {
71 if (flags & FunctionPrototypeScope)
72 ; // Prototype scopes are uninteresting.
73 else if ((flags & ClassScope) && getParent()->isClassScope())
74 ; // Nested class scopes aren't ambiguous.
75 else if ((flags & ClassScope) && getParent()->getFlags() == DeclScope)
76 ; // Classes inside of namespaces aren't ambiguous.
77 else
78 incrementMSLocalManglingNumber();
79 }
John McCall8fb0d9d2011-05-01 22:35:37 +000080
81 DeclsInScope.clear();
82 UsingDirectives.clear();
Craig Topperc3ec1492014-05-26 06:22:03 +000083 Entity = nullptr;
John McCall8fb0d9d2011-05-01 22:35:37 +000084 ErrorTrap.reset();
Nick Lewyckyd78f92f2014-05-03 00:41:18 +000085 NRVO.setPointerAndInt(nullptr, 0);
John McCall8fb0d9d2011-05-01 22:35:37 +000086}
James Molloy6f8780b2012-02-29 10:24:19 +000087
88bool Scope::containedInPrototypeScope() const {
89 const Scope *S = this;
90 while (S) {
91 if (S->isFunctionPrototypeScope())
92 return true;
93 S = S->getParent();
94 }
95 return false;
96}
Serge Pavlov09f99242014-01-23 15:05:00 +000097
98void Scope::AddFlags(unsigned FlagsToSet) {
99 assert((FlagsToSet & ~(BreakScope | ContinueScope)) == 0 &&
100 "Unsupported scope flags");
101 if (FlagsToSet & BreakScope) {
102 assert((Flags & BreakScope) == 0 && "Already set");
103 BreakParent = this;
104 }
105 if (FlagsToSet & ContinueScope) {
106 assert((Flags & ContinueScope) == 0 && "Already set");
107 ContinueParent = this;
108 }
109 Flags |= FlagsToSet;
110}
111
Nick Lewyckyd78f92f2014-05-03 00:41:18 +0000112void Scope::mergeNRVOIntoParent() {
113 if (VarDecl *Candidate = NRVO.getPointer()) {
114 if (isDeclScope(Candidate))
115 Candidate->setNRVOVariable(true);
116 }
117
118 if (getEntity())
119 return;
120
121 if (NRVO.getInt())
122 getParent()->setNoNRVO();
123 else if (NRVO.getPointer())
124 getParent()->addNRVOCandidate(NRVO.getPointer());
125}
126
David Majnemer2206bf52014-03-05 08:57:59 +0000127void Scope::dump() const { dumpImpl(llvm::errs()); }
128
129void Scope::dumpImpl(raw_ostream &OS) const {
130 unsigned Flags = getFlags();
131 bool HasFlags = Flags != 0;
132
133 if (HasFlags)
134 OS << "Flags: ";
135
136 while (Flags) {
137 if (Flags & FnScope) {
138 OS << "FnScope";
139 Flags &= ~FnScope;
140 } else if (Flags & BreakScope) {
141 OS << "BreakScope";
142 Flags &= ~BreakScope;
143 } else if (Flags & ContinueScope) {
144 OS << "ContinueScope";
145 Flags &= ~ContinueScope;
146 } else if (Flags & DeclScope) {
147 OS << "DeclScope";
148 Flags &= ~DeclScope;
149 } else if (Flags & ControlScope) {
150 OS << "ControlScope";
151 Flags &= ~ControlScope;
152 } else if (Flags & ClassScope) {
153 OS << "ClassScope";
154 Flags &= ~ClassScope;
155 } else if (Flags & BlockScope) {
156 OS << "BlockScope";
157 Flags &= ~BlockScope;
158 } else if (Flags & TemplateParamScope) {
159 OS << "TemplateParamScope";
160 Flags &= ~TemplateParamScope;
161 } else if (Flags & FunctionPrototypeScope) {
162 OS << "FunctionPrototypeScope";
163 Flags &= ~FunctionPrototypeScope;
164 } else if (Flags & FunctionDeclarationScope) {
165 OS << "FunctionDeclarationScope";
166 Flags &= ~FunctionDeclarationScope;
167 } else if (Flags & AtCatchScope) {
168 OS << "AtCatchScope";
169 Flags &= ~AtCatchScope;
170 } else if (Flags & ObjCMethodScope) {
171 OS << "ObjCMethodScope";
172 Flags &= ~ObjCMethodScope;
173 } else if (Flags & SwitchScope) {
174 OS << "SwitchScope";
175 Flags &= ~SwitchScope;
176 } else if (Flags & TryScope) {
177 OS << "TryScope";
178 Flags &= ~TryScope;
179 } else if (Flags & FnTryCatchScope) {
180 OS << "FnTryCatchScope";
181 Flags &= ~FnTryCatchScope;
182 } else if (Flags & OpenMPDirectiveScope) {
183 OS << "OpenMPDirectiveScope";
184 Flags &= ~OpenMPDirectiveScope;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000185 } else if (Flags & OpenMPLoopDirectiveScope) {
186 OS << "OpenMPLoopDirectiveScope";
187 Flags &= ~OpenMPLoopDirectiveScope;
188 } else if (Flags & OpenMPSimdDirectiveScope) {
189 OS << "OpenMPSimdDirectiveScope";
190 Flags &= ~OpenMPSimdDirectiveScope;
David Majnemer2206bf52014-03-05 08:57:59 +0000191 }
192
193 if (Flags)
194 OS << " | ";
195 }
196 if (HasFlags)
197 OS << '\n';
198
199 if (const Scope *Parent = getParent())
200 OS << "Parent: (clang::Scope*)" << Parent << '\n';
201
202 OS << "Depth: " << Depth << '\n';
203 OS << "MSLocalManglingNumber: " << getMSLocalManglingNumber() << '\n';
204 if (const DeclContext *DC = getEntity())
205 OS << "Entity : (clang::DeclContext*)" << DC << '\n';
Nick Lewyckyd78f92f2014-05-03 00:41:18 +0000206
207 if (NRVO.getInt())
208 OS << "NRVO not allowed";
209 else if (NRVO.getPointer())
210 OS << "NRVO candidate : (clang::VarDecl*)" << NRVO.getPointer() << '\n';
David Majnemer2206bf52014-03-05 08:57:59 +0000211}