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 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 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); |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 48 | /// isTypeName - This looks at the IdentifierInfo::FETokenInfo field to |
| 49 | /// determine whether the name is a type name (objc class name or typedef) or |
| 50 | /// not in this scope. |
| 51 | Action::DeclTy * |
| 52 | MinimalAction::isTypeName(const IdentifierInfo &II, Scope *S) const { |
| 53 | if (TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>()) |
| 54 | if (TI->isTypeName) |
| 55 | return TI; |
| 56 | return 0; |
| 57 | } |
| 58 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 59 | /// ActOnDeclarator - If this is a typedef declarator, we modify the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 60 | /// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is |
| 61 | /// popped. |
| 62 | Action::DeclTy * |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 63 | MinimalAction::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *LastInGroup) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 64 | IdentifierInfo *II = D.getIdentifier(); |
| 65 | |
| 66 | // If there is no identifier associated with this declarator, bail out. |
| 67 | if (II == 0) return 0; |
| 68 | |
| 69 | TypeNameInfo *weCurrentlyHaveTypeInfo = II->getFETokenInfo<TypeNameInfo>(); |
| 70 | bool isTypeName = |
| 71 | D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef; |
| 72 | |
| 73 | // this check avoids creating TypeNameInfo objects for the common case. |
| 74 | // It does need to handle the uncommon case of shadowing a typedef name with a |
| 75 | // non-typedef name. e.g. { typedef int a; a xx; { int a; } } |
| 76 | if (weCurrentlyHaveTypeInfo || isTypeName) { |
| 77 | TypeNameInfo *TI = new TypeNameInfo(isTypeName, weCurrentlyHaveTypeInfo); |
| 78 | |
| 79 | II->setFETokenInfo(TI); |
| 80 | |
| 81 | // Remember that this needs to be removed when the scope is popped. |
| 82 | S->AddDecl(II); |
| 83 | } |
| 84 | return 0; |
| 85 | } |
| 86 | |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 87 | Action::DeclTy * |
Steve Naroff | e440eb8 | 2007-10-10 17:32:04 +0000 | [diff] [blame] | 88 | MinimalAction::ActOnStartClassInterface(SourceLocation AtInterafceLoc, |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 89 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 90 | IdentifierInfo *SuperName, SourceLocation SuperLoc, |
| 91 | IdentifierInfo **ProtocolNames, unsigned NumProtocols, |
Steve Naroff | f908a87 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 92 | SourceLocation EndProtoLoc, AttributeList *AttrList) { |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 93 | TypeNameInfo *TI = |
| 94 | new TypeNameInfo(1, ClassName->getFETokenInfo<TypeNameInfo>()); |
| 95 | |
| 96 | ClassName->setFETokenInfo(TI); |
| 97 | return 0; |
| 98 | } |
| 99 | |
Steve Naroff | 37e58d1 | 2007-10-02 22:39:18 +0000 | [diff] [blame] | 100 | /// ActOnForwardClassDeclaration - |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 101 | /// Scope will always be top level file scope. |
| 102 | Action::DeclTy * |
Steve Naroff | e440eb8 | 2007-10-10 17:32:04 +0000 | [diff] [blame] | 103 | MinimalAction::ActOnForwardClassDeclaration(SourceLocation AtClassLoc, |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 104 | IdentifierInfo **IdentList, unsigned NumElts) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 105 | for (unsigned i = 0; i != NumElts; ++i) { |
| 106 | TypeNameInfo *TI = |
| 107 | new TypeNameInfo(1, IdentList[i]->getFETokenInfo<TypeNameInfo>()); |
| 108 | |
| 109 | IdentList[i]->setFETokenInfo(TI); |
| 110 | |
| 111 | // Remember that this needs to be removed when the scope is popped. |
Steve Naroff | e440eb8 | 2007-10-10 17:32:04 +0000 | [diff] [blame] | 112 | TUScope->AddDecl(IdentList[i]); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 113 | } |
| 114 | return 0; |
| 115 | } |
| 116 | |
Steve Naroff | 640db42 | 2007-10-10 17:45:44 +0000 | [diff] [blame] | 117 | /// ActOnPopScope - When a scope is popped, if any typedefs are now out-of-scope, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 118 | /// they are removed from the IdentifierInfo::FETokenInfo field. |
Steve Naroff | 640db42 | 2007-10-10 17:45:44 +0000 | [diff] [blame] | 119 | void MinimalAction::ActOnPopScope(SourceLocation Loc, Scope *S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 120 | for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end(); |
| 121 | I != E; ++I) { |
| 122 | IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I); |
| 123 | TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>(); |
| 124 | assert(TI && "This decl didn't get pushed??"); |
| 125 | |
| 126 | if (TI) { |
| 127 | TypeNameInfo *Next = TI->Prev; |
| 128 | delete TI; |
| 129 | |
| 130 | II.setFETokenInfo(Next); |
| 131 | } |
| 132 | } |
| 133 | } |