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 llvm; |
| 18 | using namespace clang; |
| 19 | |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 20 | /// TypeNameInfo - A link exists here for each scope that an identifier is |
Chris Lattner | ffe65b3 | 2006-08-14 01:28:29 +0000 | [diff] [blame] | 21 | /// defined. |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 22 | struct TypeNameInfo { |
| 23 | TypeNameInfo *Prev; |
| 24 | bool isTypeName; |
| 25 | |
| 26 | TypeNameInfo(bool istypename, TypeNameInfo *prev) { |
| 27 | isTypeName = istypename; |
| 28 | Prev = prev; |
| 29 | } |
Chris Lattner | ffe65b3 | 2006-08-14 01:28:29 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
Chris Lattner | 64b09ee | 2006-11-03 07:35:12 +0000 | [diff] [blame] | 32 | /// isTypeName - This looks at the IdentifierInfo::FETokenInfo field to |
| 33 | /// determine whether the name is a type name (objc class name or typedef) or |
| 34 | /// not in this scope. |
Chris Lattner | 2ebe4bb | 2006-11-20 01:29:42 +0000 | [diff] [blame] | 35 | Action::DeclTy * |
| 36 | MinimalAction::isTypeName(const IdentifierInfo &II, Scope *S) const { |
| 37 | if (TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>()) |
| 38 | if (TI->isTypeName) |
| 39 | return TI; |
| 40 | return 0; |
Chris Lattner | a5534f9 | 2006-08-14 00:38:06 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /// ParseDeclarator - If this is a typedef declarator, we modify the |
| 44 | /// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is |
| 45 | /// popped. |
Chris Lattner | 2dacc3f | 2006-10-16 00:33:54 +0000 | [diff] [blame] | 46 | Action::DeclTy * |
Chris Lattner | c62b6c2 | 2006-11-05 18:44:26 +0000 | [diff] [blame] | 47 | MinimalAction::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init, |
| 48 | DeclTy *LastInGroup) { |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 49 | IdentifierInfo *II = D.getIdentifier(); |
| 50 | |
Chris Lattner | ffe65b3 | 2006-08-14 01:28:29 +0000 | [diff] [blame] | 51 | // If there is no identifier associated with this declarator, bail out. |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 52 | if (II == 0) return 0; |
Chris Lattner | ffe65b3 | 2006-08-14 01:28:29 +0000 | [diff] [blame] | 53 | |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 54 | TypeNameInfo *weCurrentlyHaveTypeInfo = II->getFETokenInfo<TypeNameInfo>(); |
| 55 | bool isTypeName = D.getDeclSpec().StorageClassSpec == DeclSpec::SCS_typedef; |
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 | // this check avoids creating TypeNameInfo objects for the common case. |
| 58 | // It does need to handle the uncommon case of shadowing a typedef name with a |
| 59 | // non-typedef name. e.g. { typedef int a; a xx; { int a; } } |
| 60 | if (weCurrentlyHaveTypeInfo || isTypeName) { |
| 61 | TypeNameInfo *TI = new TypeNameInfo(isTypeName, weCurrentlyHaveTypeInfo); |
| 62 | |
| 63 | II->setFETokenInfo(TI); |
Chris Lattner | ffe65b3 | 2006-08-14 01:28:29 +0000 | [diff] [blame] | 64 | |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 65 | // Remember that this needs to be removed when the scope is popped. |
| 66 | S->AddDecl(II); |
| 67 | } |
| 68 | return 0; |
| 69 | } |
| 70 | |
Chris Lattner | 94c22cd | 2006-11-19 02:35:21 +0000 | [diff] [blame] | 71 | /// ParsedObjcClassDeclaration - |
Chris Lattner | 64b09ee | 2006-11-03 07:35:12 +0000 | [diff] [blame] | 72 | /// Scope will always be top level file scope. |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 73 | Action::DeclTy * |
Chris Lattner | 94c22cd | 2006-11-19 02:35:21 +0000 | [diff] [blame] | 74 | MinimalAction::ParsedObjcClassDeclaration(Scope *S, |
| 75 | IdentifierInfo **IdentList, |
| 76 | unsigned NumElts) { |
Chris Lattner | 64b09ee | 2006-11-03 07:35:12 +0000 | [diff] [blame] | 77 | for (unsigned i = 0; i != NumElts; ++i) { |
| 78 | TypeNameInfo *TI = |
| 79 | new TypeNameInfo(1, IdentList[i]->getFETokenInfo<TypeNameInfo>()); |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 80 | |
Chris Lattner | 64b09ee | 2006-11-03 07:35:12 +0000 | [diff] [blame] | 81 | IdentList[i]->setFETokenInfo(TI); |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 82 | |
| 83 | // Remember that this needs to be removed when the scope is popped. |
Chris Lattner | 64b09ee | 2006-11-03 07:35:12 +0000 | [diff] [blame] | 84 | S->AddDecl(IdentList[i]); |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 85 | } |
Chris Lattner | 2dacc3f | 2006-10-16 00:33:54 +0000 | [diff] [blame] | 86 | return 0; |
Chris Lattner | a5534f9 | 2006-08-14 00:38:06 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /// PopScope - When a scope is popped, if any typedefs are now out-of-scope, |
| 90 | /// they are removed from the IdentifierInfo::FETokenInfo field. |
Chris Lattner | c62b6c2 | 2006-11-05 18:44:26 +0000 | [diff] [blame] | 91 | void MinimalAction::PopScope(SourceLocation Loc, Scope *S) { |
Chris Lattner | ffe65b3 | 2006-08-14 01:28:29 +0000 | [diff] [blame] | 92 | for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end(); |
| 93 | I != E; ++I) { |
| 94 | IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I); |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 95 | TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>(); |
Chris Lattner | ffe65b3 | 2006-08-14 01:28:29 +0000 | [diff] [blame] | 96 | assert(TI && "This decl didn't get pushed??"); |
Chris Lattner | ffe65b3 | 2006-08-14 01:28:29 +0000 | [diff] [blame] | 97 | |
Chris Lattner | c62b6c2 | 2006-11-05 18:44:26 +0000 | [diff] [blame] | 98 | if (TI) { |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 99 | TypeNameInfo *Next = TI->Prev; |
| 100 | delete TI; |
Chris Lattner | c62b6c2 | 2006-11-05 18:44:26 +0000 | [diff] [blame] | 101 | |
Steve Naroff | b419d3a | 2006-10-27 23:18:49 +0000 | [diff] [blame] | 102 | II.setFETokenInfo(Next); |
Chris Lattner | c62b6c2 | 2006-11-05 18:44:26 +0000 | [diff] [blame] | 103 | } |
Chris Lattner | ffe65b3 | 2006-08-14 01:28:29 +0000 | [diff] [blame] | 104 | } |
Chris Lattner | a5534f9 | 2006-08-14 00:38:06 +0000 | [diff] [blame] | 105 | } |