Chris Lattner | c62b6c2 | 2006-11-05 18:44:26 +0000 | [diff] [blame] | 1 | //===--- MinimalAction.cpp - Implement the MinimalAction class ------------===// |
Chris Lattner | a5534f9 | 2006-08-14 00:38:06 +0000 | [diff] [blame] | 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 | // |
Chris Lattner | c62b6c2 | 2006-11-05 18:44:26 +0000 | [diff] [blame] | 10 | // This file implements the MinimalAction interface. |
Chris Lattner | a5534f9 | 2006-08-14 00:38:06 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Parse/Parser.h" |
Chris Lattner | 288e86ff1 | 2006-11-11 23:03:42 +0000 | [diff] [blame] | 15 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | ffe65b3 | 2006-08-14 01:28:29 +0000 | [diff] [blame] | 16 | #include "clang/Parse/Scope.h" |
Chris Lattner | a5534f9 | 2006-08-14 00:38:06 +0000 | [diff] [blame] | 17 | using namespace clang; |
| 18 | |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 19 | /// TypeNameInfo - A link exists here for each scope that an identifier is |
Chris Lattner | ffe65b3 | 2006-08-14 01:28:29 +0000 | [diff] [blame] | 20 | /// defined. |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 21 | struct TypeNameInfo { |
| 22 | TypeNameInfo *Prev; |
| 23 | bool isTypeName; |
| 24 | |
| 25 | TypeNameInfo(bool istypename, TypeNameInfo *prev) { |
| 26 | isTypeName = istypename; |
Chris Lattner | f055d43 | 2006-11-28 04:28:12 +0000 | [diff] [blame] | 27 | Prev = prev; |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 28 | } |
Chris Lattner | ffe65b3 | 2006-08-14 01:28:29 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
Chris Lattner | 64b09ee | 2006-11-03 07:35:12 +0000 | [diff] [blame] | 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. |
Chris Lattner | 2ebe4bb | 2006-11-20 01:29:42 +0000 | [diff] [blame] | 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; |
Chris Lattner | a5534f9 | 2006-08-14 00:38:06 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Steve Naroff | 30d242c | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 42 | /// ActOnDeclarator - If this is a typedef declarator, we modify the |
Chris Lattner | a5534f9 | 2006-08-14 00:38:06 +0000 | [diff] [blame] | 43 | /// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is |
| 44 | /// popped. |
Chris Lattner | 2dacc3f | 2006-10-16 00:33:54 +0000 | [diff] [blame] | 45 | Action::DeclTy * |
Steve Naroff | 30d242c | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 46 | MinimalAction::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *LastInGroup) { |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 47 | IdentifierInfo *II = D.getIdentifier(); |
| 48 | |
Chris Lattner | ffe65b3 | 2006-08-14 01:28:29 +0000 | [diff] [blame] | 49 | // If there is no identifier associated with this declarator, bail out. |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 50 | if (II == 0) return 0; |
Chris Lattner | ffe65b3 | 2006-08-14 01:28:29 +0000 | [diff] [blame] | 51 | |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 52 | TypeNameInfo *weCurrentlyHaveTypeInfo = II->getFETokenInfo<TypeNameInfo>(); |
Chris Lattner | f055d43 | 2006-11-28 04:28:12 +0000 | [diff] [blame] | 53 | bool isTypeName = |
| 54 | D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef; |
Chris Lattner | ffe65b3 | 2006-08-14 01:28:29 +0000 | [diff] [blame] | 55 | |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 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); |
Chris Lattner | ffe65b3 | 2006-08-14 01:28:29 +0000 | [diff] [blame] | 63 | |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 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 | 09bf815 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 70 | Action::DeclTy * |
| 71 | MinimalAction::ObjcStartClassInterface(SourceLocation AtInterafceLoc, |
| 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 | 39d641f | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 83 | Action::DeclTy * |
| 84 | MinimalAction::ObjcStartProtoInterface(SourceLocation AtProtoInterfaceLoc, |
| 85 | IdentifierInfo *ProtocolName, SourceLocation ProtocolLoc, |
| 86 | IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs) { |
| 87 | |
| 88 | TypeNameInfo *TI = |
| 89 | new TypeNameInfo(1, ProtocolName->getFETokenInfo<TypeNameInfo>()); |
| 90 | |
| 91 | ProtocolName->setFETokenInfo(TI); |
| 92 | return 0; |
| 93 | } |
| 94 | |
Steve Naroff | 09bf815 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 95 | /// ObjcClassDeclaration - |
Chris Lattner | 64b09ee | 2006-11-03 07:35:12 +0000 | [diff] [blame] | 96 | /// Scope will always be top level file scope. |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 97 | Action::DeclTy * |
Steve Naroff | 09bf815 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 98 | MinimalAction::ObjcClassDeclaration(Scope *S, SourceLocation AtClassLoc, |
| 99 | IdentifierInfo **IdentList, unsigned NumElts) { |
Chris Lattner | 64b09ee | 2006-11-03 07:35:12 +0000 | [diff] [blame] | 100 | for (unsigned i = 0; i != NumElts; ++i) { |
| 101 | TypeNameInfo *TI = |
| 102 | new TypeNameInfo(1, IdentList[i]->getFETokenInfo<TypeNameInfo>()); |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 103 | |
Chris Lattner | 64b09ee | 2006-11-03 07:35:12 +0000 | [diff] [blame] | 104 | IdentList[i]->setFETokenInfo(TI); |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 105 | |
| 106 | // Remember that this needs to be removed when the scope is popped. |
Chris Lattner | 64b09ee | 2006-11-03 07:35:12 +0000 | [diff] [blame] | 107 | S->AddDecl(IdentList[i]); |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 108 | } |
Chris Lattner | 2dacc3f | 2006-10-16 00:33:54 +0000 | [diff] [blame] | 109 | return 0; |
Chris Lattner | a5534f9 | 2006-08-14 00:38:06 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Fariborz Jahanian | 876e27d | 2007-09-21 15:40:54 +0000 | [diff] [blame] | 112 | /// ObjcForwardProtocolDeclaration - |
| 113 | /// Scope will always be top level file scope. |
| 114 | Action::DeclTy * |
| 115 | MinimalAction::ObjcForwardProtocolDeclaration(Scope *S, SourceLocation AtClassLoc, |
| 116 | IdentifierInfo **IdentList, unsigned NumElts) { |
| 117 | for (unsigned i = 0; i != NumElts; ++i) { |
| 118 | TypeNameInfo *TI = |
| 119 | new TypeNameInfo(1, IdentList[i]->getFETokenInfo<TypeNameInfo>()); |
| 120 | |
| 121 | IdentList[i]->setFETokenInfo(TI); |
| 122 | |
| 123 | // Remember that this needs to be removed when the scope is popped. |
| 124 | S->AddDecl(IdentList[i]); |
| 125 | } |
| 126 | return 0; |
| 127 | } |
| 128 | |
Chris Lattner | a5534f9 | 2006-08-14 00:38:06 +0000 | [diff] [blame] | 129 | /// PopScope - When a scope is popped, if any typedefs are now out-of-scope, |
| 130 | /// they are removed from the IdentifierInfo::FETokenInfo field. |
Chris Lattner | c62b6c2 | 2006-11-05 18:44:26 +0000 | [diff] [blame] | 131 | void MinimalAction::PopScope(SourceLocation Loc, Scope *S) { |
Chris Lattner | ffe65b3 | 2006-08-14 01:28:29 +0000 | [diff] [blame] | 132 | for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end(); |
| 133 | I != E; ++I) { |
| 134 | IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I); |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 135 | TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>(); |
Chris Lattner | ffe65b3 | 2006-08-14 01:28:29 +0000 | [diff] [blame] | 136 | assert(TI && "This decl didn't get pushed??"); |
Chris Lattner | ffe65b3 | 2006-08-14 01:28:29 +0000 | [diff] [blame] | 137 | |
Chris Lattner | c62b6c2 | 2006-11-05 18:44:26 +0000 | [diff] [blame] | 138 | if (TI) { |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 139 | TypeNameInfo *Next = TI->Prev; |
| 140 | delete TI; |
Chris Lattner | c62b6c2 | 2006-11-05 18:44:26 +0000 | [diff] [blame] | 141 | |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 142 | II.setFETokenInfo(Next); |
Chris Lattner | c62b6c2 | 2006-11-05 18:44:26 +0000 | [diff] [blame] | 143 | } |
Chris Lattner | ffe65b3 | 2006-08-14 01:28:29 +0000 | [diff] [blame] | 144 | } |
Chris Lattner | a5534f9 | 2006-08-14 00:38:06 +0000 | [diff] [blame] | 145 | } |