blob: bc7c41bec398e734a85c59f2cdeb2b3a2b6dd9ea [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
Steve Naroff6d40db02007-10-31 18:42:27 +000031void 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 Lattner64b09ee2006-11-03 07:35:12 +000036/// 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 Lattner2ebe4bb2006-11-20 01:29:42 +000039Action::DeclTy *
40MinimalAction::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 Lattnera5534f92006-08-14 00:38:06 +000045}
46
Steve Naroff30d242c2007-09-15 18:49:24 +000047/// ActOnDeclarator - If this is a typedef declarator, we modify the
Chris Lattnera5534f92006-08-14 00:38:06 +000048/// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is
49/// popped.
Chris Lattner2dacc3f2006-10-16 00:33:54 +000050Action::DeclTy *
Steve Naroff30d242c2007-09-15 18:49:24 +000051MinimalAction::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *LastInGroup) {
Steve Naroffb419d3a2006-10-27 23:18:49 +000052 IdentifierInfo *II = D.getIdentifier();
53
Chris Lattnerffe65b32006-08-14 01:28:29 +000054 // If there is no identifier associated with this declarator, bail out.
Steve Naroffb419d3a2006-10-27 23:18:49 +000055 if (II == 0) return 0;
Chris Lattnerffe65b32006-08-14 01:28:29 +000056
Steve Naroffb419d3a2006-10-27 23:18:49 +000057 TypeNameInfo *weCurrentlyHaveTypeInfo = II->getFETokenInfo<TypeNameInfo>();
Chris Lattnerf055d432006-11-28 04:28:12 +000058 bool isTypeName =
59 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef;
Chris Lattnerffe65b32006-08-14 01:28:29 +000060
Steve Naroffb419d3a2006-10-27 23:18:49 +000061 // 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 Lattnerffe65b32006-08-14 01:28:29 +000068
Steve Naroffb419d3a2006-10-27 23:18:49 +000069 // Remember that this needs to be removed when the scope is popped.
70 S->AddDecl(II);
71 }
72 return 0;
73}
74
Steve Naroff09bf8152007-09-06 21:24:23 +000075Action::DeclTy *
Steve Naroff93eb5f12007-10-10 17:32:04 +000076MinimalAction::ActOnStartClassInterface(SourceLocation AtInterafceLoc,
Steve Naroff09bf8152007-09-06 21:24:23 +000077 IdentifierInfo *ClassName, SourceLocation ClassLoc,
78 IdentifierInfo *SuperName, SourceLocation SuperLoc,
79 IdentifierInfo **ProtocolNames, unsigned NumProtocols,
Steve Naroffc5484042007-10-30 02:23:23 +000080 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Steve Naroff09bf8152007-09-06 21:24:23 +000081 TypeNameInfo *TI =
82 new TypeNameInfo(1, ClassName->getFETokenInfo<TypeNameInfo>());
83
84 ClassName->setFETokenInfo(TI);
85 return 0;
86}
87
Steve Naroff0c37b0c2007-10-02 22:39:18 +000088/// ActOnForwardClassDeclaration -
Chris Lattner64b09ee2006-11-03 07:35:12 +000089/// Scope will always be top level file scope.
Steve Naroffb419d3a2006-10-27 23:18:49 +000090Action::DeclTy *
Steve Naroff93eb5f12007-10-10 17:32:04 +000091MinimalAction::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Steve Naroff09bf8152007-09-06 21:24:23 +000092 IdentifierInfo **IdentList, unsigned NumElts) {
Chris Lattner64b09ee2006-11-03 07:35:12 +000093 for (unsigned i = 0; i != NumElts; ++i) {
94 TypeNameInfo *TI =
95 new TypeNameInfo(1, IdentList[i]->getFETokenInfo<TypeNameInfo>());
Steve Naroffb419d3a2006-10-27 23:18:49 +000096
Chris Lattner64b09ee2006-11-03 07:35:12 +000097 IdentList[i]->setFETokenInfo(TI);
Steve Naroffb419d3a2006-10-27 23:18:49 +000098
99 // Remember that this needs to be removed when the scope is popped.
Steve Naroff93eb5f12007-10-10 17:32:04 +0000100 TUScope->AddDecl(IdentList[i]);
Steve Naroffb419d3a2006-10-27 23:18:49 +0000101 }
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000102 return 0;
Chris Lattnera5534f92006-08-14 00:38:06 +0000103}
104
Steve Naroff8308f602007-10-10 17:45:44 +0000105/// ActOnPopScope - When a scope is popped, if any typedefs are now out-of-scope,
Chris Lattnera5534f92006-08-14 00:38:06 +0000106/// they are removed from the IdentifierInfo::FETokenInfo field.
Steve Naroff8308f602007-10-10 17:45:44 +0000107void MinimalAction::ActOnPopScope(SourceLocation Loc, Scope *S) {
Chris Lattnerffe65b32006-08-14 01:28:29 +0000108 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
109 I != E; ++I) {
110 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
Steve Naroffb419d3a2006-10-27 23:18:49 +0000111 TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>();
Chris Lattnerffe65b32006-08-14 01:28:29 +0000112 assert(TI && "This decl didn't get pushed??");
Chris Lattnerffe65b32006-08-14 01:28:29 +0000113
Chris Lattnerc62b6c22006-11-05 18:44:26 +0000114 if (TI) {
Steve Naroffb419d3a2006-10-27 23:18:49 +0000115 TypeNameInfo *Next = TI->Prev;
116 delete TI;
Chris Lattnerc62b6c22006-11-05 18:44:26 +0000117
Steve Naroffb419d3a2006-10-27 23:18:49 +0000118 II.setFETokenInfo(Next);
Chris Lattnerc62b6c22006-11-05 18:44:26 +0000119 }
Chris Lattnerffe65b32006-08-14 01:28:29 +0000120 }
Chris Lattnera5534f92006-08-14 00:38:06 +0000121}