blob: c9ea54fa0e9b5260580390209814df04eed38572 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- SemaDecl.cpp - Semantic Analysis for Declarations ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Chris Lattner33aad6e2008-02-06 00:51:33 +000015#include "clang/AST/ASTConsumer.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/AST/ASTContext.h"
Anders Carlsson3f70c542008-02-15 07:04:12 +000017#include "clang/AST/Attr.h"
Chris Lattner4b009652007-07-25 00:24:17 +000018#include "clang/AST/Builtins.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/Expr.h"
21#include "clang/AST/Type.h"
22#include "clang/Parse/DeclSpec.h"
23#include "clang/Parse/Scope.h"
Chris Lattner4b009652007-07-25 00:24:17 +000024#include "clang/Basic/LangOptions.h"
25#include "clang/Basic/TargetInfo.h"
Steve Naroffa9eae582008-01-30 23:46:05 +000026#include "clang/Basic/SourceManager.h"
27// FIXME: layering (ideally, Sema shouldn't be dependent on Lex API's)
Chris Lattner33aad6e2008-02-06 00:51:33 +000028#include "clang/Lex/Preprocessor.h"
Steve Naroffa9eae582008-01-30 23:46:05 +000029#include "clang/Lex/HeaderSearch.h"
Steve Naroffc39ca262007-09-18 23:55:05 +000030#include "llvm/ADT/SmallString.h"
Chris Lattner4b009652007-07-25 00:24:17 +000031#include "llvm/ADT/SmallSet.h"
Fariborz Jahanian67907bd2007-10-05 18:00:57 +000032#include "llvm/ADT/DenseSet.h"
Chris Lattner4b009652007-07-25 00:24:17 +000033using namespace clang;
34
Steve Naroff6384a012008-04-02 14:35:35 +000035Sema::DeclTy *Sema::isTypeName(const IdentifierInfo &II, Scope *S) {
36 Decl *IIDecl = LookupDecl(&II, Decl::IDNS_Ordinary, S, false);
37
38 if (IIDecl && (isa<TypedefDecl>(IIDecl) || isa<ObjCInterfaceDecl>(IIDecl)))
Fariborz Jahanian23f968b2007-10-12 16:34:10 +000039 return IIDecl;
Steve Naroff81f1bba2007-09-06 21:24:23 +000040 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000041}
42
Steve Naroff9637a9b2007-10-09 22:01:59 +000043void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) {
Chris Lattnera7549902007-08-26 06:24:45 +000044 if (S->decl_empty()) return;
45 assert((S->getFlags() & Scope::DeclScope) &&"Scope shouldn't contain decls!");
46
Chris Lattner4b009652007-07-25 00:24:17 +000047 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
48 I != E; ++I) {
Steve Naroffd21bc0d2007-09-13 18:10:37 +000049 Decl *TmpD = static_cast<Decl*>(*I);
50 assert(TmpD && "This decl didn't get pushed??");
51 ScopedDecl *D = dyn_cast<ScopedDecl>(TmpD);
52 assert(D && "This decl isn't a ScopedDecl?");
53
Chris Lattner4b009652007-07-25 00:24:17 +000054 IdentifierInfo *II = D->getIdentifier();
55 if (!II) continue;
56
57 // Unlink this decl from the identifier. Because the scope contains decls
58 // in an unordered collection, and because we have multiple identifier
59 // namespaces (e.g. tag, normal, label),the decl may not be the first entry.
60 if (II->getFETokenInfo<Decl>() == D) {
61 // Normal case, no multiple decls in different namespaces.
62 II->setFETokenInfo(D->getNext());
63 } else {
64 // Scan ahead. There are only three namespaces in C, so this loop can
65 // never execute more than 3 times.
Steve Naroffd21bc0d2007-09-13 18:10:37 +000066 ScopedDecl *SomeDecl = II->getFETokenInfo<ScopedDecl>();
Chris Lattner4b009652007-07-25 00:24:17 +000067 while (SomeDecl->getNext() != D) {
68 SomeDecl = SomeDecl->getNext();
69 assert(SomeDecl && "Didn't find this decl on its identifier's chain!");
70 }
71 SomeDecl->setNext(D->getNext());
72 }
73
74 // This will have to be revisited for C++: there we want to nest stuff in
75 // namespace decls etc. Even for C, we might want a top-level translation
76 // unit decl or something.
77 if (!CurFunctionDecl)
78 continue;
79
80 // Chain this decl to the containing function, it now owns the memory for
81 // the decl.
82 D->setNext(CurFunctionDecl->getDeclChain());
83 CurFunctionDecl->setDeclChain(D);
84 }
85}
86
Steve Naroffe57c21a2008-04-01 23:04:06 +000087/// getObjCInterfaceDecl - Look up a for a class declaration in the scope.
88/// return 0 if one not found.
Steve Naroffe57c21a2008-04-01 23:04:06 +000089ObjCInterfaceDecl *Sema::getObjCInterfaceDecl(IdentifierInfo *Id) {
Steve Naroff15208162008-04-02 18:30:49 +000090 // The third "scope" argument is 0 since we aren't enabling lazy built-in
91 // creation from this context.
92 Decl *IDecl = LookupDecl(Id, Decl::IDNS_Ordinary, 0, false);
Fariborz Jahaniandc36dc12007-10-12 19:38:20 +000093
Steve Naroff6384a012008-04-02 14:35:35 +000094 return dyn_cast_or_null<ObjCInterfaceDecl>(IDecl);
Fariborz Jahaniandc36dc12007-10-12 19:38:20 +000095}
96
Steve Naroffe57c21a2008-04-01 23:04:06 +000097/// LookupDecl - Look up the inner-most declaration in the specified
Chris Lattner4b009652007-07-25 00:24:17 +000098/// namespace.
Steve Naroff6384a012008-04-02 14:35:35 +000099Decl *Sema::LookupDecl(const IdentifierInfo *II, unsigned NSI,
100 Scope *S, bool enableLazyBuiltinCreation) {
Chris Lattner4b009652007-07-25 00:24:17 +0000101 if (II == 0) return 0;
102 Decl::IdentifierNamespace NS = (Decl::IdentifierNamespace)NSI;
103
104 // Scan up the scope chain looking for a decl that matches this identifier
105 // that is in the appropriate namespace. This search should not take long, as
106 // shadowing of names is uncommon, and deep shadowing is extremely uncommon.
Steve Naroffd21bc0d2007-09-13 18:10:37 +0000107 for (ScopedDecl *D = II->getFETokenInfo<ScopedDecl>(); D; D = D->getNext())
Chris Lattner4b009652007-07-25 00:24:17 +0000108 if (D->getIdentifierNamespace() == NS)
109 return D;
110
111 // If we didn't find a use of this identifier, and if the identifier
112 // corresponds to a compiler builtin, create the decl object for the builtin
113 // now, injecting it into translation unit scope, and return it.
114 if (NS == Decl::IDNS_Ordinary) {
Steve Naroff6384a012008-04-02 14:35:35 +0000115 if (enableLazyBuiltinCreation) {
116 // If this is a builtin on this (or all) targets, create the decl.
117 if (unsigned BuiltinID = II->getBuiltinID())
118 return LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, S);
119 }
Steve Naroffe57c21a2008-04-01 23:04:06 +0000120 if (getLangOptions().ObjC1) {
121 // @interface and @compatibility_alias introduce typedef-like names.
122 // Unlike typedef's, they can only be introduced at file-scope (and are
Steve Naroff64334ea2008-04-02 00:39:51 +0000123 // therefore not scoped decls). They can, however, be shadowed by
Steve Naroffe57c21a2008-04-01 23:04:06 +0000124 // other names in IDNS_Ordinary.
Steve Naroff15208162008-04-02 18:30:49 +0000125 ObjCInterfaceDeclsTy::iterator IDI = ObjCInterfaceDecls.find(II);
126 if (IDI != ObjCInterfaceDecls.end())
127 return IDI->second;
Steve Naroffe57c21a2008-04-01 23:04:06 +0000128 ObjCAliasTy::iterator I = ObjCAliasDecls.find(II);
129 if (I != ObjCAliasDecls.end())
130 return I->second->getClassInterface();
131 }
Chris Lattner4b009652007-07-25 00:24:17 +0000132 }
133 return 0;
134}
135
Anders Carlsson36760332007-10-15 20:28:48 +0000136void Sema::InitBuiltinVaListType()
137{
138 if (!Context.getBuiltinVaListType().isNull())
139 return;
140
141 IdentifierInfo *VaIdent = &Context.Idents.get("__builtin_va_list");
Steve Naroff6384a012008-04-02 14:35:35 +0000142 Decl *VaDecl = LookupDecl(VaIdent, Decl::IDNS_Ordinary, TUScope);
Steve Naroffbc8c52e2007-10-18 22:17:45 +0000143 TypedefDecl *VaTypedef = cast<TypedefDecl>(VaDecl);
Anders Carlsson36760332007-10-15 20:28:48 +0000144 Context.setBuiltinVaListType(Context.getTypedefType(VaTypedef));
145}
146
Chris Lattner4b009652007-07-25 00:24:17 +0000147/// LazilyCreateBuiltin - The specified Builtin-ID was first used at file scope.
148/// lazily create a decl for it.
Chris Lattner71c01112007-10-10 23:42:28 +0000149ScopedDecl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid,
150 Scope *S) {
Chris Lattner4b009652007-07-25 00:24:17 +0000151 Builtin::ID BID = (Builtin::ID)bid;
152
Anders Carlsson36760332007-10-15 20:28:48 +0000153 if (BID == Builtin::BI__builtin_va_start ||
Anders Carlssoncebb8d62007-10-12 23:56:29 +0000154 BID == Builtin::BI__builtin_va_copy ||
Anders Carlsson36760332007-10-15 20:28:48 +0000155 BID == Builtin::BI__builtin_va_end)
156 InitBuiltinVaListType();
157
Anders Carlssonfb5b1e82007-10-11 01:00:40 +0000158 QualType R = Context.BuiltinInfo.GetBuiltinType(BID, Context);
Chris Lattner4c7802b2008-03-15 21:24:04 +0000159 FunctionDecl *New = FunctionDecl::Create(Context, SourceLocation(), II, R,
160 FunctionDecl::Extern, false, 0);
Chris Lattner4b009652007-07-25 00:24:17 +0000161
162 // Find translation-unit scope to insert this function into.
Chris Lattnera7549902007-08-26 06:24:45 +0000163 if (Scope *FnS = S->getFnParent())
164 S = FnS->getParent(); // Skip all scopes in a function at once.
Chris Lattner4b009652007-07-25 00:24:17 +0000165 while (S->getParent())
166 S = S->getParent();
167 S->AddDecl(New);
168
169 // Add this decl to the end of the identifier info.
Steve Naroffd21bc0d2007-09-13 18:10:37 +0000170 if (ScopedDecl *LastDecl = II->getFETokenInfo<ScopedDecl>()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000171 // Scan until we find the last (outermost) decl in the id chain.
172 while (LastDecl->getNext())
173 LastDecl = LastDecl->getNext();
174 // Insert before (outside) it.
175 LastDecl->setNext(New);
176 } else {
177 II->setFETokenInfo(New);
178 }
Chris Lattner4b009652007-07-25 00:24:17 +0000179 return New;
180}
181
182/// MergeTypeDefDecl - We just parsed a typedef 'New' which has the same name
183/// and scope as a previous declaration 'Old'. Figure out how to resolve this
184/// situation, merging decls or emitting diagnostics as appropriate.
185///
Steve Naroffe57c21a2008-04-01 23:04:06 +0000186TypedefDecl *Sema::MergeTypeDefDecl(TypedefDecl *New, Decl *OldD) {
Chris Lattner4b009652007-07-25 00:24:17 +0000187 // Verify the old decl was also a typedef.
188 TypedefDecl *Old = dyn_cast<TypedefDecl>(OldD);
189 if (!Old) {
190 Diag(New->getLocation(), diag::err_redefinition_different_kind,
191 New->getName());
192 Diag(OldD->getLocation(), diag::err_previous_definition);
193 return New;
194 }
195
Steve Naroffae84af82007-10-31 18:42:27 +0000196 // Allow multiple definitions for ObjC built-in typedefs.
197 // FIXME: Verify the underlying types are equivalent!
Ted Kremenek42730c52008-01-07 19:49:32 +0000198 if (getLangOptions().ObjC1 && isBuiltinObjCType(New))
Steve Naroffae84af82007-10-31 18:42:27 +0000199 return Old;
Steve Naroffa9eae582008-01-30 23:46:05 +0000200
201 // Redeclaration of a type is a constraint violation (6.7.2.3p1).
202 // Apparently GCC, Intel, and Sun all silently ignore the redeclaration if
203 // *either* declaration is in a system header. The code below implements
204 // this adhoc compatibility rule. FIXME: The following code will not
205 // work properly when compiling ".i" files (containing preprocessed output).
206 SourceManager &SrcMgr = Context.getSourceManager();
207 const FileEntry *OldDeclFile = SrcMgr.getFileEntryForLoc(Old->getLocation());
208 const FileEntry *NewDeclFile = SrcMgr.getFileEntryForLoc(New->getLocation());
209 HeaderSearch &HdrInfo = PP.getHeaderSearchInfo();
210 DirectoryLookup::DirType OldDirType = HdrInfo.getFileDirFlavor(OldDeclFile);
211 DirectoryLookup::DirType NewDirType = HdrInfo.getFileDirFlavor(NewDeclFile);
212
Steve Naroff1997d2c2008-03-26 21:27:00 +0000213 // Allow reclarations in both SystemHeaderDir and ExternCSystemHeaderDir.
214 if ((OldDirType != DirectoryLookup::NormalHeaderDir ||
215 NewDirType != DirectoryLookup::NormalHeaderDir) ||
Steve Naroff73a07032008-02-07 03:50:06 +0000216 getLangOptions().Microsoft)
Steve Naroffa9eae582008-01-30 23:46:05 +0000217 return New;
Steve Naroff1997d2c2008-03-26 21:27:00 +0000218
Chris Lattner4b009652007-07-25 00:24:17 +0000219 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
220 // TODO: This is totally simplistic. It should handle merging functions
221 // together etc, merging extern int X; int X; ...
222 Diag(New->getLocation(), diag::err_redefinition, New->getName());
223 Diag(Old->getLocation(), diag::err_previous_definition);
224 return New;
225}
226
Chris Lattner402b3372008-03-03 03:28:21 +0000227/// DeclhasAttr - returns true if decl Declaration already has the target attribute.
228static bool DeclHasAttr(const Decl *decl, const Attr *target) {
229 for (const Attr *attr = decl->getAttrs(); attr; attr = attr->getNext())
230 if (attr->getKind() == target->getKind())
231 return true;
232
233 return false;
234}
235
236/// MergeAttributes - append attributes from the Old decl to the New one.
237static void MergeAttributes(Decl *New, Decl *Old) {
238 Attr *attr = const_cast<Attr*>(Old->getAttrs()), *tmp;
239
240// FIXME: fix this code to cleanup the Old attrs correctly
241 while (attr) {
242 tmp = attr;
243 attr = attr->getNext();
244
245 if (!DeclHasAttr(New, tmp)) {
246 New->addAttr(tmp);
247 } else {
248 tmp->setNext(0);
249 delete(tmp);
250 }
251 }
252}
253
Chris Lattner4b009652007-07-25 00:24:17 +0000254/// MergeFunctionDecl - We just parsed a function 'New' which has the same name
255/// and scope as a previous declaration 'Old'. Figure out how to resolve this
256/// situation, merging decls or emitting diagnostics as appropriate.
257///
Steve Naroffe57c21a2008-04-01 23:04:06 +0000258FunctionDecl *Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD) {
Chris Lattner4b009652007-07-25 00:24:17 +0000259 // Verify the old decl was also a function.
260 FunctionDecl *Old = dyn_cast<FunctionDecl>(OldD);
261 if (!Old) {
262 Diag(New->getLocation(), diag::err_redefinition_different_kind,
263 New->getName());
264 Diag(OldD->getLocation(), diag::err_previous_definition);
265 return New;
266 }
Chris Lattneree4c3bf2008-02-29 16:48:43 +0000267
Chris Lattner402b3372008-03-03 03:28:21 +0000268 MergeAttributes(New, Old);
269
Chris Lattner4b009652007-07-25 00:24:17 +0000270
Chris Lattner60476ff2007-11-20 19:04:50 +0000271 QualType OldQType = Old->getCanonicalType();
272 QualType NewQType = New->getCanonicalType();
273
Steve Naroff1d5bd642008-01-14 20:51:29 +0000274 // Function types need to be compatible, not identical. This handles
275 // duplicate function decls like "void f(int); void f(enum X);" properly.
276 if (Context.functionTypesAreCompatible(OldQType, NewQType))
277 return New;
Chris Lattner1470b072007-11-06 06:07:26 +0000278
Steve Naroff6c9e7922008-01-16 15:01:34 +0000279 // A function that has already been declared has been redeclared or defined
280 // with a different type- show appropriate diagnostic
281 diag::kind PrevDiag = Old->getBody() ? diag::err_previous_definition :
282 diag::err_previous_declaration;
283
Chris Lattner4b009652007-07-25 00:24:17 +0000284 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
285 // TODO: This is totally simplistic. It should handle merging functions
286 // together etc, merging extern int X; int X; ...
Steve Naroff6c9e7922008-01-16 15:01:34 +0000287 Diag(New->getLocation(), diag::err_conflicting_types, New->getName());
288 Diag(Old->getLocation(), PrevDiag);
Chris Lattner4b009652007-07-25 00:24:17 +0000289 return New;
290}
291
Chris Lattnerf9167d12007-11-06 04:28:31 +0000292/// equivalentArrayTypes - Used to determine whether two array types are
293/// equivalent.
294/// We need to check this explicitly as an incomplete array definition is
295/// considered a VariableArrayType, so will not match a complete array
296/// definition that would be otherwise equivalent.
297static bool areEquivalentArrayTypes(QualType NewQType, QualType OldQType) {
298 const ArrayType *NewAT = NewQType->getAsArrayType();
299 const ArrayType *OldAT = OldQType->getAsArrayType();
300
301 if (!NewAT || !OldAT)
302 return false;
303
304 // If either (or both) array types in incomplete we need to strip off the
305 // outer VariableArrayType. Once the outer VAT is removed the remaining
306 // types must be identical if the array types are to be considered
307 // equivalent.
308 // eg. int[][1] and int[1][1] become
309 // VAT(null, CAT(1, int)) and CAT(1, CAT(1, int))
310 // removing the outermost VAT gives
311 // CAT(1, int) and CAT(1, int)
312 // which are equal, therefore the array types are equivalent.
Eli Friedmane0079792008-02-15 12:53:51 +0000313 if (NewAT->isIncompleteArrayType() || OldAT->isIncompleteArrayType()) {
Chris Lattnerf9167d12007-11-06 04:28:31 +0000314 if (NewAT->getIndexTypeQualifier() != OldAT->getIndexTypeQualifier())
315 return false;
Eli Friedmand32157f2008-01-29 07:51:12 +0000316 NewQType = NewAT->getElementType().getCanonicalType();
317 OldQType = OldAT->getElementType().getCanonicalType();
Chris Lattnerf9167d12007-11-06 04:28:31 +0000318 }
319
320 return NewQType == OldQType;
321}
322
Chris Lattner4b009652007-07-25 00:24:17 +0000323/// MergeVarDecl - We just parsed a variable 'New' which has the same name
324/// and scope as a previous declaration 'Old'. Figure out how to resolve this
325/// situation, merging decls or emitting diagnostics as appropriate.
326///
327/// FIXME: Need to carefully consider tentative definition rules (C99 6.9.2p2).
328/// For example, we incorrectly complain about i1, i4 from C99 6.9.2p4.
329///
Steve Naroffe57c21a2008-04-01 23:04:06 +0000330VarDecl *Sema::MergeVarDecl(VarDecl *New, Decl *OldD) {
Chris Lattner4b009652007-07-25 00:24:17 +0000331 // Verify the old decl was also a variable.
332 VarDecl *Old = dyn_cast<VarDecl>(OldD);
333 if (!Old) {
334 Diag(New->getLocation(), diag::err_redefinition_different_kind,
335 New->getName());
336 Diag(OldD->getLocation(), diag::err_previous_definition);
337 return New;
338 }
Chris Lattner402b3372008-03-03 03:28:21 +0000339
340 MergeAttributes(New, Old);
341
Chris Lattner4b009652007-07-25 00:24:17 +0000342 // Verify the types match.
Chris Lattnerf9167d12007-11-06 04:28:31 +0000343 if (Old->getCanonicalType() != New->getCanonicalType() &&
344 !areEquivalentArrayTypes(New->getCanonicalType(), Old->getCanonicalType())) {
Chris Lattner4b009652007-07-25 00:24:17 +0000345 Diag(New->getLocation(), diag::err_redefinition, New->getName());
346 Diag(Old->getLocation(), diag::err_previous_definition);
347 return New;
348 }
Steve Naroffb00247f2008-01-30 00:44:01 +0000349 // C99 6.2.2p4: Check if we have a static decl followed by a non-static.
350 if (New->getStorageClass() == VarDecl::Static &&
351 (Old->getStorageClass() == VarDecl::None ||
352 Old->getStorageClass() == VarDecl::Extern)) {
353 Diag(New->getLocation(), diag::err_static_non_static, New->getName());
354 Diag(Old->getLocation(), diag::err_previous_definition);
355 return New;
356 }
357 // C99 6.2.2p4: Check if we have a non-static decl followed by a static.
358 if (New->getStorageClass() != VarDecl::Static &&
359 Old->getStorageClass() == VarDecl::Static) {
360 Diag(New->getLocation(), diag::err_non_static_static, New->getName());
361 Diag(Old->getLocation(), diag::err_previous_definition);
362 return New;
363 }
364 // We've verified the types match, now handle "tentative" definitions.
365 FileVarDecl *OldFSDecl = dyn_cast<FileVarDecl>(Old);
366 FileVarDecl *NewFSDecl = dyn_cast<FileVarDecl>(New);
367
368 if (OldFSDecl && NewFSDecl) {
369 // Handle C "tentative" external object definitions (C99 6.9.2).
370 bool OldIsTentative = false;
371 bool NewIsTentative = false;
372
373 if (!OldFSDecl->getInit() &&
374 (OldFSDecl->getStorageClass() == VarDecl::None ||
375 OldFSDecl->getStorageClass() == VarDecl::Static))
376 OldIsTentative = true;
377
378 // FIXME: this check doesn't work (since the initializer hasn't been
379 // attached yet). This check should be moved to FinalizeDeclaratorGroup.
380 // Unfortunately, by the time we get to FinializeDeclaratorGroup, we've
381 // thrown out the old decl.
382 if (!NewFSDecl->getInit() &&
383 (NewFSDecl->getStorageClass() == VarDecl::None ||
384 NewFSDecl->getStorageClass() == VarDecl::Static))
385 ; // change to NewIsTentative = true; once the code is moved.
386
387 if (NewIsTentative || OldIsTentative)
388 return New;
389 }
390 if (Old->getStorageClass() != VarDecl::Extern &&
391 New->getStorageClass() != VarDecl::Extern) {
Chris Lattner4b009652007-07-25 00:24:17 +0000392 Diag(New->getLocation(), diag::err_redefinition, New->getName());
393 Diag(Old->getLocation(), diag::err_previous_definition);
394 }
395 return New;
396}
397
398/// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
399/// no declarator (e.g. "struct foo;") is parsed.
400Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) {
401 // TODO: emit error on 'int;' or 'const enum foo;'.
402 // TODO: emit error on 'typedef int;'
403 // if (!DS.isMissingDeclaratorOk()) Diag(...);
404
Steve Naroffedafc0b2007-11-17 21:37:36 +0000405 return dyn_cast_or_null<TagDecl>(static_cast<Decl *>(DS.getTypeRep()));
Chris Lattner4b009652007-07-25 00:24:17 +0000406}
407
Steve Narofff0b23542008-01-10 22:15:12 +0000408bool Sema::CheckSingleInitializer(Expr *&Init, QualType DeclType) {
Steve Naroffe14e5542007-09-02 02:04:30 +0000409 // Get the type before calling CheckSingleAssignmentConstraints(), since
410 // it can promote the expression.
Chris Lattner005ed752008-01-04 18:04:52 +0000411 QualType InitType = Init->getType();
Steve Naroffe14e5542007-09-02 02:04:30 +0000412
Chris Lattner005ed752008-01-04 18:04:52 +0000413 AssignConvertType ConvTy = CheckSingleAssignmentConstraints(DeclType, Init);
414 return DiagnoseAssignmentResult(ConvTy, Init->getLocStart(), DeclType,
415 InitType, Init, "initializing");
Steve Naroffe14e5542007-09-02 02:04:30 +0000416}
417
Steve Naroffe6a8c9b2007-09-04 14:36:54 +0000418bool Sema::CheckInitExpr(Expr *expr, InitListExpr *IList, unsigned slot,
Steve Narofff0b23542008-01-10 22:15:12 +0000419 QualType ElementType) {
Chris Lattnerba0f1cb2007-12-11 23:15:04 +0000420 Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
Steve Narofff0b23542008-01-10 22:15:12 +0000421 if (CheckSingleInitializer(expr, ElementType))
Chris Lattnerba0f1cb2007-12-11 23:15:04 +0000422 return true; // types weren't compatible.
423
Steve Naroffe6a8c9b2007-09-04 14:36:54 +0000424 if (savExpr != expr) // The type was promoted, update initializer list.
425 IList->setInit(slot, expr);
Steve Naroff509d0b52007-09-04 02:20:04 +0000426 return false;
427}
428
Steve Naroff0d4e6ad2008-01-22 00:55:40 +0000429bool Sema::CheckStringLiteralInit(StringLiteral *strLiteral, QualType &DeclT) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000430 if (const IncompleteArrayType *IAT = DeclT->getAsIncompleteArrayType()) {
Steve Naroff0d4e6ad2008-01-22 00:55:40 +0000431 // C99 6.7.8p14. We have an array of character type with unknown size
432 // being initialized to a string literal.
433 llvm::APSInt ConstVal(32);
434 ConstVal = strLiteral->getByteLength() + 1;
435 // Return a new array type (C99 6.7.8p22).
Eli Friedman8ff07782008-02-15 18:16:39 +0000436 DeclT = Context.getConstantArrayType(IAT->getElementType(), ConstVal,
Steve Naroff0d4e6ad2008-01-22 00:55:40 +0000437 ArrayType::Normal, 0);
438 } else if (const ConstantArrayType *CAT = DeclT->getAsConstantArrayType()) {
439 // C99 6.7.8p14. We have an array of character type with known size.
440 if (strLiteral->getByteLength() > (unsigned)CAT->getMaximumElements())
441 Diag(strLiteral->getSourceRange().getBegin(),
442 diag::warn_initializer_string_for_char_array_too_long,
443 strLiteral->getSourceRange());
444 } else {
445 assert(0 && "HandleStringLiteralInit(): Invalid array type");
446 }
447 // Set type from "char *" to "constant array of char".
448 strLiteral->setType(DeclT);
449 // For now, we always return false (meaning success).
450 return false;
451}
452
453StringLiteral *Sema::IsStringLiteralInit(Expr *Init, QualType DeclType) {
Steve Naroff0d4e6ad2008-01-22 00:55:40 +0000454 const ArrayType *AT = DeclType->getAsArrayType();
Steve Narofff3cb5142008-01-25 00:51:06 +0000455 if (AT && AT->getElementType()->isCharType()) {
456 return dyn_cast<StringLiteral>(Init);
457 }
Steve Naroff0d4e6ad2008-01-22 00:55:40 +0000458 return 0;
459}
460
Steve Narofff3cb5142008-01-25 00:51:06 +0000461// CheckInitializerListTypes - Checks the types of elements of an initializer
462// list. This function is recursive: it calls itself to initialize subelements
463// of aggregate types. Note that the topLevel parameter essentially refers to
464// whether this expression "owns" the initializer list passed in, or if this
465// initialization is taking elements out of a parent initializer. Each
466// call to this function adds zero or more to startIndex, reports any errors,
467// and returns true if it found any inconsistent types.
468bool Sema::CheckInitializerListTypes(InitListExpr*& IList, QualType &DeclType,
469 bool topLevel, unsigned& startIndex) {
Steve Naroffcb69fb72007-12-10 22:44:33 +0000470 bool hadError = false;
Steve Narofff3cb5142008-01-25 00:51:06 +0000471
472 if (DeclType->isScalarType()) {
473 // The simplest case: initializing a single scalar
474 if (topLevel) {
475 Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init,
476 IList->getSourceRange());
477 }
478 if (startIndex < IList->getNumInits()) {
479 Expr* expr = IList->getInit(startIndex);
480 if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
481 // FIXME: Should an error be reported here instead?
482 unsigned newIndex = 0;
483 CheckInitializerListTypes(SubInitList, DeclType, true, newIndex);
484 } else {
485 hadError |= CheckInitExpr(expr, IList, startIndex, DeclType);
486 }
487 ++startIndex;
488 }
489 // FIXME: Should an error be reported for empty initializer list + scalar?
490 } else if (DeclType->isVectorType()) {
491 if (startIndex < IList->getNumInits()) {
492 const VectorType *VT = DeclType->getAsVectorType();
493 int maxElements = VT->getNumElements();
494 QualType elementType = VT->getElementType();
495
496 for (int i = 0; i < maxElements; ++i) {
497 // Don't attempt to go past the end of the init list
498 if (startIndex >= IList->getNumInits())
499 break;
500 Expr* expr = IList->getInit(startIndex);
501 if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
502 unsigned newIndex = 0;
503 hadError |= CheckInitializerListTypes(SubInitList, elementType,
504 true, newIndex);
505 ++startIndex;
506 } else {
507 hadError |= CheckInitializerListTypes(IList, elementType,
508 false, startIndex);
509 }
510 }
511 }
512 } else if (DeclType->isAggregateType() || DeclType->isUnionType()) {
513 if (DeclType->isStructureType() || DeclType->isUnionType()) {
Steve Naroffedce4ec2008-01-28 02:00:41 +0000514 if (startIndex < IList->getNumInits() && !topLevel &&
515 Context.typesAreCompatible(IList->getInit(startIndex)->getType(),
516 DeclType)) {
Steve Narofff3cb5142008-01-25 00:51:06 +0000517 // We found a compatible struct; per the standard, this initializes the
518 // struct. (The C standard technically says that this only applies for
519 // initializers for declarations with automatic scope; however, this
520 // construct is unambiguous anyway because a struct cannot contain
521 // a type compatible with itself. We'll output an error when we check
522 // if the initializer is constant.)
523 // FIXME: Is a call to CheckSingleInitializer required here?
524 ++startIndex;
525 } else {
526 RecordDecl* structDecl = DeclType->getAsRecordType()->getDecl();
Steve Naroffee467032008-02-11 00:06:17 +0000527
Steve Naroff576df292008-02-11 21:52:37 +0000528 // If the record is invalid, some of it's members are invalid. To avoid
529 // confusion, we forgo checking the intializer for the entire record.
Steve Naroffee467032008-02-11 00:06:17 +0000530 if (structDecl->isInvalidDecl())
531 return true;
532
Steve Narofff3cb5142008-01-25 00:51:06 +0000533 // If structDecl is a forward declaration, this loop won't do anything;
534 // That's okay, because an error should get printed out elsewhere. It
535 // might be worthwhile to skip over the rest of the initializer, though.
536 int numMembers = structDecl->getNumMembers() -
537 structDecl->hasFlexibleArrayMember();
538 for (int i = 0; i < numMembers; i++) {
539 // Don't attempt to go past the end of the init list
540 if (startIndex >= IList->getNumInits())
541 break;
542 FieldDecl * curField = structDecl->getMember(i);
543 if (!curField->getIdentifier()) {
544 // Don't initialize unnamed fields, e.g. "int : 20;"
545 continue;
546 }
547 QualType fieldType = curField->getType();
548 Expr* expr = IList->getInit(startIndex);
549 if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
550 unsigned newStart = 0;
551 hadError |= CheckInitializerListTypes(SubInitList, fieldType,
552 true, newStart);
553 ++startIndex;
554 } else {
555 hadError |= CheckInitializerListTypes(IList, fieldType,
556 false, startIndex);
557 }
558 if (DeclType->isUnionType())
559 break;
560 }
561 // FIXME: Implement flexible array initialization GCC extension (it's a
562 // really messy extension to implement, unfortunately...the necessary
563 // information isn't actually even here!)
564 }
565 } else if (DeclType->isArrayType()) {
566 // Check for the special-case of initializing an array with a string.
567 if (startIndex < IList->getNumInits()) {
568 if (StringLiteral *lit = IsStringLiteralInit(IList->getInit(startIndex),
569 DeclType)) {
570 CheckStringLiteralInit(lit, DeclType);
571 ++startIndex;
572 if (topLevel && startIndex < IList->getNumInits()) {
573 // We have leftover initializers; warn
574 Diag(IList->getInit(startIndex)->getLocStart(),
575 diag::err_excess_initializers_in_char_array_initializer,
576 IList->getInit(startIndex)->getSourceRange());
577 }
578 return false;
579 }
580 }
581 int maxElements;
Eli Friedman8ff07782008-02-15 18:16:39 +0000582 if (DeclType->isIncompleteArrayType()) {
Steve Narofff3cb5142008-01-25 00:51:06 +0000583 // FIXME: use a proper constant
584 maxElements = 0x7FFFFFFF;
Chris Lattnerb9716a62008-02-20 23:17:35 +0000585 } else if (const VariableArrayType *VAT =
586 DeclType->getAsVariableArrayType()) {
Steve Narofff3cb5142008-01-25 00:51:06 +0000587 // Check for VLAs; in standard C it would be possible to check this
588 // earlier, but I don't know where clang accepts VLAs (gcc accepts
589 // them in all sorts of strange places).
Eli Friedman8ff07782008-02-15 18:16:39 +0000590 Diag(VAT->getSizeExpr()->getLocStart(),
591 diag::err_variable_object_no_init,
592 VAT->getSizeExpr()->getSourceRange());
593 hadError = true;
594 maxElements = 0x7FFFFFFF;
Steve Narofff3cb5142008-01-25 00:51:06 +0000595 } else {
596 const ConstantArrayType *CAT = DeclType->getAsConstantArrayType();
597 maxElements = static_cast<int>(CAT->getSize().getZExtValue());
598 }
599 QualType elementType = DeclType->getAsArrayType()->getElementType();
600 int numElements = 0;
601 for (int i = 0; i < maxElements; ++i, ++numElements) {
602 // Don't attempt to go past the end of the init list
603 if (startIndex >= IList->getNumInits())
604 break;
605 Expr* expr = IList->getInit(startIndex);
606 if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
607 unsigned newIndex = 0;
608 hadError |= CheckInitializerListTypes(SubInitList, elementType,
609 true, newIndex);
610 ++startIndex;
611 } else {
612 hadError |= CheckInitializerListTypes(IList, elementType,
613 false, startIndex);
614 }
615 }
Eli Friedmane0079792008-02-15 12:53:51 +0000616 if (DeclType->isIncompleteArrayType()) {
Steve Narofff3cb5142008-01-25 00:51:06 +0000617 // If this is an incomplete array type, the actual type needs to
618 // be calculated here
619 if (numElements == 0) {
620 // Sizing an array implicitly to zero is not allowed
621 // (It could in theory be allowed, but it doesn't really matter.)
622 Diag(IList->getLocStart(),
623 diag::err_at_least_one_initializer_needed_to_size_array);
624 hadError = true;
625 } else {
626 llvm::APSInt ConstVal(32);
627 ConstVal = numElements;
628 DeclType = Context.getConstantArrayType(elementType, ConstVal,
629 ArrayType::Normal, 0);
630 }
631 }
632 } else {
633 assert(0 && "Aggregate that isn't a function or array?!");
634 }
635 } else {
636 // In C, all types are either scalars or aggregates, but
637 // additional handling is needed here for C++ (and possibly others?).
638 assert(0 && "Unsupported initializer type");
639 }
640
641 // If this init list is a base list, we set the type; an initializer doesn't
642 // fundamentally have a type, but this makes the ASTs a bit easier to read
643 if (topLevel)
644 IList->setType(DeclType);
645
646 if (topLevel && startIndex < IList->getNumInits()) {
647 // We have leftover initializers; warn
648 Diag(IList->getInit(startIndex)->getLocStart(),
649 diag::warn_excess_initializers,
650 IList->getInit(startIndex)->getSourceRange());
651 }
652 return hadError;
653}
654
655bool Sema::CheckInitializerTypes(Expr *&Init, QualType &DeclType) {
Steve Naroff8e9337f2008-01-21 23:53:58 +0000656 // C99 6.7.8p3: The type of the entity to be initialized shall be an array
657 // of unknown size ("[]") or an object type that is not a variable array type.
Eli Friedman8ff07782008-02-15 18:16:39 +0000658 if (const VariableArrayType *VAT = DeclType->getAsVariableArrayType())
Steve Naroff8e9337f2008-01-21 23:53:58 +0000659 return Diag(VAT->getSizeExpr()->getLocStart(),
660 diag::err_variable_object_no_init,
661 VAT->getSizeExpr()->getSourceRange());
662
Steve Naroffcb69fb72007-12-10 22:44:33 +0000663 InitListExpr *InitList = dyn_cast<InitListExpr>(Init);
664 if (!InitList) {
Steve Naroff0d4e6ad2008-01-22 00:55:40 +0000665 // FIXME: Handle wide strings
666 if (StringLiteral *strLiteral = IsStringLiteralInit(Init, DeclType))
667 return CheckStringLiteralInit(strLiteral, DeclType);
Eli Friedman65280992008-02-08 00:48:24 +0000668
669 if (DeclType->isArrayType())
670 return Diag(Init->getLocStart(),
671 diag::err_array_init_list_required,
672 Init->getSourceRange());
673
Steve Narofff0b23542008-01-10 22:15:12 +0000674 return CheckSingleInitializer(Init, DeclType);
Steve Naroffcb69fb72007-12-10 22:44:33 +0000675 }
Steve Narofff3cb5142008-01-25 00:51:06 +0000676 unsigned newIndex = 0;
677 return CheckInitializerListTypes(InitList, DeclType, true, newIndex);
Steve Naroffe14e5542007-09-02 02:04:30 +0000678}
679
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000680Sema::DeclTy *
Steve Naroff0acc9c92007-09-15 18:49:24 +0000681Sema::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *lastDecl) {
Steve Naroff2591e1b2007-09-13 23:52:58 +0000682 ScopedDecl *LastDeclarator = dyn_cast_or_null<ScopedDecl>((Decl *)lastDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000683 IdentifierInfo *II = D.getIdentifier();
684
685 // All of these full declarators require an identifier. If it doesn't have
686 // one, the ParsedFreeStandingDeclSpec action should be used.
687 if (II == 0) {
Chris Lattner6fe8b272007-10-16 22:36:42 +0000688 Diag(D.getDeclSpec().getSourceRange().getBegin(),
Chris Lattner87492f42007-08-28 06:17:15 +0000689 diag::err_declarator_need_ident,
Chris Lattner4b009652007-07-25 00:24:17 +0000690 D.getDeclSpec().getSourceRange(), D.getSourceRange());
691 return 0;
692 }
693
Chris Lattnera7549902007-08-26 06:24:45 +0000694 // The scope passed in may not be a decl scope. Zip up the scope tree until
695 // we find one that is.
696 while ((S->getFlags() & Scope::DeclScope) == 0)
697 S = S->getParent();
698
Chris Lattner4b009652007-07-25 00:24:17 +0000699 // See if this is a redefinition of a variable in the same scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000700 Decl *PrevDecl = LookupDecl(II, Decl::IDNS_Ordinary, S);
Steve Naroffd21bc0d2007-09-13 18:10:37 +0000701 ScopedDecl *New;
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000702 bool InvalidDecl = false;
703
Chris Lattner82bb4792007-11-14 06:34:38 +0000704 QualType R = GetTypeForDeclarator(D, S);
705 assert(!R.isNull() && "GetTypeForDeclarator() returned null type");
706
Chris Lattner4b009652007-07-25 00:24:17 +0000707 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Chris Lattner82bb4792007-11-14 06:34:38 +0000708 TypedefDecl *NewTD = ParseTypedefDecl(S, D, R, LastDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +0000709 if (!NewTD) return 0;
710
711 // Handle attributes prior to checking for duplicates in MergeVarDecl
712 HandleDeclAttributes(NewTD, D.getDeclSpec().getAttributes(),
713 D.getAttributes());
Steve Narofff8a09432008-01-09 23:34:55 +0000714 // Merge the decl with the existing one if appropriate. If the decl is
715 // in an outer scope, it isn't the same thing.
716 if (PrevDecl && S->isDeclScope(PrevDecl)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000717 NewTD = MergeTypeDefDecl(NewTD, PrevDecl);
718 if (NewTD == 0) return 0;
719 }
720 New = NewTD;
721 if (S->getParent() == 0) {
722 // C99 6.7.7p2: If a typedef name specifies a variably modified type
723 // then it shall have block scope.
Eli Friedmane0079792008-02-15 12:53:51 +0000724 if (NewTD->getUnderlyingType()->isVariablyModifiedType()) {
725 // FIXME: Diagnostic needs to be fixed.
726 Diag(D.getIdentifierLoc(), diag::err_typecheck_illegal_vla);
Steve Naroff5eb879b2007-08-31 17:20:07 +0000727 InvalidDecl = true;
Chris Lattner4b009652007-07-25 00:24:17 +0000728 }
729 }
Chris Lattner82bb4792007-11-14 06:34:38 +0000730 } else if (R.getTypePtr()->isFunctionType()) {
Chris Lattner265c8172007-09-27 15:15:46 +0000731 FunctionDecl::StorageClass SC = FunctionDecl::None;
Chris Lattner4b009652007-07-25 00:24:17 +0000732 switch (D.getDeclSpec().getStorageClassSpec()) {
733 default: assert(0 && "Unknown storage class!");
734 case DeclSpec::SCS_auto:
735 case DeclSpec::SCS_register:
736 Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_func,
737 R.getAsString());
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000738 InvalidDecl = true;
739 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000740 case DeclSpec::SCS_unspecified: SC = FunctionDecl::None; break;
741 case DeclSpec::SCS_extern: SC = FunctionDecl::Extern; break;
742 case DeclSpec::SCS_static: SC = FunctionDecl::Static; break;
Steve Naroffd404c352008-01-28 21:57:15 +0000743 case DeclSpec::SCS_private_extern: SC = FunctionDecl::PrivateExtern;break;
Chris Lattner4b009652007-07-25 00:24:17 +0000744 }
745
Chris Lattner4c7802b2008-03-15 21:24:04 +0000746 bool isInline = D.getDeclSpec().isInlineSpecified();
747 FunctionDecl *NewFD = FunctionDecl::Create(Context, D.getIdentifierLoc(),
748 II, R, SC, isInline,
749 LastDeclarator);
Ted Kremenek117f1862008-02-27 22:18:07 +0000750 // Handle attributes.
Ted Kremenek117f1862008-02-27 22:18:07 +0000751 HandleDeclAttributes(NewFD, D.getDeclSpec().getAttributes(),
752 D.getAttributes());
Chris Lattner4b009652007-07-25 00:24:17 +0000753
Steve Narofff8a09432008-01-09 23:34:55 +0000754 // Merge the decl with the existing one if appropriate. Since C functions
755 // are in a flat namespace, make sure we consider decls in outer scopes.
Chris Lattner4b009652007-07-25 00:24:17 +0000756 if (PrevDecl) {
757 NewFD = MergeFunctionDecl(NewFD, PrevDecl);
758 if (NewFD == 0) return 0;
759 }
760 New = NewFD;
761 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000762 if (R.getTypePtr()->isObjCInterfaceType()) {
Fariborz Jahanian550e0502007-10-12 22:10:42 +0000763 Diag(D.getIdentifierLoc(), diag::err_statically_allocated_object,
764 D.getIdentifier()->getName());
765 InvalidDecl = true;
766 }
Chris Lattner4b009652007-07-25 00:24:17 +0000767
768 VarDecl *NewVD;
769 VarDecl::StorageClass SC;
770 switch (D.getDeclSpec().getStorageClassSpec()) {
Chris Lattner48d225c2008-03-15 21:10:16 +0000771 default: assert(0 && "Unknown storage class!");
772 case DeclSpec::SCS_unspecified: SC = VarDecl::None; break;
773 case DeclSpec::SCS_extern: SC = VarDecl::Extern; break;
774 case DeclSpec::SCS_static: SC = VarDecl::Static; break;
775 case DeclSpec::SCS_auto: SC = VarDecl::Auto; break;
776 case DeclSpec::SCS_register: SC = VarDecl::Register; break;
777 case DeclSpec::SCS_private_extern: SC = VarDecl::PrivateExtern; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000778 }
779 if (S->getParent() == 0) {
Chris Lattner4b009652007-07-25 00:24:17 +0000780 // C99 6.9p2: The storage-class specifiers auto and register shall not
781 // appear in the declaration specifiers in an external declaration.
782 if (SC == VarDecl::Auto || SC == VarDecl::Register) {
783 Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope,
784 R.getAsString());
Steve Naroffcae537d2007-08-28 18:45:29 +0000785 InvalidDecl = true;
Chris Lattner4b009652007-07-25 00:24:17 +0000786 }
Chris Lattner58114f02008-03-15 21:32:50 +0000787 NewVD = FileVarDecl::Create(Context, D.getIdentifierLoc(), II, R, SC,
788 LastDeclarator);
Steve Naroffe14e5542007-09-02 02:04:30 +0000789 } else {
Chris Lattner58114f02008-03-15 21:32:50 +0000790 NewVD = BlockVarDecl::Create(Context, D.getIdentifierLoc(), II, R, SC,
791 LastDeclarator);
Steve Naroffcae537d2007-08-28 18:45:29 +0000792 }
Chris Lattner4b009652007-07-25 00:24:17 +0000793 // Handle attributes prior to checking for duplicates in MergeVarDecl
794 HandleDeclAttributes(NewVD, D.getDeclSpec().getAttributes(),
795 D.getAttributes());
Nate Begemanea583262008-03-14 18:07:10 +0000796
797 // Emit an error if an address space was applied to decl with local storage.
798 // This includes arrays of objects with address space qualifiers, but not
799 // automatic variables that point to other address spaces.
800 // ISO/IEC TR 18037 S5.1.2
Nate Begemanefc11212008-03-25 18:36:32 +0000801 if (NewVD->hasLocalStorage() && (NewVD->getType().getAddressSpace() != 0)) {
802 Diag(D.getIdentifierLoc(), diag::err_as_qualified_auto_decl);
803 InvalidDecl = true;
Nate Begeman06068192008-03-14 00:22:18 +0000804 }
Steve Narofff8a09432008-01-09 23:34:55 +0000805 // Merge the decl with the existing one if appropriate. If the decl is
806 // in an outer scope, it isn't the same thing.
807 if (PrevDecl && S->isDeclScope(PrevDecl)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000808 NewVD = MergeVarDecl(NewVD, PrevDecl);
809 if (NewVD == 0) return 0;
810 }
Chris Lattner4b009652007-07-25 00:24:17 +0000811 New = NewVD;
812 }
813
814 // If this has an identifier, add it to the scope stack.
815 if (II) {
Steve Naroffd21bc0d2007-09-13 18:10:37 +0000816 New->setNext(II->getFETokenInfo<ScopedDecl>());
Chris Lattner4b009652007-07-25 00:24:17 +0000817 II->setFETokenInfo(New);
818 S->AddDecl(New);
819 }
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000820 // If any semantic error occurred, mark the decl as invalid.
821 if (D.getInvalidType() || InvalidDecl)
822 New->setInvalidDecl();
Chris Lattner4b009652007-07-25 00:24:17 +0000823
824 return New;
825}
826
Steve Narofff0b23542008-01-10 22:15:12 +0000827bool Sema::CheckForConstantInitializer(Expr *Init, QualType DclT) {
828 SourceLocation loc;
829 // FIXME: Remove the isReference check and handle assignment to a reference.
830 if (!DclT->isReferenceType() && !Init->isConstantExpr(Context, &loc)) {
831 assert(loc.isValid() && "isConstantExpr didn't return a loc!");
832 Diag(loc, diag::err_init_element_not_constant, Init->getSourceRange());
833 return true;
834 }
835 return false;
836}
837
Steve Naroff6a0e2092007-09-12 14:07:44 +0000838void Sema::AddInitializerToDecl(DeclTy *dcl, ExprTy *init) {
Steve Naroff420d0f52007-09-12 20:13:48 +0000839 Decl *RealDecl = static_cast<Decl *>(dcl);
Steve Naroff6a0e2092007-09-12 14:07:44 +0000840 Expr *Init = static_cast<Expr *>(init);
Chris Lattnerf31a2fb2007-10-19 20:10:30 +0000841 assert(Init && "missing initializer");
Steve Naroff6a0e2092007-09-12 14:07:44 +0000842
Chris Lattnerf31a2fb2007-10-19 20:10:30 +0000843 // If there is no declaration, there was an error parsing it. Just ignore
844 // the initializer.
845 if (RealDecl == 0) {
846 delete Init;
847 return;
848 }
Steve Naroff6a0e2092007-09-12 14:07:44 +0000849
Steve Naroff420d0f52007-09-12 20:13:48 +0000850 VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl);
851 if (!VDecl) {
Steve Naroffcb597472007-09-13 21:41:19 +0000852 Diag(dyn_cast<ScopedDecl>(RealDecl)->getLocation(),
853 diag::err_illegal_initializer);
Steve Naroff420d0f52007-09-12 20:13:48 +0000854 RealDecl->setInvalidDecl();
855 return;
856 }
Steve Naroff6a0e2092007-09-12 14:07:44 +0000857 // Get the decls type and save a reference for later, since
Steve Narofff0b23542008-01-10 22:15:12 +0000858 // CheckInitializerTypes may change it.
Steve Naroff420d0f52007-09-12 20:13:48 +0000859 QualType DclT = VDecl->getType(), SavT = DclT;
860 if (BlockVarDecl *BVD = dyn_cast<BlockVarDecl>(VDecl)) {
Steve Naroff6a0e2092007-09-12 14:07:44 +0000861 VarDecl::StorageClass SC = BVD->getStorageClass();
862 if (SC == VarDecl::Extern) { // C99 6.7.8p5
Steve Naroff420d0f52007-09-12 20:13:48 +0000863 Diag(VDecl->getLocation(), diag::err_block_extern_cant_init);
Steve Naroff6a0e2092007-09-12 14:07:44 +0000864 BVD->setInvalidDecl();
865 } else if (!BVD->isInvalidDecl()) {
Steve Narofff3cb5142008-01-25 00:51:06 +0000866 if (CheckInitializerTypes(Init, DclT))
867 BVD->setInvalidDecl();
Steve Narofff0b23542008-01-10 22:15:12 +0000868 if (SC == VarDecl::Static) // C99 6.7.8p4.
869 CheckForConstantInitializer(Init, DclT);
Steve Naroff6a0e2092007-09-12 14:07:44 +0000870 }
Steve Naroff420d0f52007-09-12 20:13:48 +0000871 } else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(VDecl)) {
Steve Naroff6a0e2092007-09-12 14:07:44 +0000872 if (FVD->getStorageClass() == VarDecl::Extern)
Steve Naroff420d0f52007-09-12 20:13:48 +0000873 Diag(VDecl->getLocation(), diag::warn_extern_init);
Steve Naroff6a0e2092007-09-12 14:07:44 +0000874 if (!FVD->isInvalidDecl())
Steve Narofff3cb5142008-01-25 00:51:06 +0000875 if (CheckInitializerTypes(Init, DclT))
876 FVD->setInvalidDecl();
Steve Narofff0b23542008-01-10 22:15:12 +0000877
878 // C99 6.7.8p4. All file scoped initializers need to be constant.
879 CheckForConstantInitializer(Init, DclT);
Steve Naroff6a0e2092007-09-12 14:07:44 +0000880 }
881 // If the type changed, it means we had an incomplete type that was
882 // completed by the initializer. For example:
883 // int ary[] = { 1, 3, 5 };
884 // "ary" transitions from a VariableArrayType to a ConstantArrayType.
Christopher Lamb62f06b62007-11-29 19:09:19 +0000885 if (!VDecl->isInvalidDecl() && (DclT != SavT)) {
Steve Naroff420d0f52007-09-12 20:13:48 +0000886 VDecl->setType(DclT);
Christopher Lamb62f06b62007-11-29 19:09:19 +0000887 Init->setType(DclT);
888 }
Steve Naroff6a0e2092007-09-12 14:07:44 +0000889
890 // Attach the initializer to the decl.
Steve Naroff420d0f52007-09-12 20:13:48 +0000891 VDecl->setInit(Init);
Steve Naroff6a0e2092007-09-12 14:07:44 +0000892 return;
893}
894
Chris Lattner4b009652007-07-25 00:24:17 +0000895/// The declarators are chained together backwards, reverse the list.
896Sema::DeclTy *Sema::FinalizeDeclaratorGroup(Scope *S, DeclTy *group) {
897 // Often we have single declarators, handle them quickly.
Steve Naroff2591e1b2007-09-13 23:52:58 +0000898 Decl *GroupDecl = static_cast<Decl*>(group);
899 if (GroupDecl == 0)
Steve Naroff6a0e2092007-09-12 14:07:44 +0000900 return 0;
Steve Naroff2591e1b2007-09-13 23:52:58 +0000901
902 ScopedDecl *Group = dyn_cast<ScopedDecl>(GroupDecl);
903 ScopedDecl *NewGroup = 0;
Steve Naroff6a0e2092007-09-12 14:07:44 +0000904 if (Group->getNextDeclarator() == 0)
Chris Lattner4b009652007-07-25 00:24:17 +0000905 NewGroup = Group;
Steve Naroff6a0e2092007-09-12 14:07:44 +0000906 else { // reverse the list.
907 while (Group) {
Steve Naroff2591e1b2007-09-13 23:52:58 +0000908 ScopedDecl *Next = Group->getNextDeclarator();
Steve Naroff6a0e2092007-09-12 14:07:44 +0000909 Group->setNextDeclarator(NewGroup);
910 NewGroup = Group;
911 Group = Next;
912 }
913 }
914 // Perform semantic analysis that depends on having fully processed both
915 // the declarator and initializer.
Steve Naroff2591e1b2007-09-13 23:52:58 +0000916 for (ScopedDecl *ID = NewGroup; ID; ID = ID->getNextDeclarator()) {
Steve Naroff6a0e2092007-09-12 14:07:44 +0000917 VarDecl *IDecl = dyn_cast<VarDecl>(ID);
918 if (!IDecl)
919 continue;
920 FileVarDecl *FVD = dyn_cast<FileVarDecl>(IDecl);
921 BlockVarDecl *BVD = dyn_cast<BlockVarDecl>(IDecl);
922 QualType T = IDecl->getType();
923
924 // C99 6.7.5.2p2: If an identifier is declared to be an object with
925 // static storage duration, it shall not have a variable length array.
926 if ((FVD || BVD) && IDecl->getStorageClass() == VarDecl::Static) {
Eli Friedman70f414d2008-02-15 19:53:52 +0000927 if (T->getAsVariableArrayType()) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000928 Diag(IDecl->getLocation(), diag::err_typecheck_illegal_vla);
929 IDecl->setInvalidDecl();
Steve Naroff6a0e2092007-09-12 14:07:44 +0000930 }
931 }
932 // Block scope. C99 6.7p7: If an identifier for an object is declared with
933 // no linkage (C99 6.2.2p6), the type for the object shall be complete...
934 if (BVD && IDecl->getStorageClass() != VarDecl::Extern) {
Chris Lattner67d3c8d2008-04-02 01:05:10 +0000935 if (T->isIncompleteType() && !IDecl->isInvalidDecl()) {
Chris Lattner2f72aa02007-12-02 07:50:03 +0000936 Diag(IDecl->getLocation(), diag::err_typecheck_decl_incomplete_type,
937 T.getAsString());
Steve Naroff6a0e2092007-09-12 14:07:44 +0000938 IDecl->setInvalidDecl();
939 }
940 }
941 // File scope. C99 6.9.2p2: A declaration of an identifier for and
942 // object that has file scope without an initializer, and without a
943 // storage-class specifier or with the storage-class specifier "static",
944 // constitutes a tentative definition. Note: A tentative definition with
945 // external linkage is valid (C99 6.2.2p5).
Steve Narofffef2f052008-01-18 00:39:39 +0000946 if (FVD && !FVD->getInit() && (FVD->getStorageClass() == VarDecl::Static ||
947 FVD->getStorageClass() == VarDecl::None)) {
Eli Friedmane0079792008-02-15 12:53:51 +0000948 if (T->isIncompleteArrayType()) {
Steve Naroff60685462008-01-18 20:40:52 +0000949 // C99 6.9.2 (p2, p5): Implicit initialization causes an incomplete
950 // array to be completed. Don't issue a diagnostic.
Chris Lattner67d3c8d2008-04-02 01:05:10 +0000951 } else if (T->isIncompleteType() && !IDecl->isInvalidDecl()) {
Steve Naroff60685462008-01-18 20:40:52 +0000952 // C99 6.9.2p3: If the declaration of an identifier for an object is
953 // a tentative definition and has internal linkage (C99 6.2.2p3), the
954 // declared type shall not be an incomplete type.
Chris Lattner2f72aa02007-12-02 07:50:03 +0000955 Diag(IDecl->getLocation(), diag::err_typecheck_decl_incomplete_type,
956 T.getAsString());
Steve Naroff6a0e2092007-09-12 14:07:44 +0000957 IDecl->setInvalidDecl();
958 }
959 }
Chris Lattner4b009652007-07-25 00:24:17 +0000960 }
961 return NewGroup;
962}
Steve Naroff91b03f72007-08-28 03:03:08 +0000963
964// Called from Sema::ParseStartOfFunctionDef().
Chris Lattner4b009652007-07-25 00:24:17 +0000965ParmVarDecl *
Nate Begemanef16c252008-02-17 21:02:04 +0000966Sema::ActOnParamDeclarator(struct DeclaratorChunk::ParamInfo &PI,
967 Scope *FnScope) {
Chris Lattner4b009652007-07-25 00:24:17 +0000968 IdentifierInfo *II = PI.Ident;
969 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
970 // Can this happen for params? We already checked that they don't conflict
971 // among each other. Here they can only shadow globals, which is ok.
Steve Naroff6384a012008-04-02 14:35:35 +0000972 if (/*Decl *PrevDecl = */LookupDecl(II, Decl::IDNS_Ordinary, FnScope)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000973
974 }
975
976 // FIXME: Handle storage class (auto, register). No declarator?
977 // TODO: Chain to previous parameter with the prevdeclarator chain?
Steve Naroff94cd93f2007-08-07 22:44:21 +0000978
979 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
980 // Doing the promotion here has a win and a loss. The win is the type for
981 // both Decl's and DeclRefExpr's will match (a convenient invariant for the
982 // code generator). The loss is the orginal type isn't preserved. For example:
983 //
984 // void func(int parmvardecl[5]) { // convert "int [5]" to "int *"
985 // int blockvardecl[5];
986 // sizeof(parmvardecl); // size == 4
987 // sizeof(blockvardecl); // size == 20
988 // }
989 //
990 // For expressions, all implicit conversions are captured using the
991 // ImplicitCastExpr AST node (we have no such mechanism for Decl's).
992 //
993 // FIXME: If a source translation tool needs to see the original type, then
994 // we need to consider storing both types (in ParmVarDecl)...
995 //
996 QualType parmDeclType = QualType::getFromOpaquePtr(PI.TypeInfo);
Chris Lattner19eb97e2008-04-02 05:18:44 +0000997 if (parmDeclType->isArrayType()) {
Chris Lattnerc08564a2008-01-02 22:50:48 +0000998 // int x[restrict 4] -> int *restrict
Chris Lattner19eb97e2008-04-02 05:18:44 +0000999 parmDeclType = Context.getArrayDecayedType(parmDeclType);
Chris Lattnerc08564a2008-01-02 22:50:48 +00001000 } else if (parmDeclType->isFunctionType())
Steve Naroff94cd93f2007-08-07 22:44:21 +00001001 parmDeclType = Context.getPointerType(parmDeclType);
1002
Chris Lattner58114f02008-03-15 21:32:50 +00001003 ParmVarDecl *New = ParmVarDecl::Create(Context, PI.IdentLoc, II, parmDeclType,
1004 VarDecl::None, 0);
Anders Carlsson3f70c542008-02-15 07:04:12 +00001005
Steve Naroffcae537d2007-08-28 18:45:29 +00001006 if (PI.InvalidType)
1007 New->setInvalidDecl();
1008
Chris Lattner4b009652007-07-25 00:24:17 +00001009 // If this has an identifier, add it to the scope stack.
1010 if (II) {
Steve Naroffd21bc0d2007-09-13 18:10:37 +00001011 New->setNext(II->getFETokenInfo<ScopedDecl>());
Chris Lattner4b009652007-07-25 00:24:17 +00001012 II->setFETokenInfo(New);
1013 FnScope->AddDecl(New);
1014 }
Nate Begeman9f3c4bb2008-02-17 21:20:31 +00001015
1016 HandleDeclAttributes(New, PI.AttrList, 0);
Chris Lattner4b009652007-07-25 00:24:17 +00001017 return New;
1018}
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +00001019
Chris Lattnerea148702007-10-09 17:14:05 +00001020Sema::DeclTy *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) {
Chris Lattner4b009652007-07-25 00:24:17 +00001021 assert(CurFunctionDecl == 0 && "Function parsing confused");
1022 assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
1023 "Not a function declarator!");
1024 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
1025
1026 // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared'
1027 // for a K&R function.
1028 if (!FTI.hasPrototype) {
1029 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
1030 if (FTI.ArgInfo[i].TypeInfo == 0) {
1031 Diag(FTI.ArgInfo[i].IdentLoc, diag::ext_param_not_declared,
1032 FTI.ArgInfo[i].Ident->getName());
1033 // Implicitly declare the argument as type 'int' for lack of a better
1034 // type.
1035 FTI.ArgInfo[i].TypeInfo = Context.IntTy.getAsOpaquePtr();
1036 }
1037 }
Chris Lattnerec9361f2008-02-17 19:31:09 +00001038
Chris Lattner4b009652007-07-25 00:24:17 +00001039 // Since this is a function definition, act as though we have information
1040 // about the arguments.
Chris Lattnerec9361f2008-02-17 19:31:09 +00001041 if (FTI.NumArgs)
1042 FTI.hasPrototype = true;
Chris Lattner4b009652007-07-25 00:24:17 +00001043 } else {
1044 // FIXME: Diagnose arguments without names in C.
1045
1046 }
1047
1048 Scope *GlobalScope = FnBodyScope->getParent();
Steve Naroff1d5bd642008-01-14 20:51:29 +00001049
1050 // See if this is a redefinition.
Steve Naroffe57c21a2008-04-01 23:04:06 +00001051 Decl *PrevDcl = LookupDecl(D.getIdentifier(), Decl::IDNS_Ordinary,
Steve Naroff6384a012008-04-02 14:35:35 +00001052 GlobalScope);
Steve Naroff1d5bd642008-01-14 20:51:29 +00001053 if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(PrevDcl)) {
1054 if (FD->getBody()) {
1055 Diag(D.getIdentifierLoc(), diag::err_redefinition,
1056 D.getIdentifier()->getName());
1057 Diag(FD->getLocation(), diag::err_previous_definition);
1058 }
1059 }
Steve Naroff4a712442008-02-12 01:09:36 +00001060 Decl *decl = static_cast<Decl*>(ActOnDeclarator(GlobalScope, D, 0));
Chris Lattner2d2216b2008-02-16 01:20:36 +00001061 FunctionDecl *FD = cast<FunctionDecl>(decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001062 CurFunctionDecl = FD;
1063
1064 // Create Decl objects for each parameter, adding them to the FunctionDecl.
1065 llvm::SmallVector<ParmVarDecl*, 16> Params;
1066
1067 // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes
1068 // no arguments, not a function that takes a single void argument.
1069 if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 &&
Chris Lattner35fef522008-02-20 20:55:12 +00001070 !QualType::getFromOpaquePtr(FTI.ArgInfo[0].TypeInfo).getCVRQualifiers() &&
Chris Lattnereee2f2b2007-11-28 18:51:29 +00001071 QualType::getFromOpaquePtr(FTI.ArgInfo[0].TypeInfo)->isVoidType()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001072 // empty arg list, don't push any params.
1073 } else {
Steve Naroff434fa8d2007-11-12 03:44:46 +00001074 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Steve Naroffbb9e1a52008-03-19 23:07:49 +00001075 ParmVarDecl *parmDecl;
1076
1077 parmDecl = ActOnParamDeclarator(D.getTypeObject(0).Fun.ArgInfo[i],
1078 FnBodyScope);
1079 // C99 6.7.5.3p4: the parameters in a parameter type list in a function
1080 // declarator that is part of a function definition of that function
1081 // shall not have incomplete type.
Chris Lattner67d3c8d2008-04-02 01:05:10 +00001082 if (parmDecl->getType()->isIncompleteType() &&
1083 !parmDecl->isInvalidDecl()) {
Steve Naroffbb9e1a52008-03-19 23:07:49 +00001084 Diag(parmDecl->getLocation(), diag::err_typecheck_decl_incomplete_type,
1085 parmDecl->getType().getAsString());
1086 parmDecl->setInvalidDecl();
1087 }
1088 Params.push_back(parmDecl);
Steve Naroff434fa8d2007-11-12 03:44:46 +00001089 }
Chris Lattner4b009652007-07-25 00:24:17 +00001090 }
1091
1092 FD->setParams(&Params[0], Params.size());
1093
1094 return FD;
1095}
1096
Steve Naroff99ee4302007-11-11 23:20:51 +00001097Sema::DeclTy *Sema::ActOnFinishFunctionBody(DeclTy *D, StmtTy *Body) {
1098 Decl *dcl = static_cast<Decl *>(D);
1099 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(dcl)) {
1100 FD->setBody((Stmt*)Body);
1101 assert(FD == CurFunctionDecl && "Function parsing confused");
Steve Naroff8ba51142007-12-13 18:18:56 +00001102 CurFunctionDecl = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001103 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(dcl)) {
Steve Naroff99ee4302007-11-11 23:20:51 +00001104 MD->setBody((Stmt*)Body);
Steve Naroffdd2e26c2007-11-12 13:56:41 +00001105 CurMethodDecl = 0;
Steve Naroff8ba51142007-12-13 18:18:56 +00001106 }
Chris Lattner4b009652007-07-25 00:24:17 +00001107 // Verify and clean out per-function state.
1108
1109 // Check goto/label use.
1110 for (llvm::DenseMap<IdentifierInfo*, LabelStmt*>::iterator
1111 I = LabelMap.begin(), E = LabelMap.end(); I != E; ++I) {
1112 // Verify that we have no forward references left. If so, there was a goto
1113 // or address of a label taken, but no definition of it. Label fwd
1114 // definitions are indicated with a null substmt.
1115 if (I->second->getSubStmt() == 0) {
1116 LabelStmt *L = I->second;
1117 // Emit error.
1118 Diag(L->getIdentLoc(), diag::err_undeclared_label_use, L->getName());
1119
1120 // At this point, we have gotos that use the bogus label. Stitch it into
1121 // the function body so that they aren't leaked and that the AST is well
1122 // formed.
Chris Lattner83343342008-01-25 00:01:10 +00001123 if (Body) {
1124 L->setSubStmt(new NullStmt(L->getIdentLoc()));
1125 cast<CompoundStmt>((Stmt*)Body)->push_back(L);
1126 } else {
1127 // The whole function wasn't parsed correctly, just delete this.
1128 delete L;
1129 }
Chris Lattner4b009652007-07-25 00:24:17 +00001130 }
1131 }
1132 LabelMap.clear();
1133
Steve Naroff99ee4302007-11-11 23:20:51 +00001134 return D;
Fariborz Jahaniane6f59f12007-11-10 16:31:34 +00001135}
1136
Chris Lattner4b009652007-07-25 00:24:17 +00001137/// ImplicitlyDefineFunction - An undeclared identifier was used in a function
1138/// call, forming a call to an implicitly defined function (per C99 6.5.1p2).
Steve Narofff0c31dd2007-09-16 16:16:00 +00001139ScopedDecl *Sema::ImplicitlyDefineFunction(SourceLocation Loc,
1140 IdentifierInfo &II, Scope *S) {
Chris Lattner4b009652007-07-25 00:24:17 +00001141 if (getLangOptions().C99) // Extension in C99.
1142 Diag(Loc, diag::ext_implicit_function_decl, II.getName());
1143 else // Legal in C90, but warn about it.
1144 Diag(Loc, diag::warn_implicit_function_decl, II.getName());
1145
1146 // FIXME: handle stuff like:
1147 // void foo() { extern float X(); }
1148 // void bar() { X(); } <-- implicit decl for X in another scope.
1149
1150 // Set a Declarator for the implicit definition: int foo();
1151 const char *Dummy;
1152 DeclSpec DS;
1153 bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy);
1154 Error = Error; // Silence warning.
1155 assert(!Error && "Error setting up implicit decl!");
1156 Declarator D(DS, Declarator::BlockContext);
1157 D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, Loc));
1158 D.SetIdentifier(&II, Loc);
1159
1160 // Find translation-unit scope to insert this function into.
Chris Lattnera7549902007-08-26 06:24:45 +00001161 if (Scope *FnS = S->getFnParent())
1162 S = FnS->getParent(); // Skip all scopes in a function at once.
Chris Lattner4b009652007-07-25 00:24:17 +00001163 while (S->getParent())
1164 S = S->getParent();
1165
Steve Narofff0c31dd2007-09-16 16:16:00 +00001166 return dyn_cast<ScopedDecl>(static_cast<Decl*>(ActOnDeclarator(S, D, 0)));
Chris Lattner4b009652007-07-25 00:24:17 +00001167}
1168
1169
Chris Lattner82bb4792007-11-14 06:34:38 +00001170TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, QualType T,
Steve Naroff2591e1b2007-09-13 23:52:58 +00001171 ScopedDecl *LastDeclarator) {
Chris Lattner4b009652007-07-25 00:24:17 +00001172 assert(D.getIdentifier() && "Wrong callback for declspec without declarator");
Steve Naroffd1ad6ae2007-08-28 20:14:24 +00001173 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
Chris Lattner4b009652007-07-25 00:24:17 +00001174
1175 // Scope manipulation handled by caller.
Chris Lattner58114f02008-03-15 21:32:50 +00001176 TypedefDecl *NewTD = TypedefDecl::Create(Context, D.getIdentifierLoc(),
Chris Lattnere4650482008-03-15 06:12:44 +00001177 D.getIdentifier(),
Chris Lattner58114f02008-03-15 21:32:50 +00001178 T, LastDeclarator);
Steve Naroffd1ad6ae2007-08-28 20:14:24 +00001179 if (D.getInvalidType())
1180 NewTD->setInvalidDecl();
1181 return NewTD;
Chris Lattner4b009652007-07-25 00:24:17 +00001182}
1183
Steve Naroff0acc9c92007-09-15 18:49:24 +00001184/// ActOnTag - This is invoked when we see 'struct foo' or 'struct {'. In the
Chris Lattner4b009652007-07-25 00:24:17 +00001185/// former case, Name will be non-null. In the later case, Name will be null.
1186/// TagType indicates what kind of tag this is. TK indicates whether this is a
1187/// reference/declaration/definition of a tag.
Steve Naroff0acc9c92007-09-15 18:49:24 +00001188Sema::DeclTy *Sema::ActOnTag(Scope *S, unsigned TagType, TagKind TK,
Chris Lattner4b009652007-07-25 00:24:17 +00001189 SourceLocation KWLoc, IdentifierInfo *Name,
1190 SourceLocation NameLoc, AttributeList *Attr) {
1191 // If this is a use of an existing tag, it must have a name.
1192 assert((Name != 0 || TK == TK_Definition) &&
1193 "Nameless record must be a definition!");
1194
1195 Decl::Kind Kind;
1196 switch (TagType) {
1197 default: assert(0 && "Unknown tag type!");
1198 case DeclSpec::TST_struct: Kind = Decl::Struct; break;
1199 case DeclSpec::TST_union: Kind = Decl::Union; break;
1200//case DeclSpec::TST_class: Kind = Decl::Class; break;
1201 case DeclSpec::TST_enum: Kind = Decl::Enum; break;
1202 }
1203
1204 // If this is a named struct, check to see if there was a previous forward
1205 // declaration or definition.
1206 if (TagDecl *PrevDecl =
Steve Naroff6384a012008-04-02 14:35:35 +00001207 dyn_cast_or_null<TagDecl>(LookupDecl(Name, Decl::IDNS_Tag, S))) {
Chris Lattner4b009652007-07-25 00:24:17 +00001208
1209 // If this is a use of a previous tag, or if the tag is already declared in
1210 // the same scope (so that the definition/declaration completes or
1211 // rementions the tag), reuse the decl.
1212 if (TK == TK_Reference || S->isDeclScope(PrevDecl)) {
1213 // Make sure that this wasn't declared as an enum and now used as a struct
1214 // or something similar.
1215 if (PrevDecl->getKind() != Kind) {
1216 Diag(KWLoc, diag::err_use_with_wrong_tag, Name->getName());
1217 Diag(PrevDecl->getLocation(), diag::err_previous_use);
1218 }
1219
1220 // If this is a use or a forward declaration, we're good.
1221 if (TK != TK_Definition)
1222 return PrevDecl;
1223
1224 // Diagnose attempts to redefine a tag.
1225 if (PrevDecl->isDefinition()) {
1226 Diag(NameLoc, diag::err_redefinition, Name->getName());
1227 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
1228 // If this is a redefinition, recover by making this struct be
1229 // anonymous, which will make any later references get the previous
1230 // definition.
1231 Name = 0;
1232 } else {
1233 // Okay, this is definition of a previously declared or referenced tag.
1234 // Move the location of the decl to be the definition site.
1235 PrevDecl->setLocation(NameLoc);
1236 return PrevDecl;
1237 }
1238 }
1239 // If we get here, this is a definition of a new struct type in a nested
1240 // scope, e.g. "struct foo; void bar() { struct foo; }", just create a new
1241 // type.
1242 }
1243
1244 // If there is an identifier, use the location of the identifier as the
1245 // location of the decl, otherwise use the location of the struct/union
1246 // keyword.
1247 SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc;
1248
1249 // Otherwise, if this is the first time we've seen this tag, create the decl.
1250 TagDecl *New;
1251 switch (Kind) {
1252 default: assert(0 && "Unknown tag kind!");
1253 case Decl::Enum:
1254 // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.:
1255 // enum X { A, B, C } D; D should chain to X.
Chris Lattner58114f02008-03-15 21:32:50 +00001256 New = EnumDecl::Create(Context, Loc, Name, 0);
Chris Lattner4b009652007-07-25 00:24:17 +00001257 // If this is an undefined enum, warn.
1258 if (TK != TK_Definition) Diag(Loc, diag::ext_forward_ref_enum);
1259 break;
1260 case Decl::Union:
1261 case Decl::Struct:
1262 case Decl::Class:
1263 // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.:
1264 // struct X { int A; } D; D should chain to X.
Chris Lattner58114f02008-03-15 21:32:50 +00001265 New = RecordDecl::Create(Context, Kind, Loc, Name, 0);
Chris Lattner4b009652007-07-25 00:24:17 +00001266 break;
1267 }
1268
1269 // If this has an identifier, add it to the scope stack.
1270 if (Name) {
Chris Lattnera7549902007-08-26 06:24:45 +00001271 // The scope passed in may not be a decl scope. Zip up the scope tree until
1272 // we find one that is.
1273 while ((S->getFlags() & Scope::DeclScope) == 0)
1274 S = S->getParent();
1275
1276 // Add it to the decl chain.
Steve Naroffd21bc0d2007-09-13 18:10:37 +00001277 New->setNext(Name->getFETokenInfo<ScopedDecl>());
Chris Lattner4b009652007-07-25 00:24:17 +00001278 Name->setFETokenInfo(New);
1279 S->AddDecl(New);
1280 }
Chris Lattner33aad6e2008-02-06 00:51:33 +00001281
Anders Carlsson136cdc32008-02-16 00:29:18 +00001282 HandleDeclAttributes(New, Attr, 0);
Chris Lattner4b009652007-07-25 00:24:17 +00001283 return New;
1284}
1285
Steve Naroff0acc9c92007-09-15 18:49:24 +00001286/// ActOnField - Each field of a struct/union/class is passed into this in order
Chris Lattner4b009652007-07-25 00:24:17 +00001287/// to create a FieldDecl object for it.
Chris Lattnere3b41132008-03-16 01:16:52 +00001288Sema::DeclTy *Sema::ActOnField(Scope *S, DeclTy *tagDecl,
Chris Lattner4b009652007-07-25 00:24:17 +00001289 SourceLocation DeclStart,
1290 Declarator &D, ExprTy *BitfieldWidth) {
1291 IdentifierInfo *II = D.getIdentifier();
1292 Expr *BitWidth = (Expr*)BitfieldWidth;
Chris Lattnere3b41132008-03-16 01:16:52 +00001293 Decl *TagDecl = static_cast<Decl *>(tagDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001294 SourceLocation Loc = DeclStart;
1295 if (II) Loc = D.getIdentifierLoc();
1296
1297 // FIXME: Unnamed fields can be handled in various different ways, for
1298 // example, unnamed unions inject all members into the struct namespace!
1299
1300
1301 if (BitWidth) {
1302 // TODO: Validate.
1303 //printf("WARNING: BITFIELDS IGNORED!\n");
1304
1305 // 6.7.2.1p3
1306 // 6.7.2.1p4
1307
1308 } else {
1309 // Not a bitfield.
1310
1311 // validate II.
1312
1313 }
1314
1315 QualType T = GetTypeForDeclarator(D, S);
Steve Naroffd1ad6ae2007-08-28 20:14:24 +00001316 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
1317 bool InvalidDecl = false;
Steve Naroff5eb879b2007-08-31 17:20:07 +00001318
Chris Lattner4b009652007-07-25 00:24:17 +00001319 // C99 6.7.2.1p8: A member of a structure or union may have any type other
1320 // than a variably modified type.
Eli Friedmane0079792008-02-15 12:53:51 +00001321 if (T->isVariablyModifiedType()) {
1322 // FIXME: This diagnostic needs work
1323 Diag(Loc, diag::err_typecheck_illegal_vla, Loc);
Steve Naroff5eb879b2007-08-31 17:20:07 +00001324 InvalidDecl = true;
Chris Lattner4b009652007-07-25 00:24:17 +00001325 }
Chris Lattner4b009652007-07-25 00:24:17 +00001326 // FIXME: Chain fielddecls together.
Steve Naroff75494892007-09-11 21:17:26 +00001327 FieldDecl *NewFD;
1328
Chris Lattnere3b41132008-03-16 01:16:52 +00001329 if (isa<RecordDecl>(TagDecl))
Chris Lattner81db64a2008-03-16 00:16:02 +00001330 NewFD = FieldDecl::Create(Context, Loc, II, T, BitWidth);
Chris Lattnere3b41132008-03-16 01:16:52 +00001331 else if (isa<ObjCInterfaceDecl>(TagDecl) ||
1332 isa<ObjCImplementationDecl>(TagDecl) ||
1333 isa<ObjCCategoryDecl>(TagDecl) ||
Steve Naroff4fbfb452007-11-14 14:15:31 +00001334 // FIXME: ivars are currently used to model properties, and
1335 // properties can appear within a protocol.
Ted Kremenek42730c52008-01-07 19:49:32 +00001336 // See corresponding FIXME in DeclObjC.h:ObjCPropertyDecl.
Chris Lattnere3b41132008-03-16 01:16:52 +00001337 isa<ObjCProtocolDecl>(TagDecl))
Chris Lattner0db541b2008-03-16 01:15:50 +00001338 NewFD = ObjCIvarDecl::Create(Context, Loc, II, T);
Steve Naroff75494892007-09-11 21:17:26 +00001339 else
Steve Naroff0acc9c92007-09-15 18:49:24 +00001340 assert(0 && "Sema::ActOnField(): Unknown TagDecl");
Steve Naroff75494892007-09-11 21:17:26 +00001341
Anders Carlsson136cdc32008-02-16 00:29:18 +00001342 HandleDeclAttributes(NewFD, D.getDeclSpec().getAttributes(),
1343 D.getAttributes());
1344
Steve Naroffd1ad6ae2007-08-28 20:14:24 +00001345 if (D.getInvalidType() || InvalidDecl)
1346 NewFD->setInvalidDecl();
1347 return NewFD;
Chris Lattner4b009652007-07-25 00:24:17 +00001348}
1349
Fariborz Jahanianbec0d562007-10-01 16:53:59 +00001350/// TranslateIvarVisibility - Translate visibility from a token ID to an
1351/// AST enum value.
Ted Kremenek42730c52008-01-07 19:49:32 +00001352static ObjCIvarDecl::AccessControl
Fariborz Jahanianbec0d562007-10-01 16:53:59 +00001353TranslateIvarVisibility(tok::ObjCKeywordKind ivarVisibility) {
Steve Naroffffeaa552007-09-14 23:09:53 +00001354 switch (ivarVisibility) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001355 case tok::objc_private: return ObjCIvarDecl::Private;
1356 case tok::objc_public: return ObjCIvarDecl::Public;
1357 case tok::objc_protected: return ObjCIvarDecl::Protected;
1358 case tok::objc_package: return ObjCIvarDecl::Package;
Fariborz Jahanianbec0d562007-10-01 16:53:59 +00001359 default: assert(false && "Unknown visitibility kind");
Steve Naroffffeaa552007-09-14 23:09:53 +00001360 }
1361}
1362
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +00001363void Sema::ActOnFields(Scope* S,
Fariborz Jahaniancbc36d42007-10-04 00:45:27 +00001364 SourceLocation RecLoc, DeclTy *RecDecl,
Steve Naroff0acc9c92007-09-15 18:49:24 +00001365 DeclTy **Fields, unsigned NumFields,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001366 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff0acc9c92007-09-15 18:49:24 +00001367 tok::ObjCKeywordKind *visibility) {
Steve Naroff9bb759f2007-09-14 22:20:54 +00001368 Decl *EnclosingDecl = static_cast<Decl*>(RecDecl);
1369 assert(EnclosingDecl && "missing record or interface decl");
1370 RecordDecl *Record = dyn_cast<RecordDecl>(EnclosingDecl);
1371
Fariborz Jahanian023a4392007-09-14 16:27:55 +00001372 if (Record && Record->isDefinition()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001373 // Diagnose code like:
1374 // struct S { struct S {} X; };
1375 // We discover this when we complete the outer S. Reject and ignore the
1376 // outer S.
1377 Diag(Record->getLocation(), diag::err_nested_redefinition,
1378 Record->getKindName());
1379 Diag(RecLoc, diag::err_previous_definition);
Steve Naroff9bb759f2007-09-14 22:20:54 +00001380 Record->setInvalidDecl();
Chris Lattner4b009652007-07-25 00:24:17 +00001381 return;
1382 }
Chris Lattner4b009652007-07-25 00:24:17 +00001383 // Verify that all the fields are okay.
1384 unsigned NumNamedMembers = 0;
1385 llvm::SmallVector<FieldDecl*, 32> RecFields;
1386 llvm::SmallSet<const IdentifierInfo*, 32> FieldIDs;
Steve Naroff9bb759f2007-09-14 22:20:54 +00001387
Chris Lattner4b009652007-07-25 00:24:17 +00001388 for (unsigned i = 0; i != NumFields; ++i) {
Fariborz Jahanian023a4392007-09-14 16:27:55 +00001389
Steve Naroff9bb759f2007-09-14 22:20:54 +00001390 FieldDecl *FD = cast_or_null<FieldDecl>(static_cast<Decl*>(Fields[i]));
1391 assert(FD && "missing field decl");
1392
1393 // Remember all fields.
1394 RecFields.push_back(FD);
Chris Lattner4b009652007-07-25 00:24:17 +00001395
1396 // Get the type for the field.
Chris Lattner36be3d82007-07-31 21:33:24 +00001397 Type *FDTy = FD->getType().getTypePtr();
Chris Lattner4b009652007-07-25 00:24:17 +00001398
Steve Naroffffeaa552007-09-14 23:09:53 +00001399 // If we have visibility info, make sure the AST is set accordingly.
1400 if (visibility)
Ted Kremenek42730c52008-01-07 19:49:32 +00001401 cast<ObjCIvarDecl>(FD)->setAccessControl(
Fariborz Jahanianbec0d562007-10-01 16:53:59 +00001402 TranslateIvarVisibility(visibility[i]));
Steve Naroffffeaa552007-09-14 23:09:53 +00001403
Chris Lattner4b009652007-07-25 00:24:17 +00001404 // C99 6.7.2.1p2 - A field may not be a function type.
Chris Lattner36be3d82007-07-31 21:33:24 +00001405 if (FDTy->isFunctionType()) {
Steve Naroff9bb759f2007-09-14 22:20:54 +00001406 Diag(FD->getLocation(), diag::err_field_declared_as_function,
Chris Lattner4b009652007-07-25 00:24:17 +00001407 FD->getName());
Steve Naroff9bb759f2007-09-14 22:20:54 +00001408 FD->setInvalidDecl();
1409 EnclosingDecl->setInvalidDecl();
Chris Lattner4b009652007-07-25 00:24:17 +00001410 continue;
1411 }
Chris Lattner4b009652007-07-25 00:24:17 +00001412 // C99 6.7.2.1p2 - A field may not be an incomplete type except...
1413 if (FDTy->isIncompleteType()) {
Fariborz Jahanian023a4392007-09-14 16:27:55 +00001414 if (!Record) { // Incomplete ivar type is always an error.
Fariborz Jahaniancbc36d42007-10-04 00:45:27 +00001415 Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName());
Steve Naroff9bb759f2007-09-14 22:20:54 +00001416 FD->setInvalidDecl();
1417 EnclosingDecl->setInvalidDecl();
Fariborz Jahaniancbc36d42007-10-04 00:45:27 +00001418 continue;
Fariborz Jahanian023a4392007-09-14 16:27:55 +00001419 }
Chris Lattner4b009652007-07-25 00:24:17 +00001420 if (i != NumFields-1 || // ... that the last member ...
1421 Record->getKind() != Decl::Struct || // ... of a structure ...
Chris Lattner36be3d82007-07-31 21:33:24 +00001422 !FDTy->isArrayType()) { //... may have incomplete array type.
Chris Lattner4b009652007-07-25 00:24:17 +00001423 Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName());
Steve Naroff9bb759f2007-09-14 22:20:54 +00001424 FD->setInvalidDecl();
1425 EnclosingDecl->setInvalidDecl();
Chris Lattner4b009652007-07-25 00:24:17 +00001426 continue;
1427 }
Fariborz Jahanian023a4392007-09-14 16:27:55 +00001428 if (NumNamedMembers < 1) { //... must have more than named member ...
Chris Lattner4b009652007-07-25 00:24:17 +00001429 Diag(FD->getLocation(), diag::err_flexible_array_empty_struct,
1430 FD->getName());
Steve Naroff9bb759f2007-09-14 22:20:54 +00001431 FD->setInvalidDecl();
1432 EnclosingDecl->setInvalidDecl();
Chris Lattner4b009652007-07-25 00:24:17 +00001433 continue;
1434 }
Chris Lattner4b009652007-07-25 00:24:17 +00001435 // Okay, we have a legal flexible array member at the end of the struct.
Fariborz Jahanian023a4392007-09-14 16:27:55 +00001436 if (Record)
1437 Record->setHasFlexibleArrayMember(true);
Chris Lattner4b009652007-07-25 00:24:17 +00001438 }
Chris Lattner4b009652007-07-25 00:24:17 +00001439 /// C99 6.7.2.1p2 - a struct ending in a flexible array member cannot be the
1440 /// field of another structure or the element of an array.
Chris Lattner36be3d82007-07-31 21:33:24 +00001441 if (const RecordType *FDTTy = FDTy->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001442 if (FDTTy->getDecl()->hasFlexibleArrayMember()) {
1443 // If this is a member of a union, then entire union becomes "flexible".
Fariborz Jahanian023a4392007-09-14 16:27:55 +00001444 if (Record && Record->getKind() == Decl::Union) {
Chris Lattner4b009652007-07-25 00:24:17 +00001445 Record->setHasFlexibleArrayMember(true);
1446 } else {
1447 // If this is a struct/class and this is not the last element, reject
1448 // it. Note that GCC supports variable sized arrays in the middle of
1449 // structures.
1450 if (i != NumFields-1) {
1451 Diag(FD->getLocation(), diag::err_variable_sized_type_in_struct,
1452 FD->getName());
Steve Naroff9bb759f2007-09-14 22:20:54 +00001453 FD->setInvalidDecl();
1454 EnclosingDecl->setInvalidDecl();
Chris Lattner4b009652007-07-25 00:24:17 +00001455 continue;
1456 }
Chris Lattner4b009652007-07-25 00:24:17 +00001457 // We support flexible arrays at the end of structs in other structs
1458 // as an extension.
1459 Diag(FD->getLocation(), diag::ext_flexible_array_in_struct,
1460 FD->getName());
Fariborz Jahaniancbc36d42007-10-04 00:45:27 +00001461 if (Record)
Fariborz Jahanian023a4392007-09-14 16:27:55 +00001462 Record->setHasFlexibleArrayMember(true);
Chris Lattner4b009652007-07-25 00:24:17 +00001463 }
1464 }
1465 }
Fariborz Jahanian550e0502007-10-12 22:10:42 +00001466 /// A field cannot be an Objective-c object
Ted Kremenek42730c52008-01-07 19:49:32 +00001467 if (FDTy->isObjCInterfaceType()) {
Fariborz Jahanian550e0502007-10-12 22:10:42 +00001468 Diag(FD->getLocation(), diag::err_statically_allocated_object,
1469 FD->getName());
1470 FD->setInvalidDecl();
1471 EnclosingDecl->setInvalidDecl();
1472 continue;
1473 }
Chris Lattner4b009652007-07-25 00:24:17 +00001474 // Keep track of the number of named members.
1475 if (IdentifierInfo *II = FD->getIdentifier()) {
1476 // Detect duplicate member names.
1477 if (!FieldIDs.insert(II)) {
1478 Diag(FD->getLocation(), diag::err_duplicate_member, II->getName());
1479 // Find the previous decl.
1480 SourceLocation PrevLoc;
1481 for (unsigned i = 0, e = RecFields.size(); ; ++i) {
1482 assert(i != e && "Didn't find previous def!");
1483 if (RecFields[i]->getIdentifier() == II) {
1484 PrevLoc = RecFields[i]->getLocation();
1485 break;
1486 }
1487 }
1488 Diag(PrevLoc, diag::err_previous_definition);
Steve Naroff9bb759f2007-09-14 22:20:54 +00001489 FD->setInvalidDecl();
1490 EnclosingDecl->setInvalidDecl();
Chris Lattner4b009652007-07-25 00:24:17 +00001491 continue;
1492 }
1493 ++NumNamedMembers;
1494 }
Chris Lattner4b009652007-07-25 00:24:17 +00001495 }
1496
Chris Lattner4b009652007-07-25 00:24:17 +00001497 // Okay, we successfully defined 'Record'.
Chris Lattner33aad6e2008-02-06 00:51:33 +00001498 if (Record) {
Fariborz Jahanian023a4392007-09-14 16:27:55 +00001499 Record->defineBody(&RecFields[0], RecFields.size());
Chris Lattner33aad6e2008-02-06 00:51:33 +00001500 Consumer.HandleTagDeclDefinition(Record);
1501 } else {
Chris Lattner1100cfb2008-02-05 22:40:55 +00001502 ObjCIvarDecl **ClsFields = reinterpret_cast<ObjCIvarDecl**>(&RecFields[0]);
1503 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(EnclosingDecl))
1504 ID->addInstanceVariablesToClass(ClsFields, RecFields.size(), RBrac);
1505 else if (ObjCImplementationDecl *IMPDecl =
1506 dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001507 assert(IMPDecl && "ActOnFields - missing ObjCImplementationDecl");
1508 IMPDecl->ObjCAddInstanceVariablesToClassImpl(ClsFields, RecFields.size());
Fariborz Jahanian87093732007-10-31 18:48:14 +00001509 CheckImplementationIvars(IMPDecl, ClsFields, RecFields.size(), RBrac);
Fariborz Jahaniand34caf92007-09-26 18:27:25 +00001510 }
Fariborz Jahanianebcc9b62007-09-14 21:08:27 +00001511 }
Chris Lattner4b009652007-07-25 00:24:17 +00001512}
1513
Steve Naroff0acc9c92007-09-15 18:49:24 +00001514Sema::DeclTy *Sema::ActOnEnumConstant(Scope *S, DeclTy *theEnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +00001515 DeclTy *lastEnumConst,
1516 SourceLocation IdLoc, IdentifierInfo *Id,
1517 SourceLocation EqualLoc, ExprTy *val) {
1518 theEnumDecl = theEnumDecl; // silence unused warning.
1519 EnumConstantDecl *LastEnumConst =
1520 cast_or_null<EnumConstantDecl>(static_cast<Decl*>(lastEnumConst));
1521 Expr *Val = static_cast<Expr*>(val);
1522
Chris Lattnera7549902007-08-26 06:24:45 +00001523 // The scope passed in may not be a decl scope. Zip up the scope tree until
1524 // we find one that is.
1525 while ((S->getFlags() & Scope::DeclScope) == 0)
1526 S = S->getParent();
1527
Chris Lattner4b009652007-07-25 00:24:17 +00001528 // Verify that there isn't already something declared with this name in this
1529 // scope.
Steve Naroff6384a012008-04-02 14:35:35 +00001530 if (Decl *PrevDecl = LookupDecl(Id, Decl::IDNS_Ordinary, S)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001531 if (S->isDeclScope(PrevDecl)) {
1532 if (isa<EnumConstantDecl>(PrevDecl))
1533 Diag(IdLoc, diag::err_redefinition_of_enumerator, Id->getName());
1534 else
1535 Diag(IdLoc, diag::err_redefinition, Id->getName());
1536 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
Chris Lattner6ea9bd42008-02-26 00:33:57 +00001537 delete Val;
Chris Lattner4b009652007-07-25 00:24:17 +00001538 return 0;
1539 }
1540 }
1541
1542 llvm::APSInt EnumVal(32);
1543 QualType EltTy;
1544 if (Val) {
Chris Lattner2cda8792007-08-27 21:16:18 +00001545 // Make sure to promote the operand type to int.
1546 UsualUnaryConversions(Val);
1547
Chris Lattner4b009652007-07-25 00:24:17 +00001548 // C99 6.7.2.2p2: Make sure we have an integer constant expression.
1549 SourceLocation ExpLoc;
1550 if (!Val->isIntegerConstantExpr(EnumVal, Context, &ExpLoc)) {
1551 Diag(ExpLoc, diag::err_enum_value_not_integer_constant_expr,
1552 Id->getName());
Chris Lattner6ea9bd42008-02-26 00:33:57 +00001553 delete Val;
Chris Lattnere7f53a42007-08-27 17:37:24 +00001554 Val = 0; // Just forget about it.
Chris Lattner7cea0552007-08-29 16:03:41 +00001555 } else {
1556 EltTy = Val->getType();
Chris Lattner4b009652007-07-25 00:24:17 +00001557 }
Chris Lattnere7f53a42007-08-27 17:37:24 +00001558 }
1559
1560 if (!Val) {
1561 if (LastEnumConst) {
1562 // Assign the last value + 1.
1563 EnumVal = LastEnumConst->getInitVal();
1564 ++EnumVal;
Chris Lattner2cda8792007-08-27 21:16:18 +00001565
1566 // Check for overflow on increment.
1567 if (EnumVal < LastEnumConst->getInitVal())
1568 Diag(IdLoc, diag::warn_enum_value_overflow);
1569
Chris Lattnere7f53a42007-08-27 17:37:24 +00001570 EltTy = LastEnumConst->getType();
1571 } else {
1572 // First value, set to zero.
1573 EltTy = Context.IntTy;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001574 EnumVal.zextOrTrunc(static_cast<uint32_t>(Context.getTypeSize(EltTy)));
Chris Lattnere7f53a42007-08-27 17:37:24 +00001575 }
Chris Lattner4b009652007-07-25 00:24:17 +00001576 }
1577
Chris Lattnere4650482008-03-15 06:12:44 +00001578 EnumConstantDecl *New =
Chris Lattner58114f02008-03-15 21:32:50 +00001579 EnumConstantDecl::Create(Context, IdLoc, Id, EltTy, Val, EnumVal,
1580 LastEnumConst);
Chris Lattner4b009652007-07-25 00:24:17 +00001581
1582 // Register this decl in the current scope stack.
Steve Naroffd21bc0d2007-09-13 18:10:37 +00001583 New->setNext(Id->getFETokenInfo<ScopedDecl>());
Chris Lattner4b009652007-07-25 00:24:17 +00001584 Id->setFETokenInfo(New);
1585 S->AddDecl(New);
1586 return New;
1587}
1588
Steve Naroff0acc9c92007-09-15 18:49:24 +00001589void Sema::ActOnEnumBody(SourceLocation EnumLoc, DeclTy *EnumDeclX,
Chris Lattner4b009652007-07-25 00:24:17 +00001590 DeclTy **Elements, unsigned NumElements) {
1591 EnumDecl *Enum = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX));
1592 assert(!Enum->isDefinition() && "Enum redefinitions can't reach here");
1593
Chris Lattner435c3fd2007-08-28 05:10:31 +00001594 // TODO: If the result value doesn't fit in an int, it must be a long or long
1595 // long value. ISO C does not support this, but GCC does as an extension,
1596 // emit a warning.
Chris Lattner8cd0e932008-03-05 18:54:05 +00001597 unsigned IntWidth = Context.Target.getIntWidth();
Chris Lattner435c3fd2007-08-28 05:10:31 +00001598
Chris Lattner206754a2007-08-28 06:15:15 +00001599 // Verify that all the values are okay, compute the size of the values, and
1600 // reverse the list.
1601 unsigned NumNegativeBits = 0;
1602 unsigned NumPositiveBits = 0;
1603
1604 // Keep track of whether all elements have type int.
1605 bool AllElementsInt = true;
1606
Chris Lattner4b009652007-07-25 00:24:17 +00001607 EnumConstantDecl *EltList = 0;
1608 for (unsigned i = 0; i != NumElements; ++i) {
1609 EnumConstantDecl *ECD =
1610 cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i]));
1611 if (!ECD) continue; // Already issued a diagnostic.
Chris Lattnercb33ae12007-08-28 05:27:00 +00001612
1613 // If the enum value doesn't fit in an int, emit an extension warning.
Chris Lattnercb33ae12007-08-28 05:27:00 +00001614 const llvm::APSInt &InitVal = ECD->getInitVal();
Chris Lattner6ea9bd42008-02-26 00:33:57 +00001615 assert(InitVal.getBitWidth() >= IntWidth &&
1616 "Should have promoted value to int");
Chris Lattnercb33ae12007-08-28 05:27:00 +00001617 if (InitVal.getBitWidth() > IntWidth) {
1618 llvm::APSInt V(InitVal);
1619 V.trunc(IntWidth);
1620 V.extend(InitVal.getBitWidth());
1621 if (V != InitVal)
1622 Diag(ECD->getLocation(), diag::ext_enum_value_not_int,
1623 InitVal.toString());
1624 }
Chris Lattner206754a2007-08-28 06:15:15 +00001625
1626 // Keep track of the size of positive and negative values.
Chris Lattner6ea9bd42008-02-26 00:33:57 +00001627 if (InitVal.isUnsigned() || InitVal.isNonNegative())
Chris Lattneraff63f02008-01-14 21:47:29 +00001628 NumPositiveBits = std::max(NumPositiveBits,
1629 (unsigned)InitVal.getActiveBits());
Chris Lattner206754a2007-08-28 06:15:15 +00001630 else
Chris Lattneraff63f02008-01-14 21:47:29 +00001631 NumNegativeBits = std::max(NumNegativeBits,
1632 (unsigned)InitVal.getMinSignedBits());
Chris Lattner4b009652007-07-25 00:24:17 +00001633
Chris Lattner206754a2007-08-28 06:15:15 +00001634 // Keep track of whether every enum element has type int (very commmon).
1635 if (AllElementsInt)
1636 AllElementsInt = ECD->getType() == Context.IntTy;
1637
Chris Lattner4b009652007-07-25 00:24:17 +00001638 ECD->setNextDeclarator(EltList);
1639 EltList = ECD;
1640 }
1641
Chris Lattner206754a2007-08-28 06:15:15 +00001642 // Figure out the type that should be used for this enum.
1643 // FIXME: Support attribute(packed) on enums and -fshort-enums.
1644 QualType BestType;
Chris Lattnerca01d0a2007-08-29 17:31:48 +00001645 unsigned BestWidth;
Chris Lattner206754a2007-08-28 06:15:15 +00001646
1647 if (NumNegativeBits) {
1648 // If there is a negative value, figure out the smallest integer type (of
1649 // int/long/longlong) that fits.
Chris Lattnerca01d0a2007-08-29 17:31:48 +00001650 if (NumNegativeBits <= IntWidth && NumPositiveBits < IntWidth) {
Chris Lattner206754a2007-08-28 06:15:15 +00001651 BestType = Context.IntTy;
Chris Lattnerca01d0a2007-08-29 17:31:48 +00001652 BestWidth = IntWidth;
1653 } else {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001654 BestWidth = Context.Target.getLongWidth();
Ted Kremenekd7f64cd2007-12-12 22:39:36 +00001655
Chris Lattnerca01d0a2007-08-29 17:31:48 +00001656 if (NumNegativeBits <= BestWidth && NumPositiveBits < BestWidth)
Chris Lattner206754a2007-08-28 06:15:15 +00001657 BestType = Context.LongTy;
1658 else {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001659 BestWidth = Context.Target.getLongLongWidth();
Ted Kremenekd7f64cd2007-12-12 22:39:36 +00001660
Chris Lattnerca01d0a2007-08-29 17:31:48 +00001661 if (NumNegativeBits > BestWidth || NumPositiveBits >= BestWidth)
Chris Lattner206754a2007-08-28 06:15:15 +00001662 Diag(Enum->getLocation(), diag::warn_enum_too_large);
1663 BestType = Context.LongLongTy;
1664 }
1665 }
1666 } else {
1667 // If there is no negative value, figure out which of uint, ulong, ulonglong
1668 // fits.
Chris Lattnerca01d0a2007-08-29 17:31:48 +00001669 if (NumPositiveBits <= IntWidth) {
Chris Lattner206754a2007-08-28 06:15:15 +00001670 BestType = Context.UnsignedIntTy;
Chris Lattnerca01d0a2007-08-29 17:31:48 +00001671 BestWidth = IntWidth;
1672 } else if (NumPositiveBits <=
Chris Lattner8cd0e932008-03-05 18:54:05 +00001673 (BestWidth = Context.Target.getLongWidth())) {
Chris Lattner206754a2007-08-28 06:15:15 +00001674 BestType = Context.UnsignedLongTy;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001675 } else {
1676 BestWidth = Context.Target.getLongLongWidth();
Chris Lattnerca01d0a2007-08-29 17:31:48 +00001677 assert(NumPositiveBits <= BestWidth &&
Chris Lattner206754a2007-08-28 06:15:15 +00001678 "How could an initializer get larger than ULL?");
1679 BestType = Context.UnsignedLongLongTy;
1680 }
1681 }
1682
Chris Lattnerca01d0a2007-08-29 17:31:48 +00001683 // Loop over all of the enumerator constants, changing their types to match
1684 // the type of the enum if needed.
1685 for (unsigned i = 0; i != NumElements; ++i) {
1686 EnumConstantDecl *ECD =
1687 cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i]));
1688 if (!ECD) continue; // Already issued a diagnostic.
1689
1690 // Standard C says the enumerators have int type, but we allow, as an
1691 // extension, the enumerators to be larger than int size. If each
1692 // enumerator value fits in an int, type it as an int, otherwise type it the
1693 // same as the enumerator decl itself. This means that in "enum { X = 1U }"
1694 // that X has type 'int', not 'unsigned'.
Chris Lattner6ea9bd42008-02-26 00:33:57 +00001695 if (ECD->getType() == Context.IntTy) {
1696 // Make sure the init value is signed.
1697 llvm::APSInt IV = ECD->getInitVal();
1698 IV.setIsSigned(true);
1699 ECD->setInitVal(IV);
Chris Lattnerca01d0a2007-08-29 17:31:48 +00001700 continue; // Already int type.
Chris Lattner6ea9bd42008-02-26 00:33:57 +00001701 }
Chris Lattnerca01d0a2007-08-29 17:31:48 +00001702
1703 // Determine whether the value fits into an int.
1704 llvm::APSInt InitVal = ECD->getInitVal();
1705 bool FitsInInt;
1706 if (InitVal.isUnsigned() || !InitVal.isNegative())
1707 FitsInInt = InitVal.getActiveBits() < IntWidth;
1708 else
1709 FitsInInt = InitVal.getMinSignedBits() <= IntWidth;
1710
1711 // If it fits into an integer type, force it. Otherwise force it to match
1712 // the enum decl type.
1713 QualType NewTy;
1714 unsigned NewWidth;
1715 bool NewSign;
1716 if (FitsInInt) {
1717 NewTy = Context.IntTy;
1718 NewWidth = IntWidth;
1719 NewSign = true;
1720 } else if (ECD->getType() == BestType) {
1721 // Already the right type!
1722 continue;
1723 } else {
1724 NewTy = BestType;
1725 NewWidth = BestWidth;
1726 NewSign = BestType->isSignedIntegerType();
1727 }
1728
1729 // Adjust the APSInt value.
1730 InitVal.extOrTrunc(NewWidth);
1731 InitVal.setIsSigned(NewSign);
1732 ECD->setInitVal(InitVal);
1733
1734 // Adjust the Expr initializer and type.
1735 ECD->setInitExpr(new ImplicitCastExpr(NewTy, ECD->getInitExpr()));
1736 ECD->setType(NewTy);
1737 }
Chris Lattner206754a2007-08-28 06:15:15 +00001738
Chris Lattner90a018d2007-08-28 18:24:31 +00001739 Enum->defineElements(EltList, BestType);
Chris Lattner33aad6e2008-02-06 00:51:33 +00001740 Consumer.HandleTagDeclDefinition(Enum);
Chris Lattner4b009652007-07-25 00:24:17 +00001741}
1742
Anders Carlsson4f7f4412008-02-08 00:33:21 +00001743Sema::DeclTy *Sema::ActOnFileScopeAsmDecl(SourceLocation Loc,
1744 ExprTy *expr) {
1745 StringLiteral *AsmString = cast<StringLiteral>((Expr*)expr);
1746
Chris Lattner81db64a2008-03-16 00:16:02 +00001747 return FileScopeAsmDecl::Create(Context, Loc, AsmString);
Anders Carlsson4f7f4412008-02-08 00:33:21 +00001748}
1749
Chris Lattner806a5f52008-01-12 07:05:38 +00001750Sema::DeclTy* Sema::ActOnLinkageSpec(SourceLocation Loc,
Chris Lattner43b885f2008-02-25 21:04:36 +00001751 SourceLocation LBrace,
1752 SourceLocation RBrace,
1753 const char *Lang,
1754 unsigned StrSize,
1755 DeclTy *D) {
Chris Lattner806a5f52008-01-12 07:05:38 +00001756 LinkageSpecDecl::LanguageIDs Language;
1757 Decl *dcl = static_cast<Decl *>(D);
1758 if (strncmp(Lang, "\"C\"", StrSize) == 0)
1759 Language = LinkageSpecDecl::lang_c;
1760 else if (strncmp(Lang, "\"C++\"", StrSize) == 0)
1761 Language = LinkageSpecDecl::lang_cxx;
1762 else {
1763 Diag(Loc, diag::err_bad_language);
1764 return 0;
1765 }
1766
1767 // FIXME: Add all the various semantics of linkage specifications
Chris Lattner81db64a2008-03-16 00:16:02 +00001768 return LinkageSpecDecl::Create(Context, Loc, Language, dcl);
Chris Lattner806a5f52008-01-12 07:05:38 +00001769}
1770
Chris Lattner49d15cb2008-02-21 00:48:22 +00001771void Sema::HandleDeclAttribute(Decl *New, AttributeList *Attr) {
Anders Carlsson28e34e32007-12-19 06:16:30 +00001772
Chris Lattner49d15cb2008-02-21 00:48:22 +00001773 switch (Attr->getKind()) {
Chris Lattnerb9716a62008-02-20 23:17:35 +00001774 case AttributeList::AT_vector_size:
Chris Lattner4b009652007-07-25 00:24:17 +00001775 if (ValueDecl *vDecl = dyn_cast<ValueDecl>(New)) {
Chris Lattner49d15cb2008-02-21 00:48:22 +00001776 QualType newType = HandleVectorTypeAttribute(vDecl->getType(), Attr);
Chris Lattner4b009652007-07-25 00:24:17 +00001777 if (!newType.isNull()) // install the new vector type into the decl
1778 vDecl->setType(newType);
1779 }
1780 if (TypedefDecl *tDecl = dyn_cast<TypedefDecl>(New)) {
1781 QualType newType = HandleVectorTypeAttribute(tDecl->getUnderlyingType(),
Chris Lattner49d15cb2008-02-21 00:48:22 +00001782 Attr);
Chris Lattner4b009652007-07-25 00:24:17 +00001783 if (!newType.isNull()) // install the new vector type into the decl
1784 tDecl->setUnderlyingType(newType);
1785 }
Chris Lattnerb9716a62008-02-20 23:17:35 +00001786 break;
1787 case AttributeList::AT_ocu_vector_type:
Steve Naroff82113e32007-07-29 16:33:31 +00001788 if (TypedefDecl *tDecl = dyn_cast<TypedefDecl>(New))
Chris Lattner49d15cb2008-02-21 00:48:22 +00001789 HandleOCUVectorTypeAttribute(tDecl, Attr);
Steve Naroff82113e32007-07-29 16:33:31 +00001790 else
Chris Lattner49d15cb2008-02-21 00:48:22 +00001791 Diag(Attr->getLoc(),
Chris Lattner4b009652007-07-25 00:24:17 +00001792 diag::err_typecheck_ocu_vector_not_typedef);
Chris Lattnerb9716a62008-02-20 23:17:35 +00001793 break;
1794 case AttributeList::AT_address_space:
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001795 if (TypedefDecl *tDecl = dyn_cast<TypedefDecl>(New)) {
1796 QualType newType = HandleAddressSpaceTypeAttribute(
1797 tDecl->getUnderlyingType(),
Chris Lattner49d15cb2008-02-21 00:48:22 +00001798 Attr);
1799 tDecl->setUnderlyingType(newType);
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001800 } else if (ValueDecl *vDecl = dyn_cast<ValueDecl>(New)) {
1801 QualType newType = HandleAddressSpaceTypeAttribute(vDecl->getType(),
Chris Lattner49d15cb2008-02-21 00:48:22 +00001802 Attr);
1803 // install the new addr spaced type into the decl
1804 vDecl->setType(newType);
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001805 }
Chris Lattnerb9716a62008-02-20 23:17:35 +00001806 break;
Chris Lattneree4c3bf2008-02-29 16:48:43 +00001807 case AttributeList::AT_deprecated:
Chris Lattner402b3372008-03-03 03:28:21 +00001808 HandleDeprecatedAttribute(New, Attr);
1809 break;
1810 case AttributeList::AT_visibility:
1811 HandleVisibilityAttribute(New, Attr);
1812 break;
1813 case AttributeList::AT_weak:
1814 HandleWeakAttribute(New, Attr);
1815 break;
1816 case AttributeList::AT_dllimport:
1817 HandleDLLImportAttribute(New, Attr);
1818 break;
1819 case AttributeList::AT_dllexport:
1820 HandleDLLExportAttribute(New, Attr);
1821 break;
1822 case AttributeList::AT_nothrow:
1823 HandleNothrowAttribute(New, Attr);
Chris Lattneree4c3bf2008-02-29 16:48:43 +00001824 break;
Nate Begemand75d28b2008-03-07 20:04:22 +00001825 case AttributeList::AT_stdcall:
1826 HandleStdCallAttribute(New, Attr);
1827 break;
1828 case AttributeList::AT_fastcall:
1829 HandleFastCallAttribute(New, Attr);
1830 break;
Chris Lattnerb9716a62008-02-20 23:17:35 +00001831 case AttributeList::AT_aligned:
Chris Lattner49d15cb2008-02-21 00:48:22 +00001832 HandleAlignedAttribute(New, Attr);
Chris Lattnerb9716a62008-02-20 23:17:35 +00001833 break;
1834 case AttributeList::AT_packed:
Chris Lattner49d15cb2008-02-21 00:48:22 +00001835 HandlePackedAttribute(New, Attr);
Chris Lattnerb9716a62008-02-20 23:17:35 +00001836 break;
Nate Begeman754d3fc2008-02-21 19:30:49 +00001837 case AttributeList::AT_annotate:
1838 HandleAnnotateAttribute(New, Attr);
1839 break;
Ted Kremenek13bfae62008-02-27 20:43:06 +00001840 case AttributeList::AT_noreturn:
1841 HandleNoReturnAttribute(New, Attr);
1842 break;
Chris Lattner402b3372008-03-03 03:28:21 +00001843 case AttributeList::AT_format:
1844 HandleFormatAttribute(New, Attr);
1845 break;
Chris Lattnerb9716a62008-02-20 23:17:35 +00001846 default:
Chris Lattneree4c3bf2008-02-29 16:48:43 +00001847#if 0
1848 // TODO: when we have the full set of attributes, warn about unknown ones.
1849 Diag(Attr->getLoc(), diag::warn_attribute_ignored,
1850 Attr->getName()->getName());
1851#endif
Chris Lattnerb9716a62008-02-20 23:17:35 +00001852 break;
1853 }
Chris Lattner4b009652007-07-25 00:24:17 +00001854}
1855
1856void Sema::HandleDeclAttributes(Decl *New, AttributeList *declspec_prefix,
1857 AttributeList *declarator_postfix) {
1858 while (declspec_prefix) {
1859 HandleDeclAttribute(New, declspec_prefix);
1860 declspec_prefix = declspec_prefix->getNext();
1861 }
1862 while (declarator_postfix) {
1863 HandleDeclAttribute(New, declarator_postfix);
1864 declarator_postfix = declarator_postfix->getNext();
1865 }
1866}
1867
Steve Naroff82113e32007-07-29 16:33:31 +00001868void Sema::HandleOCUVectorTypeAttribute(TypedefDecl *tDecl,
1869 AttributeList *rawAttr) {
1870 QualType curType = tDecl->getUnderlyingType();
Anders Carlssonc8b44122007-12-19 07:19:40 +00001871 // check the attribute arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00001872 if (rawAttr->getNumArgs() != 1) {
Chris Lattner9384f502008-02-20 23:25:22 +00001873 Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
Chris Lattner4b009652007-07-25 00:24:17 +00001874 std::string("1"));
Steve Naroff82113e32007-07-29 16:33:31 +00001875 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001876 }
1877 Expr *sizeExpr = static_cast<Expr *>(rawAttr->getArg(0));
1878 llvm::APSInt vecSize(32);
1879 if (!sizeExpr->isIntegerConstantExpr(vecSize, Context)) {
Chris Lattner9384f502008-02-20 23:25:22 +00001880 Diag(rawAttr->getLoc(), diag::err_attribute_argument_not_int,
Anders Carlsson7dce0292008-02-16 19:51:27 +00001881 "ocu_vector_type", sizeExpr->getSourceRange());
Steve Naroff82113e32007-07-29 16:33:31 +00001882 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001883 }
1884 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
1885 // in conjunction with complex types (pointers, arrays, functions, etc.).
1886 Type *canonType = curType.getCanonicalType().getTypePtr();
1887 if (!(canonType->isIntegerType() || canonType->isRealFloatingType())) {
Chris Lattner9384f502008-02-20 23:25:22 +00001888 Diag(rawAttr->getLoc(), diag::err_attribute_invalid_vector_type,
Chris Lattner4b009652007-07-25 00:24:17 +00001889 curType.getCanonicalType().getAsString());
Steve Naroff82113e32007-07-29 16:33:31 +00001890 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001891 }
1892 // unlike gcc's vector_size attribute, the size is specified as the
1893 // number of elements, not the number of bytes.
Chris Lattner3496d522007-09-04 02:45:27 +00001894 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
Chris Lattner4b009652007-07-25 00:24:17 +00001895
1896 if (vectorSize == 0) {
Chris Lattner9384f502008-02-20 23:25:22 +00001897 Diag(rawAttr->getLoc(), diag::err_attribute_zero_size,
Chris Lattner4b009652007-07-25 00:24:17 +00001898 sizeExpr->getSourceRange());
Steve Naroff82113e32007-07-29 16:33:31 +00001899 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001900 }
Steve Naroff82113e32007-07-29 16:33:31 +00001901 // Instantiate/Install the vector type, the number of elements is > 0.
1902 tDecl->setUnderlyingType(Context.getOCUVectorType(curType, vectorSize));
1903 // Remember this typedef decl, we will need it later for diagnostics.
1904 OCUVectorDecls.push_back(tDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001905}
1906
1907QualType Sema::HandleVectorTypeAttribute(QualType curType,
1908 AttributeList *rawAttr) {
1909 // check the attribute arugments.
1910 if (rawAttr->getNumArgs() != 1) {
Chris Lattner9384f502008-02-20 23:25:22 +00001911 Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
Chris Lattner4b009652007-07-25 00:24:17 +00001912 std::string("1"));
1913 return QualType();
1914 }
1915 Expr *sizeExpr = static_cast<Expr *>(rawAttr->getArg(0));
1916 llvm::APSInt vecSize(32);
1917 if (!sizeExpr->isIntegerConstantExpr(vecSize, Context)) {
Chris Lattner9384f502008-02-20 23:25:22 +00001918 Diag(rawAttr->getLoc(), diag::err_attribute_argument_not_int,
Anders Carlsson7dce0292008-02-16 19:51:27 +00001919 "vector_size", sizeExpr->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +00001920 return QualType();
1921 }
1922 // navigate to the base type - we need to provide for vector pointers,
1923 // vector arrays, and functions returning vectors.
1924 Type *canonType = curType.getCanonicalType().getTypePtr();
1925
1926 if (canonType->isPointerType() || canonType->isArrayType() ||
1927 canonType->isFunctionType()) {
Chris Lattner5b5e1982007-12-19 05:38:06 +00001928 assert(0 && "HandleVector(): Complex type construction unimplemented");
Chris Lattner4b009652007-07-25 00:24:17 +00001929 /* FIXME: rebuild the type from the inside out, vectorizing the inner type.
1930 do {
1931 if (PointerType *PT = dyn_cast<PointerType>(canonType))
1932 canonType = PT->getPointeeType().getTypePtr();
1933 else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
1934 canonType = AT->getElementType().getTypePtr();
1935 else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
1936 canonType = FT->getResultType().getTypePtr();
1937 } while (canonType->isPointerType() || canonType->isArrayType() ||
1938 canonType->isFunctionType());
1939 */
1940 }
1941 // the base type must be integer or float.
1942 if (!(canonType->isIntegerType() || canonType->isRealFloatingType())) {
Chris Lattner9384f502008-02-20 23:25:22 +00001943 Diag(rawAttr->getLoc(), diag::err_attribute_invalid_vector_type,
Chris Lattner4b009652007-07-25 00:24:17 +00001944 curType.getCanonicalType().getAsString());
1945 return QualType();
1946 }
Chris Lattner8cd0e932008-03-05 18:54:05 +00001947 unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(curType));
Chris Lattner4b009652007-07-25 00:24:17 +00001948 // vecSize is specified in bytes - convert to bits.
Chris Lattner3496d522007-09-04 02:45:27 +00001949 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
Chris Lattner4b009652007-07-25 00:24:17 +00001950
1951 // the vector size needs to be an integral multiple of the type size.
1952 if (vectorSize % typeSize) {
Chris Lattner9384f502008-02-20 23:25:22 +00001953 Diag(rawAttr->getLoc(), diag::err_attribute_invalid_size,
Chris Lattner4b009652007-07-25 00:24:17 +00001954 sizeExpr->getSourceRange());
1955 return QualType();
1956 }
1957 if (vectorSize == 0) {
Chris Lattner9384f502008-02-20 23:25:22 +00001958 Diag(rawAttr->getLoc(), diag::err_attribute_zero_size,
Chris Lattner4b009652007-07-25 00:24:17 +00001959 sizeExpr->getSourceRange());
1960 return QualType();
1961 }
Nate Begeman754d3fc2008-02-21 19:30:49 +00001962 // Instantiate the vector type, the number of elements is > 0, and not
1963 // required to be a power of 2, unlike GCC.
Chris Lattner4b009652007-07-25 00:24:17 +00001964 return Context.getVectorType(curType, vectorSize/typeSize);
1965}
1966
Chris Lattner9384f502008-02-20 23:25:22 +00001967void Sema::HandlePackedAttribute(Decl *d, AttributeList *rawAttr) {
Anders Carlsson136cdc32008-02-16 00:29:18 +00001968 // check the attribute arguments.
1969 if (rawAttr->getNumArgs() > 0) {
Chris Lattner9384f502008-02-20 23:25:22 +00001970 Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
Anders Carlsson136cdc32008-02-16 00:29:18 +00001971 std::string("0"));
1972 return;
1973 }
1974
1975 if (TagDecl *TD = dyn_cast<TagDecl>(d))
1976 TD->addAttr(new PackedAttr);
1977 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
1978 // If the alignment is less than or equal to 8 bits, the packed attribute
1979 // has no effect.
Chris Lattner8cd0e932008-03-05 18:54:05 +00001980 if (Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner9384f502008-02-20 23:25:22 +00001981 Diag(rawAttr->getLoc(),
Anders Carlsson136cdc32008-02-16 00:29:18 +00001982 diag::warn_attribute_ignored_for_field_of_type,
Chris Lattner9384f502008-02-20 23:25:22 +00001983 rawAttr->getName()->getName(), FD->getType().getAsString());
Anders Carlsson136cdc32008-02-16 00:29:18 +00001984 else
Anders Carlssonca133d92008-02-16 00:39:40 +00001985 FD->addAttr(new PackedAttr);
Anders Carlsson136cdc32008-02-16 00:29:18 +00001986 } else
Chris Lattner9384f502008-02-20 23:25:22 +00001987 Diag(rawAttr->getLoc(), diag::warn_attribute_ignored,
1988 rawAttr->getName()->getName());
Anders Carlsson136cdc32008-02-16 00:29:18 +00001989}
Nate Begeman754d3fc2008-02-21 19:30:49 +00001990
Ted Kremenek13bfae62008-02-27 20:43:06 +00001991void Sema::HandleNoReturnAttribute(Decl *d, AttributeList *rawAttr) {
1992 // check the attribute arguments.
1993 if (rawAttr->getNumArgs() != 0) {
1994 Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
1995 std::string("0"));
1996 return;
1997 }
1998
Ted Kremenek40b95e52008-03-03 16:52:27 +00001999 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
2000
2001 if (!Fn) {
2002 Diag(rawAttr->getLoc(), diag::warn_attribute_wrong_decl_type,
2003 "noreturn", "function");
2004 return;
2005 }
2006
Ted Kremenek13bfae62008-02-27 20:43:06 +00002007 d->addAttr(new NoReturnAttr());
2008}
2009
Chris Lattner402b3372008-03-03 03:28:21 +00002010void Sema::HandleDeprecatedAttribute(Decl *d, AttributeList *rawAttr) {
2011 // check the attribute arguments.
2012 if (rawAttr->getNumArgs() != 0) {
2013 Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
2014 std::string("0"));
2015 return;
2016 }
2017
2018 d->addAttr(new DeprecatedAttr());
2019}
2020
2021void Sema::HandleVisibilityAttribute(Decl *d, AttributeList *rawAttr) {
2022 // check the attribute arguments.
Chris Lattnere9d83be2008-03-04 18:08:48 +00002023 if (rawAttr->getNumArgs() != 1) {
Chris Lattner402b3372008-03-03 03:28:21 +00002024 Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
2025 std::string("1"));
2026 return;
2027 }
2028
Chris Lattnere9d83be2008-03-04 18:08:48 +00002029 Expr *Arg = static_cast<Expr*>(rawAttr->getArg(0));
2030 Arg = Arg->IgnoreParenCasts();
2031 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
2032
2033 if (Str == 0 || Str->isWide()) {
Chris Lattner402b3372008-03-03 03:28:21 +00002034 Diag(rawAttr->getLoc(), diag::err_attribute_argument_n_not_string,
Chris Lattnere9d83be2008-03-04 18:08:48 +00002035 "visibility", std::string("1"));
Chris Lattner402b3372008-03-03 03:28:21 +00002036 return;
2037 }
2038
Chris Lattnere9d83be2008-03-04 18:08:48 +00002039 const char *TypeStr = Str->getStrData();
2040 unsigned TypeLen = Str->getByteLength();
Chris Lattner402b3372008-03-03 03:28:21 +00002041 llvm::GlobalValue::VisibilityTypes type;
2042
Chris Lattnere9d83be2008-03-04 18:08:48 +00002043 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
Chris Lattner402b3372008-03-03 03:28:21 +00002044 type = llvm::GlobalValue::DefaultVisibility;
Chris Lattnere9d83be2008-03-04 18:08:48 +00002045 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
Chris Lattner402b3372008-03-03 03:28:21 +00002046 type = llvm::GlobalValue::HiddenVisibility;
Chris Lattnere9d83be2008-03-04 18:08:48 +00002047 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
Chris Lattner402b3372008-03-03 03:28:21 +00002048 type = llvm::GlobalValue::HiddenVisibility; // FIXME
Chris Lattnere9d83be2008-03-04 18:08:48 +00002049 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
Chris Lattner402b3372008-03-03 03:28:21 +00002050 type = llvm::GlobalValue::ProtectedVisibility;
2051 else {
2052 Diag(rawAttr->getLoc(), diag::warn_attribute_type_not_supported,
Chris Lattnere9d83be2008-03-04 18:08:48 +00002053 "visibility", TypeStr);
Chris Lattner402b3372008-03-03 03:28:21 +00002054 return;
2055 }
2056
2057 d->addAttr(new VisibilityAttr(type));
2058}
2059
2060void Sema::HandleWeakAttribute(Decl *d, AttributeList *rawAttr) {
2061 // check the attribute arguments.
2062 if (rawAttr->getNumArgs() != 0) {
2063 Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
2064 std::string("0"));
2065 return;
2066 }
2067
2068 d->addAttr(new WeakAttr());
2069}
2070
2071void Sema::HandleDLLImportAttribute(Decl *d, AttributeList *rawAttr) {
2072 // check the attribute arguments.
2073 if (rawAttr->getNumArgs() != 0) {
2074 Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
2075 std::string("0"));
2076 return;
2077 }
2078
2079 d->addAttr(new DLLImportAttr());
2080}
2081
2082void Sema::HandleDLLExportAttribute(Decl *d, AttributeList *rawAttr) {
2083 // check the attribute arguments.
2084 if (rawAttr->getNumArgs() != 0) {
2085 Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
2086 std::string("0"));
2087 return;
2088 }
2089
2090 d->addAttr(new DLLExportAttr());
2091}
2092
Nate Begemand75d28b2008-03-07 20:04:22 +00002093void Sema::HandleStdCallAttribute(Decl *d, AttributeList *rawAttr) {
2094 // check the attribute arguments.
2095 if (rawAttr->getNumArgs() != 0) {
2096 Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
2097 std::string("0"));
2098 return;
2099 }
2100
2101 d->addAttr(new StdCallAttr());
2102}
2103
2104void Sema::HandleFastCallAttribute(Decl *d, AttributeList *rawAttr) {
2105 // check the attribute arguments.
2106 if (rawAttr->getNumArgs() != 0) {
2107 Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
2108 std::string("0"));
2109 return;
2110 }
2111
2112 d->addAttr(new FastCallAttr());
2113}
2114
Chris Lattner402b3372008-03-03 03:28:21 +00002115void Sema::HandleNothrowAttribute(Decl *d, AttributeList *rawAttr) {
2116 // check the attribute arguments.
2117 if (rawAttr->getNumArgs() != 0) {
2118 Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
2119 std::string("0"));
2120 return;
2121 }
2122
2123 d->addAttr(new NoThrowAttr());
2124}
2125
Nuno Lopes02564e52008-03-25 23:01:48 +00002126static const FunctionTypeProto *getFunctionProto(Decl *d) {
2127 ValueDecl *decl = dyn_cast<ValueDecl>(d);
2128 if (!decl) return 0;
2129
2130 QualType Ty = decl->getType();
2131
2132 if (Ty->isFunctionPointerType()) {
2133 const PointerType *PtrTy = Ty->getAsPointerType();
2134 Ty = PtrTy->getPointeeType();
2135 }
2136
2137 if (const FunctionType *FnTy = Ty->getAsFunctionType())
2138 return dyn_cast<FunctionTypeProto>(FnTy->getAsFunctionType());
2139
2140 return 0;
2141}
2142
2143
Ted Kremeneke5769412008-03-07 18:43:49 +00002144/// Handle __attribute__((format(type,idx,firstarg))) attributes
2145/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner402b3372008-03-03 03:28:21 +00002146void Sema::HandleFormatAttribute(Decl *d, AttributeList *rawAttr) {
2147
2148 if (!rawAttr->getParameterName()) {
2149 Diag(rawAttr->getLoc(), diag::err_attribute_argument_n_not_string,
2150 "format", std::string("1"));
2151 return;
2152 }
2153
2154 if (rawAttr->getNumArgs() != 2) {
2155 Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
2156 std::string("3"));
2157 return;
2158 }
2159
Nuno Lopes02564e52008-03-25 23:01:48 +00002160 // GCC ignores the format attribute on K&R style function
2161 // prototypes, so we ignore it as well
2162 const FunctionTypeProto *proto = getFunctionProto(d);
2163
2164 if (!proto) {
Chris Lattner402b3372008-03-03 03:28:21 +00002165 Diag(rawAttr->getLoc(), diag::warn_attribute_wrong_decl_type,
2166 "format", "function");
2167 return;
2168 }
2169
2170 // FIXME: in C++ the implicit 'this' function parameter also counts.
Ted Kremeneke5769412008-03-07 18:43:49 +00002171 // this is needed in order to be compatible with GCC
Chris Lattner402b3372008-03-03 03:28:21 +00002172 // the index must start in 1 and the limit is numargs+1
Nuno Lopes02564e52008-03-25 23:01:48 +00002173 unsigned NumArgs = proto->getNumArgs();
Ted Kremeneke5769412008-03-07 18:43:49 +00002174 unsigned FirstIdx = 1;
Chris Lattner402b3372008-03-03 03:28:21 +00002175
2176 const char *Format = rawAttr->getParameterName()->getName();
2177 unsigned FormatLen = rawAttr->getParameterName()->getLength();
2178
2179 // Normalize the argument, __foo__ becomes foo.
2180 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
2181 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
2182 Format += 2;
2183 FormatLen -= 4;
2184 }
2185
2186 if (!((FormatLen == 5 && !memcmp(Format, "scanf", 5))
2187 || (FormatLen == 6 && !memcmp(Format, "printf", 6))
2188 || (FormatLen == 7 && !memcmp(Format, "strfmon", 7))
2189 || (FormatLen == 8 && !memcmp(Format, "strftime", 8)))) {
2190 Diag(rawAttr->getLoc(), diag::warn_attribute_type_not_supported,
2191 "format", rawAttr->getParameterName()->getName());
2192 return;
2193 }
2194
Ted Kremeneke5769412008-03-07 18:43:49 +00002195 // checks for the 2nd argument
Chris Lattner402b3372008-03-03 03:28:21 +00002196 Expr *IdxExpr = static_cast<Expr *>(rawAttr->getArg(0));
Ted Kremeneke5769412008-03-07 18:43:49 +00002197 llvm::APSInt Idx(Context.getTypeSize(IdxExpr->getType()));
Chris Lattner402b3372008-03-03 03:28:21 +00002198 if (!IdxExpr->isIntegerConstantExpr(Idx, Context)) {
2199 Diag(rawAttr->getLoc(), diag::err_attribute_argument_n_not_int,
2200 "format", std::string("2"), IdxExpr->getSourceRange());
2201 return;
2202 }
2203
Ted Kremeneke5769412008-03-07 18:43:49 +00002204 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner402b3372008-03-03 03:28:21 +00002205 Diag(rawAttr->getLoc(), diag::err_attribute_argument_out_of_bounds,
2206 "format", std::string("2"), IdxExpr->getSourceRange());
2207 return;
2208 }
2209
Ted Kremeneke5769412008-03-07 18:43:49 +00002210 // make sure the format string is really a string
2211 QualType Ty = proto->getArgType(Idx.getZExtValue()-1);
2212 if (!Ty->isPointerType() ||
2213 !Ty->getAsPointerType()->getPointeeType()->isCharType()) {
2214 Diag(rawAttr->getLoc(), diag::err_format_attribute_not_string,
2215 IdxExpr->getSourceRange());
2216 return;
2217 }
2218
2219
2220 // check the 3rd argument
Chris Lattner402b3372008-03-03 03:28:21 +00002221 Expr *FirstArgExpr = static_cast<Expr *>(rawAttr->getArg(1));
Ted Kremeneke5769412008-03-07 18:43:49 +00002222 llvm::APSInt FirstArg(Context.getTypeSize(FirstArgExpr->getType()));
Chris Lattner402b3372008-03-03 03:28:21 +00002223 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, Context)) {
2224 Diag(rawAttr->getLoc(), diag::err_attribute_argument_n_not_int,
2225 "format", std::string("3"), FirstArgExpr->getSourceRange());
2226 return;
2227 }
2228
Ted Kremeneke5769412008-03-07 18:43:49 +00002229 // check if the function is variadic if the 3rd argument non-zero
2230 if (FirstArg != 0) {
2231 if (proto->isVariadic()) {
2232 ++NumArgs; // +1 for ...
2233 } else {
2234 Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
2235 return;
2236 }
2237 }
2238
2239 // strftime requires FirstArg to be 0 because it doesn't read from any variable
2240 // the input is just the current time + the format string
Chris Lattner402b3372008-03-03 03:28:21 +00002241 if (FormatLen == 8 && !memcmp(Format, "strftime", 8)) {
Ted Kremeneke5769412008-03-07 18:43:49 +00002242 if (FirstArg != 0) {
Chris Lattner402b3372008-03-03 03:28:21 +00002243 Diag(rawAttr->getLoc(), diag::err_format_strftime_third_parameter,
2244 FirstArgExpr->getSourceRange());
2245 return;
2246 }
Ted Kremeneke5769412008-03-07 18:43:49 +00002247 // if 0 it disables parameter checking (to use with e.g. va_list)
2248 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner402b3372008-03-03 03:28:21 +00002249 Diag(rawAttr->getLoc(), diag::err_attribute_argument_out_of_bounds,
2250 "format", std::string("3"), FirstArgExpr->getSourceRange());
2251 return;
2252 }
2253
2254 d->addAttr(new FormatAttr(std::string(Format, FormatLen),
2255 Idx.getZExtValue(), FirstArg.getZExtValue()));
2256}
2257
Nate Begeman754d3fc2008-02-21 19:30:49 +00002258void Sema::HandleAnnotateAttribute(Decl *d, AttributeList *rawAttr) {
2259 // check the attribute arguments.
2260 if (rawAttr->getNumArgs() != 1) {
2261 Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
2262 std::string("1"));
2263 return;
2264 }
2265 Expr *argExpr = static_cast<Expr *>(rawAttr->getArg(0));
2266 StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
Anders Carlsson136cdc32008-02-16 00:29:18 +00002267
Nate Begeman754d3fc2008-02-21 19:30:49 +00002268 // Make sure that there is a string literal as the annotation's single
2269 // argument.
2270 if (!SE) {
2271 Diag(rawAttr->getLoc(), diag::err_attribute_annotate_no_string);
2272 return;
2273 }
2274 d->addAttr(new AnnotateAttr(std::string(SE->getStrData(),
2275 SE->getByteLength())));
2276}
2277
Anders Carlssonc8b44122007-12-19 07:19:40 +00002278void Sema::HandleAlignedAttribute(Decl *d, AttributeList *rawAttr)
2279{
2280 // check the attribute arguments.
Eli Friedman74820702008-01-30 17:38:42 +00002281 if (rawAttr->getNumArgs() > 1) {
Chris Lattner9384f502008-02-20 23:25:22 +00002282 Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
Anders Carlssonc8b44122007-12-19 07:19:40 +00002283 std::string("1"));
2284 return;
2285 }
Eli Friedman74820702008-01-30 17:38:42 +00002286
Anders Carlsson7dce0292008-02-16 19:51:27 +00002287 unsigned Align = 0;
2288
2289 if (rawAttr->getNumArgs() == 0) {
2290 // FIXME: This should be the target specific maximum alignment.
2291 // (For now we just use 128 bits which is the maximum on X86.
2292 Align = 128;
Eli Friedman74820702008-01-30 17:38:42 +00002293 return;
Anders Carlsson7dce0292008-02-16 19:51:27 +00002294 } else {
2295 Expr *alignmentExpr = static_cast<Expr *>(rawAttr->getArg(0));
2296 llvm::APSInt alignment(32);
2297 if (!alignmentExpr->isIntegerConstantExpr(alignment, Context)) {
Chris Lattner9384f502008-02-20 23:25:22 +00002298 Diag(rawAttr->getLoc(), diag::err_attribute_argument_not_int,
Anders Carlsson7dce0292008-02-16 19:51:27 +00002299 "aligned", alignmentExpr->getSourceRange());
2300 return;
2301 }
2302
2303 Align = alignment.getZExtValue() * 8;
2304 }
Eli Friedman74820702008-01-30 17:38:42 +00002305
Anders Carlsson7dce0292008-02-16 19:51:27 +00002306 d->addAttr(new AlignedAttr(Align));
Anders Carlssonc8b44122007-12-19 07:19:40 +00002307}