Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- MinimalAction.cpp - Implement the MinimalAction class ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the MinimalAction interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Parse/Parser.h" |
| 15 | #include "clang/Parse/DeclSpec.h" |
| 16 | #include "clang/Parse/Scope.h" |
Chris Lattner | e1ae1e9 | 2009-01-18 09:39:41 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Allocator.h" |
| 18 | #include "llvm/Support/RecyclingAllocator.h" |
Chris Lattner | 21ff9c9 | 2009-03-05 01:25:28 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
Chris Lattner | 0102c30 | 2009-03-05 07:24:28 +0000 | [diff] [blame] | 22 | /// Out-of-line virtual destructor to provide home for Action class. |
| 23 | ActionBase::~ActionBase() {} |
| 24 | |
| 25 | /// Out-of-line virtual destructor to provide home for Action class. |
| 26 | Action::~Action() {} |
| 27 | |
| 28 | // Defined out-of-line here because of dependecy on AttributeList |
| 29 | Action::DeclTy *Action::ActOnUsingDirective(Scope *CurScope, |
| 30 | SourceLocation UsingLoc, |
| 31 | SourceLocation NamespcLoc, |
| 32 | const CXXScopeSpec &SS, |
| 33 | SourceLocation IdentLoc, |
| 34 | IdentifierInfo *NamespcName, |
| 35 | AttributeList *AttrList) { |
| 36 | |
| 37 | // FIXME: Parser seems to assume that Action::ActOn* takes ownership over |
| 38 | // passed AttributeList, however other actions don't free it, is it |
| 39 | // temporary state or bug? |
| 40 | delete AttrList; |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 45 | void PrettyStackTraceActionsDecl::print(llvm::raw_ostream &OS) const { |
Chris Lattner | 21ff9c9 | 2009-03-05 01:25:28 +0000 | [diff] [blame] | 46 | if (Loc.isValid()) { |
| 47 | Loc.print(OS, SM); |
| 48 | OS << ": "; |
| 49 | } |
| 50 | OS << Message; |
| 51 | |
| 52 | std::string Name = Actions.getDeclName(TheDecl); |
| 53 | if (!Name.empty()) |
| 54 | OS << " '" << Name << '\''; |
| 55 | |
| 56 | OS << '\n'; |
| 57 | } |
| 58 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 59 | /// TypeNameInfo - A link exists here for each scope that an identifier is |
| 60 | /// defined. |
Chris Lattner | e1ae1e9 | 2009-01-18 09:39:41 +0000 | [diff] [blame] | 61 | namespace { |
| 62 | struct TypeNameInfo { |
| 63 | TypeNameInfo *Prev; |
| 64 | bool isTypeName; |
| 65 | |
| 66 | TypeNameInfo(bool istypename, TypeNameInfo *prev) { |
| 67 | isTypeName = istypename; |
| 68 | Prev = prev; |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | struct TypeNameInfoTable { |
| 73 | llvm::RecyclingAllocator<llvm::BumpPtrAllocator, TypeNameInfo> Allocator; |
| 74 | |
| 75 | void AddEntry(bool isTypename, IdentifierInfo *II) { |
| 76 | TypeNameInfo *TI = Allocator.Allocate<TypeNameInfo>(); |
| 77 | new (TI) TypeNameInfo(1, II->getFETokenInfo<TypeNameInfo>()); |
| 78 | II->setFETokenInfo(TI); |
| 79 | } |
| 80 | |
| 81 | void DeleteEntry(TypeNameInfo *Entry) { |
| 82 | Entry->~TypeNameInfo(); |
| 83 | Allocator.Deallocate(Entry); |
| 84 | } |
| 85 | }; |
| 86 | } |
| 87 | |
| 88 | static TypeNameInfoTable *getTable(void *TP) { |
| 89 | return static_cast<TypeNameInfoTable*>(TP); |
| 90 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 91 | |
Daniel Dunbar | e10b0f2 | 2008-10-31 08:56:51 +0000 | [diff] [blame] | 92 | MinimalAction::MinimalAction(Preprocessor &pp) |
Chris Lattner | e1ae1e9 | 2009-01-18 09:39:41 +0000 | [diff] [blame] | 93 | : Idents(pp.getIdentifierTable()), PP(pp) { |
| 94 | TypeNameInfoTablePtr = new TypeNameInfoTable(); |
| 95 | } |
| 96 | |
| 97 | MinimalAction::~MinimalAction() { |
| 98 | delete getTable(TypeNameInfoTablePtr); |
| 99 | } |
Daniel Dunbar | e10b0f2 | 2008-10-31 08:56:51 +0000 | [diff] [blame] | 100 | |
| 101 | void MinimalAction::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) { |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 102 | TUScope = S; |
Daniel Dunbar | e10b0f2 | 2008-10-31 08:56:51 +0000 | [diff] [blame] | 103 | if (!PP.getLangOptions().ObjC1) return; |
| 104 | |
Chris Lattner | e1ae1e9 | 2009-01-18 09:39:41 +0000 | [diff] [blame] | 105 | |
| 106 | TypeNameInfoTable &TNIT = *getTable(TypeNameInfoTablePtr); |
| 107 | |
| 108 | // Recognize the ObjC built-in type identifiers as types. |
| 109 | TNIT.AddEntry(true, &Idents.get("id")); |
| 110 | TNIT.AddEntry(true, &Idents.get("SEL")); |
| 111 | TNIT.AddEntry(true, &Idents.get("Class")); |
| 112 | TNIT.AddEntry(true, &Idents.get("Protocol")); |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 115 | /// isTypeName - This looks at the IdentifierInfo::FETokenInfo field to |
| 116 | /// determine whether the name is a type name (objc class name or typedef) or |
| 117 | /// not in this scope. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 118 | /// |
| 119 | /// FIXME: Use the passed CXXScopeSpec for accurate C++ type checking. |
Argyrios Kyrtzidis | 39caa08 | 2008-08-01 10:35:27 +0000 | [diff] [blame] | 120 | Action::TypeTy * |
Douglas Gregor | b696ea3 | 2009-02-04 17:00:24 +0000 | [diff] [blame] | 121 | MinimalAction::getTypeName(IdentifierInfo &II, SourceLocation Loc, |
| 122 | Scope *S, const CXXScopeSpec *SS) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 123 | if (TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>()) |
| 124 | if (TI->isTypeName) |
| 125 | return TI; |
| 126 | return 0; |
| 127 | } |
| 128 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 129 | /// isCurrentClassName - Always returns false, because MinimalAction |
| 130 | /// does not support C++ classes with constructors. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 131 | bool MinimalAction::isCurrentClassName(const IdentifierInfo &, Scope *, |
| 132 | const CXXScopeSpec *) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 133 | return false; |
| 134 | } |
| 135 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 136 | TemplateNameKind |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 137 | MinimalAction::isTemplateName(IdentifierInfo &II, Scope *S, |
| 138 | DeclTy *&TemplateDecl, |
| 139 | const CXXScopeSpec *SS) { |
| 140 | return TNK_Non_template; |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 143 | /// ActOnDeclarator - If this is a typedef declarator, we modify the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 144 | /// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is |
| 145 | /// popped. |
| 146 | Action::DeclTy * |
Daniel Dunbar | 914701e | 2008-08-05 16:28:08 +0000 | [diff] [blame] | 147 | MinimalAction::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *LastInGroup) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 148 | IdentifierInfo *II = D.getIdentifier(); |
| 149 | |
| 150 | // If there is no identifier associated with this declarator, bail out. |
| 151 | if (II == 0) return 0; |
| 152 | |
| 153 | TypeNameInfo *weCurrentlyHaveTypeInfo = II->getFETokenInfo<TypeNameInfo>(); |
| 154 | bool isTypeName = |
| 155 | D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef; |
| 156 | |
| 157 | // this check avoids creating TypeNameInfo objects for the common case. |
Ted Kremenek | a34ea07 | 2008-08-04 22:51:42 +0000 | [diff] [blame] | 158 | // It does need to handle the uncommon case of shadowing a typedef name with a |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 159 | // non-typedef name. e.g. { typedef int a; a xx; { int a; } } |
| 160 | if (weCurrentlyHaveTypeInfo || isTypeName) { |
Chris Lattner | e1ae1e9 | 2009-01-18 09:39:41 +0000 | [diff] [blame] | 161 | // Allocate and add the 'TypeNameInfo' "decl". |
| 162 | getTable(TypeNameInfoTablePtr)->AddEntry(isTypeName, II); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 163 | |
| 164 | // Remember that this needs to be removed when the scope is popped. |
| 165 | S->AddDecl(II); |
| 166 | } |
| 167 | return 0; |
| 168 | } |
| 169 | |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 170 | Action::DeclTy * |
Chris Lattner | cfb0ef5 | 2008-07-21 07:13:18 +0000 | [diff] [blame] | 171 | MinimalAction::ActOnStartClassInterface(SourceLocation AtInterfaceLoc, |
Chris Lattner | 06036d3 | 2008-07-26 04:13:19 +0000 | [diff] [blame] | 172 | IdentifierInfo *ClassName, |
| 173 | SourceLocation ClassLoc, |
| 174 | IdentifierInfo *SuperName, |
| 175 | SourceLocation SuperLoc, |
| 176 | DeclTy * const *ProtoRefs, |
| 177 | unsigned NumProtocols, |
| 178 | SourceLocation EndProtoLoc, |
| 179 | AttributeList *AttrList) { |
Chris Lattner | e1ae1e9 | 2009-01-18 09:39:41 +0000 | [diff] [blame] | 180 | // Allocate and add the 'TypeNameInfo' "decl". |
| 181 | getTable(TypeNameInfoTablePtr)->AddEntry(true, ClassName); |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | |
Steve Naroff | 37e58d1 | 2007-10-02 22:39:18 +0000 | [diff] [blame] | 185 | /// ActOnForwardClassDeclaration - |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 186 | /// Scope will always be top level file scope. |
| 187 | Action::DeclTy * |
Steve Naroff | e440eb8 | 2007-10-10 17:32:04 +0000 | [diff] [blame] | 188 | MinimalAction::ActOnForwardClassDeclaration(SourceLocation AtClassLoc, |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 189 | IdentifierInfo **IdentList, unsigned NumElts) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 190 | for (unsigned i = 0; i != NumElts; ++i) { |
Chris Lattner | e1ae1e9 | 2009-01-18 09:39:41 +0000 | [diff] [blame] | 191 | // Allocate and add the 'TypeNameInfo' "decl". |
| 192 | getTable(TypeNameInfoTablePtr)->AddEntry(true, IdentList[i]); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 193 | |
| 194 | // Remember that this needs to be removed when the scope is popped. |
Steve Naroff | e440eb8 | 2007-10-10 17:32:04 +0000 | [diff] [blame] | 195 | TUScope->AddDecl(IdentList[i]); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 196 | } |
| 197 | return 0; |
| 198 | } |
| 199 | |
Ted Kremenek | a34ea07 | 2008-08-04 22:51:42 +0000 | [diff] [blame] | 200 | /// ActOnPopScope - When a scope is popped, if any typedefs are now |
| 201 | /// out-of-scope, they are removed from the IdentifierInfo::FETokenInfo field. |
Steve Naroff | 640db42 | 2007-10-10 17:45:44 +0000 | [diff] [blame] | 202 | void MinimalAction::ActOnPopScope(SourceLocation Loc, Scope *S) { |
Chris Lattner | e1ae1e9 | 2009-01-18 09:39:41 +0000 | [diff] [blame] | 203 | TypeNameInfoTable &Table = *getTable(TypeNameInfoTablePtr); |
| 204 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 205 | for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end(); |
| 206 | I != E; ++I) { |
| 207 | IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I); |
| 208 | TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>(); |
| 209 | assert(TI && "This decl didn't get pushed??"); |
| 210 | |
| 211 | if (TI) { |
| 212 | TypeNameInfo *Next = TI->Prev; |
Chris Lattner | e1ae1e9 | 2009-01-18 09:39:41 +0000 | [diff] [blame] | 213 | Table.DeleteEntry(TI); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 214 | |
| 215 | II.setFETokenInfo(Next); |
| 216 | } |
| 217 | } |
| 218 | } |