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 | |
Steve Naroff | 6d40db0 | 2007-10-31 18:42:27 +0000 | [diff] [blame^] | 31 | void MinimalAction:: ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) { |
| 32 | TUScope = S; |
| 33 | // FIXME: add id/SEL/Class. We need to get our paws on the identifier table. |
| 34 | } |
| 35 | |
Chris Lattner | 64b09ee | 2006-11-03 07:35:12 +0000 | [diff] [blame] | 36 | /// isTypeName - This looks at the IdentifierInfo::FETokenInfo field to |
| 37 | /// determine whether the name is a type name (objc class name or typedef) or |
| 38 | /// not in this scope. |
Chris Lattner | 2ebe4bb | 2006-11-20 01:29:42 +0000 | [diff] [blame] | 39 | Action::DeclTy * |
| 40 | MinimalAction::isTypeName(const IdentifierInfo &II, Scope *S) const { |
| 41 | if (TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>()) |
| 42 | if (TI->isTypeName) |
| 43 | return TI; |
| 44 | return 0; |
Chris Lattner | a5534f9 | 2006-08-14 00:38:06 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Steve Naroff | 30d242c | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 47 | /// ActOnDeclarator - If this is a typedef declarator, we modify the |
Chris Lattner | a5534f9 | 2006-08-14 00:38:06 +0000 | [diff] [blame] | 48 | /// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is |
| 49 | /// popped. |
Chris Lattner | 2dacc3f | 2006-10-16 00:33:54 +0000 | [diff] [blame] | 50 | Action::DeclTy * |
Steve Naroff | 30d242c | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 51 | MinimalAction::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *LastInGroup) { |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 52 | IdentifierInfo *II = D.getIdentifier(); |
| 53 | |
Chris Lattner | ffe65b3 | 2006-08-14 01:28:29 +0000 | [diff] [blame] | 54 | // If there is no identifier associated with this declarator, bail out. |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 55 | if (II == 0) return 0; |
Chris Lattner | ffe65b3 | 2006-08-14 01:28:29 +0000 | [diff] [blame] | 56 | |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 57 | TypeNameInfo *weCurrentlyHaveTypeInfo = II->getFETokenInfo<TypeNameInfo>(); |
Chris Lattner | f055d43 | 2006-11-28 04:28:12 +0000 | [diff] [blame] | 58 | bool isTypeName = |
| 59 | D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef; |
Chris Lattner | ffe65b3 | 2006-08-14 01:28:29 +0000 | [diff] [blame] | 60 | |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 61 | // this check avoids creating TypeNameInfo objects for the common case. |
| 62 | // It does need to handle the uncommon case of shadowing a typedef name with a |
| 63 | // non-typedef name. e.g. { typedef int a; a xx; { int a; } } |
| 64 | if (weCurrentlyHaveTypeInfo || isTypeName) { |
| 65 | TypeNameInfo *TI = new TypeNameInfo(isTypeName, weCurrentlyHaveTypeInfo); |
| 66 | |
| 67 | II->setFETokenInfo(TI); |
Chris Lattner | ffe65b3 | 2006-08-14 01:28:29 +0000 | [diff] [blame] | 68 | |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 69 | // Remember that this needs to be removed when the scope is popped. |
| 70 | S->AddDecl(II); |
| 71 | } |
| 72 | return 0; |
| 73 | } |
| 74 | |
Steve Naroff | 09bf815 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 75 | Action::DeclTy * |
Steve Naroff | 93eb5f1 | 2007-10-10 17:32:04 +0000 | [diff] [blame] | 76 | MinimalAction::ActOnStartClassInterface(SourceLocation AtInterafceLoc, |
Steve Naroff | 09bf815 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 77 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 78 | IdentifierInfo *SuperName, SourceLocation SuperLoc, |
| 79 | IdentifierInfo **ProtocolNames, unsigned NumProtocols, |
Steve Naroff | c548404 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 80 | SourceLocation EndProtoLoc, AttributeList *AttrList) { |
Steve Naroff | 09bf815 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 81 | TypeNameInfo *TI = |
| 82 | new TypeNameInfo(1, ClassName->getFETokenInfo<TypeNameInfo>()); |
| 83 | |
| 84 | ClassName->setFETokenInfo(TI); |
| 85 | return 0; |
| 86 | } |
| 87 | |
Steve Naroff | 0c37b0c | 2007-10-02 22:39:18 +0000 | [diff] [blame] | 88 | /// ActOnForwardClassDeclaration - |
Chris Lattner | 64b09ee | 2006-11-03 07:35:12 +0000 | [diff] [blame] | 89 | /// Scope will always be top level file scope. |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 90 | Action::DeclTy * |
Steve Naroff | 93eb5f1 | 2007-10-10 17:32:04 +0000 | [diff] [blame] | 91 | MinimalAction::ActOnForwardClassDeclaration(SourceLocation AtClassLoc, |
Steve Naroff | 09bf815 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 92 | IdentifierInfo **IdentList, unsigned NumElts) { |
Chris Lattner | 64b09ee | 2006-11-03 07:35:12 +0000 | [diff] [blame] | 93 | for (unsigned i = 0; i != NumElts; ++i) { |
| 94 | TypeNameInfo *TI = |
| 95 | new TypeNameInfo(1, IdentList[i]->getFETokenInfo<TypeNameInfo>()); |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 96 | |
Chris Lattner | 64b09ee | 2006-11-03 07:35:12 +0000 | [diff] [blame] | 97 | IdentList[i]->setFETokenInfo(TI); |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 98 | |
| 99 | // Remember that this needs to be removed when the scope is popped. |
Steve Naroff | 93eb5f1 | 2007-10-10 17:32:04 +0000 | [diff] [blame] | 100 | TUScope->AddDecl(IdentList[i]); |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 101 | } |
Chris Lattner | 2dacc3f | 2006-10-16 00:33:54 +0000 | [diff] [blame] | 102 | return 0; |
Chris Lattner | a5534f9 | 2006-08-14 00:38:06 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Steve Naroff | 8308f60 | 2007-10-10 17:45:44 +0000 | [diff] [blame] | 105 | /// ActOnPopScope - When a scope is popped, if any typedefs are now out-of-scope, |
Chris Lattner | a5534f9 | 2006-08-14 00:38:06 +0000 | [diff] [blame] | 106 | /// they are removed from the IdentifierInfo::FETokenInfo field. |
Steve Naroff | 8308f60 | 2007-10-10 17:45:44 +0000 | [diff] [blame] | 107 | void MinimalAction::ActOnPopScope(SourceLocation Loc, Scope *S) { |
Chris Lattner | ffe65b3 | 2006-08-14 01:28:29 +0000 | [diff] [blame] | 108 | for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end(); |
| 109 | I != E; ++I) { |
| 110 | IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I); |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 111 | TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>(); |
Chris Lattner | ffe65b3 | 2006-08-14 01:28:29 +0000 | [diff] [blame] | 112 | assert(TI && "This decl didn't get pushed??"); |
Chris Lattner | ffe65b3 | 2006-08-14 01:28:29 +0000 | [diff] [blame] | 113 | |
Chris Lattner | c62b6c2 | 2006-11-05 18:44:26 +0000 | [diff] [blame] | 114 | if (TI) { |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 115 | TypeNameInfo *Next = TI->Prev; |
| 116 | delete TI; |
Chris Lattner | c62b6c2 | 2006-11-05 18:44:26 +0000 | [diff] [blame] | 117 | |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 118 | II.setFETokenInfo(Next); |
Chris Lattner | c62b6c2 | 2006-11-05 18:44:26 +0000 | [diff] [blame] | 119 | } |
Chris Lattner | ffe65b3 | 2006-08-14 01:28:29 +0000 | [diff] [blame] | 120 | } |
Chris Lattner | a5534f9 | 2006-08-14 00:38:06 +0000 | [diff] [blame] | 121 | } |