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