2006-11-06 02:44:26 +08:00
|
|
|
//===--- MinimalAction.cpp - Implement the MinimalAction class ------------===//
|
2006-08-14 08:38:06 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-08-14 08:38:06 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2006-11-06 02:44:26 +08:00
|
|
|
// This file implements the MinimalAction interface.
|
2006-08-14 08:38:06 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Parse/Parser.h"
|
2006-11-12 07:03:42 +08:00
|
|
|
#include "clang/Parse/DeclSpec.h"
|
2006-08-14 09:28:29 +08:00
|
|
|
#include "clang/Parse/Scope.h"
|
2009-01-18 17:39:41 +08:00
|
|
|
#include "llvm/Support/Allocator.h"
|
|
|
|
#include "llvm/Support/RecyclingAllocator.h"
|
2009-03-05 09:25:28 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2006-08-14 08:38:06 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2009-03-05 15:24:28 +08:00
|
|
|
/// Out-of-line virtual destructor to provide home for Action class.
|
|
|
|
ActionBase::~ActionBase() {}
|
|
|
|
|
|
|
|
/// Out-of-line virtual destructor to provide home for Action class.
|
|
|
|
Action::~Action() {}
|
|
|
|
|
|
|
|
// Defined out-of-line here because of dependecy on AttributeList
|
|
|
|
Action::DeclTy *Action::ActOnUsingDirective(Scope *CurScope,
|
|
|
|
SourceLocation UsingLoc,
|
|
|
|
SourceLocation NamespcLoc,
|
|
|
|
const CXXScopeSpec &SS,
|
|
|
|
SourceLocation IdentLoc,
|
|
|
|
IdentifierInfo *NamespcName,
|
|
|
|
AttributeList *AttrList) {
|
|
|
|
|
|
|
|
// FIXME: Parser seems to assume that Action::ActOn* takes ownership over
|
|
|
|
// passed AttributeList, however other actions don't free it, is it
|
|
|
|
// temporary state or bug?
|
|
|
|
delete AttrList;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 09:25:28 +08:00
|
|
|
void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
|
|
|
|
if (Loc.isValid()) {
|
|
|
|
Loc.print(OS, SM);
|
|
|
|
OS << ": ";
|
|
|
|
}
|
|
|
|
OS << Message;
|
|
|
|
|
|
|
|
std::string Name = Actions.getDeclName(TheDecl);
|
|
|
|
if (!Name.empty())
|
|
|
|
OS << " '" << Name << '\'';
|
|
|
|
|
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
|
2006-10-28 07:18:49 +08:00
|
|
|
/// TypeNameInfo - A link exists here for each scope that an identifier is
|
2006-08-14 09:28:29 +08:00
|
|
|
/// defined.
|
2009-01-18 17:39:41 +08:00
|
|
|
namespace {
|
|
|
|
struct TypeNameInfo {
|
|
|
|
TypeNameInfo *Prev;
|
|
|
|
bool isTypeName;
|
|
|
|
|
|
|
|
TypeNameInfo(bool istypename, TypeNameInfo *prev) {
|
|
|
|
isTypeName = istypename;
|
|
|
|
Prev = prev;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TypeNameInfoTable {
|
|
|
|
llvm::RecyclingAllocator<llvm::BumpPtrAllocator, TypeNameInfo> Allocator;
|
|
|
|
|
|
|
|
void AddEntry(bool isTypename, IdentifierInfo *II) {
|
|
|
|
TypeNameInfo *TI = Allocator.Allocate<TypeNameInfo>();
|
|
|
|
new (TI) TypeNameInfo(1, II->getFETokenInfo<TypeNameInfo>());
|
|
|
|
II->setFETokenInfo(TI);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeleteEntry(TypeNameInfo *Entry) {
|
|
|
|
Entry->~TypeNameInfo();
|
|
|
|
Allocator.Deallocate(Entry);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static TypeNameInfoTable *getTable(void *TP) {
|
|
|
|
return static_cast<TypeNameInfoTable*>(TP);
|
|
|
|
}
|
2006-08-14 09:28:29 +08:00
|
|
|
|
2008-10-31 16:56:51 +08:00
|
|
|
MinimalAction::MinimalAction(Preprocessor &pp)
|
2009-01-18 17:39:41 +08:00
|
|
|
: Idents(pp.getIdentifierTable()), PP(pp) {
|
|
|
|
TypeNameInfoTablePtr = new TypeNameInfoTable();
|
|
|
|
}
|
|
|
|
|
|
|
|
MinimalAction::~MinimalAction() {
|
|
|
|
delete getTable(TypeNameInfoTablePtr);
|
|
|
|
}
|
2008-10-31 16:56:51 +08:00
|
|
|
|
|
|
|
void MinimalAction::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
|
2007-11-01 02:42:27 +08:00
|
|
|
TUScope = S;
|
2008-10-31 16:56:51 +08:00
|
|
|
if (!PP.getLangOptions().ObjC1) return;
|
|
|
|
|
2009-01-18 17:39:41 +08:00
|
|
|
|
|
|
|
TypeNameInfoTable &TNIT = *getTable(TypeNameInfoTablePtr);
|
|
|
|
|
|
|
|
// Recognize the ObjC built-in type identifiers as types.
|
|
|
|
TNIT.AddEntry(true, &Idents.get("id"));
|
|
|
|
TNIT.AddEntry(true, &Idents.get("SEL"));
|
|
|
|
TNIT.AddEntry(true, &Idents.get("Class"));
|
|
|
|
TNIT.AddEntry(true, &Idents.get("Protocol"));
|
2007-11-01 02:42:27 +08:00
|
|
|
}
|
|
|
|
|
2006-11-03 15:35:12 +08:00
|
|
|
/// isTypeName - This looks at the IdentifierInfo::FETokenInfo field to
|
|
|
|
/// determine whether the name is a type name (objc class name or typedef) or
|
|
|
|
/// not in this scope.
|
2008-11-09 00:45:02 +08:00
|
|
|
///
|
|
|
|
/// FIXME: Use the passed CXXScopeSpec for accurate C++ type checking.
|
2008-08-01 18:35:27 +08:00
|
|
|
Action::TypeTy *
|
2009-02-05 01:00:24 +08:00
|
|
|
MinimalAction::getTypeName(IdentifierInfo &II, SourceLocation Loc,
|
|
|
|
Scope *S, const CXXScopeSpec *SS) {
|
2006-11-20 09:29:42 +08:00
|
|
|
if (TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>())
|
|
|
|
if (TI->isTypeName)
|
|
|
|
return TI;
|
|
|
|
return 0;
|
2006-08-14 08:38:06 +08:00
|
|
|
}
|
|
|
|
|
2008-10-31 17:07:45 +08:00
|
|
|
/// isCurrentClassName - Always returns false, because MinimalAction
|
|
|
|
/// does not support C++ classes with constructors.
|
2008-11-09 00:45:02 +08:00
|
|
|
bool MinimalAction::isCurrentClassName(const IdentifierInfo &, Scope *,
|
|
|
|
const CXXScopeSpec *) {
|
2008-10-31 17:07:45 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
TemplateNameKind
|
2009-02-10 02:46:07 +08:00
|
|
|
MinimalAction::isTemplateName(IdentifierInfo &II, Scope *S,
|
|
|
|
DeclTy *&TemplateDecl,
|
|
|
|
const CXXScopeSpec *SS) {
|
|
|
|
return TNK_Non_template;
|
2008-12-19 03:37:40 +08:00
|
|
|
}
|
|
|
|
|
2007-09-16 02:49:24 +08:00
|
|
|
/// ActOnDeclarator - If this is a typedef declarator, we modify the
|
2006-08-14 08:38:06 +08:00
|
|
|
/// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is
|
|
|
|
/// popped.
|
2006-10-16 08:33:54 +08:00
|
|
|
Action::DeclTy *
|
2008-08-06 00:28:08 +08:00
|
|
|
MinimalAction::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *LastInGroup) {
|
2006-10-28 07:18:49 +08:00
|
|
|
IdentifierInfo *II = D.getIdentifier();
|
|
|
|
|
2006-08-14 09:28:29 +08:00
|
|
|
// If there is no identifier associated with this declarator, bail out.
|
2006-10-28 07:18:49 +08:00
|
|
|
if (II == 0) return 0;
|
2006-08-14 09:28:29 +08:00
|
|
|
|
2006-10-28 07:18:49 +08:00
|
|
|
TypeNameInfo *weCurrentlyHaveTypeInfo = II->getFETokenInfo<TypeNameInfo>();
|
2006-11-28 12:28:12 +08:00
|
|
|
bool isTypeName =
|
|
|
|
D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef;
|
2006-08-14 09:28:29 +08:00
|
|
|
|
2006-10-28 07:18:49 +08:00
|
|
|
// this check avoids creating TypeNameInfo objects for the common case.
|
2008-08-05 06:51:42 +08:00
|
|
|
// It does need to handle the uncommon case of shadowing a typedef name with a
|
2006-10-28 07:18:49 +08:00
|
|
|
// non-typedef name. e.g. { typedef int a; a xx; { int a; } }
|
|
|
|
if (weCurrentlyHaveTypeInfo || isTypeName) {
|
2009-01-18 17:39:41 +08:00
|
|
|
// Allocate and add the 'TypeNameInfo' "decl".
|
|
|
|
getTable(TypeNameInfoTablePtr)->AddEntry(isTypeName, II);
|
2006-08-14 09:28:29 +08:00
|
|
|
|
2006-10-28 07:18:49 +08:00
|
|
|
// Remember that this needs to be removed when the scope is popped.
|
|
|
|
S->AddDecl(II);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-09-07 05:24:23 +08:00
|
|
|
Action::DeclTy *
|
2008-07-21 15:13:18 +08:00
|
|
|
MinimalAction::ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
|
2008-07-26 12:13:19 +08:00
|
|
|
IdentifierInfo *ClassName,
|
|
|
|
SourceLocation ClassLoc,
|
|
|
|
IdentifierInfo *SuperName,
|
|
|
|
SourceLocation SuperLoc,
|
|
|
|
DeclTy * const *ProtoRefs,
|
|
|
|
unsigned NumProtocols,
|
|
|
|
SourceLocation EndProtoLoc,
|
|
|
|
AttributeList *AttrList) {
|
2009-01-18 17:39:41 +08:00
|
|
|
// Allocate and add the 'TypeNameInfo' "decl".
|
|
|
|
getTable(TypeNameInfoTablePtr)->AddEntry(true, ClassName);
|
2007-09-07 05:24:23 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-03 06:39:18 +08:00
|
|
|
/// ActOnForwardClassDeclaration -
|
2006-11-03 15:35:12 +08:00
|
|
|
/// Scope will always be top level file scope.
|
2006-10-28 07:18:49 +08:00
|
|
|
Action::DeclTy *
|
2007-10-11 01:32:04 +08:00
|
|
|
MinimalAction::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
|
2007-09-07 05:24:23 +08:00
|
|
|
IdentifierInfo **IdentList, unsigned NumElts) {
|
2006-11-03 15:35:12 +08:00
|
|
|
for (unsigned i = 0; i != NumElts; ++i) {
|
2009-01-18 17:39:41 +08:00
|
|
|
// Allocate and add the 'TypeNameInfo' "decl".
|
|
|
|
getTable(TypeNameInfoTablePtr)->AddEntry(true, IdentList[i]);
|
2006-10-28 07:18:49 +08:00
|
|
|
|
|
|
|
// Remember that this needs to be removed when the scope is popped.
|
2007-10-11 01:32:04 +08:00
|
|
|
TUScope->AddDecl(IdentList[i]);
|
2006-10-28 07:18:49 +08:00
|
|
|
}
|
2006-10-16 08:33:54 +08:00
|
|
|
return 0;
|
2006-08-14 08:38:06 +08:00
|
|
|
}
|
|
|
|
|
2008-08-05 06:51:42 +08:00
|
|
|
/// ActOnPopScope - When a scope is popped, if any typedefs are now
|
|
|
|
/// out-of-scope, they are removed from the IdentifierInfo::FETokenInfo field.
|
2007-10-11 01:45:44 +08:00
|
|
|
void MinimalAction::ActOnPopScope(SourceLocation Loc, Scope *S) {
|
2009-01-18 17:39:41 +08:00
|
|
|
TypeNameInfoTable &Table = *getTable(TypeNameInfoTablePtr);
|
|
|
|
|
2006-08-14 09:28:29 +08:00
|
|
|
for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
|
2006-10-28 07:18:49 +08:00
|
|
|
TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>();
|
2006-08-14 09:28:29 +08:00
|
|
|
assert(TI && "This decl didn't get pushed??");
|
|
|
|
|
2006-11-06 02:44:26 +08:00
|
|
|
if (TI) {
|
2006-10-28 07:18:49 +08:00
|
|
|
TypeNameInfo *Next = TI->Prev;
|
2009-01-18 17:39:41 +08:00
|
|
|
Table.DeleteEntry(TI);
|
2006-11-06 02:44:26 +08:00
|
|
|
|
2006-10-28 07:18:49 +08:00
|
|
|
II.setFETokenInfo(Next);
|
2006-11-06 02:44:26 +08:00
|
|
|
}
|
2006-08-14 09:28:29 +08:00
|
|
|
}
|
2006-08-14 08:38:06 +08:00
|
|
|
}
|