blob: 9429d4b4130374083821b65cbd732cf27e32daea [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- SemaDecl.cpp - Semantic Analysis for Declarations ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Chris Lattnere1e79852008-02-06 00:51:33 +000015#include "clang/AST/ASTConsumer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/AST/ASTContext.h"
Anders Carlssonf78915f2008-02-15 07:04:12 +000017#include "clang/AST/Attr.h"
Reid Spencer5f016e22007-07-11 17:01:13 +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"
Reid Spencer5f016e22007-07-11 17:01:13 +000024#include "clang/Basic/LangOptions.h"
25#include "clang/Basic/TargetInfo.h"
Steve Naroff4c49a6c2008-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 Lattnere1e79852008-02-06 00:51:33 +000028#include "clang/Lex/Preprocessor.h"
Steve Naroff4c49a6c2008-01-30 23:46:05 +000029#include "clang/Lex/HeaderSearch.h"
Steve Naroff563477d2007-09-18 23:55:05 +000030#include "llvm/ADT/SmallString.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000031#include "llvm/ADT/SmallSet.h"
Fariborz Jahanian85ff2642007-10-05 18:00:57 +000032#include "llvm/ADT/DenseSet.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000033using namespace clang;
34
Reid Spencer5f016e22007-07-11 17:01:13 +000035Sema::DeclTy *Sema::isTypeName(const IdentifierInfo &II, Scope *S) const {
Fariborz Jahanianbece4ac2007-10-12 16:34:10 +000036 Decl *IIDecl = II.getFETokenInfo<Decl>();
37 // Find first occurance of none-tagged declaration
38 while(IIDecl && IIDecl->getIdentifierNamespace() != Decl::IDNS_Ordinary)
39 IIDecl = cast<ScopedDecl>(IIDecl)->getNext();
40 if (!IIDecl)
41 return 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +000042 if (isa<TypedefDecl>(IIDecl) || isa<ObjCInterfaceDecl>(IIDecl))
Fariborz Jahanianbece4ac2007-10-12 16:34:10 +000043 return IIDecl;
Ted Kremeneka526c5c2008-01-07 19:49:32 +000044 if (ObjCCompatibleAliasDecl *ADecl =
45 dyn_cast<ObjCCompatibleAliasDecl>(IIDecl))
Fariborz Jahanianbece4ac2007-10-12 16:34:10 +000046 return ADecl->getClassInterface();
Steve Naroff3536b442007-09-06 21:24:23 +000047 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000048}
49
Steve Naroffb216c882007-10-09 22:01:59 +000050void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) {
Chris Lattner31e05722007-08-26 06:24:45 +000051 if (S->decl_empty()) return;
52 assert((S->getFlags() & Scope::DeclScope) &&"Scope shouldn't contain decls!");
53
Reid Spencer5f016e22007-07-11 17:01:13 +000054 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
55 I != E; ++I) {
Steve Naroffc752d042007-09-13 18:10:37 +000056 Decl *TmpD = static_cast<Decl*>(*I);
57 assert(TmpD && "This decl didn't get pushed??");
58 ScopedDecl *D = dyn_cast<ScopedDecl>(TmpD);
59 assert(D && "This decl isn't a ScopedDecl?");
60
Reid Spencer5f016e22007-07-11 17:01:13 +000061 IdentifierInfo *II = D->getIdentifier();
62 if (!II) continue;
63
64 // Unlink this decl from the identifier. Because the scope contains decls
65 // in an unordered collection, and because we have multiple identifier
66 // namespaces (e.g. tag, normal, label),the decl may not be the first entry.
67 if (II->getFETokenInfo<Decl>() == D) {
68 // Normal case, no multiple decls in different namespaces.
69 II->setFETokenInfo(D->getNext());
70 } else {
71 // Scan ahead. There are only three namespaces in C, so this loop can
72 // never execute more than 3 times.
Steve Naroffc752d042007-09-13 18:10:37 +000073 ScopedDecl *SomeDecl = II->getFETokenInfo<ScopedDecl>();
Reid Spencer5f016e22007-07-11 17:01:13 +000074 while (SomeDecl->getNext() != D) {
75 SomeDecl = SomeDecl->getNext();
76 assert(SomeDecl && "Didn't find this decl on its identifier's chain!");
77 }
78 SomeDecl->setNext(D->getNext());
79 }
80
81 // This will have to be revisited for C++: there we want to nest stuff in
82 // namespace decls etc. Even for C, we might want a top-level translation
83 // unit decl or something.
84 if (!CurFunctionDecl)
85 continue;
86
87 // Chain this decl to the containing function, it now owns the memory for
88 // the decl.
89 D->setNext(CurFunctionDecl->getDeclChain());
90 CurFunctionDecl->setDeclChain(D);
91 }
92}
93
Fariborz Jahanian4cabdfc2007-10-12 19:38:20 +000094/// LookupInterfaceDecl - Lookup interface declaration in the scope chain.
95/// Return the first declaration found (which may or may not be a class
Fariborz Jahanian3fe44e42007-10-12 19:53:08 +000096/// declaration. Caller is responsible for handling the none-class case.
Fariborz Jahanian4cabdfc2007-10-12 19:38:20 +000097/// Bypassing the alias of a class by returning the aliased class.
98ScopedDecl *Sema::LookupInterfaceDecl(IdentifierInfo *ClassName) {
99 ScopedDecl *IDecl;
100 // Scan up the scope chain looking for a decl that matches this identifier
101 // that is in the appropriate namespace.
102 for (IDecl = ClassName->getFETokenInfo<ScopedDecl>(); IDecl;
103 IDecl = IDecl->getNext())
104 if (IDecl->getIdentifierNamespace() == Decl::IDNS_Ordinary)
105 break;
106
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000107 if (ObjCCompatibleAliasDecl *ADecl =
108 dyn_cast_or_null<ObjCCompatibleAliasDecl>(IDecl))
Fariborz Jahanian4cabdfc2007-10-12 19:38:20 +0000109 return ADecl->getClassInterface();
110 return IDecl;
111}
112
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000113/// getObjCInterfaceDecl - Look up a for a class declaration in the scope.
Fariborz Jahanian9d048ff2007-09-29 00:54:24 +0000114/// return 0 if one not found.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000115ObjCInterfaceDecl *Sema::getObjCInterfaceDecl(IdentifierInfo *Id) {
Fariborz Jahanian4cabdfc2007-10-12 19:38:20 +0000116 ScopedDecl *IdDecl = LookupInterfaceDecl(Id);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000117 return cast_or_null<ObjCInterfaceDecl>(IdDecl);
Fariborz Jahanian9d048ff2007-09-29 00:54:24 +0000118}
119
Reid Spencer5f016e22007-07-11 17:01:13 +0000120/// LookupScopedDecl - Look up the inner-most declaration in the specified
121/// namespace.
Steve Naroffc752d042007-09-13 18:10:37 +0000122ScopedDecl *Sema::LookupScopedDecl(IdentifierInfo *II, unsigned NSI,
123 SourceLocation IdLoc, Scope *S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 if (II == 0) return 0;
125 Decl::IdentifierNamespace NS = (Decl::IdentifierNamespace)NSI;
126
127 // Scan up the scope chain looking for a decl that matches this identifier
128 // that is in the appropriate namespace. This search should not take long, as
129 // shadowing of names is uncommon, and deep shadowing is extremely uncommon.
Steve Naroffc752d042007-09-13 18:10:37 +0000130 for (ScopedDecl *D = II->getFETokenInfo<ScopedDecl>(); D; D = D->getNext())
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 if (D->getIdentifierNamespace() == NS)
132 return D;
133
134 // If we didn't find a use of this identifier, and if the identifier
135 // corresponds to a compiler builtin, create the decl object for the builtin
136 // now, injecting it into translation unit scope, and return it.
137 if (NS == Decl::IDNS_Ordinary) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 // If this is a builtin on this (or all) targets, create the decl.
139 if (unsigned BuiltinID = II->getBuiltinID())
140 return LazilyCreateBuiltin(II, BuiltinID, S);
141 }
142 return 0;
143}
144
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000145void Sema::InitBuiltinVaListType()
146{
147 if (!Context.getBuiltinVaListType().isNull())
148 return;
149
150 IdentifierInfo *VaIdent = &Context.Idents.get("__builtin_va_list");
151 ScopedDecl *VaDecl = LookupScopedDecl(VaIdent, Decl::IDNS_Ordinary,
152 SourceLocation(), TUScope);
Steve Naroff733002f2007-10-18 22:17:45 +0000153 TypedefDecl *VaTypedef = cast<TypedefDecl>(VaDecl);
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000154 Context.setBuiltinVaListType(Context.getTypedefType(VaTypedef));
155}
156
Reid Spencer5f016e22007-07-11 17:01:13 +0000157/// LazilyCreateBuiltin - The specified Builtin-ID was first used at file scope.
158/// lazily create a decl for it.
Chris Lattner22b73ba2007-10-10 23:42:28 +0000159ScopedDecl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid,
160 Scope *S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 Builtin::ID BID = (Builtin::ID)bid;
162
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000163 if (BID == Builtin::BI__builtin_va_start ||
Anders Carlsson793680e2007-10-12 23:56:29 +0000164 BID == Builtin::BI__builtin_va_copy ||
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000165 BID == Builtin::BI__builtin_va_end)
166 InitBuiltinVaListType();
167
Anders Carlssonb2cf3572007-10-11 01:00:40 +0000168 QualType R = Context.BuiltinInfo.GetBuiltinType(BID, Context);
Chris Lattnera98e58d2008-03-15 21:24:04 +0000169 FunctionDecl *New = FunctionDecl::Create(Context, SourceLocation(), II, R,
170 FunctionDecl::Extern, false, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000171
172 // Find translation-unit scope to insert this function into.
Chris Lattner31e05722007-08-26 06:24:45 +0000173 if (Scope *FnS = S->getFnParent())
174 S = FnS->getParent(); // Skip all scopes in a function at once.
Reid Spencer5f016e22007-07-11 17:01:13 +0000175 while (S->getParent())
176 S = S->getParent();
177 S->AddDecl(New);
178
179 // Add this decl to the end of the identifier info.
Steve Naroffc752d042007-09-13 18:10:37 +0000180 if (ScopedDecl *LastDecl = II->getFETokenInfo<ScopedDecl>()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 // Scan until we find the last (outermost) decl in the id chain.
182 while (LastDecl->getNext())
183 LastDecl = LastDecl->getNext();
184 // Insert before (outside) it.
185 LastDecl->setNext(New);
186 } else {
187 II->setFETokenInfo(New);
188 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 return New;
190}
191
192/// MergeTypeDefDecl - We just parsed a typedef 'New' which has the same name
193/// and scope as a previous declaration 'Old'. Figure out how to resolve this
194/// situation, merging decls or emitting diagnostics as appropriate.
195///
Steve Naroff8e74c932007-09-13 21:41:19 +0000196TypedefDecl *Sema::MergeTypeDefDecl(TypedefDecl *New, ScopedDecl *OldD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 // Verify the old decl was also a typedef.
198 TypedefDecl *Old = dyn_cast<TypedefDecl>(OldD);
199 if (!Old) {
200 Diag(New->getLocation(), diag::err_redefinition_different_kind,
201 New->getName());
202 Diag(OldD->getLocation(), diag::err_previous_definition);
203 return New;
204 }
205
Steve Naroff8ee529b2007-10-31 18:42:27 +0000206 // Allow multiple definitions for ObjC built-in typedefs.
207 // FIXME: Verify the underlying types are equivalent!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000208 if (getLangOptions().ObjC1 && isBuiltinObjCType(New))
Steve Naroff8ee529b2007-10-31 18:42:27 +0000209 return Old;
Steve Naroff4c49a6c2008-01-30 23:46:05 +0000210
211 // Redeclaration of a type is a constraint violation (6.7.2.3p1).
212 // Apparently GCC, Intel, and Sun all silently ignore the redeclaration if
213 // *either* declaration is in a system header. The code below implements
214 // this adhoc compatibility rule. FIXME: The following code will not
215 // work properly when compiling ".i" files (containing preprocessed output).
216 SourceManager &SrcMgr = Context.getSourceManager();
217 const FileEntry *OldDeclFile = SrcMgr.getFileEntryForLoc(Old->getLocation());
218 const FileEntry *NewDeclFile = SrcMgr.getFileEntryForLoc(New->getLocation());
219 HeaderSearch &HdrInfo = PP.getHeaderSearchInfo();
220 DirectoryLookup::DirType OldDirType = HdrInfo.getFileDirFlavor(OldDeclFile);
221 DirectoryLookup::DirType NewDirType = HdrInfo.getFileDirFlavor(NewDeclFile);
222
Steve Naroffc5e2f342008-03-26 21:27:00 +0000223 // Allow reclarations in both SystemHeaderDir and ExternCSystemHeaderDir.
224 if ((OldDirType != DirectoryLookup::NormalHeaderDir ||
225 NewDirType != DirectoryLookup::NormalHeaderDir) ||
Steve Naroffd62701b2008-02-07 03:50:06 +0000226 getLangOptions().Microsoft)
Steve Naroff4c49a6c2008-01-30 23:46:05 +0000227 return New;
Steve Naroffc5e2f342008-03-26 21:27:00 +0000228
Reid Spencer5f016e22007-07-11 17:01:13 +0000229 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
230 // TODO: This is totally simplistic. It should handle merging functions
231 // together etc, merging extern int X; int X; ...
232 Diag(New->getLocation(), diag::err_redefinition, New->getName());
233 Diag(Old->getLocation(), diag::err_previous_definition);
234 return New;
235}
236
Chris Lattnerddee4232008-03-03 03:28:21 +0000237/// DeclhasAttr - returns true if decl Declaration already has the target attribute.
238static bool DeclHasAttr(const Decl *decl, const Attr *target) {
239 for (const Attr *attr = decl->getAttrs(); attr; attr = attr->getNext())
240 if (attr->getKind() == target->getKind())
241 return true;
242
243 return false;
244}
245
246/// MergeAttributes - append attributes from the Old decl to the New one.
247static void MergeAttributes(Decl *New, Decl *Old) {
248 Attr *attr = const_cast<Attr*>(Old->getAttrs()), *tmp;
249
250// FIXME: fix this code to cleanup the Old attrs correctly
251 while (attr) {
252 tmp = attr;
253 attr = attr->getNext();
254
255 if (!DeclHasAttr(New, tmp)) {
256 New->addAttr(tmp);
257 } else {
258 tmp->setNext(0);
259 delete(tmp);
260 }
261 }
262}
263
Reid Spencer5f016e22007-07-11 17:01:13 +0000264/// MergeFunctionDecl - We just parsed a function 'New' which has the same name
265/// and scope as a previous declaration 'Old'. Figure out how to resolve this
266/// situation, merging decls or emitting diagnostics as appropriate.
267///
Steve Naroff8e74c932007-09-13 21:41:19 +0000268FunctionDecl *Sema::MergeFunctionDecl(FunctionDecl *New, ScopedDecl *OldD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000269 // Verify the old decl was also a function.
270 FunctionDecl *Old = dyn_cast<FunctionDecl>(OldD);
271 if (!Old) {
272 Diag(New->getLocation(), diag::err_redefinition_different_kind,
273 New->getName());
274 Diag(OldD->getLocation(), diag::err_previous_definition);
275 return New;
276 }
Chris Lattner7e669b22008-02-29 16:48:43 +0000277
Chris Lattnerddee4232008-03-03 03:28:21 +0000278 MergeAttributes(New, Old);
279
Reid Spencer5f016e22007-07-11 17:01:13 +0000280
Chris Lattner55196442007-11-20 19:04:50 +0000281 QualType OldQType = Old->getCanonicalType();
282 QualType NewQType = New->getCanonicalType();
283
Steve Naroffadbbd0c2008-01-14 20:51:29 +0000284 // Function types need to be compatible, not identical. This handles
285 // duplicate function decls like "void f(int); void f(enum X);" properly.
286 if (Context.functionTypesAreCompatible(OldQType, NewQType))
287 return New;
Chris Lattnere3995fe2007-11-06 06:07:26 +0000288
Steve Naroff837618c2008-01-16 15:01:34 +0000289 // A function that has already been declared has been redeclared or defined
290 // with a different type- show appropriate diagnostic
291 diag::kind PrevDiag = Old->getBody() ? diag::err_previous_definition :
292 diag::err_previous_declaration;
293
Reid Spencer5f016e22007-07-11 17:01:13 +0000294 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
295 // TODO: This is totally simplistic. It should handle merging functions
296 // together etc, merging extern int X; int X; ...
Steve Naroff837618c2008-01-16 15:01:34 +0000297 Diag(New->getLocation(), diag::err_conflicting_types, New->getName());
298 Diag(Old->getLocation(), PrevDiag);
Reid Spencer5f016e22007-07-11 17:01:13 +0000299 return New;
300}
301
Chris Lattnerfcc2d262007-11-06 04:28:31 +0000302/// equivalentArrayTypes - Used to determine whether two array types are
303/// equivalent.
304/// We need to check this explicitly as an incomplete array definition is
305/// considered a VariableArrayType, so will not match a complete array
306/// definition that would be otherwise equivalent.
307static bool areEquivalentArrayTypes(QualType NewQType, QualType OldQType) {
308 const ArrayType *NewAT = NewQType->getAsArrayType();
309 const ArrayType *OldAT = OldQType->getAsArrayType();
310
311 if (!NewAT || !OldAT)
312 return false;
313
314 // If either (or both) array types in incomplete we need to strip off the
315 // outer VariableArrayType. Once the outer VAT is removed the remaining
316 // types must be identical if the array types are to be considered
317 // equivalent.
318 // eg. int[][1] and int[1][1] become
319 // VAT(null, CAT(1, int)) and CAT(1, CAT(1, int))
320 // removing the outermost VAT gives
321 // CAT(1, int) and CAT(1, int)
322 // which are equal, therefore the array types are equivalent.
Eli Friedman9db13972008-02-15 12:53:51 +0000323 if (NewAT->isIncompleteArrayType() || OldAT->isIncompleteArrayType()) {
Chris Lattnerfcc2d262007-11-06 04:28:31 +0000324 if (NewAT->getIndexTypeQualifier() != OldAT->getIndexTypeQualifier())
325 return false;
Eli Friedman04930252008-01-29 07:51:12 +0000326 NewQType = NewAT->getElementType().getCanonicalType();
327 OldQType = OldAT->getElementType().getCanonicalType();
Chris Lattnerfcc2d262007-11-06 04:28:31 +0000328 }
329
330 return NewQType == OldQType;
331}
332
Reid Spencer5f016e22007-07-11 17:01:13 +0000333/// MergeVarDecl - We just parsed a variable 'New' which has the same name
334/// and scope as a previous declaration 'Old'. Figure out how to resolve this
335/// situation, merging decls or emitting diagnostics as appropriate.
336///
337/// FIXME: Need to carefully consider tentative definition rules (C99 6.9.2p2).
338/// For example, we incorrectly complain about i1, i4 from C99 6.9.2p4.
339///
Steve Naroff8e74c932007-09-13 21:41:19 +0000340VarDecl *Sema::MergeVarDecl(VarDecl *New, ScopedDecl *OldD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000341 // Verify the old decl was also a variable.
342 VarDecl *Old = dyn_cast<VarDecl>(OldD);
343 if (!Old) {
344 Diag(New->getLocation(), diag::err_redefinition_different_kind,
345 New->getName());
346 Diag(OldD->getLocation(), diag::err_previous_definition);
347 return New;
348 }
Chris Lattnerddee4232008-03-03 03:28:21 +0000349
350 MergeAttributes(New, Old);
351
Reid Spencer5f016e22007-07-11 17:01:13 +0000352 // Verify the types match.
Chris Lattnerfcc2d262007-11-06 04:28:31 +0000353 if (Old->getCanonicalType() != New->getCanonicalType() &&
354 !areEquivalentArrayTypes(New->getCanonicalType(), Old->getCanonicalType())) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000355 Diag(New->getLocation(), diag::err_redefinition, New->getName());
356 Diag(Old->getLocation(), diag::err_previous_definition);
357 return New;
358 }
Steve Naroffb7b032e2008-01-30 00:44:01 +0000359 // C99 6.2.2p4: Check if we have a static decl followed by a non-static.
360 if (New->getStorageClass() == VarDecl::Static &&
361 (Old->getStorageClass() == VarDecl::None ||
362 Old->getStorageClass() == VarDecl::Extern)) {
363 Diag(New->getLocation(), diag::err_static_non_static, New->getName());
364 Diag(Old->getLocation(), diag::err_previous_definition);
365 return New;
366 }
367 // C99 6.2.2p4: Check if we have a non-static decl followed by a static.
368 if (New->getStorageClass() != VarDecl::Static &&
369 Old->getStorageClass() == VarDecl::Static) {
370 Diag(New->getLocation(), diag::err_non_static_static, New->getName());
371 Diag(Old->getLocation(), diag::err_previous_definition);
372 return New;
373 }
374 // We've verified the types match, now handle "tentative" definitions.
375 FileVarDecl *OldFSDecl = dyn_cast<FileVarDecl>(Old);
376 FileVarDecl *NewFSDecl = dyn_cast<FileVarDecl>(New);
377
378 if (OldFSDecl && NewFSDecl) {
379 // Handle C "tentative" external object definitions (C99 6.9.2).
380 bool OldIsTentative = false;
381 bool NewIsTentative = false;
382
383 if (!OldFSDecl->getInit() &&
384 (OldFSDecl->getStorageClass() == VarDecl::None ||
385 OldFSDecl->getStorageClass() == VarDecl::Static))
386 OldIsTentative = true;
387
388 // FIXME: this check doesn't work (since the initializer hasn't been
389 // attached yet). This check should be moved to FinalizeDeclaratorGroup.
390 // Unfortunately, by the time we get to FinializeDeclaratorGroup, we've
391 // thrown out the old decl.
392 if (!NewFSDecl->getInit() &&
393 (NewFSDecl->getStorageClass() == VarDecl::None ||
394 NewFSDecl->getStorageClass() == VarDecl::Static))
395 ; // change to NewIsTentative = true; once the code is moved.
396
397 if (NewIsTentative || OldIsTentative)
398 return New;
399 }
400 if (Old->getStorageClass() != VarDecl::Extern &&
401 New->getStorageClass() != VarDecl::Extern) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000402 Diag(New->getLocation(), diag::err_redefinition, New->getName());
403 Diag(Old->getLocation(), diag::err_previous_definition);
404 }
405 return New;
406}
407
408/// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
409/// no declarator (e.g. "struct foo;") is parsed.
410Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) {
411 // TODO: emit error on 'int;' or 'const enum foo;'.
412 // TODO: emit error on 'typedef int;'
413 // if (!DS.isMissingDeclaratorOk()) Diag(...);
414
Steve Naroff92199282007-11-17 21:37:36 +0000415 return dyn_cast_or_null<TagDecl>(static_cast<Decl *>(DS.getTypeRep()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000416}
417
Steve Naroffd0091aa2008-01-10 22:15:12 +0000418bool Sema::CheckSingleInitializer(Expr *&Init, QualType DeclType) {
Steve Narofff0090632007-09-02 02:04:30 +0000419 // Get the type before calling CheckSingleAssignmentConstraints(), since
420 // it can promote the expression.
Chris Lattner5cf216b2008-01-04 18:04:52 +0000421 QualType InitType = Init->getType();
Steve Narofff0090632007-09-02 02:04:30 +0000422
Chris Lattner5cf216b2008-01-04 18:04:52 +0000423 AssignConvertType ConvTy = CheckSingleAssignmentConstraints(DeclType, Init);
424 return DiagnoseAssignmentResult(ConvTy, Init->getLocStart(), DeclType,
425 InitType, Init, "initializing");
Steve Narofff0090632007-09-02 02:04:30 +0000426}
427
Steve Naroff9e8925e2007-09-04 14:36:54 +0000428bool Sema::CheckInitExpr(Expr *expr, InitListExpr *IList, unsigned slot,
Steve Naroffd0091aa2008-01-10 22:15:12 +0000429 QualType ElementType) {
Chris Lattner33b7b062007-12-11 23:15:04 +0000430 Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
Steve Naroffd0091aa2008-01-10 22:15:12 +0000431 if (CheckSingleInitializer(expr, ElementType))
Chris Lattner33b7b062007-12-11 23:15:04 +0000432 return true; // types weren't compatible.
433
Steve Naroff9e8925e2007-09-04 14:36:54 +0000434 if (savExpr != expr) // The type was promoted, update initializer list.
435 IList->setInit(slot, expr);
Steve Naroff371227d2007-09-04 02:20:04 +0000436 return false;
437}
438
Steve Naroffa49e1fa2008-01-22 00:55:40 +0000439bool Sema::CheckStringLiteralInit(StringLiteral *strLiteral, QualType &DeclT) {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000440 if (const IncompleteArrayType *IAT = DeclT->getAsIncompleteArrayType()) {
Steve Naroffa49e1fa2008-01-22 00:55:40 +0000441 // C99 6.7.8p14. We have an array of character type with unknown size
442 // being initialized to a string literal.
443 llvm::APSInt ConstVal(32);
444 ConstVal = strLiteral->getByteLength() + 1;
445 // Return a new array type (C99 6.7.8p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000446 DeclT = Context.getConstantArrayType(IAT->getElementType(), ConstVal,
Steve Naroffa49e1fa2008-01-22 00:55:40 +0000447 ArrayType::Normal, 0);
448 } else if (const ConstantArrayType *CAT = DeclT->getAsConstantArrayType()) {
449 // C99 6.7.8p14. We have an array of character type with known size.
450 if (strLiteral->getByteLength() > (unsigned)CAT->getMaximumElements())
451 Diag(strLiteral->getSourceRange().getBegin(),
452 diag::warn_initializer_string_for_char_array_too_long,
453 strLiteral->getSourceRange());
454 } else {
455 assert(0 && "HandleStringLiteralInit(): Invalid array type");
456 }
457 // Set type from "char *" to "constant array of char".
458 strLiteral->setType(DeclT);
459 // For now, we always return false (meaning success).
460 return false;
461}
462
463StringLiteral *Sema::IsStringLiteralInit(Expr *Init, QualType DeclType) {
Steve Naroffa49e1fa2008-01-22 00:55:40 +0000464 const ArrayType *AT = DeclType->getAsArrayType();
Steve Naroffa9960332008-01-25 00:51:06 +0000465 if (AT && AT->getElementType()->isCharType()) {
466 return dyn_cast<StringLiteral>(Init);
467 }
Steve Naroffa49e1fa2008-01-22 00:55:40 +0000468 return 0;
469}
470
Steve Naroffa9960332008-01-25 00:51:06 +0000471// CheckInitializerListTypes - Checks the types of elements of an initializer
472// list. This function is recursive: it calls itself to initialize subelements
473// of aggregate types. Note that the topLevel parameter essentially refers to
474// whether this expression "owns" the initializer list passed in, or if this
475// initialization is taking elements out of a parent initializer. Each
476// call to this function adds zero or more to startIndex, reports any errors,
477// and returns true if it found any inconsistent types.
478bool Sema::CheckInitializerListTypes(InitListExpr*& IList, QualType &DeclType,
479 bool topLevel, unsigned& startIndex) {
Steve Naroff2fdc3742007-12-10 22:44:33 +0000480 bool hadError = false;
Steve Naroffa9960332008-01-25 00:51:06 +0000481
482 if (DeclType->isScalarType()) {
483 // The simplest case: initializing a single scalar
484 if (topLevel) {
485 Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init,
486 IList->getSourceRange());
487 }
488 if (startIndex < IList->getNumInits()) {
489 Expr* expr = IList->getInit(startIndex);
490 if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
491 // FIXME: Should an error be reported here instead?
492 unsigned newIndex = 0;
493 CheckInitializerListTypes(SubInitList, DeclType, true, newIndex);
494 } else {
495 hadError |= CheckInitExpr(expr, IList, startIndex, DeclType);
496 }
497 ++startIndex;
498 }
499 // FIXME: Should an error be reported for empty initializer list + scalar?
500 } else if (DeclType->isVectorType()) {
501 if (startIndex < IList->getNumInits()) {
502 const VectorType *VT = DeclType->getAsVectorType();
503 int maxElements = VT->getNumElements();
504 QualType elementType = VT->getElementType();
505
506 for (int i = 0; i < maxElements; ++i) {
507 // Don't attempt to go past the end of the init list
508 if (startIndex >= IList->getNumInits())
509 break;
510 Expr* expr = IList->getInit(startIndex);
511 if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
512 unsigned newIndex = 0;
513 hadError |= CheckInitializerListTypes(SubInitList, elementType,
514 true, newIndex);
515 ++startIndex;
516 } else {
517 hadError |= CheckInitializerListTypes(IList, elementType,
518 false, startIndex);
519 }
520 }
521 }
522 } else if (DeclType->isAggregateType() || DeclType->isUnionType()) {
523 if (DeclType->isStructureType() || DeclType->isUnionType()) {
Steve Naroff578edc62008-01-28 02:00:41 +0000524 if (startIndex < IList->getNumInits() && !topLevel &&
525 Context.typesAreCompatible(IList->getInit(startIndex)->getType(),
526 DeclType)) {
Steve Naroffa9960332008-01-25 00:51:06 +0000527 // We found a compatible struct; per the standard, this initializes the
528 // struct. (The C standard technically says that this only applies for
529 // initializers for declarations with automatic scope; however, this
530 // construct is unambiguous anyway because a struct cannot contain
531 // a type compatible with itself. We'll output an error when we check
532 // if the initializer is constant.)
533 // FIXME: Is a call to CheckSingleInitializer required here?
534 ++startIndex;
535 } else {
536 RecordDecl* structDecl = DeclType->getAsRecordType()->getDecl();
Steve Naroffb43eaa52008-02-11 00:06:17 +0000537
Steve Naroff406db932008-02-11 21:52:37 +0000538 // If the record is invalid, some of it's members are invalid. To avoid
539 // confusion, we forgo checking the intializer for the entire record.
Steve Naroffb43eaa52008-02-11 00:06:17 +0000540 if (structDecl->isInvalidDecl())
541 return true;
542
Steve Naroffa9960332008-01-25 00:51:06 +0000543 // If structDecl is a forward declaration, this loop won't do anything;
544 // That's okay, because an error should get printed out elsewhere. It
545 // might be worthwhile to skip over the rest of the initializer, though.
546 int numMembers = structDecl->getNumMembers() -
547 structDecl->hasFlexibleArrayMember();
548 for (int i = 0; i < numMembers; i++) {
549 // Don't attempt to go past the end of the init list
550 if (startIndex >= IList->getNumInits())
551 break;
552 FieldDecl * curField = structDecl->getMember(i);
553 if (!curField->getIdentifier()) {
554 // Don't initialize unnamed fields, e.g. "int : 20;"
555 continue;
556 }
557 QualType fieldType = curField->getType();
558 Expr* expr = IList->getInit(startIndex);
559 if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
560 unsigned newStart = 0;
561 hadError |= CheckInitializerListTypes(SubInitList, fieldType,
562 true, newStart);
563 ++startIndex;
564 } else {
565 hadError |= CheckInitializerListTypes(IList, fieldType,
566 false, startIndex);
567 }
568 if (DeclType->isUnionType())
569 break;
570 }
571 // FIXME: Implement flexible array initialization GCC extension (it's a
572 // really messy extension to implement, unfortunately...the necessary
573 // information isn't actually even here!)
574 }
575 } else if (DeclType->isArrayType()) {
576 // Check for the special-case of initializing an array with a string.
577 if (startIndex < IList->getNumInits()) {
578 if (StringLiteral *lit = IsStringLiteralInit(IList->getInit(startIndex),
579 DeclType)) {
580 CheckStringLiteralInit(lit, DeclType);
581 ++startIndex;
582 if (topLevel && startIndex < IList->getNumInits()) {
583 // We have leftover initializers; warn
584 Diag(IList->getInit(startIndex)->getLocStart(),
585 diag::err_excess_initializers_in_char_array_initializer,
586 IList->getInit(startIndex)->getSourceRange());
587 }
588 return false;
589 }
590 }
591 int maxElements;
Eli Friedmanc5773c42008-02-15 18:16:39 +0000592 if (DeclType->isIncompleteArrayType()) {
Steve Naroffa9960332008-01-25 00:51:06 +0000593 // FIXME: use a proper constant
594 maxElements = 0x7FFFFFFF;
Chris Lattner212839c2008-02-20 23:17:35 +0000595 } else if (const VariableArrayType *VAT =
596 DeclType->getAsVariableArrayType()) {
Steve Naroffa9960332008-01-25 00:51:06 +0000597 // Check for VLAs; in standard C it would be possible to check this
598 // earlier, but I don't know where clang accepts VLAs (gcc accepts
599 // them in all sorts of strange places).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000600 Diag(VAT->getSizeExpr()->getLocStart(),
601 diag::err_variable_object_no_init,
602 VAT->getSizeExpr()->getSourceRange());
603 hadError = true;
604 maxElements = 0x7FFFFFFF;
Steve Naroffa9960332008-01-25 00:51:06 +0000605 } else {
606 const ConstantArrayType *CAT = DeclType->getAsConstantArrayType();
607 maxElements = static_cast<int>(CAT->getSize().getZExtValue());
608 }
609 QualType elementType = DeclType->getAsArrayType()->getElementType();
610 int numElements = 0;
611 for (int i = 0; i < maxElements; ++i, ++numElements) {
612 // Don't attempt to go past the end of the init list
613 if (startIndex >= IList->getNumInits())
614 break;
615 Expr* expr = IList->getInit(startIndex);
616 if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
617 unsigned newIndex = 0;
618 hadError |= CheckInitializerListTypes(SubInitList, elementType,
619 true, newIndex);
620 ++startIndex;
621 } else {
622 hadError |= CheckInitializerListTypes(IList, elementType,
623 false, startIndex);
624 }
625 }
Eli Friedman9db13972008-02-15 12:53:51 +0000626 if (DeclType->isIncompleteArrayType()) {
Steve Naroffa9960332008-01-25 00:51:06 +0000627 // If this is an incomplete array type, the actual type needs to
628 // be calculated here
629 if (numElements == 0) {
630 // Sizing an array implicitly to zero is not allowed
631 // (It could in theory be allowed, but it doesn't really matter.)
632 Diag(IList->getLocStart(),
633 diag::err_at_least_one_initializer_needed_to_size_array);
634 hadError = true;
635 } else {
636 llvm::APSInt ConstVal(32);
637 ConstVal = numElements;
638 DeclType = Context.getConstantArrayType(elementType, ConstVal,
639 ArrayType::Normal, 0);
640 }
641 }
642 } else {
643 assert(0 && "Aggregate that isn't a function or array?!");
644 }
645 } else {
646 // In C, all types are either scalars or aggregates, but
647 // additional handling is needed here for C++ (and possibly others?).
648 assert(0 && "Unsupported initializer type");
649 }
650
651 // If this init list is a base list, we set the type; an initializer doesn't
652 // fundamentally have a type, but this makes the ASTs a bit easier to read
653 if (topLevel)
654 IList->setType(DeclType);
655
656 if (topLevel && startIndex < IList->getNumInits()) {
657 // We have leftover initializers; warn
658 Diag(IList->getInit(startIndex)->getLocStart(),
659 diag::warn_excess_initializers,
660 IList->getInit(startIndex)->getSourceRange());
661 }
662 return hadError;
663}
664
665bool Sema::CheckInitializerTypes(Expr *&Init, QualType &DeclType) {
Steve Naroffca107302008-01-21 23:53:58 +0000666 // C99 6.7.8p3: The type of the entity to be initialized shall be an array
667 // of unknown size ("[]") or an object type that is not a variable array type.
Eli Friedmanc5773c42008-02-15 18:16:39 +0000668 if (const VariableArrayType *VAT = DeclType->getAsVariableArrayType())
Steve Naroffca107302008-01-21 23:53:58 +0000669 return Diag(VAT->getSizeExpr()->getLocStart(),
670 diag::err_variable_object_no_init,
671 VAT->getSizeExpr()->getSourceRange());
672
Steve Naroff2fdc3742007-12-10 22:44:33 +0000673 InitListExpr *InitList = dyn_cast<InitListExpr>(Init);
674 if (!InitList) {
Steve Naroffa49e1fa2008-01-22 00:55:40 +0000675 // FIXME: Handle wide strings
676 if (StringLiteral *strLiteral = IsStringLiteralInit(Init, DeclType))
677 return CheckStringLiteralInit(strLiteral, DeclType);
Eli Friedmana312ce22008-02-08 00:48:24 +0000678
679 if (DeclType->isArrayType())
680 return Diag(Init->getLocStart(),
681 diag::err_array_init_list_required,
682 Init->getSourceRange());
683
Steve Naroffd0091aa2008-01-10 22:15:12 +0000684 return CheckSingleInitializer(Init, DeclType);
Steve Naroff2fdc3742007-12-10 22:44:33 +0000685 }
Steve Naroffa9960332008-01-25 00:51:06 +0000686 unsigned newIndex = 0;
687 return CheckInitializerListTypes(InitList, DeclType, true, newIndex);
Steve Narofff0090632007-09-02 02:04:30 +0000688}
689
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000690Sema::DeclTy *
Steve Naroff08d92e42007-09-15 18:49:24 +0000691Sema::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *lastDecl) {
Steve Naroff94745042007-09-13 23:52:58 +0000692 ScopedDecl *LastDeclarator = dyn_cast_or_null<ScopedDecl>((Decl *)lastDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000693 IdentifierInfo *II = D.getIdentifier();
694
Chris Lattnere80a59c2007-07-25 00:24:17 +0000695 // All of these full declarators require an identifier. If it doesn't have
696 // one, the ParsedFreeStandingDeclSpec action should be used.
697 if (II == 0) {
Chris Lattner311ff022007-10-16 22:36:42 +0000698 Diag(D.getDeclSpec().getSourceRange().getBegin(),
Chris Lattner98e08632007-08-28 06:17:15 +0000699 diag::err_declarator_need_ident,
Chris Lattnere80a59c2007-07-25 00:24:17 +0000700 D.getDeclSpec().getSourceRange(), D.getSourceRange());
701 return 0;
702 }
703
Chris Lattner31e05722007-08-26 06:24:45 +0000704 // The scope passed in may not be a decl scope. Zip up the scope tree until
705 // we find one that is.
706 while ((S->getFlags() & Scope::DeclScope) == 0)
707 S = S->getParent();
708
Reid Spencer5f016e22007-07-11 17:01:13 +0000709 // See if this is a redefinition of a variable in the same scope.
Steve Naroffc752d042007-09-13 18:10:37 +0000710 ScopedDecl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary,
711 D.getIdentifierLoc(), S);
Steve Naroffc752d042007-09-13 18:10:37 +0000712 ScopedDecl *New;
Steve Naroff5912a352007-08-28 20:14:24 +0000713 bool InvalidDecl = false;
714
Chris Lattner41af0932007-11-14 06:34:38 +0000715 QualType R = GetTypeForDeclarator(D, S);
716 assert(!R.isNull() && "GetTypeForDeclarator() returned null type");
717
Reid Spencer5f016e22007-07-11 17:01:13 +0000718 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Chris Lattner41af0932007-11-14 06:34:38 +0000719 TypedefDecl *NewTD = ParseTypedefDecl(S, D, R, LastDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +0000720 if (!NewTD) return 0;
721
722 // Handle attributes prior to checking for duplicates in MergeVarDecl
723 HandleDeclAttributes(NewTD, D.getDeclSpec().getAttributes(),
724 D.getAttributes());
Steve Naroffffce4d52008-01-09 23:34:55 +0000725 // Merge the decl with the existing one if appropriate. If the decl is
726 // in an outer scope, it isn't the same thing.
727 if (PrevDecl && S->isDeclScope(PrevDecl)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000728 NewTD = MergeTypeDefDecl(NewTD, PrevDecl);
729 if (NewTD == 0) return 0;
730 }
731 New = NewTD;
732 if (S->getParent() == 0) {
733 // C99 6.7.7p2: If a typedef name specifies a variably modified type
734 // then it shall have block scope.
Eli Friedman9db13972008-02-15 12:53:51 +0000735 if (NewTD->getUnderlyingType()->isVariablyModifiedType()) {
736 // FIXME: Diagnostic needs to be fixed.
737 Diag(D.getIdentifierLoc(), diag::err_typecheck_illegal_vla);
Steve Naroffd7444aa2007-08-31 17:20:07 +0000738 InvalidDecl = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000739 }
740 }
Chris Lattner41af0932007-11-14 06:34:38 +0000741 } else if (R.getTypePtr()->isFunctionType()) {
Chris Lattner271f1a62007-09-27 15:15:46 +0000742 FunctionDecl::StorageClass SC = FunctionDecl::None;
Reid Spencer5f016e22007-07-11 17:01:13 +0000743 switch (D.getDeclSpec().getStorageClassSpec()) {
744 default: assert(0 && "Unknown storage class!");
745 case DeclSpec::SCS_auto:
746 case DeclSpec::SCS_register:
747 Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_func,
748 R.getAsString());
Steve Naroff5912a352007-08-28 20:14:24 +0000749 InvalidDecl = true;
750 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000751 case DeclSpec::SCS_unspecified: SC = FunctionDecl::None; break;
752 case DeclSpec::SCS_extern: SC = FunctionDecl::Extern; break;
753 case DeclSpec::SCS_static: SC = FunctionDecl::Static; break;
Steve Naroff7dd0bd42008-01-28 21:57:15 +0000754 case DeclSpec::SCS_private_extern: SC = FunctionDecl::PrivateExtern;break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000755 }
756
Chris Lattnera98e58d2008-03-15 21:24:04 +0000757 bool isInline = D.getDeclSpec().isInlineSpecified();
758 FunctionDecl *NewFD = FunctionDecl::Create(Context, D.getIdentifierLoc(),
759 II, R, SC, isInline,
760 LastDeclarator);
Ted Kremenekf5c93c12008-02-27 22:18:07 +0000761 // Handle attributes.
Ted Kremenekf5c93c12008-02-27 22:18:07 +0000762 HandleDeclAttributes(NewFD, D.getDeclSpec().getAttributes(),
763 D.getAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +0000764
Steve Naroffffce4d52008-01-09 23:34:55 +0000765 // Merge the decl with the existing one if appropriate. Since C functions
766 // are in a flat namespace, make sure we consider decls in outer scopes.
Reid Spencer5f016e22007-07-11 17:01:13 +0000767 if (PrevDecl) {
768 NewFD = MergeFunctionDecl(NewFD, PrevDecl);
769 if (NewFD == 0) return 0;
770 }
771 New = NewFD;
772 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000773 if (R.getTypePtr()->isObjCInterfaceType()) {
Fariborz Jahaniane7f64cc2007-10-12 22:10:42 +0000774 Diag(D.getIdentifierLoc(), diag::err_statically_allocated_object,
775 D.getIdentifier()->getName());
776 InvalidDecl = true;
777 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000778
779 VarDecl *NewVD;
780 VarDecl::StorageClass SC;
781 switch (D.getDeclSpec().getStorageClassSpec()) {
Chris Lattner9e151e12008-03-15 21:10:16 +0000782 default: assert(0 && "Unknown storage class!");
783 case DeclSpec::SCS_unspecified: SC = VarDecl::None; break;
784 case DeclSpec::SCS_extern: SC = VarDecl::Extern; break;
785 case DeclSpec::SCS_static: SC = VarDecl::Static; break;
786 case DeclSpec::SCS_auto: SC = VarDecl::Auto; break;
787 case DeclSpec::SCS_register: SC = VarDecl::Register; break;
788 case DeclSpec::SCS_private_extern: SC = VarDecl::PrivateExtern; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000789 }
790 if (S->getParent() == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000791 // C99 6.9p2: The storage-class specifiers auto and register shall not
792 // appear in the declaration specifiers in an external declaration.
793 if (SC == VarDecl::Auto || SC == VarDecl::Register) {
794 Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope,
795 R.getAsString());
Steve Naroff53a32342007-08-28 18:45:29 +0000796 InvalidDecl = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000797 }
Chris Lattnerc63e6602008-03-15 21:32:50 +0000798 NewVD = FileVarDecl::Create(Context, D.getIdentifierLoc(), II, R, SC,
799 LastDeclarator);
Steve Narofff0090632007-09-02 02:04:30 +0000800 } else {
Chris Lattnerc63e6602008-03-15 21:32:50 +0000801 NewVD = BlockVarDecl::Create(Context, D.getIdentifierLoc(), II, R, SC,
802 LastDeclarator);
Steve Naroff53a32342007-08-28 18:45:29 +0000803 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000804 // Handle attributes prior to checking for duplicates in MergeVarDecl
805 HandleDeclAttributes(NewVD, D.getDeclSpec().getAttributes(),
806 D.getAttributes());
Nate Begemanc8e89a82008-03-14 18:07:10 +0000807
808 // Emit an error if an address space was applied to decl with local storage.
809 // This includes arrays of objects with address space qualifiers, but not
810 // automatic variables that point to other address spaces.
811 // ISO/IEC TR 18037 S5.1.2
Nate Begeman8e7dafe2008-03-25 18:36:32 +0000812 if (NewVD->hasLocalStorage() && (NewVD->getType().getAddressSpace() != 0)) {
813 Diag(D.getIdentifierLoc(), diag::err_as_qualified_auto_decl);
814 InvalidDecl = true;
Nate Begeman5af27e02008-03-14 00:22:18 +0000815 }
Steve Naroffffce4d52008-01-09 23:34:55 +0000816 // Merge the decl with the existing one if appropriate. If the decl is
817 // in an outer scope, it isn't the same thing.
818 if (PrevDecl && S->isDeclScope(PrevDecl)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000819 NewVD = MergeVarDecl(NewVD, PrevDecl);
820 if (NewVD == 0) return 0;
821 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000822 New = NewVD;
823 }
824
825 // If this has an identifier, add it to the scope stack.
826 if (II) {
Steve Naroffc752d042007-09-13 18:10:37 +0000827 New->setNext(II->getFETokenInfo<ScopedDecl>());
Reid Spencer5f016e22007-07-11 17:01:13 +0000828 II->setFETokenInfo(New);
829 S->AddDecl(New);
830 }
Steve Naroff5912a352007-08-28 20:14:24 +0000831 // If any semantic error occurred, mark the decl as invalid.
832 if (D.getInvalidType() || InvalidDecl)
833 New->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +0000834
835 return New;
836}
837
Steve Naroffd0091aa2008-01-10 22:15:12 +0000838bool Sema::CheckForConstantInitializer(Expr *Init, QualType DclT) {
839 SourceLocation loc;
840 // FIXME: Remove the isReference check and handle assignment to a reference.
841 if (!DclT->isReferenceType() && !Init->isConstantExpr(Context, &loc)) {
842 assert(loc.isValid() && "isConstantExpr didn't return a loc!");
843 Diag(loc, diag::err_init_element_not_constant, Init->getSourceRange());
844 return true;
845 }
846 return false;
847}
848
Steve Naroffbb204692007-09-12 14:07:44 +0000849void Sema::AddInitializerToDecl(DeclTy *dcl, ExprTy *init) {
Steve Naroff410e3e22007-09-12 20:13:48 +0000850 Decl *RealDecl = static_cast<Decl *>(dcl);
Steve Naroffbb204692007-09-12 14:07:44 +0000851 Expr *Init = static_cast<Expr *>(init);
Chris Lattner9a11b9a2007-10-19 20:10:30 +0000852 assert(Init && "missing initializer");
Steve Naroffbb204692007-09-12 14:07:44 +0000853
Chris Lattner9a11b9a2007-10-19 20:10:30 +0000854 // If there is no declaration, there was an error parsing it. Just ignore
855 // the initializer.
856 if (RealDecl == 0) {
857 delete Init;
858 return;
859 }
Steve Naroffbb204692007-09-12 14:07:44 +0000860
Steve Naroff410e3e22007-09-12 20:13:48 +0000861 VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl);
862 if (!VDecl) {
Steve Naroff8e74c932007-09-13 21:41:19 +0000863 Diag(dyn_cast<ScopedDecl>(RealDecl)->getLocation(),
864 diag::err_illegal_initializer);
Steve Naroff410e3e22007-09-12 20:13:48 +0000865 RealDecl->setInvalidDecl();
866 return;
867 }
Steve Naroffbb204692007-09-12 14:07:44 +0000868 // Get the decls type and save a reference for later, since
Steve Naroffd0091aa2008-01-10 22:15:12 +0000869 // CheckInitializerTypes may change it.
Steve Naroff410e3e22007-09-12 20:13:48 +0000870 QualType DclT = VDecl->getType(), SavT = DclT;
871 if (BlockVarDecl *BVD = dyn_cast<BlockVarDecl>(VDecl)) {
Steve Naroffbb204692007-09-12 14:07:44 +0000872 VarDecl::StorageClass SC = BVD->getStorageClass();
873 if (SC == VarDecl::Extern) { // C99 6.7.8p5
Steve Naroff410e3e22007-09-12 20:13:48 +0000874 Diag(VDecl->getLocation(), diag::err_block_extern_cant_init);
Steve Naroffbb204692007-09-12 14:07:44 +0000875 BVD->setInvalidDecl();
876 } else if (!BVD->isInvalidDecl()) {
Steve Naroffa9960332008-01-25 00:51:06 +0000877 if (CheckInitializerTypes(Init, DclT))
878 BVD->setInvalidDecl();
Steve Naroffd0091aa2008-01-10 22:15:12 +0000879 if (SC == VarDecl::Static) // C99 6.7.8p4.
880 CheckForConstantInitializer(Init, DclT);
Steve Naroffbb204692007-09-12 14:07:44 +0000881 }
Steve Naroff410e3e22007-09-12 20:13:48 +0000882 } else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(VDecl)) {
Steve Naroffbb204692007-09-12 14:07:44 +0000883 if (FVD->getStorageClass() == VarDecl::Extern)
Steve Naroff410e3e22007-09-12 20:13:48 +0000884 Diag(VDecl->getLocation(), diag::warn_extern_init);
Steve Naroffbb204692007-09-12 14:07:44 +0000885 if (!FVD->isInvalidDecl())
Steve Naroffa9960332008-01-25 00:51:06 +0000886 if (CheckInitializerTypes(Init, DclT))
887 FVD->setInvalidDecl();
Steve Naroffd0091aa2008-01-10 22:15:12 +0000888
889 // C99 6.7.8p4. All file scoped initializers need to be constant.
890 CheckForConstantInitializer(Init, DclT);
Steve Naroffbb204692007-09-12 14:07:44 +0000891 }
892 // If the type changed, it means we had an incomplete type that was
893 // completed by the initializer. For example:
894 // int ary[] = { 1, 3, 5 };
895 // "ary" transitions from a VariableArrayType to a ConstantArrayType.
Christopher Lamb48b12392007-11-29 19:09:19 +0000896 if (!VDecl->isInvalidDecl() && (DclT != SavT)) {
Steve Naroff410e3e22007-09-12 20:13:48 +0000897 VDecl->setType(DclT);
Christopher Lamb48b12392007-11-29 19:09:19 +0000898 Init->setType(DclT);
899 }
Steve Naroffbb204692007-09-12 14:07:44 +0000900
901 // Attach the initializer to the decl.
Steve Naroff410e3e22007-09-12 20:13:48 +0000902 VDecl->setInit(Init);
Steve Naroffbb204692007-09-12 14:07:44 +0000903 return;
904}
905
Reid Spencer5f016e22007-07-11 17:01:13 +0000906/// The declarators are chained together backwards, reverse the list.
907Sema::DeclTy *Sema::FinalizeDeclaratorGroup(Scope *S, DeclTy *group) {
908 // Often we have single declarators, handle them quickly.
Steve Naroff94745042007-09-13 23:52:58 +0000909 Decl *GroupDecl = static_cast<Decl*>(group);
910 if (GroupDecl == 0)
Steve Naroffbb204692007-09-12 14:07:44 +0000911 return 0;
Steve Naroff94745042007-09-13 23:52:58 +0000912
913 ScopedDecl *Group = dyn_cast<ScopedDecl>(GroupDecl);
914 ScopedDecl *NewGroup = 0;
Steve Naroffbb204692007-09-12 14:07:44 +0000915 if (Group->getNextDeclarator() == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000916 NewGroup = Group;
Steve Naroffbb204692007-09-12 14:07:44 +0000917 else { // reverse the list.
918 while (Group) {
Steve Naroff94745042007-09-13 23:52:58 +0000919 ScopedDecl *Next = Group->getNextDeclarator();
Steve Naroffbb204692007-09-12 14:07:44 +0000920 Group->setNextDeclarator(NewGroup);
921 NewGroup = Group;
922 Group = Next;
923 }
924 }
925 // Perform semantic analysis that depends on having fully processed both
926 // the declarator and initializer.
Steve Naroff94745042007-09-13 23:52:58 +0000927 for (ScopedDecl *ID = NewGroup; ID; ID = ID->getNextDeclarator()) {
Steve Naroffbb204692007-09-12 14:07:44 +0000928 VarDecl *IDecl = dyn_cast<VarDecl>(ID);
929 if (!IDecl)
930 continue;
931 FileVarDecl *FVD = dyn_cast<FileVarDecl>(IDecl);
932 BlockVarDecl *BVD = dyn_cast<BlockVarDecl>(IDecl);
933 QualType T = IDecl->getType();
934
935 // C99 6.7.5.2p2: If an identifier is declared to be an object with
936 // static storage duration, it shall not have a variable length array.
937 if ((FVD || BVD) && IDecl->getStorageClass() == VarDecl::Static) {
Eli Friedman3fe02932008-02-15 19:53:52 +0000938 if (T->getAsVariableArrayType()) {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000939 Diag(IDecl->getLocation(), diag::err_typecheck_illegal_vla);
940 IDecl->setInvalidDecl();
Steve Naroffbb204692007-09-12 14:07:44 +0000941 }
942 }
943 // Block scope. C99 6.7p7: If an identifier for an object is declared with
944 // no linkage (C99 6.2.2p6), the type for the object shall be complete...
945 if (BVD && IDecl->getStorageClass() != VarDecl::Extern) {
946 if (T->isIncompleteType()) {
Chris Lattner8b1be772007-12-02 07:50:03 +0000947 Diag(IDecl->getLocation(), diag::err_typecheck_decl_incomplete_type,
948 T.getAsString());
Steve Naroffbb204692007-09-12 14:07:44 +0000949 IDecl->setInvalidDecl();
950 }
951 }
952 // File scope. C99 6.9.2p2: A declaration of an identifier for and
953 // object that has file scope without an initializer, and without a
954 // storage-class specifier or with the storage-class specifier "static",
955 // constitutes a tentative definition. Note: A tentative definition with
956 // external linkage is valid (C99 6.2.2p5).
Steve Naroffd3cd1e52008-01-18 00:39:39 +0000957 if (FVD && !FVD->getInit() && (FVD->getStorageClass() == VarDecl::Static ||
958 FVD->getStorageClass() == VarDecl::None)) {
Eli Friedman9db13972008-02-15 12:53:51 +0000959 if (T->isIncompleteArrayType()) {
Steve Naroff9a75f8a2008-01-18 20:40:52 +0000960 // C99 6.9.2 (p2, p5): Implicit initialization causes an incomplete
961 // array to be completed. Don't issue a diagnostic.
962 } else if (T->isIncompleteType()) {
963 // C99 6.9.2p3: If the declaration of an identifier for an object is
964 // a tentative definition and has internal linkage (C99 6.2.2p3), the
965 // declared type shall not be an incomplete type.
Chris Lattner8b1be772007-12-02 07:50:03 +0000966 Diag(IDecl->getLocation(), diag::err_typecheck_decl_incomplete_type,
967 T.getAsString());
Steve Naroffbb204692007-09-12 14:07:44 +0000968 IDecl->setInvalidDecl();
969 }
970 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000971 }
972 return NewGroup;
973}
Steve Naroffe1223f72007-08-28 03:03:08 +0000974
975// Called from Sema::ParseStartOfFunctionDef().
Reid Spencer5f016e22007-07-11 17:01:13 +0000976ParmVarDecl *
Nate Begeman6d20d032008-02-17 21:02:04 +0000977Sema::ActOnParamDeclarator(struct DeclaratorChunk::ParamInfo &PI,
978 Scope *FnScope) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000979 IdentifierInfo *II = PI.Ident;
980 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
981 // Can this happen for params? We already checked that they don't conflict
982 // among each other. Here they can only shadow globals, which is ok.
Chris Lattner8b9023b2007-07-13 03:05:23 +0000983 if (/*Decl *PrevDecl = */LookupScopedDecl(II, Decl::IDNS_Ordinary,
Reid Spencer5f016e22007-07-11 17:01:13 +0000984 PI.IdentLoc, FnScope)) {
985
986 }
987
988 // FIXME: Handle storage class (auto, register). No declarator?
989 // TODO: Chain to previous parameter with the prevdeclarator chain?
Steve Naroff6a9f3e32007-08-07 22:44:21 +0000990
991 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
992 // Doing the promotion here has a win and a loss. The win is the type for
993 // both Decl's and DeclRefExpr's will match (a convenient invariant for the
994 // code generator). The loss is the orginal type isn't preserved. For example:
995 //
996 // void func(int parmvardecl[5]) { // convert "int [5]" to "int *"
997 // int blockvardecl[5];
998 // sizeof(parmvardecl); // size == 4
999 // sizeof(blockvardecl); // size == 20
1000 // }
1001 //
1002 // For expressions, all implicit conversions are captured using the
1003 // ImplicitCastExpr AST node (we have no such mechanism for Decl's).
1004 //
1005 // FIXME: If a source translation tool needs to see the original type, then
1006 // we need to consider storing both types (in ParmVarDecl)...
1007 //
1008 QualType parmDeclType = QualType::getFromOpaquePtr(PI.TypeInfo);
Chris Lattner529bd022008-01-02 22:50:48 +00001009 if (const ArrayType *AT = parmDeclType->getAsArrayType()) {
1010 // int x[restrict 4] -> int *restrict
Steve Naroff6a9f3e32007-08-07 22:44:21 +00001011 parmDeclType = Context.getPointerType(AT->getElementType());
Chris Lattner529bd022008-01-02 22:50:48 +00001012 parmDeclType = parmDeclType.getQualifiedType(AT->getIndexTypeQualifier());
1013 } else if (parmDeclType->isFunctionType())
Steve Naroff6a9f3e32007-08-07 22:44:21 +00001014 parmDeclType = Context.getPointerType(parmDeclType);
1015
Chris Lattnerc63e6602008-03-15 21:32:50 +00001016 ParmVarDecl *New = ParmVarDecl::Create(Context, PI.IdentLoc, II, parmDeclType,
1017 VarDecl::None, 0);
Anders Carlssonf78915f2008-02-15 07:04:12 +00001018
Steve Naroff53a32342007-08-28 18:45:29 +00001019 if (PI.InvalidType)
1020 New->setInvalidDecl();
1021
Reid Spencer5f016e22007-07-11 17:01:13 +00001022 // If this has an identifier, add it to the scope stack.
1023 if (II) {
Steve Naroffc752d042007-09-13 18:10:37 +00001024 New->setNext(II->getFETokenInfo<ScopedDecl>());
Reid Spencer5f016e22007-07-11 17:01:13 +00001025 II->setFETokenInfo(New);
1026 FnScope->AddDecl(New);
1027 }
Nate Begemanb7894b52008-02-17 21:20:31 +00001028
1029 HandleDeclAttributes(New, PI.AttrList, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +00001030 return New;
1031}
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001032
Chris Lattnerb652cea2007-10-09 17:14:05 +00001033Sema::DeclTy *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001034 assert(CurFunctionDecl == 0 && "Function parsing confused");
1035 assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
1036 "Not a function declarator!");
1037 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
1038
1039 // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared'
1040 // for a K&R function.
1041 if (!FTI.hasPrototype) {
1042 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
1043 if (FTI.ArgInfo[i].TypeInfo == 0) {
1044 Diag(FTI.ArgInfo[i].IdentLoc, diag::ext_param_not_declared,
1045 FTI.ArgInfo[i].Ident->getName());
1046 // Implicitly declare the argument as type 'int' for lack of a better
1047 // type.
1048 FTI.ArgInfo[i].TypeInfo = Context.IntTy.getAsOpaquePtr();
1049 }
1050 }
Chris Lattner52804082008-02-17 19:31:09 +00001051
Reid Spencer5f016e22007-07-11 17:01:13 +00001052 // Since this is a function definition, act as though we have information
1053 // about the arguments.
Chris Lattner52804082008-02-17 19:31:09 +00001054 if (FTI.NumArgs)
1055 FTI.hasPrototype = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001056 } else {
1057 // FIXME: Diagnose arguments without names in C.
1058
1059 }
1060
1061 Scope *GlobalScope = FnBodyScope->getParent();
Steve Naroffadbbd0c2008-01-14 20:51:29 +00001062
1063 // See if this is a redefinition.
1064 ScopedDecl *PrevDcl = LookupScopedDecl(D.getIdentifier(), Decl::IDNS_Ordinary,
1065 D.getIdentifierLoc(), GlobalScope);
1066 if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(PrevDcl)) {
1067 if (FD->getBody()) {
1068 Diag(D.getIdentifierLoc(), diag::err_redefinition,
1069 D.getIdentifier()->getName());
1070 Diag(FD->getLocation(), diag::err_previous_definition);
1071 }
1072 }
Steve Narofffabbc342008-02-12 01:09:36 +00001073 Decl *decl = static_cast<Decl*>(ActOnDeclarator(GlobalScope, D, 0));
Chris Lattnere9ba3232008-02-16 01:20:36 +00001074 FunctionDecl *FD = cast<FunctionDecl>(decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001075 CurFunctionDecl = FD;
1076
1077 // Create Decl objects for each parameter, adding them to the FunctionDecl.
1078 llvm::SmallVector<ParmVarDecl*, 16> Params;
1079
1080 // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes
1081 // no arguments, not a function that takes a single void argument.
1082 if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 &&
Chris Lattnerf46699c2008-02-20 20:55:12 +00001083 !QualType::getFromOpaquePtr(FTI.ArgInfo[0].TypeInfo).getCVRQualifiers() &&
Chris Lattnerb751c282007-11-28 18:51:29 +00001084 QualType::getFromOpaquePtr(FTI.ArgInfo[0].TypeInfo)->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001085 // empty arg list, don't push any params.
1086 } else {
Steve Naroff66499922007-11-12 03:44:46 +00001087 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Steve Naroff657aefe2008-03-19 23:07:49 +00001088 ParmVarDecl *parmDecl;
1089
1090 parmDecl = ActOnParamDeclarator(D.getTypeObject(0).Fun.ArgInfo[i],
1091 FnBodyScope);
1092 // C99 6.7.5.3p4: the parameters in a parameter type list in a function
1093 // declarator that is part of a function definition of that function
1094 // shall not have incomplete type.
1095 if (parmDecl->getType()->isIncompleteType()) {
1096 Diag(parmDecl->getLocation(), diag::err_typecheck_decl_incomplete_type,
1097 parmDecl->getType().getAsString());
1098 parmDecl->setInvalidDecl();
1099 }
1100 Params.push_back(parmDecl);
Steve Naroff66499922007-11-12 03:44:46 +00001101 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001102 }
1103
1104 FD->setParams(&Params[0], Params.size());
1105
1106 return FD;
1107}
1108
Steve Naroffd6d054d2007-11-11 23:20:51 +00001109Sema::DeclTy *Sema::ActOnFinishFunctionBody(DeclTy *D, StmtTy *Body) {
1110 Decl *dcl = static_cast<Decl *>(D);
1111 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(dcl)) {
1112 FD->setBody((Stmt*)Body);
1113 assert(FD == CurFunctionDecl && "Function parsing confused");
Steve Naroff4d832202007-12-13 18:18:56 +00001114 CurFunctionDecl = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001115 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(dcl)) {
Steve Naroffd6d054d2007-11-11 23:20:51 +00001116 MD->setBody((Stmt*)Body);
Steve Naroff03300712007-11-12 13:56:41 +00001117 CurMethodDecl = 0;
Steve Naroff4d832202007-12-13 18:18:56 +00001118 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001119 // Verify and clean out per-function state.
1120
1121 // Check goto/label use.
1122 for (llvm::DenseMap<IdentifierInfo*, LabelStmt*>::iterator
1123 I = LabelMap.begin(), E = LabelMap.end(); I != E; ++I) {
1124 // Verify that we have no forward references left. If so, there was a goto
1125 // or address of a label taken, but no definition of it. Label fwd
1126 // definitions are indicated with a null substmt.
1127 if (I->second->getSubStmt() == 0) {
1128 LabelStmt *L = I->second;
1129 // Emit error.
1130 Diag(L->getIdentLoc(), diag::err_undeclared_label_use, L->getName());
1131
1132 // At this point, we have gotos that use the bogus label. Stitch it into
1133 // the function body so that they aren't leaked and that the AST is well
1134 // formed.
Chris Lattner0cbc2152008-01-25 00:01:10 +00001135 if (Body) {
1136 L->setSubStmt(new NullStmt(L->getIdentLoc()));
1137 cast<CompoundStmt>((Stmt*)Body)->push_back(L);
1138 } else {
1139 // The whole function wasn't parsed correctly, just delete this.
1140 delete L;
1141 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001142 }
1143 }
1144 LabelMap.clear();
1145
Steve Naroffd6d054d2007-11-11 23:20:51 +00001146 return D;
Fariborz Jahanian60fbca02007-11-10 16:31:34 +00001147}
1148
Reid Spencer5f016e22007-07-11 17:01:13 +00001149/// ImplicitlyDefineFunction - An undeclared identifier was used in a function
1150/// call, forming a call to an implicitly defined function (per C99 6.5.1p2).
Steve Naroff8c9f13e2007-09-16 16:16:00 +00001151ScopedDecl *Sema::ImplicitlyDefineFunction(SourceLocation Loc,
1152 IdentifierInfo &II, Scope *S) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001153 if (getLangOptions().C99) // Extension in C99.
1154 Diag(Loc, diag::ext_implicit_function_decl, II.getName());
1155 else // Legal in C90, but warn about it.
1156 Diag(Loc, diag::warn_implicit_function_decl, II.getName());
1157
1158 // FIXME: handle stuff like:
1159 // void foo() { extern float X(); }
1160 // void bar() { X(); } <-- implicit decl for X in another scope.
1161
1162 // Set a Declarator for the implicit definition: int foo();
1163 const char *Dummy;
1164 DeclSpec DS;
1165 bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy);
1166 Error = Error; // Silence warning.
1167 assert(!Error && "Error setting up implicit decl!");
1168 Declarator D(DS, Declarator::BlockContext);
1169 D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, Loc));
1170 D.SetIdentifier(&II, Loc);
1171
1172 // Find translation-unit scope to insert this function into.
Chris Lattner31e05722007-08-26 06:24:45 +00001173 if (Scope *FnS = S->getFnParent())
1174 S = FnS->getParent(); // Skip all scopes in a function at once.
Reid Spencer5f016e22007-07-11 17:01:13 +00001175 while (S->getParent())
1176 S = S->getParent();
1177
Steve Naroff8c9f13e2007-09-16 16:16:00 +00001178 return dyn_cast<ScopedDecl>(static_cast<Decl*>(ActOnDeclarator(S, D, 0)));
Reid Spencer5f016e22007-07-11 17:01:13 +00001179}
1180
1181
Chris Lattner41af0932007-11-14 06:34:38 +00001182TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, QualType T,
Steve Naroff94745042007-09-13 23:52:58 +00001183 ScopedDecl *LastDeclarator) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001184 assert(D.getIdentifier() && "Wrong callback for declspec without declarator");
Steve Naroff5912a352007-08-28 20:14:24 +00001185 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001186
1187 // Scope manipulation handled by caller.
Chris Lattnerc63e6602008-03-15 21:32:50 +00001188 TypedefDecl *NewTD = TypedefDecl::Create(Context, D.getIdentifierLoc(),
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001189 D.getIdentifier(),
Chris Lattnerc63e6602008-03-15 21:32:50 +00001190 T, LastDeclarator);
Steve Naroff5912a352007-08-28 20:14:24 +00001191 if (D.getInvalidType())
1192 NewTD->setInvalidDecl();
1193 return NewTD;
Reid Spencer5f016e22007-07-11 17:01:13 +00001194}
1195
Steve Naroff08d92e42007-09-15 18:49:24 +00001196/// ActOnTag - This is invoked when we see 'struct foo' or 'struct {'. In the
Reid Spencer5f016e22007-07-11 17:01:13 +00001197/// former case, Name will be non-null. In the later case, Name will be null.
1198/// TagType indicates what kind of tag this is. TK indicates whether this is a
1199/// reference/declaration/definition of a tag.
Steve Naroff08d92e42007-09-15 18:49:24 +00001200Sema::DeclTy *Sema::ActOnTag(Scope *S, unsigned TagType, TagKind TK,
Reid Spencer5f016e22007-07-11 17:01:13 +00001201 SourceLocation KWLoc, IdentifierInfo *Name,
1202 SourceLocation NameLoc, AttributeList *Attr) {
1203 // If this is a use of an existing tag, it must have a name.
1204 assert((Name != 0 || TK == TK_Definition) &&
1205 "Nameless record must be a definition!");
1206
1207 Decl::Kind Kind;
1208 switch (TagType) {
1209 default: assert(0 && "Unknown tag type!");
1210 case DeclSpec::TST_struct: Kind = Decl::Struct; break;
1211 case DeclSpec::TST_union: Kind = Decl::Union; break;
1212//case DeclSpec::TST_class: Kind = Decl::Class; break;
1213 case DeclSpec::TST_enum: Kind = Decl::Enum; break;
1214 }
1215
1216 // If this is a named struct, check to see if there was a previous forward
1217 // declaration or definition.
1218 if (TagDecl *PrevDecl =
1219 dyn_cast_or_null<TagDecl>(LookupScopedDecl(Name, Decl::IDNS_Tag,
1220 NameLoc, S))) {
1221
1222 // If this is a use of a previous tag, or if the tag is already declared in
1223 // the same scope (so that the definition/declaration completes or
1224 // rementions the tag), reuse the decl.
1225 if (TK == TK_Reference || S->isDeclScope(PrevDecl)) {
1226 // Make sure that this wasn't declared as an enum and now used as a struct
1227 // or something similar.
1228 if (PrevDecl->getKind() != Kind) {
1229 Diag(KWLoc, diag::err_use_with_wrong_tag, Name->getName());
1230 Diag(PrevDecl->getLocation(), diag::err_previous_use);
1231 }
1232
1233 // If this is a use or a forward declaration, we're good.
1234 if (TK != TK_Definition)
1235 return PrevDecl;
1236
1237 // Diagnose attempts to redefine a tag.
1238 if (PrevDecl->isDefinition()) {
1239 Diag(NameLoc, diag::err_redefinition, Name->getName());
1240 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
1241 // If this is a redefinition, recover by making this struct be
1242 // anonymous, which will make any later references get the previous
1243 // definition.
1244 Name = 0;
1245 } else {
1246 // Okay, this is definition of a previously declared or referenced tag.
1247 // Move the location of the decl to be the definition site.
1248 PrevDecl->setLocation(NameLoc);
1249 return PrevDecl;
1250 }
1251 }
1252 // If we get here, this is a definition of a new struct type in a nested
1253 // scope, e.g. "struct foo; void bar() { struct foo; }", just create a new
1254 // type.
1255 }
1256
1257 // If there is an identifier, use the location of the identifier as the
1258 // location of the decl, otherwise use the location of the struct/union
1259 // keyword.
1260 SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc;
1261
1262 // Otherwise, if this is the first time we've seen this tag, create the decl.
1263 TagDecl *New;
1264 switch (Kind) {
1265 default: assert(0 && "Unknown tag kind!");
1266 case Decl::Enum:
1267 // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.:
1268 // enum X { A, B, C } D; D should chain to X.
Chris Lattnerc63e6602008-03-15 21:32:50 +00001269 New = EnumDecl::Create(Context, Loc, Name, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +00001270 // If this is an undefined enum, warn.
1271 if (TK != TK_Definition) Diag(Loc, diag::ext_forward_ref_enum);
1272 break;
1273 case Decl::Union:
1274 case Decl::Struct:
1275 case Decl::Class:
1276 // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.:
1277 // struct X { int A; } D; D should chain to X.
Chris Lattnerc63e6602008-03-15 21:32:50 +00001278 New = RecordDecl::Create(Context, Kind, Loc, Name, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +00001279 break;
1280 }
1281
1282 // If this has an identifier, add it to the scope stack.
1283 if (Name) {
Chris Lattner31e05722007-08-26 06:24:45 +00001284 // The scope passed in may not be a decl scope. Zip up the scope tree until
1285 // we find one that is.
1286 while ((S->getFlags() & Scope::DeclScope) == 0)
1287 S = S->getParent();
1288
1289 // Add it to the decl chain.
Steve Naroffc752d042007-09-13 18:10:37 +00001290 New->setNext(Name->getFETokenInfo<ScopedDecl>());
Reid Spencer5f016e22007-07-11 17:01:13 +00001291 Name->setFETokenInfo(New);
1292 S->AddDecl(New);
1293 }
Chris Lattnere1e79852008-02-06 00:51:33 +00001294
Anders Carlssonad148062008-02-16 00:29:18 +00001295 HandleDeclAttributes(New, Attr, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +00001296 return New;
1297}
1298
Steve Naroff08d92e42007-09-15 18:49:24 +00001299/// ActOnField - Each field of a struct/union/class is passed into this in order
Reid Spencer5f016e22007-07-11 17:01:13 +00001300/// to create a FieldDecl object for it.
Chris Lattnerf9628082008-03-16 01:16:52 +00001301Sema::DeclTy *Sema::ActOnField(Scope *S, DeclTy *tagDecl,
Reid Spencer5f016e22007-07-11 17:01:13 +00001302 SourceLocation DeclStart,
1303 Declarator &D, ExprTy *BitfieldWidth) {
1304 IdentifierInfo *II = D.getIdentifier();
1305 Expr *BitWidth = (Expr*)BitfieldWidth;
Chris Lattnerf9628082008-03-16 01:16:52 +00001306 Decl *TagDecl = static_cast<Decl *>(tagDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001307 SourceLocation Loc = DeclStart;
1308 if (II) Loc = D.getIdentifierLoc();
1309
1310 // FIXME: Unnamed fields can be handled in various different ways, for
1311 // example, unnamed unions inject all members into the struct namespace!
1312
1313
1314 if (BitWidth) {
1315 // TODO: Validate.
1316 //printf("WARNING: BITFIELDS IGNORED!\n");
1317
1318 // 6.7.2.1p3
1319 // 6.7.2.1p4
1320
1321 } else {
1322 // Not a bitfield.
1323
1324 // validate II.
1325
1326 }
1327
1328 QualType T = GetTypeForDeclarator(D, S);
Steve Naroff5912a352007-08-28 20:14:24 +00001329 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
1330 bool InvalidDecl = false;
Steve Naroffd7444aa2007-08-31 17:20:07 +00001331
Reid Spencer5f016e22007-07-11 17:01:13 +00001332 // C99 6.7.2.1p8: A member of a structure or union may have any type other
1333 // than a variably modified type.
Eli Friedman9db13972008-02-15 12:53:51 +00001334 if (T->isVariablyModifiedType()) {
1335 // FIXME: This diagnostic needs work
1336 Diag(Loc, diag::err_typecheck_illegal_vla, Loc);
Steve Naroffd7444aa2007-08-31 17:20:07 +00001337 InvalidDecl = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001338 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001339 // FIXME: Chain fielddecls together.
Steve Naroff44739212007-09-11 21:17:26 +00001340 FieldDecl *NewFD;
1341
Chris Lattnerf9628082008-03-16 01:16:52 +00001342 if (isa<RecordDecl>(TagDecl))
Chris Lattner8e25d862008-03-16 00:16:02 +00001343 NewFD = FieldDecl::Create(Context, Loc, II, T, BitWidth);
Chris Lattnerf9628082008-03-16 01:16:52 +00001344 else if (isa<ObjCInterfaceDecl>(TagDecl) ||
1345 isa<ObjCImplementationDecl>(TagDecl) ||
1346 isa<ObjCCategoryDecl>(TagDecl) ||
Steve Naroffddd600f2007-11-14 14:15:31 +00001347 // FIXME: ivars are currently used to model properties, and
1348 // properties can appear within a protocol.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001349 // See corresponding FIXME in DeclObjC.h:ObjCPropertyDecl.
Chris Lattnerf9628082008-03-16 01:16:52 +00001350 isa<ObjCProtocolDecl>(TagDecl))
Chris Lattner0e77ba02008-03-16 01:15:50 +00001351 NewFD = ObjCIvarDecl::Create(Context, Loc, II, T);
Steve Naroff44739212007-09-11 21:17:26 +00001352 else
Steve Naroff08d92e42007-09-15 18:49:24 +00001353 assert(0 && "Sema::ActOnField(): Unknown TagDecl");
Steve Naroff44739212007-09-11 21:17:26 +00001354
Anders Carlssonad148062008-02-16 00:29:18 +00001355 HandleDeclAttributes(NewFD, D.getDeclSpec().getAttributes(),
1356 D.getAttributes());
1357
Steve Naroff5912a352007-08-28 20:14:24 +00001358 if (D.getInvalidType() || InvalidDecl)
1359 NewFD->setInvalidDecl();
1360 return NewFD;
Reid Spencer5f016e22007-07-11 17:01:13 +00001361}
1362
Fariborz Jahanian89204a12007-10-01 16:53:59 +00001363/// TranslateIvarVisibility - Translate visibility from a token ID to an
1364/// AST enum value.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001365static ObjCIvarDecl::AccessControl
Fariborz Jahanian89204a12007-10-01 16:53:59 +00001366TranslateIvarVisibility(tok::ObjCKeywordKind ivarVisibility) {
Steve Narofff13271f2007-09-14 23:09:53 +00001367 switch (ivarVisibility) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001368 case tok::objc_private: return ObjCIvarDecl::Private;
1369 case tok::objc_public: return ObjCIvarDecl::Public;
1370 case tok::objc_protected: return ObjCIvarDecl::Protected;
1371 case tok::objc_package: return ObjCIvarDecl::Package;
Fariborz Jahanian89204a12007-10-01 16:53:59 +00001372 default: assert(false && "Unknown visitibility kind");
Steve Narofff13271f2007-09-14 23:09:53 +00001373 }
1374}
1375
Fariborz Jahanian9d048ff2007-09-29 00:54:24 +00001376void Sema::ActOnFields(Scope* S,
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +00001377 SourceLocation RecLoc, DeclTy *RecDecl,
Steve Naroff08d92e42007-09-15 18:49:24 +00001378 DeclTy **Fields, unsigned NumFields,
Steve Naroff60fccee2007-10-29 21:38:07 +00001379 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff08d92e42007-09-15 18:49:24 +00001380 tok::ObjCKeywordKind *visibility) {
Steve Naroff74216642007-09-14 22:20:54 +00001381 Decl *EnclosingDecl = static_cast<Decl*>(RecDecl);
1382 assert(EnclosingDecl && "missing record or interface decl");
1383 RecordDecl *Record = dyn_cast<RecordDecl>(EnclosingDecl);
1384
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00001385 if (Record && Record->isDefinition()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001386 // Diagnose code like:
1387 // struct S { struct S {} X; };
1388 // We discover this when we complete the outer S. Reject and ignore the
1389 // outer S.
1390 Diag(Record->getLocation(), diag::err_nested_redefinition,
1391 Record->getKindName());
1392 Diag(RecLoc, diag::err_previous_definition);
Steve Naroff74216642007-09-14 22:20:54 +00001393 Record->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00001394 return;
1395 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001396 // Verify that all the fields are okay.
1397 unsigned NumNamedMembers = 0;
1398 llvm::SmallVector<FieldDecl*, 32> RecFields;
1399 llvm::SmallSet<const IdentifierInfo*, 32> FieldIDs;
Steve Naroff74216642007-09-14 22:20:54 +00001400
Reid Spencer5f016e22007-07-11 17:01:13 +00001401 for (unsigned i = 0; i != NumFields; ++i) {
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00001402
Steve Naroff74216642007-09-14 22:20:54 +00001403 FieldDecl *FD = cast_or_null<FieldDecl>(static_cast<Decl*>(Fields[i]));
1404 assert(FD && "missing field decl");
1405
1406 // Remember all fields.
1407 RecFields.push_back(FD);
Reid Spencer5f016e22007-07-11 17:01:13 +00001408
1409 // Get the type for the field.
Chris Lattner02c642e2007-07-31 21:33:24 +00001410 Type *FDTy = FD->getType().getTypePtr();
Reid Spencer5f016e22007-07-11 17:01:13 +00001411
Steve Narofff13271f2007-09-14 23:09:53 +00001412 // If we have visibility info, make sure the AST is set accordingly.
1413 if (visibility)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001414 cast<ObjCIvarDecl>(FD)->setAccessControl(
Fariborz Jahanian89204a12007-10-01 16:53:59 +00001415 TranslateIvarVisibility(visibility[i]));
Steve Narofff13271f2007-09-14 23:09:53 +00001416
Reid Spencer5f016e22007-07-11 17:01:13 +00001417 // C99 6.7.2.1p2 - A field may not be a function type.
Chris Lattner02c642e2007-07-31 21:33:24 +00001418 if (FDTy->isFunctionType()) {
Steve Naroff74216642007-09-14 22:20:54 +00001419 Diag(FD->getLocation(), diag::err_field_declared_as_function,
Reid Spencer5f016e22007-07-11 17:01:13 +00001420 FD->getName());
Steve Naroff74216642007-09-14 22:20:54 +00001421 FD->setInvalidDecl();
1422 EnclosingDecl->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00001423 continue;
1424 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001425 // C99 6.7.2.1p2 - A field may not be an incomplete type except...
1426 if (FDTy->isIncompleteType()) {
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00001427 if (!Record) { // Incomplete ivar type is always an error.
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +00001428 Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName());
Steve Naroff74216642007-09-14 22:20:54 +00001429 FD->setInvalidDecl();
1430 EnclosingDecl->setInvalidDecl();
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +00001431 continue;
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00001432 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001433 if (i != NumFields-1 || // ... that the last member ...
1434 Record->getKind() != Decl::Struct || // ... of a structure ...
Chris Lattner02c642e2007-07-31 21:33:24 +00001435 !FDTy->isArrayType()) { //... may have incomplete array type.
Reid Spencer5f016e22007-07-11 17:01:13 +00001436 Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName());
Steve Naroff74216642007-09-14 22:20:54 +00001437 FD->setInvalidDecl();
1438 EnclosingDecl->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00001439 continue;
1440 }
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00001441 if (NumNamedMembers < 1) { //... must have more than named member ...
Reid Spencer5f016e22007-07-11 17:01:13 +00001442 Diag(FD->getLocation(), diag::err_flexible_array_empty_struct,
1443 FD->getName());
Steve Naroff74216642007-09-14 22:20:54 +00001444 FD->setInvalidDecl();
1445 EnclosingDecl->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00001446 continue;
1447 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001448 // Okay, we have a legal flexible array member at the end of the struct.
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00001449 if (Record)
1450 Record->setHasFlexibleArrayMember(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001451 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001452 /// C99 6.7.2.1p2 - a struct ending in a flexible array member cannot be the
1453 /// field of another structure or the element of an array.
Chris Lattner02c642e2007-07-31 21:33:24 +00001454 if (const RecordType *FDTTy = FDTy->getAsRecordType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001455 if (FDTTy->getDecl()->hasFlexibleArrayMember()) {
1456 // If this is a member of a union, then entire union becomes "flexible".
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00001457 if (Record && Record->getKind() == Decl::Union) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001458 Record->setHasFlexibleArrayMember(true);
1459 } else {
1460 // If this is a struct/class and this is not the last element, reject
1461 // it. Note that GCC supports variable sized arrays in the middle of
1462 // structures.
1463 if (i != NumFields-1) {
1464 Diag(FD->getLocation(), diag::err_variable_sized_type_in_struct,
1465 FD->getName());
Steve Naroff74216642007-09-14 22:20:54 +00001466 FD->setInvalidDecl();
1467 EnclosingDecl->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00001468 continue;
1469 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001470 // We support flexible arrays at the end of structs in other structs
1471 // as an extension.
1472 Diag(FD->getLocation(), diag::ext_flexible_array_in_struct,
1473 FD->getName());
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +00001474 if (Record)
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00001475 Record->setHasFlexibleArrayMember(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001476 }
1477 }
1478 }
Fariborz Jahaniane7f64cc2007-10-12 22:10:42 +00001479 /// A field cannot be an Objective-c object
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001480 if (FDTy->isObjCInterfaceType()) {
Fariborz Jahaniane7f64cc2007-10-12 22:10:42 +00001481 Diag(FD->getLocation(), diag::err_statically_allocated_object,
1482 FD->getName());
1483 FD->setInvalidDecl();
1484 EnclosingDecl->setInvalidDecl();
1485 continue;
1486 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001487 // Keep track of the number of named members.
1488 if (IdentifierInfo *II = FD->getIdentifier()) {
1489 // Detect duplicate member names.
1490 if (!FieldIDs.insert(II)) {
1491 Diag(FD->getLocation(), diag::err_duplicate_member, II->getName());
1492 // Find the previous decl.
1493 SourceLocation PrevLoc;
1494 for (unsigned i = 0, e = RecFields.size(); ; ++i) {
1495 assert(i != e && "Didn't find previous def!");
1496 if (RecFields[i]->getIdentifier() == II) {
1497 PrevLoc = RecFields[i]->getLocation();
1498 break;
1499 }
1500 }
1501 Diag(PrevLoc, diag::err_previous_definition);
Steve Naroff74216642007-09-14 22:20:54 +00001502 FD->setInvalidDecl();
1503 EnclosingDecl->setInvalidDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00001504 continue;
1505 }
1506 ++NumNamedMembers;
1507 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001508 }
1509
Reid Spencer5f016e22007-07-11 17:01:13 +00001510 // Okay, we successfully defined 'Record'.
Chris Lattnere1e79852008-02-06 00:51:33 +00001511 if (Record) {
Fariborz Jahaniane267ab62007-09-14 16:27:55 +00001512 Record->defineBody(&RecFields[0], RecFields.size());
Chris Lattnere1e79852008-02-06 00:51:33 +00001513 Consumer.HandleTagDeclDefinition(Record);
1514 } else {
Chris Lattnera91d3812008-02-05 22:40:55 +00001515 ObjCIvarDecl **ClsFields = reinterpret_cast<ObjCIvarDecl**>(&RecFields[0]);
1516 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(EnclosingDecl))
1517 ID->addInstanceVariablesToClass(ClsFields, RecFields.size(), RBrac);
1518 else if (ObjCImplementationDecl *IMPDecl =
1519 dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001520 assert(IMPDecl && "ActOnFields - missing ObjCImplementationDecl");
1521 IMPDecl->ObjCAddInstanceVariablesToClassImpl(ClsFields, RecFields.size());
Fariborz Jahanian3a3ca1b2007-10-31 18:48:14 +00001522 CheckImplementationIvars(IMPDecl, ClsFields, RecFields.size(), RBrac);
Fariborz Jahaniand0b90bf2007-09-26 18:27:25 +00001523 }
Fariborz Jahanianb04a0212007-09-14 21:08:27 +00001524 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001525}
1526
Steve Naroff08d92e42007-09-15 18:49:24 +00001527Sema::DeclTy *Sema::ActOnEnumConstant(Scope *S, DeclTy *theEnumDecl,
Reid Spencer5f016e22007-07-11 17:01:13 +00001528 DeclTy *lastEnumConst,
1529 SourceLocation IdLoc, IdentifierInfo *Id,
1530 SourceLocation EqualLoc, ExprTy *val) {
1531 theEnumDecl = theEnumDecl; // silence unused warning.
1532 EnumConstantDecl *LastEnumConst =
1533 cast_or_null<EnumConstantDecl>(static_cast<Decl*>(lastEnumConst));
1534 Expr *Val = static_cast<Expr*>(val);
1535
Chris Lattner31e05722007-08-26 06:24:45 +00001536 // The scope passed in may not be a decl scope. Zip up the scope tree until
1537 // we find one that is.
1538 while ((S->getFlags() & Scope::DeclScope) == 0)
1539 S = S->getParent();
1540
Reid Spencer5f016e22007-07-11 17:01:13 +00001541 // Verify that there isn't already something declared with this name in this
1542 // scope.
Steve Naroff8e74c932007-09-13 21:41:19 +00001543 if (ScopedDecl *PrevDecl = LookupScopedDecl(Id, Decl::IDNS_Ordinary,
1544 IdLoc, S)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001545 if (S->isDeclScope(PrevDecl)) {
1546 if (isa<EnumConstantDecl>(PrevDecl))
1547 Diag(IdLoc, diag::err_redefinition_of_enumerator, Id->getName());
1548 else
1549 Diag(IdLoc, diag::err_redefinition, Id->getName());
1550 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
Chris Lattnera73349d2008-02-26 00:33:57 +00001551 delete Val;
Reid Spencer5f016e22007-07-11 17:01:13 +00001552 return 0;
1553 }
1554 }
1555
1556 llvm::APSInt EnumVal(32);
1557 QualType EltTy;
1558 if (Val) {
Chris Lattner421a23d2007-08-27 21:16:18 +00001559 // Make sure to promote the operand type to int.
1560 UsualUnaryConversions(Val);
1561
Reid Spencer5f016e22007-07-11 17:01:13 +00001562 // C99 6.7.2.2p2: Make sure we have an integer constant expression.
1563 SourceLocation ExpLoc;
Chris Lattner590b6642007-07-15 23:26:56 +00001564 if (!Val->isIntegerConstantExpr(EnumVal, Context, &ExpLoc)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001565 Diag(ExpLoc, diag::err_enum_value_not_integer_constant_expr,
1566 Id->getName());
Chris Lattnera73349d2008-02-26 00:33:57 +00001567 delete Val;
Chris Lattnerb7416f92007-08-27 17:37:24 +00001568 Val = 0; // Just forget about it.
Chris Lattnere9ca8512007-08-29 16:03:41 +00001569 } else {
1570 EltTy = Val->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001571 }
Chris Lattnerb7416f92007-08-27 17:37:24 +00001572 }
1573
1574 if (!Val) {
1575 if (LastEnumConst) {
1576 // Assign the last value + 1.
1577 EnumVal = LastEnumConst->getInitVal();
1578 ++EnumVal;
Chris Lattner421a23d2007-08-27 21:16:18 +00001579
1580 // Check for overflow on increment.
1581 if (EnumVal < LastEnumConst->getInitVal())
1582 Diag(IdLoc, diag::warn_enum_value_overflow);
1583
Chris Lattnerb7416f92007-08-27 17:37:24 +00001584 EltTy = LastEnumConst->getType();
1585 } else {
1586 // First value, set to zero.
1587 EltTy = Context.IntTy;
Chris Lattner98be4942008-03-05 18:54:05 +00001588 EnumVal.zextOrTrunc(static_cast<uint32_t>(Context.getTypeSize(EltTy)));
Chris Lattnerb7416f92007-08-27 17:37:24 +00001589 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001590 }
1591
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001592 EnumConstantDecl *New =
Chris Lattnerc63e6602008-03-15 21:32:50 +00001593 EnumConstantDecl::Create(Context, IdLoc, Id, EltTy, Val, EnumVal,
1594 LastEnumConst);
Reid Spencer5f016e22007-07-11 17:01:13 +00001595
1596 // Register this decl in the current scope stack.
Steve Naroffc752d042007-09-13 18:10:37 +00001597 New->setNext(Id->getFETokenInfo<ScopedDecl>());
Reid Spencer5f016e22007-07-11 17:01:13 +00001598 Id->setFETokenInfo(New);
1599 S->AddDecl(New);
1600 return New;
1601}
1602
Steve Naroff08d92e42007-09-15 18:49:24 +00001603void Sema::ActOnEnumBody(SourceLocation EnumLoc, DeclTy *EnumDeclX,
Reid Spencer5f016e22007-07-11 17:01:13 +00001604 DeclTy **Elements, unsigned NumElements) {
1605 EnumDecl *Enum = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX));
1606 assert(!Enum->isDefinition() && "Enum redefinitions can't reach here");
1607
Chris Lattnere37f0be2007-08-28 05:10:31 +00001608 // TODO: If the result value doesn't fit in an int, it must be a long or long
1609 // long value. ISO C does not support this, but GCC does as an extension,
1610 // emit a warning.
Chris Lattner98be4942008-03-05 18:54:05 +00001611 unsigned IntWidth = Context.Target.getIntWidth();
Chris Lattnere37f0be2007-08-28 05:10:31 +00001612
Chris Lattnerac609682007-08-28 06:15:15 +00001613 // Verify that all the values are okay, compute the size of the values, and
1614 // reverse the list.
1615 unsigned NumNegativeBits = 0;
1616 unsigned NumPositiveBits = 0;
1617
1618 // Keep track of whether all elements have type int.
1619 bool AllElementsInt = true;
1620
Reid Spencer5f016e22007-07-11 17:01:13 +00001621 EnumConstantDecl *EltList = 0;
1622 for (unsigned i = 0; i != NumElements; ++i) {
1623 EnumConstantDecl *ECD =
1624 cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i]));
1625 if (!ECD) continue; // Already issued a diagnostic.
Chris Lattner211a30e2007-08-28 05:27:00 +00001626
1627 // If the enum value doesn't fit in an int, emit an extension warning.
Chris Lattner211a30e2007-08-28 05:27:00 +00001628 const llvm::APSInt &InitVal = ECD->getInitVal();
Chris Lattnera73349d2008-02-26 00:33:57 +00001629 assert(InitVal.getBitWidth() >= IntWidth &&
1630 "Should have promoted value to int");
Chris Lattner211a30e2007-08-28 05:27:00 +00001631 if (InitVal.getBitWidth() > IntWidth) {
1632 llvm::APSInt V(InitVal);
1633 V.trunc(IntWidth);
1634 V.extend(InitVal.getBitWidth());
1635 if (V != InitVal)
1636 Diag(ECD->getLocation(), diag::ext_enum_value_not_int,
1637 InitVal.toString());
1638 }
Chris Lattnerac609682007-08-28 06:15:15 +00001639
1640 // Keep track of the size of positive and negative values.
Chris Lattnera73349d2008-02-26 00:33:57 +00001641 if (InitVal.isUnsigned() || InitVal.isNonNegative())
Chris Lattner21dd8212008-01-14 21:47:29 +00001642 NumPositiveBits = std::max(NumPositiveBits,
1643 (unsigned)InitVal.getActiveBits());
Chris Lattnerac609682007-08-28 06:15:15 +00001644 else
Chris Lattner21dd8212008-01-14 21:47:29 +00001645 NumNegativeBits = std::max(NumNegativeBits,
1646 (unsigned)InitVal.getMinSignedBits());
Reid Spencer5f016e22007-07-11 17:01:13 +00001647
Chris Lattnerac609682007-08-28 06:15:15 +00001648 // Keep track of whether every enum element has type int (very commmon).
1649 if (AllElementsInt)
1650 AllElementsInt = ECD->getType() == Context.IntTy;
1651
Reid Spencer5f016e22007-07-11 17:01:13 +00001652 ECD->setNextDeclarator(EltList);
1653 EltList = ECD;
1654 }
1655
Chris Lattnerac609682007-08-28 06:15:15 +00001656 // Figure out the type that should be used for this enum.
1657 // FIXME: Support attribute(packed) on enums and -fshort-enums.
1658 QualType BestType;
Chris Lattnerb7f6e082007-08-29 17:31:48 +00001659 unsigned BestWidth;
Chris Lattnerac609682007-08-28 06:15:15 +00001660
1661 if (NumNegativeBits) {
1662 // If there is a negative value, figure out the smallest integer type (of
1663 // int/long/longlong) that fits.
Chris Lattnerb7f6e082007-08-29 17:31:48 +00001664 if (NumNegativeBits <= IntWidth && NumPositiveBits < IntWidth) {
Chris Lattnerac609682007-08-28 06:15:15 +00001665 BestType = Context.IntTy;
Chris Lattnerb7f6e082007-08-29 17:31:48 +00001666 BestWidth = IntWidth;
1667 } else {
Chris Lattner98be4942008-03-05 18:54:05 +00001668 BestWidth = Context.Target.getLongWidth();
Ted Kremenek9c728dc2007-12-12 22:39:36 +00001669
Chris Lattnerb7f6e082007-08-29 17:31:48 +00001670 if (NumNegativeBits <= BestWidth && NumPositiveBits < BestWidth)
Chris Lattnerac609682007-08-28 06:15:15 +00001671 BestType = Context.LongTy;
1672 else {
Chris Lattner98be4942008-03-05 18:54:05 +00001673 BestWidth = Context.Target.getLongLongWidth();
Ted Kremenek9c728dc2007-12-12 22:39:36 +00001674
Chris Lattnerb7f6e082007-08-29 17:31:48 +00001675 if (NumNegativeBits > BestWidth || NumPositiveBits >= BestWidth)
Chris Lattnerac609682007-08-28 06:15:15 +00001676 Diag(Enum->getLocation(), diag::warn_enum_too_large);
1677 BestType = Context.LongLongTy;
1678 }
1679 }
1680 } else {
1681 // If there is no negative value, figure out which of uint, ulong, ulonglong
1682 // fits.
Chris Lattnerb7f6e082007-08-29 17:31:48 +00001683 if (NumPositiveBits <= IntWidth) {
Chris Lattnerac609682007-08-28 06:15:15 +00001684 BestType = Context.UnsignedIntTy;
Chris Lattnerb7f6e082007-08-29 17:31:48 +00001685 BestWidth = IntWidth;
1686 } else if (NumPositiveBits <=
Chris Lattner98be4942008-03-05 18:54:05 +00001687 (BestWidth = Context.Target.getLongWidth())) {
Chris Lattnerac609682007-08-28 06:15:15 +00001688 BestType = Context.UnsignedLongTy;
Chris Lattner98be4942008-03-05 18:54:05 +00001689 } else {
1690 BestWidth = Context.Target.getLongLongWidth();
Chris Lattnerb7f6e082007-08-29 17:31:48 +00001691 assert(NumPositiveBits <= BestWidth &&
Chris Lattnerac609682007-08-28 06:15:15 +00001692 "How could an initializer get larger than ULL?");
1693 BestType = Context.UnsignedLongLongTy;
1694 }
1695 }
1696
Chris Lattnerb7f6e082007-08-29 17:31:48 +00001697 // Loop over all of the enumerator constants, changing their types to match
1698 // the type of the enum if needed.
1699 for (unsigned i = 0; i != NumElements; ++i) {
1700 EnumConstantDecl *ECD =
1701 cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i]));
1702 if (!ECD) continue; // Already issued a diagnostic.
1703
1704 // Standard C says the enumerators have int type, but we allow, as an
1705 // extension, the enumerators to be larger than int size. If each
1706 // enumerator value fits in an int, type it as an int, otherwise type it the
1707 // same as the enumerator decl itself. This means that in "enum { X = 1U }"
1708 // that X has type 'int', not 'unsigned'.
Chris Lattnera73349d2008-02-26 00:33:57 +00001709 if (ECD->getType() == Context.IntTy) {
1710 // Make sure the init value is signed.
1711 llvm::APSInt IV = ECD->getInitVal();
1712 IV.setIsSigned(true);
1713 ECD->setInitVal(IV);
Chris Lattnerb7f6e082007-08-29 17:31:48 +00001714 continue; // Already int type.
Chris Lattnera73349d2008-02-26 00:33:57 +00001715 }
Chris Lattnerb7f6e082007-08-29 17:31:48 +00001716
1717 // Determine whether the value fits into an int.
1718 llvm::APSInt InitVal = ECD->getInitVal();
1719 bool FitsInInt;
1720 if (InitVal.isUnsigned() || !InitVal.isNegative())
1721 FitsInInt = InitVal.getActiveBits() < IntWidth;
1722 else
1723 FitsInInt = InitVal.getMinSignedBits() <= IntWidth;
1724
1725 // If it fits into an integer type, force it. Otherwise force it to match
1726 // the enum decl type.
1727 QualType NewTy;
1728 unsigned NewWidth;
1729 bool NewSign;
1730 if (FitsInInt) {
1731 NewTy = Context.IntTy;
1732 NewWidth = IntWidth;
1733 NewSign = true;
1734 } else if (ECD->getType() == BestType) {
1735 // Already the right type!
1736 continue;
1737 } else {
1738 NewTy = BestType;
1739 NewWidth = BestWidth;
1740 NewSign = BestType->isSignedIntegerType();
1741 }
1742
1743 // Adjust the APSInt value.
1744 InitVal.extOrTrunc(NewWidth);
1745 InitVal.setIsSigned(NewSign);
1746 ECD->setInitVal(InitVal);
1747
1748 // Adjust the Expr initializer and type.
1749 ECD->setInitExpr(new ImplicitCastExpr(NewTy, ECD->getInitExpr()));
1750 ECD->setType(NewTy);
1751 }
Chris Lattnerac609682007-08-28 06:15:15 +00001752
Chris Lattnere00b18c2007-08-28 18:24:31 +00001753 Enum->defineElements(EltList, BestType);
Chris Lattnere1e79852008-02-06 00:51:33 +00001754 Consumer.HandleTagDeclDefinition(Enum);
Reid Spencer5f016e22007-07-11 17:01:13 +00001755}
1756
Anders Carlssondfab6cb2008-02-08 00:33:21 +00001757Sema::DeclTy *Sema::ActOnFileScopeAsmDecl(SourceLocation Loc,
1758 ExprTy *expr) {
1759 StringLiteral *AsmString = cast<StringLiteral>((Expr*)expr);
1760
Chris Lattner8e25d862008-03-16 00:16:02 +00001761 return FileScopeAsmDecl::Create(Context, Loc, AsmString);
Anders Carlssondfab6cb2008-02-08 00:33:21 +00001762}
1763
Chris Lattnerc6fdc342008-01-12 07:05:38 +00001764Sema::DeclTy* Sema::ActOnLinkageSpec(SourceLocation Loc,
Chris Lattnerc81c8142008-02-25 21:04:36 +00001765 SourceLocation LBrace,
1766 SourceLocation RBrace,
1767 const char *Lang,
1768 unsigned StrSize,
1769 DeclTy *D) {
Chris Lattnerc6fdc342008-01-12 07:05:38 +00001770 LinkageSpecDecl::LanguageIDs Language;
1771 Decl *dcl = static_cast<Decl *>(D);
1772 if (strncmp(Lang, "\"C\"", StrSize) == 0)
1773 Language = LinkageSpecDecl::lang_c;
1774 else if (strncmp(Lang, "\"C++\"", StrSize) == 0)
1775 Language = LinkageSpecDecl::lang_cxx;
1776 else {
1777 Diag(Loc, diag::err_bad_language);
1778 return 0;
1779 }
1780
1781 // FIXME: Add all the various semantics of linkage specifications
Chris Lattner8e25d862008-03-16 00:16:02 +00001782 return LinkageSpecDecl::Create(Context, Loc, Language, dcl);
Chris Lattnerc6fdc342008-01-12 07:05:38 +00001783}
1784
Chris Lattner74788ba2008-02-21 00:48:22 +00001785void Sema::HandleDeclAttribute(Decl *New, AttributeList *Attr) {
Anders Carlsson6ede0ff2007-12-19 06:16:30 +00001786
Chris Lattner74788ba2008-02-21 00:48:22 +00001787 switch (Attr->getKind()) {
Chris Lattner212839c2008-02-20 23:17:35 +00001788 case AttributeList::AT_vector_size:
Reid Spencer5f016e22007-07-11 17:01:13 +00001789 if (ValueDecl *vDecl = dyn_cast<ValueDecl>(New)) {
Chris Lattner74788ba2008-02-21 00:48:22 +00001790 QualType newType = HandleVectorTypeAttribute(vDecl->getType(), Attr);
Reid Spencer5f016e22007-07-11 17:01:13 +00001791 if (!newType.isNull()) // install the new vector type into the decl
1792 vDecl->setType(newType);
1793 }
1794 if (TypedefDecl *tDecl = dyn_cast<TypedefDecl>(New)) {
1795 QualType newType = HandleVectorTypeAttribute(tDecl->getUnderlyingType(),
Chris Lattner74788ba2008-02-21 00:48:22 +00001796 Attr);
Reid Spencer5f016e22007-07-11 17:01:13 +00001797 if (!newType.isNull()) // install the new vector type into the decl
1798 tDecl->setUnderlyingType(newType);
1799 }
Chris Lattner212839c2008-02-20 23:17:35 +00001800 break;
1801 case AttributeList::AT_ocu_vector_type:
Steve Naroffbea0b342007-07-29 16:33:31 +00001802 if (TypedefDecl *tDecl = dyn_cast<TypedefDecl>(New))
Chris Lattner74788ba2008-02-21 00:48:22 +00001803 HandleOCUVectorTypeAttribute(tDecl, Attr);
Steve Naroffbea0b342007-07-29 16:33:31 +00001804 else
Chris Lattner74788ba2008-02-21 00:48:22 +00001805 Diag(Attr->getLoc(),
Steve Naroff73322922007-07-18 18:00:27 +00001806 diag::err_typecheck_ocu_vector_not_typedef);
Chris Lattner212839c2008-02-20 23:17:35 +00001807 break;
1808 case AttributeList::AT_address_space:
Christopher Lambebb97e92008-02-04 02:31:56 +00001809 if (TypedefDecl *tDecl = dyn_cast<TypedefDecl>(New)) {
1810 QualType newType = HandleAddressSpaceTypeAttribute(
1811 tDecl->getUnderlyingType(),
Chris Lattner74788ba2008-02-21 00:48:22 +00001812 Attr);
1813 tDecl->setUnderlyingType(newType);
Christopher Lambebb97e92008-02-04 02:31:56 +00001814 } else if (ValueDecl *vDecl = dyn_cast<ValueDecl>(New)) {
1815 QualType newType = HandleAddressSpaceTypeAttribute(vDecl->getType(),
Chris Lattner74788ba2008-02-21 00:48:22 +00001816 Attr);
1817 // install the new addr spaced type into the decl
1818 vDecl->setType(newType);
Christopher Lambebb97e92008-02-04 02:31:56 +00001819 }
Chris Lattner212839c2008-02-20 23:17:35 +00001820 break;
Chris Lattner7e669b22008-02-29 16:48:43 +00001821 case AttributeList::AT_deprecated:
Chris Lattnerddee4232008-03-03 03:28:21 +00001822 HandleDeprecatedAttribute(New, Attr);
1823 break;
1824 case AttributeList::AT_visibility:
1825 HandleVisibilityAttribute(New, Attr);
1826 break;
1827 case AttributeList::AT_weak:
1828 HandleWeakAttribute(New, Attr);
1829 break;
1830 case AttributeList::AT_dllimport:
1831 HandleDLLImportAttribute(New, Attr);
1832 break;
1833 case AttributeList::AT_dllexport:
1834 HandleDLLExportAttribute(New, Attr);
1835 break;
1836 case AttributeList::AT_nothrow:
1837 HandleNothrowAttribute(New, Attr);
Chris Lattner7e669b22008-02-29 16:48:43 +00001838 break;
Nate Begeman440b4562008-03-07 20:04:22 +00001839 case AttributeList::AT_stdcall:
1840 HandleStdCallAttribute(New, Attr);
1841 break;
1842 case AttributeList::AT_fastcall:
1843 HandleFastCallAttribute(New, Attr);
1844 break;
Chris Lattner212839c2008-02-20 23:17:35 +00001845 case AttributeList::AT_aligned:
Chris Lattner74788ba2008-02-21 00:48:22 +00001846 HandleAlignedAttribute(New, Attr);
Chris Lattner212839c2008-02-20 23:17:35 +00001847 break;
1848 case AttributeList::AT_packed:
Chris Lattner74788ba2008-02-21 00:48:22 +00001849 HandlePackedAttribute(New, Attr);
Chris Lattner212839c2008-02-20 23:17:35 +00001850 break;
Nate Begemanc398f0b2008-02-21 19:30:49 +00001851 case AttributeList::AT_annotate:
1852 HandleAnnotateAttribute(New, Attr);
1853 break;
Ted Kremenekaecb3832008-02-27 20:43:06 +00001854 case AttributeList::AT_noreturn:
1855 HandleNoReturnAttribute(New, Attr);
1856 break;
Chris Lattnerddee4232008-03-03 03:28:21 +00001857 case AttributeList::AT_format:
1858 HandleFormatAttribute(New, Attr);
1859 break;
Chris Lattner212839c2008-02-20 23:17:35 +00001860 default:
Chris Lattner7e669b22008-02-29 16:48:43 +00001861#if 0
1862 // TODO: when we have the full set of attributes, warn about unknown ones.
1863 Diag(Attr->getLoc(), diag::warn_attribute_ignored,
1864 Attr->getName()->getName());
1865#endif
Chris Lattner212839c2008-02-20 23:17:35 +00001866 break;
1867 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001868}
1869
1870void Sema::HandleDeclAttributes(Decl *New, AttributeList *declspec_prefix,
1871 AttributeList *declarator_postfix) {
1872 while (declspec_prefix) {
1873 HandleDeclAttribute(New, declspec_prefix);
1874 declspec_prefix = declspec_prefix->getNext();
1875 }
1876 while (declarator_postfix) {
1877 HandleDeclAttribute(New, declarator_postfix);
1878 declarator_postfix = declarator_postfix->getNext();
1879 }
1880}
1881
Steve Naroffbea0b342007-07-29 16:33:31 +00001882void Sema::HandleOCUVectorTypeAttribute(TypedefDecl *tDecl,
1883 AttributeList *rawAttr) {
1884 QualType curType = tDecl->getUnderlyingType();
Anders Carlsson78aaae92007-12-19 07:19:40 +00001885 // check the attribute arguments.
Steve Naroff73322922007-07-18 18:00:27 +00001886 if (rawAttr->getNumArgs() != 1) {
Chris Lattner2070d802008-02-20 23:25:22 +00001887 Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
Steve Naroff73322922007-07-18 18:00:27 +00001888 std::string("1"));
Steve Naroffbea0b342007-07-29 16:33:31 +00001889 return;
Steve Naroff73322922007-07-18 18:00:27 +00001890 }
1891 Expr *sizeExpr = static_cast<Expr *>(rawAttr->getArg(0));
1892 llvm::APSInt vecSize(32);
1893 if (!sizeExpr->isIntegerConstantExpr(vecSize, Context)) {
Chris Lattner2070d802008-02-20 23:25:22 +00001894 Diag(rawAttr->getLoc(), diag::err_attribute_argument_not_int,
Anders Carlsson042c4e72008-02-16 19:51:27 +00001895 "ocu_vector_type", sizeExpr->getSourceRange());
Steve Naroffbea0b342007-07-29 16:33:31 +00001896 return;
Steve Naroff73322922007-07-18 18:00:27 +00001897 }
1898 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
1899 // in conjunction with complex types (pointers, arrays, functions, etc.).
1900 Type *canonType = curType.getCanonicalType().getTypePtr();
1901 if (!(canonType->isIntegerType() || canonType->isRealFloatingType())) {
Chris Lattner2070d802008-02-20 23:25:22 +00001902 Diag(rawAttr->getLoc(), diag::err_attribute_invalid_vector_type,
Steve Naroff73322922007-07-18 18:00:27 +00001903 curType.getCanonicalType().getAsString());
Steve Naroffbea0b342007-07-29 16:33:31 +00001904 return;
Steve Naroff73322922007-07-18 18:00:27 +00001905 }
1906 // unlike gcc's vector_size attribute, the size is specified as the
1907 // number of elements, not the number of bytes.
Chris Lattner701e5eb2007-09-04 02:45:27 +00001908 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
Steve Naroff73322922007-07-18 18:00:27 +00001909
1910 if (vectorSize == 0) {
Chris Lattner2070d802008-02-20 23:25:22 +00001911 Diag(rawAttr->getLoc(), diag::err_attribute_zero_size,
Steve Naroff73322922007-07-18 18:00:27 +00001912 sizeExpr->getSourceRange());
Steve Naroffbea0b342007-07-29 16:33:31 +00001913 return;
Steve Naroff73322922007-07-18 18:00:27 +00001914 }
Steve Naroffbea0b342007-07-29 16:33:31 +00001915 // Instantiate/Install the vector type, the number of elements is > 0.
1916 tDecl->setUnderlyingType(Context.getOCUVectorType(curType, vectorSize));
1917 // Remember this typedef decl, we will need it later for diagnostics.
1918 OCUVectorDecls.push_back(tDecl);
Steve Naroff73322922007-07-18 18:00:27 +00001919}
1920
Reid Spencer5f016e22007-07-11 17:01:13 +00001921QualType Sema::HandleVectorTypeAttribute(QualType curType,
Chris Lattnera7674d82007-07-13 22:13:22 +00001922 AttributeList *rawAttr) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001923 // check the attribute arugments.
1924 if (rawAttr->getNumArgs() != 1) {
Chris Lattner2070d802008-02-20 23:25:22 +00001925 Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
Reid Spencer5f016e22007-07-11 17:01:13 +00001926 std::string("1"));
1927 return QualType();
1928 }
1929 Expr *sizeExpr = static_cast<Expr *>(rawAttr->getArg(0));
1930 llvm::APSInt vecSize(32);
Chris Lattner590b6642007-07-15 23:26:56 +00001931 if (!sizeExpr->isIntegerConstantExpr(vecSize, Context)) {
Chris Lattner2070d802008-02-20 23:25:22 +00001932 Diag(rawAttr->getLoc(), diag::err_attribute_argument_not_int,
Anders Carlsson042c4e72008-02-16 19:51:27 +00001933 "vector_size", sizeExpr->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +00001934 return QualType();
1935 }
1936 // navigate to the base type - we need to provide for vector pointers,
1937 // vector arrays, and functions returning vectors.
1938 Type *canonType = curType.getCanonicalType().getTypePtr();
1939
Steve Naroff73322922007-07-18 18:00:27 +00001940 if (canonType->isPointerType() || canonType->isArrayType() ||
1941 canonType->isFunctionType()) {
Chris Lattner54b263b2007-12-19 05:38:06 +00001942 assert(0 && "HandleVector(): Complex type construction unimplemented");
Steve Naroff73322922007-07-18 18:00:27 +00001943 /* FIXME: rebuild the type from the inside out, vectorizing the inner type.
1944 do {
1945 if (PointerType *PT = dyn_cast<PointerType>(canonType))
1946 canonType = PT->getPointeeType().getTypePtr();
1947 else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
1948 canonType = AT->getElementType().getTypePtr();
1949 else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
1950 canonType = FT->getResultType().getTypePtr();
1951 } while (canonType->isPointerType() || canonType->isArrayType() ||
1952 canonType->isFunctionType());
1953 */
Reid Spencer5f016e22007-07-11 17:01:13 +00001954 }
1955 // the base type must be integer or float.
1956 if (!(canonType->isIntegerType() || canonType->isRealFloatingType())) {
Chris Lattner2070d802008-02-20 23:25:22 +00001957 Diag(rawAttr->getLoc(), diag::err_attribute_invalid_vector_type,
Reid Spencer5f016e22007-07-11 17:01:13 +00001958 curType.getCanonicalType().getAsString());
1959 return QualType();
1960 }
Chris Lattner98be4942008-03-05 18:54:05 +00001961 unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(curType));
Reid Spencer5f016e22007-07-11 17:01:13 +00001962 // vecSize is specified in bytes - convert to bits.
Chris Lattner701e5eb2007-09-04 02:45:27 +00001963 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
Reid Spencer5f016e22007-07-11 17:01:13 +00001964
1965 // the vector size needs to be an integral multiple of the type size.
1966 if (vectorSize % typeSize) {
Chris Lattner2070d802008-02-20 23:25:22 +00001967 Diag(rawAttr->getLoc(), diag::err_attribute_invalid_size,
Reid Spencer5f016e22007-07-11 17:01:13 +00001968 sizeExpr->getSourceRange());
1969 return QualType();
1970 }
1971 if (vectorSize == 0) {
Chris Lattner2070d802008-02-20 23:25:22 +00001972 Diag(rawAttr->getLoc(), diag::err_attribute_zero_size,
Reid Spencer5f016e22007-07-11 17:01:13 +00001973 sizeExpr->getSourceRange());
1974 return QualType();
1975 }
Nate Begemanc398f0b2008-02-21 19:30:49 +00001976 // Instantiate the vector type, the number of elements is > 0, and not
1977 // required to be a power of 2, unlike GCC.
Steve Naroff73322922007-07-18 18:00:27 +00001978 return Context.getVectorType(curType, vectorSize/typeSize);
Reid Spencer5f016e22007-07-11 17:01:13 +00001979}
1980
Chris Lattner2070d802008-02-20 23:25:22 +00001981void Sema::HandlePackedAttribute(Decl *d, AttributeList *rawAttr) {
Anders Carlssonad148062008-02-16 00:29:18 +00001982 // check the attribute arguments.
1983 if (rawAttr->getNumArgs() > 0) {
Chris Lattner2070d802008-02-20 23:25:22 +00001984 Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
Anders Carlssonad148062008-02-16 00:29:18 +00001985 std::string("0"));
1986 return;
1987 }
1988
1989 if (TagDecl *TD = dyn_cast<TagDecl>(d))
1990 TD->addAttr(new PackedAttr);
1991 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
1992 // If the alignment is less than or equal to 8 bits, the packed attribute
1993 // has no effect.
Chris Lattner98be4942008-03-05 18:54:05 +00001994 if (Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner2070d802008-02-20 23:25:22 +00001995 Diag(rawAttr->getLoc(),
Anders Carlssonad148062008-02-16 00:29:18 +00001996 diag::warn_attribute_ignored_for_field_of_type,
Chris Lattner2070d802008-02-20 23:25:22 +00001997 rawAttr->getName()->getName(), FD->getType().getAsString());
Anders Carlssonad148062008-02-16 00:29:18 +00001998 else
Anders Carlsson425a6092008-02-16 00:39:40 +00001999 FD->addAttr(new PackedAttr);
Anders Carlssonad148062008-02-16 00:29:18 +00002000 } else
Chris Lattner2070d802008-02-20 23:25:22 +00002001 Diag(rawAttr->getLoc(), diag::warn_attribute_ignored,
2002 rawAttr->getName()->getName());
Anders Carlssonad148062008-02-16 00:29:18 +00002003}
Nate Begemanc398f0b2008-02-21 19:30:49 +00002004
Ted Kremenekaecb3832008-02-27 20:43:06 +00002005void Sema::HandleNoReturnAttribute(Decl *d, AttributeList *rawAttr) {
2006 // check the attribute arguments.
2007 if (rawAttr->getNumArgs() != 0) {
2008 Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
2009 std::string("0"));
2010 return;
2011 }
2012
Ted Kremenek3465fb32008-03-03 16:52:27 +00002013 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
2014
2015 if (!Fn) {
2016 Diag(rawAttr->getLoc(), diag::warn_attribute_wrong_decl_type,
2017 "noreturn", "function");
2018 return;
2019 }
2020
Ted Kremenekaecb3832008-02-27 20:43:06 +00002021 d->addAttr(new NoReturnAttr());
2022}
2023
Chris Lattnerddee4232008-03-03 03:28:21 +00002024void Sema::HandleDeprecatedAttribute(Decl *d, AttributeList *rawAttr) {
2025 // check the attribute arguments.
2026 if (rawAttr->getNumArgs() != 0) {
2027 Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
2028 std::string("0"));
2029 return;
2030 }
2031
2032 d->addAttr(new DeprecatedAttr());
2033}
2034
2035void Sema::HandleVisibilityAttribute(Decl *d, AttributeList *rawAttr) {
2036 // check the attribute arguments.
Chris Lattner7b937ae2008-03-04 18:08:48 +00002037 if (rawAttr->getNumArgs() != 1) {
Chris Lattnerddee4232008-03-03 03:28:21 +00002038 Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
2039 std::string("1"));
2040 return;
2041 }
2042
Chris Lattner7b937ae2008-03-04 18:08:48 +00002043 Expr *Arg = static_cast<Expr*>(rawAttr->getArg(0));
2044 Arg = Arg->IgnoreParenCasts();
2045 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
2046
2047 if (Str == 0 || Str->isWide()) {
Chris Lattnerddee4232008-03-03 03:28:21 +00002048 Diag(rawAttr->getLoc(), diag::err_attribute_argument_n_not_string,
Chris Lattner7b937ae2008-03-04 18:08:48 +00002049 "visibility", std::string("1"));
Chris Lattnerddee4232008-03-03 03:28:21 +00002050 return;
2051 }
2052
Chris Lattner7b937ae2008-03-04 18:08:48 +00002053 const char *TypeStr = Str->getStrData();
2054 unsigned TypeLen = Str->getByteLength();
Chris Lattnerddee4232008-03-03 03:28:21 +00002055 llvm::GlobalValue::VisibilityTypes type;
2056
Chris Lattner7b937ae2008-03-04 18:08:48 +00002057 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
Chris Lattnerddee4232008-03-03 03:28:21 +00002058 type = llvm::GlobalValue::DefaultVisibility;
Chris Lattner7b937ae2008-03-04 18:08:48 +00002059 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
Chris Lattnerddee4232008-03-03 03:28:21 +00002060 type = llvm::GlobalValue::HiddenVisibility;
Chris Lattner7b937ae2008-03-04 18:08:48 +00002061 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
Chris Lattnerddee4232008-03-03 03:28:21 +00002062 type = llvm::GlobalValue::HiddenVisibility; // FIXME
Chris Lattner7b937ae2008-03-04 18:08:48 +00002063 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
Chris Lattnerddee4232008-03-03 03:28:21 +00002064 type = llvm::GlobalValue::ProtectedVisibility;
2065 else {
2066 Diag(rawAttr->getLoc(), diag::warn_attribute_type_not_supported,
Chris Lattner7b937ae2008-03-04 18:08:48 +00002067 "visibility", TypeStr);
Chris Lattnerddee4232008-03-03 03:28:21 +00002068 return;
2069 }
2070
2071 d->addAttr(new VisibilityAttr(type));
2072}
2073
2074void Sema::HandleWeakAttribute(Decl *d, AttributeList *rawAttr) {
2075 // check the attribute arguments.
2076 if (rawAttr->getNumArgs() != 0) {
2077 Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
2078 std::string("0"));
2079 return;
2080 }
2081
2082 d->addAttr(new WeakAttr());
2083}
2084
2085void Sema::HandleDLLImportAttribute(Decl *d, AttributeList *rawAttr) {
2086 // check the attribute arguments.
2087 if (rawAttr->getNumArgs() != 0) {
2088 Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
2089 std::string("0"));
2090 return;
2091 }
2092
2093 d->addAttr(new DLLImportAttr());
2094}
2095
2096void Sema::HandleDLLExportAttribute(Decl *d, AttributeList *rawAttr) {
2097 // check the attribute arguments.
2098 if (rawAttr->getNumArgs() != 0) {
2099 Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
2100 std::string("0"));
2101 return;
2102 }
2103
2104 d->addAttr(new DLLExportAttr());
2105}
2106
Nate Begeman440b4562008-03-07 20:04:22 +00002107void Sema::HandleStdCallAttribute(Decl *d, AttributeList *rawAttr) {
2108 // check the attribute arguments.
2109 if (rawAttr->getNumArgs() != 0) {
2110 Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
2111 std::string("0"));
2112 return;
2113 }
2114
2115 d->addAttr(new StdCallAttr());
2116}
2117
2118void Sema::HandleFastCallAttribute(Decl *d, AttributeList *rawAttr) {
2119 // check the attribute arguments.
2120 if (rawAttr->getNumArgs() != 0) {
2121 Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
2122 std::string("0"));
2123 return;
2124 }
2125
2126 d->addAttr(new FastCallAttr());
2127}
2128
Chris Lattnerddee4232008-03-03 03:28:21 +00002129void Sema::HandleNothrowAttribute(Decl *d, AttributeList *rawAttr) {
2130 // check the attribute arguments.
2131 if (rawAttr->getNumArgs() != 0) {
2132 Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
2133 std::string("0"));
2134 return;
2135 }
2136
2137 d->addAttr(new NoThrowAttr());
2138}
2139
Nuno Lopes8c1a9a82008-03-25 23:01:48 +00002140static const FunctionTypeProto *getFunctionProto(Decl *d) {
2141 ValueDecl *decl = dyn_cast<ValueDecl>(d);
2142 if (!decl) return 0;
2143
2144 QualType Ty = decl->getType();
2145
2146 if (Ty->isFunctionPointerType()) {
2147 const PointerType *PtrTy = Ty->getAsPointerType();
2148 Ty = PtrTy->getPointeeType();
2149 }
2150
2151 if (const FunctionType *FnTy = Ty->getAsFunctionType())
2152 return dyn_cast<FunctionTypeProto>(FnTy->getAsFunctionType());
2153
2154 return 0;
2155}
2156
2157
Ted Kremenekaa8f9762008-03-07 18:43:49 +00002158/// Handle __attribute__((format(type,idx,firstarg))) attributes
2159/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattnerddee4232008-03-03 03:28:21 +00002160void Sema::HandleFormatAttribute(Decl *d, AttributeList *rawAttr) {
2161
2162 if (!rawAttr->getParameterName()) {
2163 Diag(rawAttr->getLoc(), diag::err_attribute_argument_n_not_string,
2164 "format", std::string("1"));
2165 return;
2166 }
2167
2168 if (rawAttr->getNumArgs() != 2) {
2169 Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
2170 std::string("3"));
2171 return;
2172 }
2173
Nuno Lopes8c1a9a82008-03-25 23:01:48 +00002174 // GCC ignores the format attribute on K&R style function
2175 // prototypes, so we ignore it as well
2176 const FunctionTypeProto *proto = getFunctionProto(d);
2177
2178 if (!proto) {
Chris Lattnerddee4232008-03-03 03:28:21 +00002179 Diag(rawAttr->getLoc(), diag::warn_attribute_wrong_decl_type,
2180 "format", "function");
2181 return;
2182 }
2183
2184 // FIXME: in C++ the implicit 'this' function parameter also counts.
Ted Kremenekaa8f9762008-03-07 18:43:49 +00002185 // this is needed in order to be compatible with GCC
Chris Lattnerddee4232008-03-03 03:28:21 +00002186 // the index must start in 1 and the limit is numargs+1
Nuno Lopes8c1a9a82008-03-25 23:01:48 +00002187 unsigned NumArgs = proto->getNumArgs();
Ted Kremenekaa8f9762008-03-07 18:43:49 +00002188 unsigned FirstIdx = 1;
Chris Lattnerddee4232008-03-03 03:28:21 +00002189
2190 const char *Format = rawAttr->getParameterName()->getName();
2191 unsigned FormatLen = rawAttr->getParameterName()->getLength();
2192
2193 // Normalize the argument, __foo__ becomes foo.
2194 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
2195 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
2196 Format += 2;
2197 FormatLen -= 4;
2198 }
2199
2200 if (!((FormatLen == 5 && !memcmp(Format, "scanf", 5))
2201 || (FormatLen == 6 && !memcmp(Format, "printf", 6))
2202 || (FormatLen == 7 && !memcmp(Format, "strfmon", 7))
2203 || (FormatLen == 8 && !memcmp(Format, "strftime", 8)))) {
2204 Diag(rawAttr->getLoc(), diag::warn_attribute_type_not_supported,
2205 "format", rawAttr->getParameterName()->getName());
2206 return;
2207 }
2208
Ted Kremenekaa8f9762008-03-07 18:43:49 +00002209 // checks for the 2nd argument
Chris Lattnerddee4232008-03-03 03:28:21 +00002210 Expr *IdxExpr = static_cast<Expr *>(rawAttr->getArg(0));
Ted Kremenekaa8f9762008-03-07 18:43:49 +00002211 llvm::APSInt Idx(Context.getTypeSize(IdxExpr->getType()));
Chris Lattnerddee4232008-03-03 03:28:21 +00002212 if (!IdxExpr->isIntegerConstantExpr(Idx, Context)) {
2213 Diag(rawAttr->getLoc(), diag::err_attribute_argument_n_not_int,
2214 "format", std::string("2"), IdxExpr->getSourceRange());
2215 return;
2216 }
2217
Ted Kremenekaa8f9762008-03-07 18:43:49 +00002218 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerddee4232008-03-03 03:28:21 +00002219 Diag(rawAttr->getLoc(), diag::err_attribute_argument_out_of_bounds,
2220 "format", std::string("2"), IdxExpr->getSourceRange());
2221 return;
2222 }
2223
Ted Kremenekaa8f9762008-03-07 18:43:49 +00002224 // make sure the format string is really a string
2225 QualType Ty = proto->getArgType(Idx.getZExtValue()-1);
2226 if (!Ty->isPointerType() ||
2227 !Ty->getAsPointerType()->getPointeeType()->isCharType()) {
2228 Diag(rawAttr->getLoc(), diag::err_format_attribute_not_string,
2229 IdxExpr->getSourceRange());
2230 return;
2231 }
2232
2233
2234 // check the 3rd argument
Chris Lattnerddee4232008-03-03 03:28:21 +00002235 Expr *FirstArgExpr = static_cast<Expr *>(rawAttr->getArg(1));
Ted Kremenekaa8f9762008-03-07 18:43:49 +00002236 llvm::APSInt FirstArg(Context.getTypeSize(FirstArgExpr->getType()));
Chris Lattnerddee4232008-03-03 03:28:21 +00002237 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, Context)) {
2238 Diag(rawAttr->getLoc(), diag::err_attribute_argument_n_not_int,
2239 "format", std::string("3"), FirstArgExpr->getSourceRange());
2240 return;
2241 }
2242
Ted Kremenekaa8f9762008-03-07 18:43:49 +00002243 // check if the function is variadic if the 3rd argument non-zero
2244 if (FirstArg != 0) {
2245 if (proto->isVariadic()) {
2246 ++NumArgs; // +1 for ...
2247 } else {
2248 Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
2249 return;
2250 }
2251 }
2252
2253 // strftime requires FirstArg to be 0 because it doesn't read from any variable
2254 // the input is just the current time + the format string
Chris Lattnerddee4232008-03-03 03:28:21 +00002255 if (FormatLen == 8 && !memcmp(Format, "strftime", 8)) {
Ted Kremenekaa8f9762008-03-07 18:43:49 +00002256 if (FirstArg != 0) {
Chris Lattnerddee4232008-03-03 03:28:21 +00002257 Diag(rawAttr->getLoc(), diag::err_format_strftime_third_parameter,
2258 FirstArgExpr->getSourceRange());
2259 return;
2260 }
Ted Kremenekaa8f9762008-03-07 18:43:49 +00002261 // if 0 it disables parameter checking (to use with e.g. va_list)
2262 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerddee4232008-03-03 03:28:21 +00002263 Diag(rawAttr->getLoc(), diag::err_attribute_argument_out_of_bounds,
2264 "format", std::string("3"), FirstArgExpr->getSourceRange());
2265 return;
2266 }
2267
2268 d->addAttr(new FormatAttr(std::string(Format, FormatLen),
2269 Idx.getZExtValue(), FirstArg.getZExtValue()));
2270}
2271
Nate Begemanc398f0b2008-02-21 19:30:49 +00002272void Sema::HandleAnnotateAttribute(Decl *d, AttributeList *rawAttr) {
2273 // check the attribute arguments.
2274 if (rawAttr->getNumArgs() != 1) {
2275 Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
2276 std::string("1"));
2277 return;
2278 }
2279 Expr *argExpr = static_cast<Expr *>(rawAttr->getArg(0));
2280 StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
Anders Carlssonad148062008-02-16 00:29:18 +00002281
Nate Begemanc398f0b2008-02-21 19:30:49 +00002282 // Make sure that there is a string literal as the annotation's single
2283 // argument.
2284 if (!SE) {
2285 Diag(rawAttr->getLoc(), diag::err_attribute_annotate_no_string);
2286 return;
2287 }
2288 d->addAttr(new AnnotateAttr(std::string(SE->getStrData(),
2289 SE->getByteLength())));
2290}
2291
Anders Carlsson78aaae92007-12-19 07:19:40 +00002292void Sema::HandleAlignedAttribute(Decl *d, AttributeList *rawAttr)
2293{
2294 // check the attribute arguments.
Eli Friedman4ca08672008-01-30 17:38:42 +00002295 if (rawAttr->getNumArgs() > 1) {
Chris Lattner2070d802008-02-20 23:25:22 +00002296 Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
Anders Carlsson78aaae92007-12-19 07:19:40 +00002297 std::string("1"));
2298 return;
2299 }
Eli Friedman4ca08672008-01-30 17:38:42 +00002300
Anders Carlsson042c4e72008-02-16 19:51:27 +00002301 unsigned Align = 0;
2302
2303 if (rawAttr->getNumArgs() == 0) {
2304 // FIXME: This should be the target specific maximum alignment.
2305 // (For now we just use 128 bits which is the maximum on X86.
2306 Align = 128;
Eli Friedman4ca08672008-01-30 17:38:42 +00002307 return;
Anders Carlsson042c4e72008-02-16 19:51:27 +00002308 } else {
2309 Expr *alignmentExpr = static_cast<Expr *>(rawAttr->getArg(0));
2310 llvm::APSInt alignment(32);
2311 if (!alignmentExpr->isIntegerConstantExpr(alignment, Context)) {
Chris Lattner2070d802008-02-20 23:25:22 +00002312 Diag(rawAttr->getLoc(), diag::err_attribute_argument_not_int,
Anders Carlsson042c4e72008-02-16 19:51:27 +00002313 "aligned", alignmentExpr->getSourceRange());
2314 return;
2315 }
2316
2317 Align = alignment.getZExtValue() * 8;
2318 }
Eli Friedman4ca08672008-01-30 17:38:42 +00002319
Anders Carlsson042c4e72008-02-16 19:51:27 +00002320 d->addAttr(new AlignedAttr(Align));
Anders Carlsson78aaae92007-12-19 07:19:40 +00002321}