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