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