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 | |
| 31 | /// isTypeName - This looks at the IdentifierInfo::FETokenInfo field to |
| 32 | /// determine whether the name is a type name (objc class name or typedef) or |
| 33 | /// not in this scope. |
| 34 | Action::DeclTy * |
| 35 | MinimalAction::isTypeName(const IdentifierInfo &II, Scope *S) const { |
| 36 | if (TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>()) |
| 37 | if (TI->isTypeName) |
| 38 | return TI; |
| 39 | return 0; |
| 40 | } |
| 41 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 42 | /// ActOnDeclarator - If this is a typedef declarator, we modify the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 43 | /// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is |
| 44 | /// popped. |
| 45 | Action::DeclTy * |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 46 | MinimalAction::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *LastInGroup) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 47 | IdentifierInfo *II = D.getIdentifier(); |
| 48 | |
| 49 | // If there is no identifier associated with this declarator, bail out. |
| 50 | if (II == 0) return 0; |
| 51 | |
| 52 | TypeNameInfo *weCurrentlyHaveTypeInfo = II->getFETokenInfo<TypeNameInfo>(); |
| 53 | bool isTypeName = |
| 54 | D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef; |
| 55 | |
| 56 | // this check avoids creating TypeNameInfo objects for the common case. |
| 57 | // It does need to handle the uncommon case of shadowing a typedef name with a |
| 58 | // non-typedef name. e.g. { typedef int a; a xx; { int a; } } |
| 59 | if (weCurrentlyHaveTypeInfo || isTypeName) { |
| 60 | TypeNameInfo *TI = new TypeNameInfo(isTypeName, weCurrentlyHaveTypeInfo); |
| 61 | |
| 62 | II->setFETokenInfo(TI); |
| 63 | |
| 64 | // Remember that this needs to be removed when the scope is popped. |
| 65 | S->AddDecl(II); |
| 66 | } |
| 67 | return 0; |
| 68 | } |
| 69 | |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 70 | Action::DeclTy * |
Fariborz Jahanian | ccb4f31 | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 71 | MinimalAction::ObjcStartClassInterface(Scope* S, SourceLocation AtInterafceLoc, |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 72 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 73 | IdentifierInfo *SuperName, SourceLocation SuperLoc, |
| 74 | IdentifierInfo **ProtocolNames, unsigned NumProtocols, |
| 75 | AttributeList *AttrList) { |
| 76 | TypeNameInfo *TI = |
| 77 | new TypeNameInfo(1, ClassName->getFETokenInfo<TypeNameInfo>()); |
| 78 | |
| 79 | ClassName->setFETokenInfo(TI); |
| 80 | return 0; |
| 81 | } |
| 82 | |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 83 | Action::DeclTy * |
Fariborz Jahanian | ccb4f31 | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 84 | MinimalAction::ObjcStartProtoInterface(Scope* S, |
| 85 | SourceLocation AtProtoInterfaceLoc, |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 86 | IdentifierInfo *ProtocolName, SourceLocation ProtocolLoc, |
| 87 | IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs) { |
| 88 | |
| 89 | TypeNameInfo *TI = |
| 90 | new TypeNameInfo(1, ProtocolName->getFETokenInfo<TypeNameInfo>()); |
| 91 | |
| 92 | ProtocolName->setFETokenInfo(TI); |
| 93 | return 0; |
| 94 | } |
| 95 | |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 96 | /// ObjcClassDeclaration - |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 97 | /// Scope will always be top level file scope. |
| 98 | Action::DeclTy * |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 99 | MinimalAction::ObjcClassDeclaration(Scope *S, SourceLocation AtClassLoc, |
| 100 | IdentifierInfo **IdentList, unsigned NumElts) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 101 | for (unsigned i = 0; i != NumElts; ++i) { |
| 102 | TypeNameInfo *TI = |
| 103 | new TypeNameInfo(1, IdentList[i]->getFETokenInfo<TypeNameInfo>()); |
| 104 | |
| 105 | IdentList[i]->setFETokenInfo(TI); |
| 106 | |
| 107 | // Remember that this needs to be removed when the scope is popped. |
| 108 | S->AddDecl(IdentList[i]); |
| 109 | } |
| 110 | return 0; |
| 111 | } |
| 112 | |
Fariborz Jahanian | 894c57f | 2007-09-21 15:40:54 +0000 | [diff] [blame] | 113 | /// ObjcForwardProtocolDeclaration - |
| 114 | /// Scope will always be top level file scope. |
| 115 | Action::DeclTy * |
| 116 | MinimalAction::ObjcForwardProtocolDeclaration(Scope *S, SourceLocation AtClassLoc, |
| 117 | IdentifierInfo **IdentList, unsigned NumElts) { |
| 118 | for (unsigned i = 0; i != NumElts; ++i) { |
| 119 | TypeNameInfo *TI = |
| 120 | new TypeNameInfo(1, IdentList[i]->getFETokenInfo<TypeNameInfo>()); |
| 121 | |
| 122 | IdentList[i]->setFETokenInfo(TI); |
| 123 | |
| 124 | // Remember that this needs to be removed when the scope is popped. |
| 125 | S->AddDecl(IdentList[i]); |
| 126 | } |
| 127 | return 0; |
| 128 | } |
| 129 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 130 | /// PopScope - When a scope is popped, if any typedefs are now out-of-scope, |
| 131 | /// they are removed from the IdentifierInfo::FETokenInfo field. |
| 132 | void MinimalAction::PopScope(SourceLocation Loc, Scope *S) { |
| 133 | for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end(); |
| 134 | I != E; ++I) { |
| 135 | IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I); |
| 136 | TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>(); |
| 137 | assert(TI && "This decl didn't get pushed??"); |
| 138 | |
| 139 | if (TI) { |
| 140 | TypeNameInfo *Next = TI->Prev; |
| 141 | delete TI; |
| 142 | |
| 143 | II.setFETokenInfo(Next); |
| 144 | } |
| 145 | } |
| 146 | } |