blob: c5be83b8fce99984fb11e42d9e18ebed8be748e8 [file] [log] [blame]
Chris Lattnerc62b6c22006-11-05 18:44:26 +00001//===--- MinimalAction.cpp - Implement the MinimalAction class ------------===//
Chris Lattnera5534f92006-08-14 00:38:06 +00002//
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 Lattnerc62b6c22006-11-05 18:44:26 +000010// This file implements the MinimalAction interface.
Chris Lattnera5534f92006-08-14 00:38:06 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner288e86ff12006-11-11 23:03:42 +000015#include "clang/Parse/DeclSpec.h"
Chris Lattnerffe65b32006-08-14 01:28:29 +000016#include "clang/Parse/Scope.h"
Chris Lattnera5534f92006-08-14 00:38:06 +000017using namespace clang;
18
Steve Naroffb419d3a2006-10-27 23:18:49 +000019/// TypeNameInfo - A link exists here for each scope that an identifier is
Chris Lattnerffe65b32006-08-14 01:28:29 +000020/// defined.
Steve Naroffb419d3a2006-10-27 23:18:49 +000021struct TypeNameInfo {
22 TypeNameInfo *Prev;
23 bool isTypeName;
24
25 TypeNameInfo(bool istypename, TypeNameInfo *prev) {
26 isTypeName = istypename;
Chris Lattnerf055d432006-11-28 04:28:12 +000027 Prev = prev;
Steve Naroffb419d3a2006-10-27 23:18:49 +000028 }
Chris Lattnerffe65b32006-08-14 01:28:29 +000029};
30
Chris Lattner64b09ee2006-11-03 07:35:12 +000031/// 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 Lattner2ebe4bb2006-11-20 01:29:42 +000034Action::DeclTy *
35MinimalAction::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 Lattnera5534f92006-08-14 00:38:06 +000040}
41
Steve Naroff30d242c2007-09-15 18:49:24 +000042/// ActOnDeclarator - If this is a typedef declarator, we modify the
Chris Lattnera5534f92006-08-14 00:38:06 +000043/// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is
44/// popped.
Chris Lattner2dacc3f2006-10-16 00:33:54 +000045Action::DeclTy *
Steve Naroff30d242c2007-09-15 18:49:24 +000046MinimalAction::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *LastInGroup) {
Steve Naroffb419d3a2006-10-27 23:18:49 +000047 IdentifierInfo *II = D.getIdentifier();
48
Chris Lattnerffe65b32006-08-14 01:28:29 +000049 // If there is no identifier associated with this declarator, bail out.
Steve Naroffb419d3a2006-10-27 23:18:49 +000050 if (II == 0) return 0;
Chris Lattnerffe65b32006-08-14 01:28:29 +000051
Steve Naroffb419d3a2006-10-27 23:18:49 +000052 TypeNameInfo *weCurrentlyHaveTypeInfo = II->getFETokenInfo<TypeNameInfo>();
Chris Lattnerf055d432006-11-28 04:28:12 +000053 bool isTypeName =
54 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef;
Chris Lattnerffe65b32006-08-14 01:28:29 +000055
Steve Naroffb419d3a2006-10-27 23:18:49 +000056 // 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 Lattnerffe65b32006-08-14 01:28:29 +000063
Steve Naroffb419d3a2006-10-27 23:18:49 +000064 // Remember that this needs to be removed when the scope is popped.
65 S->AddDecl(II);
66 }
67 return 0;
68}
69
Steve Naroff09bf8152007-09-06 21:24:23 +000070Action::DeclTy *
71MinimalAction::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 Jahanian39d641f2007-09-17 21:07:36 +000083Action::DeclTy *
84MinimalAction::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 Naroff09bf8152007-09-06 21:24:23 +000095/// ObjcClassDeclaration -
Chris Lattner64b09ee2006-11-03 07:35:12 +000096/// Scope will always be top level file scope.
Steve Naroffb419d3a2006-10-27 23:18:49 +000097Action::DeclTy *
Steve Naroff09bf8152007-09-06 21:24:23 +000098MinimalAction::ObjcClassDeclaration(Scope *S, SourceLocation AtClassLoc,
99 IdentifierInfo **IdentList, unsigned NumElts) {
Chris Lattner64b09ee2006-11-03 07:35:12 +0000100 for (unsigned i = 0; i != NumElts; ++i) {
101 TypeNameInfo *TI =
102 new TypeNameInfo(1, IdentList[i]->getFETokenInfo<TypeNameInfo>());
Steve Naroffb419d3a2006-10-27 23:18:49 +0000103
Chris Lattner64b09ee2006-11-03 07:35:12 +0000104 IdentList[i]->setFETokenInfo(TI);
Steve Naroffb419d3a2006-10-27 23:18:49 +0000105
106 // Remember that this needs to be removed when the scope is popped.
Chris Lattner64b09ee2006-11-03 07:35:12 +0000107 S->AddDecl(IdentList[i]);
Steve Naroffb419d3a2006-10-27 23:18:49 +0000108 }
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000109 return 0;
Chris Lattnera5534f92006-08-14 00:38:06 +0000110}
111
Fariborz Jahanian876e27d2007-09-21 15:40:54 +0000112/// ObjcForwardProtocolDeclaration -
113/// Scope will always be top level file scope.
114Action::DeclTy *
115MinimalAction::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 Lattnera5534f92006-08-14 00:38:06 +0000129/// PopScope - When a scope is popped, if any typedefs are now out-of-scope,
130/// they are removed from the IdentifierInfo::FETokenInfo field.
Chris Lattnerc62b6c22006-11-05 18:44:26 +0000131void MinimalAction::PopScope(SourceLocation Loc, Scope *S) {
Chris Lattnerffe65b32006-08-14 01:28:29 +0000132 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
133 I != E; ++I) {
134 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
Steve Naroffb419d3a2006-10-27 23:18:49 +0000135 TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>();
Chris Lattnerffe65b32006-08-14 01:28:29 +0000136 assert(TI && "This decl didn't get pushed??");
Chris Lattnerffe65b32006-08-14 01:28:29 +0000137
Chris Lattnerc62b6c22006-11-05 18:44:26 +0000138 if (TI) {
Steve Naroffb419d3a2006-10-27 23:18:49 +0000139 TypeNameInfo *Next = TI->Prev;
140 delete TI;
Chris Lattnerc62b6c22006-11-05 18:44:26 +0000141
Steve Naroffb419d3a2006-10-27 23:18:49 +0000142 II.setFETokenInfo(Next);
Chris Lattnerc62b6c22006-11-05 18:44:26 +0000143 }
Chris Lattnerffe65b32006-08-14 01:28:29 +0000144 }
Chris Lattnera5534f92006-08-14 00:38:06 +0000145}