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" |
| 17 | using namespace clang; |
| 18 | |
| 19 | /// TypeNameInfo - A link exists here for each scope that an identifier is |
| 20 | /// defined. |
| 21 | struct TypeNameInfo { |
| 22 | TypeNameInfo *Prev; |
| 23 | bool isTypeName; |
| 24 | |
| 25 | TypeNameInfo(bool istypename, TypeNameInfo *prev) { |
| 26 | isTypeName = istypename; |
| 27 | Prev = prev; |
| 28 | } |
| 29 | }; |
| 30 | |
Daniel Dunbar | e10b0f2 | 2008-10-31 08:56:51 +0000 | [diff] [blame] | 31 | MinimalAction::MinimalAction(Preprocessor &pp) |
| 32 | : Idents(pp.getIdentifierTable()), PP(pp) {} |
| 33 | |
| 34 | void MinimalAction::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) { |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 35 | TUScope = S; |
Daniel Dunbar | e10b0f2 | 2008-10-31 08:56:51 +0000 | [diff] [blame] | 36 | if (!PP.getLangOptions().ObjC1) return; |
| 37 | |
| 38 | // recognize the ObjC built-in type identifiers. |
Steve Naroff | b4292f2 | 2007-10-31 20:55:39 +0000 | [diff] [blame] | 39 | IdentifierInfo *II; |
| 40 | TypeNameInfo *TI; |
Steve Naroff | b4292f2 | 2007-10-31 20:55:39 +0000 | [diff] [blame] | 41 | II = &Idents.get("id"); |
| 42 | TI = new TypeNameInfo(1, II->getFETokenInfo<TypeNameInfo>()); |
| 43 | II->setFETokenInfo(TI); |
| 44 | II = &Idents.get("SEL"); |
| 45 | TI = new TypeNameInfo(1, II->getFETokenInfo<TypeNameInfo>()); |
| 46 | II->setFETokenInfo(TI); |
| 47 | II = &Idents.get("Class"); |
| 48 | TI = new TypeNameInfo(1, II->getFETokenInfo<TypeNameInfo>()); |
| 49 | II->setFETokenInfo(TI); |
Fariborz Jahanian | 66c5dfc | 2007-12-07 00:18:54 +0000 | [diff] [blame] | 50 | II = &Idents.get("Protocol"); |
| 51 | TI = new TypeNameInfo(1, II->getFETokenInfo<TypeNameInfo>()); |
| 52 | II->setFETokenInfo(TI); |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 55 | /// isTypeName - This looks at the IdentifierInfo::FETokenInfo field to |
| 56 | /// determine whether the name is a type name (objc class name or typedef) or |
| 57 | /// not in this scope. |
Argyrios Kyrtzidis | 39caa08 | 2008-08-01 10:35:27 +0000 | [diff] [blame] | 58 | Action::TypeTy * |
Steve Naroff | b327ce0 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 59 | MinimalAction::isTypeName(const IdentifierInfo &II, Scope *S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 60 | if (TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>()) |
| 61 | if (TI->isTypeName) |
| 62 | return TI; |
| 63 | return 0; |
| 64 | } |
| 65 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame^] | 66 | /// isCurrentClassName - Always returns false, because MinimalAction |
| 67 | /// does not support C++ classes with constructors. |
| 68 | bool MinimalAction::isCurrentClassName(const IdentifierInfo &, Scope *) { |
| 69 | return false; |
| 70 | } |
| 71 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 72 | /// ActOnDeclarator - If this is a typedef declarator, we modify the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 73 | /// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is |
| 74 | /// popped. |
| 75 | Action::DeclTy * |
Daniel Dunbar | 914701e | 2008-08-05 16:28:08 +0000 | [diff] [blame] | 76 | MinimalAction::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *LastInGroup) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 77 | IdentifierInfo *II = D.getIdentifier(); |
| 78 | |
| 79 | // If there is no identifier associated with this declarator, bail out. |
| 80 | if (II == 0) return 0; |
| 81 | |
| 82 | TypeNameInfo *weCurrentlyHaveTypeInfo = II->getFETokenInfo<TypeNameInfo>(); |
| 83 | bool isTypeName = |
| 84 | D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef; |
| 85 | |
| 86 | // this check avoids creating TypeNameInfo objects for the common case. |
Ted Kremenek | a34ea07 | 2008-08-04 22:51:42 +0000 | [diff] [blame] | 87 | // 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] | 88 | // non-typedef name. e.g. { typedef int a; a xx; { int a; } } |
| 89 | if (weCurrentlyHaveTypeInfo || isTypeName) { |
| 90 | TypeNameInfo *TI = new TypeNameInfo(isTypeName, weCurrentlyHaveTypeInfo); |
| 91 | |
| 92 | II->setFETokenInfo(TI); |
| 93 | |
| 94 | // Remember that this needs to be removed when the scope is popped. |
| 95 | S->AddDecl(II); |
| 96 | } |
| 97 | return 0; |
| 98 | } |
| 99 | |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 100 | Action::DeclTy * |
Chris Lattner | cfb0ef5 | 2008-07-21 07:13:18 +0000 | [diff] [blame] | 101 | MinimalAction::ActOnStartClassInterface(SourceLocation AtInterfaceLoc, |
Chris Lattner | 06036d3 | 2008-07-26 04:13:19 +0000 | [diff] [blame] | 102 | IdentifierInfo *ClassName, |
| 103 | SourceLocation ClassLoc, |
| 104 | IdentifierInfo *SuperName, |
| 105 | SourceLocation SuperLoc, |
| 106 | DeclTy * const *ProtoRefs, |
| 107 | unsigned NumProtocols, |
| 108 | SourceLocation EndProtoLoc, |
| 109 | AttributeList *AttrList) { |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 110 | TypeNameInfo *TI = |
| 111 | new TypeNameInfo(1, ClassName->getFETokenInfo<TypeNameInfo>()); |
| 112 | |
| 113 | ClassName->setFETokenInfo(TI); |
| 114 | return 0; |
| 115 | } |
| 116 | |
Steve Naroff | 37e58d1 | 2007-10-02 22:39:18 +0000 | [diff] [blame] | 117 | /// ActOnForwardClassDeclaration - |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 118 | /// Scope will always be top level file scope. |
| 119 | Action::DeclTy * |
Steve Naroff | e440eb8 | 2007-10-10 17:32:04 +0000 | [diff] [blame] | 120 | MinimalAction::ActOnForwardClassDeclaration(SourceLocation AtClassLoc, |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 121 | IdentifierInfo **IdentList, unsigned NumElts) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 122 | for (unsigned i = 0; i != NumElts; ++i) { |
| 123 | TypeNameInfo *TI = |
| 124 | new TypeNameInfo(1, IdentList[i]->getFETokenInfo<TypeNameInfo>()); |
| 125 | |
| 126 | IdentList[i]->setFETokenInfo(TI); |
| 127 | |
| 128 | // Remember that this needs to be removed when the scope is popped. |
Steve Naroff | e440eb8 | 2007-10-10 17:32:04 +0000 | [diff] [blame] | 129 | TUScope->AddDecl(IdentList[i]); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 130 | } |
| 131 | return 0; |
| 132 | } |
| 133 | |
Ted Kremenek | a34ea07 | 2008-08-04 22:51:42 +0000 | [diff] [blame] | 134 | /// ActOnPopScope - When a scope is popped, if any typedefs are now |
| 135 | /// out-of-scope, they are removed from the IdentifierInfo::FETokenInfo field. |
Steve Naroff | 640db42 | 2007-10-10 17:45:44 +0000 | [diff] [blame] | 136 | void MinimalAction::ActOnPopScope(SourceLocation Loc, Scope *S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 137 | for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end(); |
| 138 | I != E; ++I) { |
| 139 | IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I); |
| 140 | TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>(); |
| 141 | assert(TI && "This decl didn't get pushed??"); |
| 142 | |
| 143 | if (TI) { |
| 144 | TypeNameInfo *Next = TI->Prev; |
| 145 | delete TI; |
| 146 | |
| 147 | II.setFETokenInfo(Next); |
| 148 | } |
| 149 | } |
| 150 | } |