2011-05-02 06:35:37 +08:00
|
|
|
//===- Scope.cpp - Lexical scope information --------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the Scope class, which is used for recording
|
|
|
|
// information about a lexical scope.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Sema/Scope.h"
|
2014-05-03 08:41:18 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
2014-03-05 16:57:59 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-05-02 06:35:37 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
void Scope::Init(Scope *parent, unsigned flags) {
|
|
|
|
AnyParent = parent;
|
|
|
|
Flags = flags;
|
2012-02-17 09:35:32 +08:00
|
|
|
|
|
|
|
if (parent && !(flags & FnScope)) {
|
|
|
|
BreakParent = parent->BreakParent;
|
|
|
|
ContinueParent = parent->ContinueParent;
|
|
|
|
} else {
|
|
|
|
// Control scopes do not contain the contents of nested function scopes for
|
|
|
|
// control flow purposes.
|
2014-05-26 14:22:03 +08:00
|
|
|
BreakParent = ContinueParent = nullptr;
|
2012-02-17 09:35:32 +08:00
|
|
|
}
|
|
|
|
|
2011-05-02 06:35:37 +08:00
|
|
|
if (parent) {
|
|
|
|
Depth = parent->Depth + 1;
|
|
|
|
PrototypeDepth = parent->PrototypeDepth;
|
|
|
|
PrototypeIndex = 0;
|
|
|
|
FnParent = parent->FnParent;
|
|
|
|
BlockParent = parent->BlockParent;
|
|
|
|
TemplateParamParent = parent->TemplateParamParent;
|
2015-03-20 05:54:30 +08:00
|
|
|
MSLastManglingParent = parent->MSLastManglingParent;
|
|
|
|
MSCurManglingNumber = getMSLastManglingNumber();
|
2014-06-03 18:16:47 +08:00
|
|
|
if ((Flags & (FnScope | ClassScope | BlockScope | TemplateParamScope |
|
|
|
|
FunctionPrototypeScope | AtCatchScope | ObjCMethodScope)) ==
|
|
|
|
0)
|
|
|
|
Flags |= parent->getFlags() & OpenMPSimdDirectiveScope;
|
2011-05-02 06:35:37 +08:00
|
|
|
} else {
|
|
|
|
Depth = 0;
|
|
|
|
PrototypeDepth = 0;
|
|
|
|
PrototypeIndex = 0;
|
2015-03-20 05:54:30 +08:00
|
|
|
MSLastManglingParent = FnParent = BlockParent = nullptr;
|
2014-05-26 14:22:03 +08:00
|
|
|
TemplateParamParent = nullptr;
|
2015-03-20 05:54:30 +08:00
|
|
|
MSLastManglingNumber = 1;
|
|
|
|
MSCurManglingNumber = 1;
|
2011-05-02 06:35:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If this scope is a function or contains breaks/continues, remember it.
|
|
|
|
if (flags & FnScope) FnParent = this;
|
2014-03-05 16:57:59 +08:00
|
|
|
// The MS mangler uses the number of scopes that can hold declarations as
|
|
|
|
// part of an external name.
|
|
|
|
if (Flags & (ClassScope | FnScope)) {
|
2015-03-20 05:54:30 +08:00
|
|
|
MSLastManglingNumber = getMSLastManglingNumber();
|
|
|
|
MSLastManglingParent = this;
|
|
|
|
MSCurManglingNumber = 1;
|
2014-03-05 16:57:59 +08:00
|
|
|
}
|
2011-05-02 06:35:37 +08:00
|
|
|
if (flags & BreakScope) BreakParent = this;
|
|
|
|
if (flags & ContinueScope) ContinueParent = this;
|
|
|
|
if (flags & BlockScope) BlockParent = this;
|
|
|
|
if (flags & TemplateParamScope) TemplateParamParent = this;
|
|
|
|
|
|
|
|
// If this is a prototype scope, record that.
|
|
|
|
if (flags & FunctionPrototypeScope) PrototypeDepth++;
|
2014-06-17 08:00:18 +08:00
|
|
|
|
2014-03-05 16:57:59 +08:00
|
|
|
if (flags & DeclScope) {
|
|
|
|
if (flags & FunctionPrototypeScope)
|
|
|
|
; // Prototype scopes are uninteresting.
|
|
|
|
else if ((flags & ClassScope) && getParent()->isClassScope())
|
|
|
|
; // Nested class scopes aren't ambiguous.
|
|
|
|
else if ((flags & ClassScope) && getParent()->getFlags() == DeclScope)
|
|
|
|
; // Classes inside of namespaces aren't ambiguous.
|
2014-06-17 08:00:18 +08:00
|
|
|
else if ((flags & EnumScope))
|
|
|
|
; // Don't increment for enum scopes.
|
2014-03-05 16:57:59 +08:00
|
|
|
else
|
2015-03-20 05:54:30 +08:00
|
|
|
incrementMSManglingNumber();
|
2014-03-05 16:57:59 +08:00
|
|
|
}
|
2011-05-02 06:35:37 +08:00
|
|
|
|
|
|
|
DeclsInScope.clear();
|
|
|
|
UsingDirectives.clear();
|
2014-05-26 14:22:03 +08:00
|
|
|
Entity = nullptr;
|
2011-05-02 06:35:37 +08:00
|
|
|
ErrorTrap.reset();
|
2014-05-03 08:41:18 +08:00
|
|
|
NRVO.setPointerAndInt(nullptr, 0);
|
2011-05-02 06:35:37 +08:00
|
|
|
}
|
2012-02-29 18:24:19 +08:00
|
|
|
|
|
|
|
bool Scope::containedInPrototypeScope() const {
|
|
|
|
const Scope *S = this;
|
|
|
|
while (S) {
|
|
|
|
if (S->isFunctionPrototypeScope())
|
|
|
|
return true;
|
|
|
|
S = S->getParent();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-01-23 23:05:00 +08:00
|
|
|
|
|
|
|
void Scope::AddFlags(unsigned FlagsToSet) {
|
|
|
|
assert((FlagsToSet & ~(BreakScope | ContinueScope)) == 0 &&
|
|
|
|
"Unsupported scope flags");
|
|
|
|
if (FlagsToSet & BreakScope) {
|
|
|
|
assert((Flags & BreakScope) == 0 && "Already set");
|
|
|
|
BreakParent = this;
|
|
|
|
}
|
|
|
|
if (FlagsToSet & ContinueScope) {
|
|
|
|
assert((Flags & ContinueScope) == 0 && "Already set");
|
|
|
|
ContinueParent = this;
|
|
|
|
}
|
|
|
|
Flags |= FlagsToSet;
|
|
|
|
}
|
|
|
|
|
2014-05-03 08:41:18 +08:00
|
|
|
void Scope::mergeNRVOIntoParent() {
|
|
|
|
if (VarDecl *Candidate = NRVO.getPointer()) {
|
|
|
|
if (isDeclScope(Candidate))
|
|
|
|
Candidate->setNRVOVariable(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getEntity())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (NRVO.getInt())
|
|
|
|
getParent()->setNoNRVO();
|
|
|
|
else if (NRVO.getPointer())
|
|
|
|
getParent()->addNRVOCandidate(NRVO.getPointer());
|
|
|
|
}
|
|
|
|
|
2014-03-05 16:57:59 +08:00
|
|
|
void Scope::dump() const { dumpImpl(llvm::errs()); }
|
|
|
|
|
|
|
|
void Scope::dumpImpl(raw_ostream &OS) const {
|
|
|
|
unsigned Flags = getFlags();
|
|
|
|
bool HasFlags = Flags != 0;
|
|
|
|
|
|
|
|
if (HasFlags)
|
|
|
|
OS << "Flags: ";
|
|
|
|
|
|
|
|
while (Flags) {
|
|
|
|
if (Flags & FnScope) {
|
|
|
|
OS << "FnScope";
|
|
|
|
Flags &= ~FnScope;
|
|
|
|
} else if (Flags & BreakScope) {
|
|
|
|
OS << "BreakScope";
|
|
|
|
Flags &= ~BreakScope;
|
|
|
|
} else if (Flags & ContinueScope) {
|
|
|
|
OS << "ContinueScope";
|
|
|
|
Flags &= ~ContinueScope;
|
|
|
|
} else if (Flags & DeclScope) {
|
|
|
|
OS << "DeclScope";
|
|
|
|
Flags &= ~DeclScope;
|
|
|
|
} else if (Flags & ControlScope) {
|
|
|
|
OS << "ControlScope";
|
|
|
|
Flags &= ~ControlScope;
|
|
|
|
} else if (Flags & ClassScope) {
|
|
|
|
OS << "ClassScope";
|
|
|
|
Flags &= ~ClassScope;
|
|
|
|
} else if (Flags & BlockScope) {
|
|
|
|
OS << "BlockScope";
|
|
|
|
Flags &= ~BlockScope;
|
|
|
|
} else if (Flags & TemplateParamScope) {
|
|
|
|
OS << "TemplateParamScope";
|
|
|
|
Flags &= ~TemplateParamScope;
|
|
|
|
} else if (Flags & FunctionPrototypeScope) {
|
|
|
|
OS << "FunctionPrototypeScope";
|
|
|
|
Flags &= ~FunctionPrototypeScope;
|
|
|
|
} else if (Flags & FunctionDeclarationScope) {
|
|
|
|
OS << "FunctionDeclarationScope";
|
|
|
|
Flags &= ~FunctionDeclarationScope;
|
|
|
|
} else if (Flags & AtCatchScope) {
|
|
|
|
OS << "AtCatchScope";
|
|
|
|
Flags &= ~AtCatchScope;
|
|
|
|
} else if (Flags & ObjCMethodScope) {
|
|
|
|
OS << "ObjCMethodScope";
|
|
|
|
Flags &= ~ObjCMethodScope;
|
|
|
|
} else if (Flags & SwitchScope) {
|
|
|
|
OS << "SwitchScope";
|
|
|
|
Flags &= ~SwitchScope;
|
|
|
|
} else if (Flags & TryScope) {
|
|
|
|
OS << "TryScope";
|
|
|
|
Flags &= ~TryScope;
|
|
|
|
} else if (Flags & FnTryCatchScope) {
|
|
|
|
OS << "FnTryCatchScope";
|
|
|
|
Flags &= ~FnTryCatchScope;
|
2014-07-07 06:53:19 +08:00
|
|
|
} else if (Flags & SEHTryScope) {
|
|
|
|
OS << "SEHTryScope";
|
|
|
|
Flags &= ~SEHTryScope;
|
Initial support for Win64 SEH IR emission
The lowering looks a lot like normal EH lowering, with the exception
that the exceptions are caught by executing filter expression code
instead of matching typeinfo globals. The filter expressions are
outlined into functions which are used in landingpad clauses where
typeinfo would normally go.
Major aspects that still need work:
- Non-call exceptions in __try bodies won't work yet. The plan is to
outline the __try block in the frontend to keep things simple.
- Filter expressions cannot use local variables until capturing is
implemented.
- __finally blocks will not run after exceptions. Fixing this requires
work in the LLVM SEH preparation pass.
The IR lowering looks like this:
// C code:
bool safe_div(int n, int d, int *r) {
__try {
*r = normal_div(n, d);
} __except(_exception_code() == EXCEPTION_INT_DIVIDE_BY_ZERO) {
return false;
}
return true;
}
; LLVM IR:
define i32 @filter(i8* %e, i8* %fp) {
%ehptrs = bitcast i8* %e to i32**
%ehrec = load i32** %ehptrs
%code = load i32* %ehrec
%matches = icmp eq i32 %code, i32 u0xC0000094
%matches.i32 = zext i1 %matches to i32
ret i32 %matches.i32
}
define i1 zeroext @safe_div(i32 %n, i32 %d, i32* %r) {
%rr = invoke i32 @normal_div(i32 %n, i32 %d)
to label %normal unwind to label %lpad
normal:
store i32 %rr, i32* %r
ret i1 1
lpad:
%ehvals = landingpad {i8*, i32} personality i32 (...)* @__C_specific_handler
catch i8* bitcast (i32 (i8*, i8*)* @filter to i8*)
%ehptr = extractvalue {i8*, i32} %ehvals, i32 0
%sel = extractvalue {i8*, i32} %ehvals, i32 1
%filter_sel = call i32 @llvm.eh.seh.typeid.for(i8* bitcast (i32 (i8*, i8*)* @filter to i8*))
%matches = icmp eq i32 %sel, %filter_sel
br i1 %matches, label %eh.except, label %eh.resume
eh.except:
ret i1 false
eh.resume:
resume
}
Reviewers: rjmccall, rsmith, majnemer
Differential Revision: http://reviews.llvm.org/D5607
llvm-svn: 226760
2015-01-22 09:36:17 +08:00
|
|
|
} else if (Flags & SEHExceptScope) {
|
|
|
|
OS << "SEHExceptScope";
|
|
|
|
Flags &= ~SEHExceptScope;
|
2014-03-05 16:57:59 +08:00
|
|
|
} else if (Flags & OpenMPDirectiveScope) {
|
|
|
|
OS << "OpenMPDirectiveScope";
|
|
|
|
Flags &= ~OpenMPDirectiveScope;
|
2014-06-03 18:16:47 +08:00
|
|
|
} else if (Flags & OpenMPLoopDirectiveScope) {
|
|
|
|
OS << "OpenMPLoopDirectiveScope";
|
|
|
|
Flags &= ~OpenMPLoopDirectiveScope;
|
|
|
|
} else if (Flags & OpenMPSimdDirectiveScope) {
|
|
|
|
OS << "OpenMPSimdDirectiveScope";
|
|
|
|
Flags &= ~OpenMPSimdDirectiveScope;
|
2014-03-05 16:57:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Flags)
|
|
|
|
OS << " | ";
|
|
|
|
}
|
|
|
|
if (HasFlags)
|
|
|
|
OS << '\n';
|
|
|
|
|
|
|
|
if (const Scope *Parent = getParent())
|
|
|
|
OS << "Parent: (clang::Scope*)" << Parent << '\n';
|
|
|
|
|
|
|
|
OS << "Depth: " << Depth << '\n';
|
2015-03-20 05:54:30 +08:00
|
|
|
OS << "MSLastManglingNumber: " << getMSLastManglingNumber() << '\n';
|
|
|
|
OS << "MSCurManglingNumber: " << getMSCurManglingNumber() << '\n';
|
2014-03-05 16:57:59 +08:00
|
|
|
if (const DeclContext *DC = getEntity())
|
|
|
|
OS << "Entity : (clang::DeclContext*)" << DC << '\n';
|
2014-05-03 08:41:18 +08:00
|
|
|
|
|
|
|
if (NRVO.getInt())
|
2015-03-20 05:54:30 +08:00
|
|
|
OS << "NRVO not allowed\n";
|
2014-05-03 08:41:18 +08:00
|
|
|
else if (NRVO.getPointer())
|
|
|
|
OS << "NRVO candidate : (clang::VarDecl*)" << NRVO.getPointer() << '\n';
|
2014-03-05 16:57:59 +08:00
|
|
|
}
|