blob: 9cd5b979ca66a6fe422993e8d9a04f53373ba939 [file] [log] [blame]
Chris Lattnerc62b6c22006-11-05 18:44:26 +00001//===--- MinimalAction.cpp - Implement the MinimalAction class ------------===//
Chris Lattnera5534f92006-08-14 00:38:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera5534f92006-08-14 00:38:06 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerc62b6c22006-11-05 18:44:26 +000010// This file implements the MinimalAction interface.
Chris Lattnera5534f92006-08-14 00:38:06 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner288e86ff12006-11-11 23:03:42 +000015#include "clang/Parse/DeclSpec.h"
Chris Lattnerffe65b32006-08-14 01:28:29 +000016#include "clang/Parse/Scope.h"
Chris Lattnerc671e062009-01-18 09:39:41 +000017#include "llvm/Support/Allocator.h"
18#include "llvm/Support/RecyclingAllocator.h"
Chris Lattnera5534f92006-08-14 00:38:06 +000019using namespace clang;
20
Steve Naroffb419d3a2006-10-27 23:18:49 +000021/// TypeNameInfo - A link exists here for each scope that an identifier is
Chris Lattnerffe65b32006-08-14 01:28:29 +000022/// defined.
Chris Lattnerc671e062009-01-18 09:39:41 +000023namespace {
24 struct TypeNameInfo {
25 TypeNameInfo *Prev;
26 bool isTypeName;
27
28 TypeNameInfo(bool istypename, TypeNameInfo *prev) {
29 isTypeName = istypename;
30 Prev = prev;
31 }
32 };
33
34 struct TypeNameInfoTable {
35 llvm::RecyclingAllocator<llvm::BumpPtrAllocator, TypeNameInfo> Allocator;
36
37 void AddEntry(bool isTypename, IdentifierInfo *II) {
38 TypeNameInfo *TI = Allocator.Allocate<TypeNameInfo>();
39 new (TI) TypeNameInfo(1, II->getFETokenInfo<TypeNameInfo>());
40 II->setFETokenInfo(TI);
41 }
42
43 void DeleteEntry(TypeNameInfo *Entry) {
44 Entry->~TypeNameInfo();
45 Allocator.Deallocate(Entry);
46 }
47 };
48}
49
50static TypeNameInfoTable *getTable(void *TP) {
51 return static_cast<TypeNameInfoTable*>(TP);
52}
Chris Lattnerffe65b32006-08-14 01:28:29 +000053
Daniel Dunbarf8362f92008-10-31 08:56:51 +000054MinimalAction::MinimalAction(Preprocessor &pp)
Chris Lattnerc671e062009-01-18 09:39:41 +000055 : Idents(pp.getIdentifierTable()), PP(pp) {
56 TypeNameInfoTablePtr = new TypeNameInfoTable();
57}
58
59MinimalAction::~MinimalAction() {
60 delete getTable(TypeNameInfoTablePtr);
61}
Daniel Dunbarf8362f92008-10-31 08:56:51 +000062
63void MinimalAction::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
Steve Naroff6d40db02007-10-31 18:42:27 +000064 TUScope = S;
Daniel Dunbarf8362f92008-10-31 08:56:51 +000065 if (!PP.getLangOptions().ObjC1) return;
66
Chris Lattnerc671e062009-01-18 09:39:41 +000067
68 TypeNameInfoTable &TNIT = *getTable(TypeNameInfoTablePtr);
69
70 // Recognize the ObjC built-in type identifiers as types.
71 TNIT.AddEntry(true, &Idents.get("id"));
72 TNIT.AddEntry(true, &Idents.get("SEL"));
73 TNIT.AddEntry(true, &Idents.get("Class"));
74 TNIT.AddEntry(true, &Idents.get("Protocol"));
Steve Naroff6d40db02007-10-31 18:42:27 +000075}
76
Chris Lattner64b09ee2006-11-03 07:35:12 +000077/// isTypeName - This looks at the IdentifierInfo::FETokenInfo field to
78/// determine whether the name is a type name (objc class name or typedef) or
79/// not in this scope.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +000080///
81/// FIXME: Use the passed CXXScopeSpec for accurate C++ type checking.
Argyrios Kyrtzidis25d05e82008-08-01 10:35:27 +000082Action::TypeTy *
Douglas Gregor8a6be5e2009-02-04 17:00:24 +000083MinimalAction::getTypeName(IdentifierInfo &II, SourceLocation Loc,
84 Scope *S, const CXXScopeSpec *SS) {
Chris Lattner2ebe4bb2006-11-20 01:29:42 +000085 if (TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>())
86 if (TI->isTypeName)
87 return TI;
88 return 0;
Chris Lattnera5534f92006-08-14 00:38:06 +000089}
90
Douglas Gregor61956c42008-10-31 09:07:45 +000091/// isCurrentClassName - Always returns false, because MinimalAction
92/// does not support C++ classes with constructors.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +000093bool MinimalAction::isCurrentClassName(const IdentifierInfo &, Scope *,
94 const CXXScopeSpec *) {
Douglas Gregor61956c42008-10-31 09:07:45 +000095 return false;
96}
97
Douglas Gregor7f741122009-02-25 19:37:18 +000098TemplateNameKind
Douglas Gregor8bf42052009-02-09 18:46:07 +000099MinimalAction::isTemplateName(IdentifierInfo &II, Scope *S,
100 DeclTy *&TemplateDecl,
101 const CXXScopeSpec *SS) {
102 return TNK_Non_template;
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000103}
104
Steve Naroff30d242c2007-09-15 18:49:24 +0000105/// ActOnDeclarator - If this is a typedef declarator, we modify the
Chris Lattnera5534f92006-08-14 00:38:06 +0000106/// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is
107/// popped.
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000108Action::DeclTy *
Daniel Dunbar1ff1d1f2008-08-05 16:28:08 +0000109MinimalAction::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *LastInGroup) {
Steve Naroffb419d3a2006-10-27 23:18:49 +0000110 IdentifierInfo *II = D.getIdentifier();
111
Chris Lattnerffe65b32006-08-14 01:28:29 +0000112 // If there is no identifier associated with this declarator, bail out.
Steve Naroffb419d3a2006-10-27 23:18:49 +0000113 if (II == 0) return 0;
Chris Lattnerffe65b32006-08-14 01:28:29 +0000114
Steve Naroffb419d3a2006-10-27 23:18:49 +0000115 TypeNameInfo *weCurrentlyHaveTypeInfo = II->getFETokenInfo<TypeNameInfo>();
Chris Lattnerf055d432006-11-28 04:28:12 +0000116 bool isTypeName =
117 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef;
Chris Lattnerffe65b32006-08-14 01:28:29 +0000118
Steve Naroffb419d3a2006-10-27 23:18:49 +0000119 // this check avoids creating TypeNameInfo objects for the common case.
Ted Kremenekbe9b33b2008-08-04 22:51:42 +0000120 // It does need to handle the uncommon case of shadowing a typedef name with a
Steve Naroffb419d3a2006-10-27 23:18:49 +0000121 // non-typedef name. e.g. { typedef int a; a xx; { int a; } }
122 if (weCurrentlyHaveTypeInfo || isTypeName) {
Chris Lattnerc671e062009-01-18 09:39:41 +0000123 // Allocate and add the 'TypeNameInfo' "decl".
124 getTable(TypeNameInfoTablePtr)->AddEntry(isTypeName, II);
Chris Lattnerffe65b32006-08-14 01:28:29 +0000125
Steve Naroffb419d3a2006-10-27 23:18:49 +0000126 // Remember that this needs to be removed when the scope is popped.
127 S->AddDecl(II);
128 }
129 return 0;
130}
131
Steve Naroff09bf8152007-09-06 21:24:23 +0000132Action::DeclTy *
Chris Lattner04648562008-07-21 07:13:18 +0000133MinimalAction::ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000134 IdentifierInfo *ClassName,
135 SourceLocation ClassLoc,
136 IdentifierInfo *SuperName,
137 SourceLocation SuperLoc,
138 DeclTy * const *ProtoRefs,
139 unsigned NumProtocols,
140 SourceLocation EndProtoLoc,
141 AttributeList *AttrList) {
Chris Lattnerc671e062009-01-18 09:39:41 +0000142 // Allocate and add the 'TypeNameInfo' "decl".
143 getTable(TypeNameInfoTablePtr)->AddEntry(true, ClassName);
Steve Naroff09bf8152007-09-06 21:24:23 +0000144 return 0;
145}
146
Steve Naroff0c37b0c2007-10-02 22:39:18 +0000147/// ActOnForwardClassDeclaration -
Chris Lattner64b09ee2006-11-03 07:35:12 +0000148/// Scope will always be top level file scope.
Steve Naroffb419d3a2006-10-27 23:18:49 +0000149Action::DeclTy *
Steve Naroff93eb5f12007-10-10 17:32:04 +0000150MinimalAction::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Steve Naroff09bf8152007-09-06 21:24:23 +0000151 IdentifierInfo **IdentList, unsigned NumElts) {
Chris Lattner64b09ee2006-11-03 07:35:12 +0000152 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnerc671e062009-01-18 09:39:41 +0000153 // Allocate and add the 'TypeNameInfo' "decl".
154 getTable(TypeNameInfoTablePtr)->AddEntry(true, IdentList[i]);
Steve Naroffb419d3a2006-10-27 23:18:49 +0000155
156 // Remember that this needs to be removed when the scope is popped.
Steve Naroff93eb5f12007-10-10 17:32:04 +0000157 TUScope->AddDecl(IdentList[i]);
Steve Naroffb419d3a2006-10-27 23:18:49 +0000158 }
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000159 return 0;
Chris Lattnera5534f92006-08-14 00:38:06 +0000160}
161
Ted Kremenekbe9b33b2008-08-04 22:51:42 +0000162/// ActOnPopScope - When a scope is popped, if any typedefs are now
163/// out-of-scope, they are removed from the IdentifierInfo::FETokenInfo field.
Steve Naroff8308f602007-10-10 17:45:44 +0000164void MinimalAction::ActOnPopScope(SourceLocation Loc, Scope *S) {
Chris Lattnerc671e062009-01-18 09:39:41 +0000165 TypeNameInfoTable &Table = *getTable(TypeNameInfoTablePtr);
166
Chris Lattnerffe65b32006-08-14 01:28:29 +0000167 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
168 I != E; ++I) {
169 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
Steve Naroffb419d3a2006-10-27 23:18:49 +0000170 TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>();
Chris Lattnerffe65b32006-08-14 01:28:29 +0000171 assert(TI && "This decl didn't get pushed??");
Chris Lattnerffe65b32006-08-14 01:28:29 +0000172
Chris Lattnerc62b6c22006-11-05 18:44:26 +0000173 if (TI) {
Steve Naroffb419d3a2006-10-27 23:18:49 +0000174 TypeNameInfo *Next = TI->Prev;
Chris Lattnerc671e062009-01-18 09:39:41 +0000175 Table.DeleteEntry(TI);
Chris Lattnerc62b6c22006-11-05 18:44:26 +0000176
Steve Naroffb419d3a2006-10-27 23:18:49 +0000177 II.setFETokenInfo(Next);
Chris Lattnerc62b6c22006-11-05 18:44:26 +0000178 }
Chris Lattnerffe65b32006-08-14 01:28:29 +0000179 }
Chris Lattnera5534f92006-08-14 00:38:06 +0000180}