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-16 01:47:39 +08:00
|
|
|
/// Out-of-line virtual destructor to provide home for ActionBase class.
|
2009-03-05 15:24:28 +08:00
|
|
|
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
|
2009-03-29 03:18:32 +08:00
|
|
|
Action::DeclPtrTy Action::ActOnUsingDirective(Scope *CurScope,
|
|
|
|
SourceLocation UsingLoc,
|
|
|
|
SourceLocation NamespcLoc,
|
|
|
|
const CXXScopeSpec &SS,
|
|
|
|
SourceLocation IdentLoc,
|
|
|
|
IdentifierInfo *NamespcName,
|
|
|
|
AttributeList *AttrList) {
|
2009-03-05 15:24:28 +08:00
|
|
|
|
|
|
|
// 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;
|
2009-03-29 03:18:32 +08:00
|
|
|
return DeclPtrTy();
|
2009-03-05 15:24:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 16:00:35 +08:00
|
|
|
void PrettyStackTraceActionsDecl::print(llvm::raw_ostream &OS) const {
|
2009-03-05 09:25:28 +08:00
|
|
|
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>();
|
2009-03-25 01:05:27 +08:00
|
|
|
new (TI) TypeNameInfo(isTypename, II->getFETokenInfo<TypeNameInfo>());
|
2009-01-18 17:39:41 +08:00
|
|
|
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-03-31 08:43:58 +08:00
|
|
|
MinimalAction::isTemplateName(const IdentifierInfo &II, Scope *S,
|
2009-03-31 06:58:21 +08:00
|
|
|
TemplateTy &TemplateDecl,
|
2009-02-10 02:46:07 +08:00
|
|
|
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.
|
2009-03-29 03:18:32 +08:00
|
|
|
Action::DeclPtrTy
|
2009-03-30 00:50:03 +08:00
|
|
|
MinimalAction::ActOnDeclarator(Scope *S, Declarator &D) {
|
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.
|
2009-03-29 03:18:32 +08:00
|
|
|
if (II == 0) return DeclPtrTy();
|
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.
|
2009-03-29 03:18:32 +08:00
|
|
|
S->AddDecl(DeclPtrTy::make(II));
|
2006-10-28 07:18:49 +08:00
|
|
|
}
|
2009-03-29 03:18:32 +08:00
|
|
|
return DeclPtrTy();
|
2006-10-28 07:18:49 +08:00
|
|
|
}
|
|
|
|
|
2009-03-29 03:18:32 +08:00
|
|
|
Action::DeclPtrTy
|
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,
|
2009-03-29 03:18:32 +08:00
|
|
|
const DeclPtrTy *ProtoRefs,
|
2008-07-26 12:13:19 +08:00
|
|
|
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);
|
2009-03-29 03:18:32 +08:00
|
|
|
return DeclPtrTy();
|
2007-09-07 05:24:23 +08:00
|
|
|
}
|
|
|
|
|
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.
|
2009-03-29 03:18:32 +08:00
|
|
|
Action::DeclPtrTy
|
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.
|
2009-03-29 03:18:32 +08:00
|
|
|
TUScope->AddDecl(DeclPtrTy::make(IdentList[i]));
|
2006-10-28 07:18:49 +08:00
|
|
|
}
|
2009-03-29 03:18:32 +08:00
|
|
|
return DeclPtrTy();
|
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) {
|
2009-03-29 03:18:32 +08:00
|
|
|
IdentifierInfo &II = *(*I).getAs<IdentifierInfo>();
|
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
|
|
|
}
|