]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/Parse/MinimalAction.cpp
Import Clang, at r72805.
[FreeBSD/FreeBSD.git] / lib / Parse / MinimalAction.cpp
1 //===--- MinimalAction.cpp - Implement the MinimalAction class ------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file implements the MinimalAction interface.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/Parse/Parser.h"
15 #include "clang/Parse/DeclSpec.h"
16 #include "clang/Parse/Scope.h"
17 #include "clang/Basic/TargetInfo.h"
18 #include "llvm/Support/Allocator.h"
19 #include "llvm/Support/RecyclingAllocator.h"
20 #include "llvm/Support/raw_ostream.h"
21 using namespace clang;
22
23 ///  Out-of-line virtual destructor to provide home for ActionBase class.
24 ActionBase::~ActionBase() {}
25
26 ///  Out-of-line virtual destructor to provide home for Action class.
27 Action::~Action() {}
28
29 // Defined out-of-line here because of dependecy on AttributeList
30 Action::DeclPtrTy Action::ActOnUsingDirective(Scope *CurScope,
31                                               SourceLocation UsingLoc,
32                                               SourceLocation NamespcLoc,
33                                               const CXXScopeSpec &SS,
34                                               SourceLocation IdentLoc,
35                                               IdentifierInfo *NamespcName,
36                                               AttributeList *AttrList) {
37   
38   // FIXME: Parser seems to assume that Action::ActOn* takes ownership over
39   // passed AttributeList, however other actions don't free it, is it
40   // temporary state or bug?
41   delete AttrList;
42   return DeclPtrTy();
43 }
44
45
46 void PrettyStackTraceActionsDecl::print(llvm::raw_ostream &OS) const {
47   if (Loc.isValid()) {
48     Loc.print(OS, SM);
49     OS << ": ";
50   }
51   OS << Message;
52   
53   std::string Name = Actions.getDeclName(TheDecl);
54   if (!Name.empty())
55     OS << " '" << Name << '\'';
56   
57   OS << '\n';
58 }
59
60 /// TypeNameInfo - A link exists here for each scope that an identifier is
61 /// defined.
62 namespace {
63   struct TypeNameInfo {
64     TypeNameInfo *Prev;
65     bool isTypeName;
66     
67     TypeNameInfo(bool istypename, TypeNameInfo *prev) {
68       isTypeName = istypename;
69       Prev = prev;
70     }
71   };
72
73   struct TypeNameInfoTable {
74     llvm::RecyclingAllocator<llvm::BumpPtrAllocator, TypeNameInfo> Allocator;
75     
76     void AddEntry(bool isTypename, IdentifierInfo *II) {
77       TypeNameInfo *TI = Allocator.Allocate<TypeNameInfo>();
78       new (TI) TypeNameInfo(isTypename, II->getFETokenInfo<TypeNameInfo>());
79       II->setFETokenInfo(TI);
80     }
81     
82     void DeleteEntry(TypeNameInfo *Entry) {
83       Entry->~TypeNameInfo();
84       Allocator.Deallocate(Entry);
85     }
86   };
87 }
88
89 static TypeNameInfoTable *getTable(void *TP) {
90   return static_cast<TypeNameInfoTable*>(TP);
91 }
92
93 MinimalAction::MinimalAction(Preprocessor &pp) 
94   : Idents(pp.getIdentifierTable()), PP(pp) {
95   TypeNameInfoTablePtr = new TypeNameInfoTable();
96 }
97
98 MinimalAction::~MinimalAction() {
99   delete getTable(TypeNameInfoTablePtr);
100 }
101
102 void MinimalAction::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
103   TUScope = S;
104
105   TypeNameInfoTable &TNIT = *getTable(TypeNameInfoTablePtr);
106
107   if (PP.getTargetInfo().getPointerWidth(0) >= 64) {
108     // Install [u]int128_t for 64-bit targets.
109     TNIT.AddEntry(true, &Idents.get("__int128_t"));
110     TNIT.AddEntry(true, &Idents.get("__uint128_t"));
111   }
112   
113   if (PP.getLangOptions().ObjC1) {
114     // Recognize the ObjC built-in type identifiers as types. 
115     TNIT.AddEntry(true, &Idents.get("id"));
116     TNIT.AddEntry(true, &Idents.get("SEL"));
117     TNIT.AddEntry(true, &Idents.get("Class"));
118     TNIT.AddEntry(true, &Idents.get("Protocol"));
119   }
120 }
121
122 /// isTypeName - This looks at the IdentifierInfo::FETokenInfo field to
123 /// determine whether the name is a type name (objc class name or typedef) or
124 /// not in this scope.
125 ///
126 /// FIXME: Use the passed CXXScopeSpec for accurate C++ type checking.
127 Action::TypeTy *
128 MinimalAction::getTypeName(IdentifierInfo &II, SourceLocation Loc,
129                            Scope *S, const CXXScopeSpec *SS) {
130   if (TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>())
131     if (TI->isTypeName)
132       return TI;
133   return 0;
134 }
135
136 /// isCurrentClassName - Always returns false, because MinimalAction
137 /// does not support C++ classes with constructors.
138 bool MinimalAction::isCurrentClassName(const IdentifierInfo &, Scope *,
139                                        const CXXScopeSpec *) {
140   return false;
141 }
142
143 TemplateNameKind 
144 MinimalAction::isTemplateName(const IdentifierInfo &II, Scope *S,
145                               TemplateTy &TemplateDecl,
146                               const CXXScopeSpec *SS) {
147   return TNK_Non_template;
148 }
149
150 /// ActOnDeclarator - If this is a typedef declarator, we modify the
151 /// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is
152 /// popped.
153 Action::DeclPtrTy
154 MinimalAction::ActOnDeclarator(Scope *S, Declarator &D) {
155   IdentifierInfo *II = D.getIdentifier();
156   
157   // If there is no identifier associated with this declarator, bail out.
158   if (II == 0) return DeclPtrTy();
159   
160   TypeNameInfo *weCurrentlyHaveTypeInfo = II->getFETokenInfo<TypeNameInfo>();
161   bool isTypeName =
162     D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef;
163
164   // this check avoids creating TypeNameInfo objects for the common case.
165   // It does need to handle the uncommon case of shadowing a typedef name with a
166   // non-typedef name. e.g. { typedef int a; a xx; { int a; } }
167   if (weCurrentlyHaveTypeInfo || isTypeName) {
168     // Allocate and add the 'TypeNameInfo' "decl".
169     getTable(TypeNameInfoTablePtr)->AddEntry(isTypeName, II);
170   
171     // Remember that this needs to be removed when the scope is popped.
172     S->AddDecl(DeclPtrTy::make(II));
173   } 
174   return DeclPtrTy();
175 }
176
177 Action::DeclPtrTy
178 MinimalAction::ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
179                                         IdentifierInfo *ClassName,
180                                         SourceLocation ClassLoc,
181                                         IdentifierInfo *SuperName,
182                                         SourceLocation SuperLoc,
183                                         const DeclPtrTy *ProtoRefs,
184                                         unsigned NumProtocols,
185                                         SourceLocation EndProtoLoc,
186                                         AttributeList *AttrList) {
187   // Allocate and add the 'TypeNameInfo' "decl".
188   getTable(TypeNameInfoTablePtr)->AddEntry(true, ClassName);
189   return DeclPtrTy();
190 }
191
192 /// ActOnForwardClassDeclaration - 
193 /// Scope will always be top level file scope. 
194 Action::DeclPtrTy
195 MinimalAction::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
196                                 IdentifierInfo **IdentList, unsigned NumElts) {
197   for (unsigned i = 0; i != NumElts; ++i) {
198     // Allocate and add the 'TypeNameInfo' "decl".
199     getTable(TypeNameInfoTablePtr)->AddEntry(true, IdentList[i]);
200   
201     // Remember that this needs to be removed when the scope is popped.
202     TUScope->AddDecl(DeclPtrTy::make(IdentList[i]));
203   }
204   return DeclPtrTy();
205 }
206
207 /// ActOnPopScope - When a scope is popped, if any typedefs are now
208 /// out-of-scope, they are removed from the IdentifierInfo::FETokenInfo field.
209 void MinimalAction::ActOnPopScope(SourceLocation Loc, Scope *S) {
210   TypeNameInfoTable &Table = *getTable(TypeNameInfoTablePtr);
211   
212   for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
213        I != E; ++I) {
214     IdentifierInfo &II = *(*I).getAs<IdentifierInfo>();
215     TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>();
216     assert(TI && "This decl didn't get pushed??");
217     
218     if (TI) {
219       TypeNameInfo *Next = TI->Prev;
220       Table.DeleteEntry(TI);
221       
222       II.setFETokenInfo(Next);
223     }
224   }
225 }