blob: 543d2af4544a478d67b98f5ef124d7055a587f39 [file] [log] [blame]
Chris Lattner697e5d62006-11-09 06:32:27 +00001//===--- SemaDecl.cpp - Semantic Analysis for Declarations ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for declarations.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattnere168f762006-11-10 05:29:30 +000014#include "Sema.h"
Chris Lattner5c5fbcc2006-12-03 08:41:30 +000015#include "clang/AST/ASTContext.h"
Chris Lattner9561a0b2007-01-28 08:20:04 +000016#include "clang/AST/Builtins.h"
Chris Lattnere168f762006-11-10 05:29:30 +000017#include "clang/AST/Decl.h"
Chris Lattner1300fb92007-01-23 23:42:53 +000018#include "clang/AST/Expr.h"
Chris Lattnerf84a79c2006-11-11 22:59:23 +000019#include "clang/AST/Type.h"
Chris Lattner591a6752006-11-19 23:16:18 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattnere168f762006-11-10 05:29:30 +000021#include "clang/Parse/Scope.h"
22#include "clang/Lex/IdentifierTable.h"
Chris Lattnerac18be92006-11-20 06:49:47 +000023#include "clang/Basic/LangOptions.h"
Chris Lattner9561a0b2007-01-28 08:20:04 +000024#include "clang/Basic/TargetInfo.h"
Chris Lattner38047f92007-01-27 06:24:01 +000025#include "llvm/ADT/SmallSet.h"
Chris Lattner697e5d62006-11-09 06:32:27 +000026using namespace clang;
27
Steve Naroff8eeeb132007-05-08 21:09:37 +000028// C99: 6.7.5p3: Used by ParseDeclarator/ParseField to make sure we have
29// a constant expression of type int with a value greater than zero.
Chris Lattner6046e002007-06-05 06:39:23 +000030bool Sema::VerifyConstantArrayType(const ArrayType *Array,
31 SourceLocation DeclLoc) {
Chris Lattnerd2b88ab2007-07-13 03:05:23 +000032 const Expr *Size = Array->getSizeExpr();
Chris Lattner6046e002007-06-05 06:39:23 +000033 if (Size == 0) return false; // incomplete type.
34
35 if (!Size->getType()->isIntegerType()) {
36 Diag(Size->getLocStart(), diag::err_array_size_non_int,
37 Size->getType().getAsString(), Size->getSourceRange());
38 return true;
Steve Naroff8eeeb132007-05-08 21:09:37 +000039 }
Chris Lattner6046e002007-06-05 06:39:23 +000040
41 // Verify that the size of the array is an integer constant expr.
42 SourceLocation Loc;
Chris Lattner23b7eb62007-06-15 23:05:46 +000043 llvm::APSInt SizeVal(32);
Chris Lattner0e9d6222007-07-15 23:26:56 +000044 if (!Size->isIntegerConstantExpr(SizeVal, Context, &Loc)) {
Chris Lattner6046e002007-06-05 06:39:23 +000045 // FIXME: This emits the diagnostic to enforce 6.7.2.1p8, but the message
46 // is wrong. It is also wrong for static variables.
Chris Lattner6d9b49a2007-06-09 05:56:19 +000047 // FIXME: This is also wrong for:
48 // int sub1(int i, char *pi) { typedef int foo[i];
49 // struct bar {foo f1; int f2:3; int f3:4} *p; }
Chris Lattner6046e002007-06-05 06:39:23 +000050 Diag(DeclLoc, diag::err_typecheck_illegal_vla, Size->getSourceRange());
51 return true;
52 }
53
54 // We have a constant expression with an integer type, now make sure
55 // value greater than zero (C99 6.7.5.2p1).
56
57 // FIXME: This check isn't specific to static VLAs, this should be moved
58 // elsewhere or replicated. 'int X[-1];' inside a function should emit an
59 // error.
60 if (SizeVal.isSigned()) {
Chris Lattner23b7eb62007-06-15 23:05:46 +000061 llvm::APSInt Zero(SizeVal.getBitWidth());
Chris Lattner6046e002007-06-05 06:39:23 +000062 Zero.setIsUnsigned(false);
63 if (SizeVal < Zero) {
64 Diag(DeclLoc, diag::err_typecheck_negative_array_size,
65 Size->getSourceRange());
66 return true;
67 } else if (SizeVal == 0) {
68 // GCC accepts zero sized static arrays.
69 Diag(DeclLoc, diag::err_typecheck_zero_array_size,
70 Size->getSourceRange());
71 }
72 }
73 return false;
Steve Naroff8eeeb132007-05-08 21:09:37 +000074}
Chris Lattnere168f762006-11-10 05:29:30 +000075
Chris Lattner2ebe4bb2006-11-20 01:29:42 +000076Sema::DeclTy *Sema::isTypeName(const IdentifierInfo &II, Scope *S) const {
Chris Lattner32d920b2007-01-26 02:01:53 +000077 return dyn_cast_or_null<TypedefDecl>(II.getFETokenInfo<Decl>());
Chris Lattnere168f762006-11-10 05:29:30 +000078}
79
Chris Lattner302b4be2006-11-19 02:31:38 +000080void Sema::PopScope(SourceLocation Loc, Scope *S) {
Chris Lattner1a76a3c2007-08-26 06:24:45 +000081 if (S->decl_empty()) return;
82 assert((S->getFlags() & Scope::DeclScope) &&"Scope shouldn't contain decls!");
83
Chris Lattner302b4be2006-11-19 02:31:38 +000084 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
85 I != E; ++I) {
Chris Lattner99d31772007-01-21 22:37:37 +000086 Decl *D = static_cast<Decl*>(*I);
Chris Lattner302b4be2006-11-19 02:31:38 +000087 assert(D && "This decl didn't get pushed??");
Chris Lattnerff65b6b2007-01-23 01:33:16 +000088 IdentifierInfo *II = D->getIdentifier();
89 if (!II) continue;
Chris Lattner302b4be2006-11-19 02:31:38 +000090
Chris Lattnerff65b6b2007-01-23 01:33:16 +000091 // Unlink this decl from the identifier. Because the scope contains decls
92 // in an unordered collection, and because we have multiple identifier
93 // namespaces (e.g. tag, normal, label),the decl may not be the first entry.
94 if (II->getFETokenInfo<Decl>() == D) {
95 // Normal case, no multiple decls in different namespaces.
96 II->setFETokenInfo(D->getNext());
97 } else {
98 // Scan ahead. There are only three namespaces in C, so this loop can
99 // never execute more than 3 times.
100 Decl *SomeDecl = II->getFETokenInfo<Decl>();
101 while (SomeDecl->getNext() != D) {
102 SomeDecl = SomeDecl->getNext();
103 assert(SomeDecl && "Didn't find this decl on its identifier's chain!");
104 }
105 SomeDecl->setNext(D->getNext());
106 }
Chris Lattner302b4be2006-11-19 02:31:38 +0000107
Chris Lattner740b2f32006-11-21 01:32:20 +0000108 // This will have to be revisited for C++: there we want to nest stuff in
109 // namespace decls etc. Even for C, we might want a top-level translation
110 // unit decl or something.
111 if (!CurFunctionDecl)
112 continue;
113
114 // Chain this decl to the containing function, it now owns the memory for
115 // the decl.
116 D->setNext(CurFunctionDecl->getDeclChain());
117 CurFunctionDecl->setDeclChain(D);
Chris Lattner302b4be2006-11-19 02:31:38 +0000118 }
119}
120
Chris Lattner18b19622007-01-22 07:39:13 +0000121/// LookupScopedDecl - Look up the inner-most declaration in the specified
122/// namespace.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000123Decl *Sema::LookupScopedDecl(IdentifierInfo *II, unsigned NSI,
124 SourceLocation IdLoc, Scope *S) {
Chris Lattner18b19622007-01-22 07:39:13 +0000125 if (II == 0) return 0;
Chris Lattnerb6738ec2007-01-28 00:38:24 +0000126 Decl::IdentifierNamespace NS = (Decl::IdentifierNamespace)NSI;
Chris Lattner18b19622007-01-22 07:39:13 +0000127
128 // Scan up the scope chain looking for a decl that matches this identifier
129 // that is in the appropriate namespace. This search should not take long, as
130 // shadowing of names is uncommon, and deep shadowing is extremely uncommon.
131 for (Decl *D = II->getFETokenInfo<Decl>(); D; D = D->getNext())
132 if (D->getIdentifierNamespace() == NS)
133 return D;
Chris Lattnerb6738ec2007-01-28 00:38:24 +0000134
Chris Lattner9561a0b2007-01-28 08:20:04 +0000135 // If we didn't find a use of this identifier, and if the identifier
136 // corresponds to a compiler builtin, create the decl object for the builtin
137 // now, injecting it into translation unit scope, and return it.
138 if (NS == Decl::IDNS_Ordinary) {
139 // If this is a builtin on some other target, or if this builtin varies
140 // across targets (e.g. in type), emit a diagnostic and mark the translation
141 // unit non-portable for using it.
142 if (II->isNonPortableBuiltin()) {
143 // Only emit this diagnostic once for this builtin.
144 II->setNonPortableBuiltin(false);
145 Context.Target.DiagnoseNonPortability(IdLoc,
146 diag::port_target_builtin_use);
147 }
Chris Lattner9561a0b2007-01-28 08:20:04 +0000148 // If this is a builtin on this (or all) targets, create the decl.
149 if (unsigned BuiltinID = II->getBuiltinID())
150 return LazilyCreateBuiltin(II, BuiltinID, S);
151 }
Chris Lattner18b19622007-01-22 07:39:13 +0000152 return 0;
153}
154
Chris Lattner9561a0b2007-01-28 08:20:04 +0000155/// LazilyCreateBuiltin - The specified Builtin-ID was first used at file scope.
156/// lazily create a decl for it.
157Decl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid, Scope *S) {
158 Builtin::ID BID = (Builtin::ID)bid;
159
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000160 QualType R = Context.BuiltinInfo.GetBuiltinType(BID, Context);
Chris Lattner776fac82007-06-09 00:53:06 +0000161 FunctionDecl *New = new FunctionDecl(SourceLocation(), II, R,
Chris Lattnerb677a932007-08-26 04:02:13 +0000162 FunctionDecl::Extern, false, 0);
Chris Lattner9561a0b2007-01-28 08:20:04 +0000163
164 // Find translation-unit scope to insert this function into.
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000165 if (Scope *FnS = S->getFnParent())
166 S = FnS->getParent(); // Skip all scopes in a function at once.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000167 while (S->getParent())
168 S = S->getParent();
169 S->AddDecl(New);
170
171 // Add this decl to the end of the identifier info.
172 if (Decl *LastDecl = II->getFETokenInfo<Decl>()) {
173 // Scan until we find the last (outermost) decl in the id chain.
174 while (LastDecl->getNext())
175 LastDecl = LastDecl->getNext();
176 // Insert before (outside) it.
177 LastDecl->setNext(New);
178 } else {
179 II->setFETokenInfo(New);
180 }
181 // Make sure clients iterating over decls see this.
182 LastInGroupList.push_back(New);
183
184 return New;
185}
186
Chris Lattner01564d92007-01-27 19:27:06 +0000187/// MergeTypeDefDecl - We just parsed a typedef 'New' which has the same name
188/// and scope as a previous declaration 'Old'. Figure out how to resolve this
189/// situation, merging decls or emitting diagnostics as appropriate.
190///
Chris Lattnerc511efb2007-01-27 19:32:14 +0000191TypedefDecl *Sema::MergeTypeDefDecl(TypedefDecl *New, Decl *OldD) {
192 // Verify the old decl was also a typedef.
193 TypedefDecl *Old = dyn_cast<TypedefDecl>(OldD);
194 if (!Old) {
195 Diag(New->getLocation(), diag::err_redefinition_different_kind,
196 New->getName());
197 Diag(OldD->getLocation(), diag::err_previous_definition);
198 return New;
199 }
200
Chris Lattner01564d92007-01-27 19:27:06 +0000201 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
202 // TODO: This is totally simplistic. It should handle merging functions
203 // together etc, merging extern int X; int X; ...
204 Diag(New->getLocation(), diag::err_redefinition, New->getName());
205 Diag(Old->getLocation(), diag::err_previous_definition);
206 return New;
207}
208
209/// MergeFunctionDecl - We just parsed a function 'New' which has the same name
210/// and scope as a previous declaration 'Old'. Figure out how to resolve this
211/// situation, merging decls or emitting diagnostics as appropriate.
212///
Chris Lattnerc511efb2007-01-27 19:32:14 +0000213FunctionDecl *Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD) {
214 // Verify the old decl was also a function.
215 FunctionDecl *Old = dyn_cast<FunctionDecl>(OldD);
216 if (!Old) {
217 Diag(New->getLocation(), diag::err_redefinition_different_kind,
218 New->getName());
219 Diag(OldD->getLocation(), diag::err_previous_definition);
220 return New;
221 }
222
Chris Lattnerefe4aea2007-01-27 19:35:39 +0000223 // This is not right, but it's a start. If 'Old' is a function prototype with
224 // the same type as 'New', silently allow this. FIXME: We should link up decl
225 // objects here.
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000226 if (Old->getBody() == 0 &&
227 Old->getCanonicalType() == New->getCanonicalType()) {
Chris Lattnerefe4aea2007-01-27 19:35:39 +0000228 return New;
229 }
Chris Lattnerc511efb2007-01-27 19:32:14 +0000230
Chris Lattner01564d92007-01-27 19:27:06 +0000231 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
232 // TODO: This is totally simplistic. It should handle merging functions
233 // together etc, merging extern int X; int X; ...
234 Diag(New->getLocation(), diag::err_redefinition, New->getName());
235 Diag(Old->getLocation(), diag::err_previous_definition);
236 return New;
237}
238
239/// MergeVarDecl - We just parsed a variable 'New' which has the same name
240/// and scope as a previous declaration 'Old'. Figure out how to resolve this
241/// situation, merging decls or emitting diagnostics as appropriate.
242///
Steve Narofffc49d672007-04-01 21:27:45 +0000243/// FIXME: Need to carefully consider tentative definition rules (C99 6.9.2p2).
244/// For example, we incorrectly complain about i1, i4 from C99 6.9.2p4.
245///
Chris Lattnerc511efb2007-01-27 19:32:14 +0000246VarDecl *Sema::MergeVarDecl(VarDecl *New, Decl *OldD) {
247 // Verify the old decl was also a variable.
248 VarDecl *Old = dyn_cast<VarDecl>(OldD);
249 if (!Old) {
250 Diag(New->getLocation(), diag::err_redefinition_different_kind,
251 New->getName());
252 Diag(OldD->getLocation(), diag::err_previous_definition);
253 return New;
254 }
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000255 // Verify the types match.
256 if (Old->getCanonicalType() != New->getCanonicalType()) {
257 Diag(New->getLocation(), diag::err_redefinition, New->getName());
258 Diag(Old->getLocation(), diag::err_previous_definition);
259 return New;
260 }
261 // We've verified the types match, now check if Old is "extern".
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000262 if (Old->getStorageClass() != VarDecl::Extern) {
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000263 Diag(New->getLocation(), diag::err_redefinition, New->getName());
264 Diag(Old->getLocation(), diag::err_previous_definition);
265 }
Chris Lattner01564d92007-01-27 19:27:06 +0000266 return New;
267}
268
Chris Lattnerb6738ec2007-01-28 00:38:24 +0000269/// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
270/// no declarator (e.g. "struct foo;") is parsed.
271Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) {
272 // TODO: emit error on 'int;' or 'const enum foo;'.
273 // TODO: emit error on 'typedef int;'
274 // if (!DS.isMissingDeclaratorOk()) Diag(...);
275
276 return 0;
277}
278
Chris Lattner776fac82007-06-09 00:53:06 +0000279Sema::DeclTy *
Chris Lattner79c57592007-07-12 00:36:32 +0000280Sema::ParseDeclarator(Scope *S, Declarator &D, ExprTy *init,
Chris Lattner776fac82007-06-09 00:53:06 +0000281 DeclTy *lastDeclarator) {
282 Decl *LastDeclarator = (Decl*)lastDeclarator;
Chris Lattner79c57592007-07-12 00:36:32 +0000283 Expr *Init = static_cast<Expr*>(init);
Chris Lattnere168f762006-11-10 05:29:30 +0000284 IdentifierInfo *II = D.getIdentifier();
Chris Lattner302b4be2006-11-19 02:31:38 +0000285
Chris Lattner02c04392007-07-25 00:24:17 +0000286 // All of these full declarators require an identifier. If it doesn't have
287 // one, the ParsedFreeStandingDeclSpec action should be used.
288 if (II == 0) {
289 Diag(D.getDeclSpec().getSourceRange().Begin(), diag::err_declarator_need_ident,
290 D.getDeclSpec().getSourceRange(), D.getSourceRange());
291 return 0;
292 }
293
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000294 // The scope passed in may not be a decl scope. Zip up the scope tree until
295 // we find one that is.
296 while ((S->getFlags() & Scope::DeclScope) == 0)
297 S = S->getParent();
298
Chris Lattner01564d92007-01-27 19:27:06 +0000299 // See if this is a redefinition of a variable in the same scope.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000300 Decl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary,
301 D.getIdentifierLoc(), S);
Chris Lattner776fac82007-06-09 00:53:06 +0000302 if (PrevDecl && !S->isDeclScope(PrevDecl))
Chris Lattner01564d92007-01-27 19:27:06 +0000303 PrevDecl = 0; // If in outer scope, it isn't the same thing.
304
Chris Lattnere168f762006-11-10 05:29:30 +0000305 Decl *New;
Chris Lattner01a7c532007-01-25 23:09:03 +0000306 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Chris Lattner79c57592007-07-12 00:36:32 +0000307 assert(Init == 0 && "Can't have initializer for a typedef!");
Chris Lattner776fac82007-06-09 00:53:06 +0000308 TypedefDecl *NewTD = ParseTypedefDecl(S, D, LastDeclarator);
Chris Lattner01564d92007-01-27 19:27:06 +0000309 if (!NewTD) return 0;
Steve Naroffa8fd9732007-06-11 00:35:03 +0000310
311 // Handle attributes prior to checking for duplicates in MergeVarDecl
312 HandleDeclAttributes(NewTD, D.getDeclSpec().getAttributes(),
313 D.getAttributes());
Chris Lattner01564d92007-01-27 19:27:06 +0000314 // Merge the decl with the existing one if appropriate.
315 if (PrevDecl) {
316 NewTD = MergeTypeDefDecl(NewTD, PrevDecl);
317 if (NewTD == 0) return 0;
318 }
319 New = NewTD;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000320 if (S->getParent() == 0) {
321 // C99 6.7.7p2: If a typedef name specifies a variably modified type
322 // then it shall have block scope.
323 if (ArrayType *ary = dyn_cast<ArrayType>(NewTD->getUnderlyingType())) {
Chris Lattner6046e002007-06-05 06:39:23 +0000324 if (VerifyConstantArrayType(ary, D.getIdentifierLoc()))
Steve Naroff8eeeb132007-05-08 21:09:37 +0000325 return 0;
326 }
327 }
Chris Lattner01a7c532007-01-25 23:09:03 +0000328 } else if (D.isFunctionDeclarator()) {
Chris Lattner79c57592007-07-12 00:36:32 +0000329 assert(Init == 0 && "Can't have an initializer for a functiondecl!");
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000330 QualType R = GetTypeForDeclarator(D, S);
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000331 if (R.isNull()) return 0; // FIXME: "auto func();" passes through...
Steve Naroff7a5af782007-07-13 16:58:59 +0000332
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000333 FunctionDecl::StorageClass SC;
334 switch (D.getDeclSpec().getStorageClassSpec()) {
335 default: assert(0 && "Unknown storage class!");
336 case DeclSpec::SCS_auto:
337 case DeclSpec::SCS_register:
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000338 Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_func,
339 R.getAsString());
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000340 return 0;
341 case DeclSpec::SCS_unspecified: SC = FunctionDecl::None; break;
342 case DeclSpec::SCS_extern: SC = FunctionDecl::Extern; break;
343 case DeclSpec::SCS_static: SC = FunctionDecl::Static; break;
344 }
345
Chris Lattner776fac82007-06-09 00:53:06 +0000346 FunctionDecl *NewFD = new FunctionDecl(D.getIdentifierLoc(), II, R, SC,
Chris Lattnerb677a932007-08-26 04:02:13 +0000347 D.getDeclSpec().isInlineSpecified(),
Chris Lattner776fac82007-06-09 00:53:06 +0000348 LastDeclarator);
Chris Lattner01564d92007-01-27 19:27:06 +0000349
350 // Merge the decl with the existing one if appropriate.
351 if (PrevDecl) {
352 NewFD = MergeFunctionDecl(NewFD, PrevDecl);
353 if (NewFD == 0) return 0;
354 }
355 New = NewFD;
Chris Lattner01a7c532007-01-25 23:09:03 +0000356 } else {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000357 QualType R = GetTypeForDeclarator(D, S);
Chris Lattner01a7c532007-01-25 23:09:03 +0000358 if (R.isNull()) return 0;
Chris Lattner01564d92007-01-27 19:27:06 +0000359
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000360 VarDecl *NewVD;
361 VarDecl::StorageClass SC;
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000362 switch (D.getDeclSpec().getStorageClassSpec()) {
363 default: assert(0 && "Unknown storage class!");
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000364 case DeclSpec::SCS_unspecified: SC = VarDecl::None; break;
365 case DeclSpec::SCS_extern: SC = VarDecl::Extern; break;
366 case DeclSpec::SCS_static: SC = VarDecl::Static; break;
367 case DeclSpec::SCS_auto: SC = VarDecl::Auto; break;
368 case DeclSpec::SCS_register: SC = VarDecl::Register; break;
369 }
Steve Narofffc49d672007-04-01 21:27:45 +0000370 if (S->getParent() == 0) {
371 // File scope. C99 6.9.2p2: A declaration of an identifier for and
372 // object that has file scope without an initializer, and without a
373 // storage-class specifier or with the storage-class specifier "static",
374 // constitutes a tentative definition. Note: A tentative definition with
375 // external linkage is valid (C99 6.2.2p5).
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000376 if (!Init && SC == VarDecl::Static) {
Steve Narofffc49d672007-04-01 21:27:45 +0000377 // C99 6.9.2p3: If the declaration of an identifier for an object is
378 // a tentative definition and has internal linkage (C99 6.2.2p3), the
379 // declared type shall not be an incomplete type.
380 if (R->isIncompleteType()) {
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000381 Diag(D.getIdentifierLoc(), diag::err_typecheck_decl_incomplete_type,
382 R.getAsString());
Steve Narofffc49d672007-04-01 21:27:45 +0000383 return 0;
384 }
Steve Naroffca8f7122007-04-01 01:41:35 +0000385 }
Bill Wendlingd6de6572007-06-02 09:40:07 +0000386 // C99 6.9p2: The storage-class specifiers auto and register shall not
387 // appear in the declaration specifiers in an external declaration.
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000388 if (SC == VarDecl::Auto || SC == VarDecl::Register) {
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000389 Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope,
390 R.getAsString());
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000391 return 0;
392 }
Steve Naroff8eeeb132007-05-08 21:09:37 +0000393 // C99 6.7.5.2p2: If an identifier is declared to be an object with
394 // static storage duration, it shall not have a variable length array.
Chris Lattner0fd893e2007-07-31 21:33:24 +0000395 if (const ArrayType *ary = R->getAsArrayType()) {
Chris Lattner6046e002007-06-05 06:39:23 +0000396 if (VerifyConstantArrayType(ary, D.getIdentifierLoc()))
Steve Naroff8eeeb132007-05-08 21:09:37 +0000397 return 0;
398 }
Chris Lattner776fac82007-06-09 00:53:06 +0000399 NewVD = new FileVarDecl(D.getIdentifierLoc(), II, R, SC, LastDeclarator);
Steve Narofffc49d672007-04-01 21:27:45 +0000400 } else {
401 // Block scope. C99 6.7p7: If an identifier for an object is declared with
402 // no linkage (C99 6.2.2p6), the type for the object shall be complete...
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000403 if (SC != VarDecl::Extern) {
Steve Narofffc49d672007-04-01 21:27:45 +0000404 if (R->isIncompleteType()) {
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000405 Diag(D.getIdentifierLoc(), diag::err_typecheck_decl_incomplete_type,
406 R.getAsString());
Steve Narofffc49d672007-04-01 21:27:45 +0000407 return 0;
408 }
409 }
Steve Naroff8eeeb132007-05-08 21:09:37 +0000410 if (SC == VarDecl::Static) {
411 // C99 6.7.5.2p2: If an identifier is declared to be an object with
412 // static storage duration, it shall not have a variable length array.
Chris Lattner0fd893e2007-07-31 21:33:24 +0000413 if (const ArrayType *ary = R->getAsArrayType()) {
Chris Lattner6046e002007-06-05 06:39:23 +0000414 if (VerifyConstantArrayType(ary, D.getIdentifierLoc()))
Steve Naroff8eeeb132007-05-08 21:09:37 +0000415 return 0;
416 }
417 }
Chris Lattner776fac82007-06-09 00:53:06 +0000418 NewVD = new BlockVarDecl(D.getIdentifierLoc(), II, R, SC, LastDeclarator);
Steve Naroffef2ab6a2007-04-02 20:50:54 +0000419 }
Steve Naroffa8fd9732007-06-11 00:35:03 +0000420 // Handle attributes prior to checking for duplicates in MergeVarDecl
421 HandleDeclAttributes(NewVD, D.getDeclSpec().getAttributes(),
422 D.getAttributes());
423
Chris Lattner01564d92007-01-27 19:27:06 +0000424 // Merge the decl with the existing one if appropriate.
425 if (PrevDecl) {
426 NewVD = MergeVarDecl(NewVD, PrevDecl);
427 if (NewVD == 0) return 0;
428 }
Steve Naroff0c1c7ed2007-08-24 22:33:52 +0000429 if (Init) {
430 AssignmentCheckResult result;
431 result = CheckSingleAssignmentConstraints(R, Init);
432 // FIXME: emit errors if appropriate.
433 NewVD->setInit(Init);
434 }
Chris Lattner01564d92007-01-27 19:27:06 +0000435 New = NewVD;
Chris Lattner01a7c532007-01-25 23:09:03 +0000436 }
Chris Lattner302b4be2006-11-19 02:31:38 +0000437
Chris Lattnere168f762006-11-10 05:29:30 +0000438 // If this has an identifier, add it to the scope stack.
439 if (II) {
Chris Lattner18b19622007-01-22 07:39:13 +0000440 New->setNext(II->getFETokenInfo<Decl>());
Chris Lattnere168f762006-11-10 05:29:30 +0000441 II->setFETokenInfo(New);
Chris Lattner99d31772007-01-21 22:37:37 +0000442 S->AddDecl(New);
Chris Lattnere168f762006-11-10 05:29:30 +0000443 }
444
Steve Naroff26c8ea52007-03-21 21:08:52 +0000445 if (S->getParent() == 0)
Chris Lattner776fac82007-06-09 00:53:06 +0000446 AddTopLevelDecl(New, LastDeclarator);
Chris Lattnere168f762006-11-10 05:29:30 +0000447
448 return New;
449}
450
Chris Lattner776fac82007-06-09 00:53:06 +0000451/// The declarators are chained together backwards, reverse the list.
452Sema::DeclTy *Sema::FinalizeDeclaratorGroup(Scope *S, DeclTy *group) {
453 // Often we have single declarators, handle them quickly.
454 Decl *Group = static_cast<Decl*>(group);
Chris Lattnerb2dd2412007-06-09 06:16:32 +0000455 if (Group == 0 || Group->getNextDeclarator() == 0) return Group;
Chris Lattner776fac82007-06-09 00:53:06 +0000456
457 Decl *NewGroup = 0;
458 while (Group) {
459 Decl *Next = Group->getNextDeclarator();
460 Group->setNextDeclarator(NewGroup);
461 NewGroup = Group;
462 Group = Next;
463 }
464 return NewGroup;
465}
466
Chris Lattner53621a52007-06-13 20:44:40 +0000467ParmVarDecl *
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000468Sema::ParseParamDeclarator(DeclaratorChunk &FTI, unsigned ArgNo,
469 Scope *FnScope) {
470 const DeclaratorChunk::ParamInfo &PI = FTI.Fun.ArgInfo[ArgNo];
Chris Lattner200bdc32006-11-19 02:43:37 +0000471
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000472 IdentifierInfo *II = PI.Ident;
Chris Lattnerc284e9b2007-01-23 05:14:32 +0000473 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
474 // Can this happen for params? We already checked that they don't conflict
475 // among each other. Here they can only shadow globals, which is ok.
Chris Lattnerd2b88ab2007-07-13 03:05:23 +0000476 if (/*Decl *PrevDecl = */LookupScopedDecl(II, Decl::IDNS_Ordinary,
Chris Lattner9561a0b2007-01-28 08:20:04 +0000477 PI.IdentLoc, FnScope)) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000478
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000479 }
480
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000481 // FIXME: Handle storage class (auto, register). No declarator?
Chris Lattner776fac82007-06-09 00:53:06 +0000482 // TODO: Chain to previous parameter with the prevdeclarator chain?
Steve Naroff773df5c2007-08-07 22:44:21 +0000483
484 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
485 // Doing the promotion here has a win and a loss. The win is the type for
486 // both Decl's and DeclRefExpr's will match (a convenient invariant for the
487 // code generator). The loss is the orginal type isn't preserved. For example:
488 //
489 // void func(int parmvardecl[5]) { // convert "int [5]" to "int *"
490 // int blockvardecl[5];
491 // sizeof(parmvardecl); // size == 4
492 // sizeof(blockvardecl); // size == 20
493 // }
494 //
495 // For expressions, all implicit conversions are captured using the
496 // ImplicitCastExpr AST node (we have no such mechanism for Decl's).
497 //
498 // FIXME: If a source translation tool needs to see the original type, then
499 // we need to consider storing both types (in ParmVarDecl)...
500 //
501 QualType parmDeclType = QualType::getFromOpaquePtr(PI.TypeInfo);
502 if (const ArrayType *AT = parmDeclType->getAsArrayType())
503 parmDeclType = Context.getPointerType(AT->getElementType());
504 else if (parmDeclType->isFunctionType())
505 parmDeclType = Context.getPointerType(parmDeclType);
506
507 ParmVarDecl *New = new ParmVarDecl(PI.IdentLoc, II, parmDeclType,
Chris Lattner53621a52007-06-13 20:44:40 +0000508 VarDecl::None, 0);
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000509
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000510 // If this has an identifier, add it to the scope stack.
511 if (II) {
Chris Lattner18b19622007-01-22 07:39:13 +0000512 New->setNext(II->getFETokenInfo<Decl>());
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000513 II->setFETokenInfo(New);
Chris Lattner99d31772007-01-21 22:37:37 +0000514 FnScope->AddDecl(New);
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000515 }
Chris Lattner229ce602006-11-21 01:21:07 +0000516
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000517 return New;
518}
519
520
521Sema::DeclTy *Sema::ParseStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) {
Chris Lattner229ce602006-11-21 01:21:07 +0000522 assert(CurFunctionDecl == 0 && "Function parsing confused");
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000523 assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
524 "Not a function declarator!");
525 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
526
527 // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared'
528 // for a K&R function.
529 if (!FTI.hasPrototype) {
530 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
531 if (FTI.ArgInfo[i].TypeInfo == 0) {
Chris Lattner843c5922007-06-10 23:40:34 +0000532 Diag(FTI.ArgInfo[i].IdentLoc, diag::ext_param_not_declared,
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000533 FTI.ArgInfo[i].Ident->getName());
534 // Implicitly declare the argument as type 'int' for lack of a better
535 // type.
536 FTI.ArgInfo[i].TypeInfo = Context.IntTy.getAsOpaquePtr();
537 }
538 }
539
540 // Since this is a function definition, act as though we have information
541 // about the arguments.
542 FTI.hasPrototype = true;
Chris Lattner2114d5e2006-12-04 07:40:24 +0000543 } else {
544 // FIXME: Diagnose arguments without names in C.
545
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000546 }
547
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000548 Scope *GlobalScope = FnBodyScope->getParent();
549
550 FunctionDecl *FD =
551 static_cast<FunctionDecl*>(ParseDeclarator(GlobalScope, D, 0, 0));
Chris Lattner229ce602006-11-21 01:21:07 +0000552 CurFunctionDecl = FD;
Chris Lattner2114d5e2006-12-04 07:40:24 +0000553
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000554 // Create Decl objects for each parameter, adding them to the FunctionDecl.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000555 llvm::SmallVector<ParmVarDecl*, 16> Params;
Chris Lattnerf61c8a82007-01-21 19:04:43 +0000556
557 // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes
558 // no arguments, not a function that takes a single void argument.
559 if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 &&
560 FTI.ArgInfo[0].TypeInfo == Context.VoidTy.getAsOpaquePtr()) {
Chris Lattner99d31772007-01-21 22:37:37 +0000561 // empty arg list, don't push any params.
Chris Lattnerf61c8a82007-01-21 19:04:43 +0000562 } else {
563 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i)
564 Params.push_back(ParseParamDeclarator(D.getTypeObject(0), i,FnBodyScope));
565 }
Chris Lattner2114d5e2006-12-04 07:40:24 +0000566
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000567 FD->setParams(&Params[0], Params.size());
Chris Lattner2114d5e2006-12-04 07:40:24 +0000568
Chris Lattnere168f762006-11-10 05:29:30 +0000569 return FD;
570}
571
Chris Lattner229ce602006-11-21 01:21:07 +0000572Sema::DeclTy *Sema::ParseFunctionDefBody(DeclTy *D, StmtTy *Body) {
573 FunctionDecl *FD = static_cast<FunctionDecl*>(D);
574 FD->setBody((Stmt*)Body);
575
576 assert(FD == CurFunctionDecl && "Function parsing confused");
577 CurFunctionDecl = 0;
Chris Lattnere2473062007-05-28 06:28:18 +0000578
579 // Verify and clean out per-function state.
580
581 // Check goto/label use.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000582 for (llvm::DenseMap<IdentifierInfo*, LabelStmt*>::iterator
583 I = LabelMap.begin(), E = LabelMap.end(); I != E; ++I) {
Chris Lattnere2473062007-05-28 06:28:18 +0000584 // Verify that we have no forward references left. If so, there was a goto
585 // or address of a label taken, but no definition of it. Label fwd
586 // definitions are indicated with a null substmt.
587 if (I->second->getSubStmt() == 0) {
588 LabelStmt *L = I->second;
589 // Emit error.
Chris Lattnereefa10e2007-05-28 06:56:27 +0000590 Diag(L->getIdentLoc(), diag::err_undeclared_label_use, L->getName());
Chris Lattnere2473062007-05-28 06:28:18 +0000591
592 // At this point, we have gotos that use the bogus label. Stitch it into
593 // the function body so that they aren't leaked and that the AST is well
594 // formed.
595 L->setSubStmt(new NullStmt(L->getIdentLoc()));
596 cast<CompoundStmt>((Stmt*)Body)->push_back(L);
597 }
598 }
599 LabelMap.clear();
600
Chris Lattner229ce602006-11-21 01:21:07 +0000601 return FD;
602}
603
604
Chris Lattnerac18be92006-11-20 06:49:47 +0000605/// ImplicitlyDefineFunction - An undeclared identifier was used in a function
606/// call, forming a call to an implicitly defined function (per C99 6.5.1p2).
607Decl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II,
608 Scope *S) {
609 if (getLangOptions().C99) // Extension in C99.
610 Diag(Loc, diag::ext_implicit_function_decl, II.getName());
611 else // Legal in C90, but warn about it.
612 Diag(Loc, diag::warn_implicit_function_decl, II.getName());
613
614 // FIXME: handle stuff like:
615 // void foo() { extern float X(); }
616 // void bar() { X(); } <-- implicit decl for X in another scope.
617
618 // Set a Declarator for the implicit definition: int foo();
Chris Lattner353f5742006-11-28 04:50:12 +0000619 const char *Dummy;
Chris Lattnerac18be92006-11-20 06:49:47 +0000620 DeclSpec DS;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000621 bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy);
Chris Lattnerb055f2d2007-02-11 08:19:57 +0000622 Error = Error; // Silence warning.
Chris Lattner353f5742006-11-28 04:50:12 +0000623 assert(!Error && "Error setting up implicit decl!");
Chris Lattnerac18be92006-11-20 06:49:47 +0000624 Declarator D(DS, Declarator::BlockContext);
Chris Lattnercbc426d2006-12-02 06:43:02 +0000625 D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, Loc));
Chris Lattnerac18be92006-11-20 06:49:47 +0000626 D.SetIdentifier(&II, Loc);
627
Chris Lattner62d2e662007-01-28 00:21:37 +0000628 // Find translation-unit scope to insert this function into.
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000629 if (Scope *FnS = S->getFnParent())
630 S = FnS->getParent(); // Skip all scopes in a function at once.
Chris Lattner62d2e662007-01-28 00:21:37 +0000631 while (S->getParent())
632 S = S->getParent();
Chris Lattnerac18be92006-11-20 06:49:47 +0000633
Chris Lattner62d2e662007-01-28 00:21:37 +0000634 return static_cast<Decl*>(ParseDeclarator(S, D, 0, 0));
Chris Lattnerac18be92006-11-20 06:49:47 +0000635}
636
Chris Lattner302b4be2006-11-19 02:31:38 +0000637
Chris Lattner776fac82007-06-09 00:53:06 +0000638TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D,
639 Decl *LastDeclarator) {
640 assert(D.getIdentifier() && "Wrong callback for declspec without declarator");
Chris Lattner302b4be2006-11-19 02:31:38 +0000641
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000642 QualType T = GetTypeForDeclarator(D, S);
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000643 if (T.isNull()) return 0;
644
Chris Lattner18b19622007-01-22 07:39:13 +0000645 // Scope manipulation handled by caller.
Chris Lattner776fac82007-06-09 00:53:06 +0000646 return new TypedefDecl(D.getIdentifierLoc(), D.getIdentifier(), T,
647 LastDeclarator);
Chris Lattnere168f762006-11-10 05:29:30 +0000648}
649
Chris Lattner18b19622007-01-22 07:39:13 +0000650
Chris Lattner1300fb92007-01-23 23:42:53 +0000651/// ParseTag - This is invoked when we see 'struct foo' or 'struct {'. In the
652/// former case, Name will be non-null. In the later case, Name will be null.
653/// TagType indicates what kind of tag this is. TK indicates whether this is a
654/// reference/declaration/definition of a tag.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000655Sema::DeclTy *Sema::ParseTag(Scope *S, unsigned TagType, TagKind TK,
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000656 SourceLocation KWLoc, IdentifierInfo *Name,
Steve Naroffb3096442007-06-09 03:47:53 +0000657 SourceLocation NameLoc, AttributeList *Attr) {
Chris Lattner8799cf22007-01-23 01:57:16 +0000658 // If this is a use of an existing tag, it must have a name.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000659 assert((Name != 0 || TK == TK_Definition) &&
660 "Nameless record must be a definition!");
Chris Lattner8799cf22007-01-23 01:57:16 +0000661
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000662 Decl::Kind Kind;
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000663 switch (TagType) {
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000664 default: assert(0 && "Unknown tag type!");
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000665 case DeclSpec::TST_struct: Kind = Decl::Struct; break;
666 case DeclSpec::TST_union: Kind = Decl::Union; break;
667//case DeclSpec::TST_class: Kind = Decl::Class; break;
668 case DeclSpec::TST_enum: Kind = Decl::Enum; break;
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000669 }
Chris Lattner7e783a12007-01-23 02:05:42 +0000670
Chris Lattner18b19622007-01-22 07:39:13 +0000671 // If this is a named struct, check to see if there was a previous forward
672 // declaration or definition.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000673 if (TagDecl *PrevDecl =
Chris Lattner9561a0b2007-01-28 08:20:04 +0000674 dyn_cast_or_null<TagDecl>(LookupScopedDecl(Name, Decl::IDNS_Tag,
675 NameLoc, S))) {
Chris Lattner8799cf22007-01-23 01:57:16 +0000676
677 // If this is a use of a previous tag, or if the tag is already declared in
678 // the same scope (so that the definition/declaration completes or
679 // rementions the tag), reuse the decl.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000680 if (TK == TK_Reference || S->isDeclScope(PrevDecl)) {
Chris Lattner7e783a12007-01-23 02:05:42 +0000681 // Make sure that this wasn't declared as an enum and now used as a struct
682 // or something similar.
683 if (PrevDecl->getKind() != Kind) {
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000684 Diag(KWLoc, diag::err_use_with_wrong_tag, Name->getName());
Chris Lattner7e783a12007-01-23 02:05:42 +0000685 Diag(PrevDecl->getLocation(), diag::err_previous_use);
686 }
Chris Lattner7b9ace62007-01-23 20:11:08 +0000687
688 // If this is a use or a forward declaration, we're good.
689 if (TK != TK_Definition)
690 return PrevDecl;
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000691
Chris Lattner7b9ace62007-01-23 20:11:08 +0000692 // Diagnose attempts to redefine a tag.
693 if (PrevDecl->isDefinition()) {
694 Diag(NameLoc, diag::err_redefinition, Name->getName());
695 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
696 // If this is a redefinition, recover by making this struct be
697 // anonymous, which will make any later references get the previous
698 // definition.
699 Name = 0;
700 } else {
701 // Okay, this is definition of a previously declared or referenced tag.
702 // Move the location of the decl to be the definition site.
703 PrevDecl->setLocation(NameLoc);
Chris Lattner7b9ace62007-01-23 20:11:08 +0000704 return PrevDecl;
705 }
Chris Lattner8799cf22007-01-23 01:57:16 +0000706 }
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000707 // If we get here, this is a definition of a new struct type in a nested
708 // scope, e.g. "struct foo; void bar() { struct foo; }", just create a new
709 // type.
Chris Lattner18b19622007-01-22 07:39:13 +0000710 }
711
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000712 // If there is an identifier, use the location of the identifier as the
713 // location of the decl, otherwise use the location of the struct/union
714 // keyword.
715 SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc;
716
Chris Lattner18b19622007-01-22 07:39:13 +0000717 // Otherwise, if this is the first time we've seen this tag, create the decl.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000718 TagDecl *New;
Chris Lattner720a0542007-01-25 00:44:24 +0000719 switch (Kind) {
720 default: assert(0 && "Unknown tag kind!");
Chris Lattner5f521502007-01-25 06:27:24 +0000721 case Decl::Enum:
Chris Lattner776fac82007-06-09 00:53:06 +0000722 // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.:
723 // enum X { A, B, C } D; D should chain to X.
724 New = new EnumDecl(Loc, Name, 0);
Chris Lattner5f521502007-01-25 06:27:24 +0000725 // If this is an undefined enum, warn.
Chris Lattnerc1915e22007-01-25 07:29:02 +0000726 if (TK != TK_Definition) Diag(Loc, diag::ext_forward_ref_enum);
Chris Lattner5f521502007-01-25 06:27:24 +0000727 break;
Chris Lattner720a0542007-01-25 00:44:24 +0000728 case Decl::Union:
729 case Decl::Struct:
730 case Decl::Class:
Chris Lattner776fac82007-06-09 00:53:06 +0000731 // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.:
732 // struct X { int A; } D; D should chain to X.
733 New = new RecordDecl(Kind, Loc, Name, 0);
Chris Lattner720a0542007-01-25 00:44:24 +0000734 break;
735 }
Chris Lattner18b19622007-01-22 07:39:13 +0000736
737 // If this has an identifier, add it to the scope stack.
738 if (Name) {
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000739 // The scope passed in may not be a decl scope. Zip up the scope tree until
740 // we find one that is.
741 while ((S->getFlags() & Scope::DeclScope) == 0)
742 S = S->getParent();
743
744 // Add it to the decl chain.
Chris Lattner18b19622007-01-22 07:39:13 +0000745 New->setNext(Name->getFETokenInfo<Decl>());
746 Name->setFETokenInfo(New);
747 S->AddDecl(New);
748 }
749
750 return New;
751}
Chris Lattner1300fb92007-01-23 23:42:53 +0000752
753/// ParseField - Each field of a struct/union/class is passed into this in order
754/// to create a FieldDecl object for it.
755Sema::DeclTy *Sema::ParseField(Scope *S, DeclTy *TagDecl,
756 SourceLocation DeclStart,
757 Declarator &D, ExprTy *BitfieldWidth) {
758 IdentifierInfo *II = D.getIdentifier();
759 Expr *BitWidth = (Expr*)BitfieldWidth;
760
761 SourceLocation Loc = DeclStart;
762 if (II) Loc = D.getIdentifierLoc();
763
Chris Lattner62d2e662007-01-28 00:21:37 +0000764 // FIXME: Unnamed fields can be handled in various different ways, for
765 // example, unnamed unions inject all members into the struct namespace!
766
767
Chris Lattner1300fb92007-01-23 23:42:53 +0000768 if (BitWidth) {
769 // TODO: Validate.
Steve Narofff84d11f2007-05-23 21:48:04 +0000770 //printf("WARNING: BITFIELDS IGNORED!\n");
Chris Lattner1300fb92007-01-23 23:42:53 +0000771
772 // 6.7.2.1p3
773 // 6.7.2.1p4
774
775 } else {
776 // Not a bitfield.
777
778 // validate II.
779
780 }
781
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000782 QualType T = GetTypeForDeclarator(D, S);
Chris Lattner01a7c532007-01-25 23:09:03 +0000783 if (T.isNull()) return 0;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000784
785 // C99 6.7.2.1p8: A member of a structure or union may have any type other
786 // than a variably modified type.
Chris Lattner0fd893e2007-07-31 21:33:24 +0000787 if (const ArrayType *ary = T->getAsArrayType()) {
Chris Lattner6046e002007-06-05 06:39:23 +0000788 if (VerifyConstantArrayType(ary, Loc))
Steve Naroff8eeeb132007-05-08 21:09:37 +0000789 return 0;
790 }
Chris Lattner776fac82007-06-09 00:53:06 +0000791
792 // FIXME: Chain fielddecls together.
793 return new FieldDecl(Loc, II, T, 0);
Chris Lattner1300fb92007-01-23 23:42:53 +0000794}
795
796void Sema::ParseRecordBody(SourceLocation RecLoc, DeclTy *RecDecl,
797 DeclTy **Fields, unsigned NumFields) {
Chris Lattnerc1915e22007-01-25 07:29:02 +0000798 RecordDecl *Record = cast<RecordDecl>(static_cast<Decl*>(RecDecl));
Chris Lattner1300fb92007-01-23 23:42:53 +0000799 if (Record->isDefinition()) {
800 // Diagnose code like:
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000801 // struct S { struct S {} X; };
Chris Lattner1300fb92007-01-23 23:42:53 +0000802 // We discover this when we complete the outer S. Reject and ignore the
803 // outer S.
804 Diag(Record->getLocation(), diag::err_nested_redefinition,
805 Record->getKindName());
806 Diag(RecLoc, diag::err_previous_definition);
807 return;
808 }
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000809
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000810 // Verify that all the fields are okay.
Chris Lattner82625602007-01-24 02:26:21 +0000811 unsigned NumNamedMembers = 0;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000812 llvm::SmallVector<FieldDecl*, 32> RecFields;
813 llvm::SmallSet<const IdentifierInfo*, 32> FieldIDs;
Chris Lattnere5a66562007-01-25 22:48:42 +0000814
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000815 for (unsigned i = 0; i != NumFields; ++i) {
816 FieldDecl *FD = cast_or_null<FieldDecl>(static_cast<Decl*>(Fields[i]));
817 if (!FD) continue; // Already issued a diagnostic.
Chris Lattner720a0542007-01-25 00:44:24 +0000818
819 // Get the type for the field.
Chris Lattner0fd893e2007-07-31 21:33:24 +0000820 Type *FDTy = FD->getType().getTypePtr();
Chris Lattner720a0542007-01-25 00:44:24 +0000821
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000822 // C99 6.7.2.1p2 - A field may not be a function type.
Chris Lattner0fd893e2007-07-31 21:33:24 +0000823 if (FDTy->isFunctionType()) {
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000824 Diag(FD->getLocation(), diag::err_field_declared_as_function,
825 FD->getName());
Chris Lattner82625602007-01-24 02:26:21 +0000826 delete FD;
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000827 continue;
828 }
829
Chris Lattner82625602007-01-24 02:26:21 +0000830 // C99 6.7.2.1p2 - A field may not be an incomplete type except...
Chris Lattner720a0542007-01-25 00:44:24 +0000831 if (FDTy->isIncompleteType()) {
Chris Lattner82625602007-01-24 02:26:21 +0000832 if (i != NumFields-1 || // ... that the last member ...
833 Record->getKind() != Decl::Struct || // ... of a structure ...
Chris Lattner0fd893e2007-07-31 21:33:24 +0000834 !FDTy->isArrayType()) { //... may have incomplete array type.
Chris Lattner82625602007-01-24 02:26:21 +0000835 Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName());
836 delete FD;
837 continue;
838 }
Chris Lattner720a0542007-01-25 00:44:24 +0000839 if (NumNamedMembers < 1) { //... must have more than named member ...
Chris Lattner82625602007-01-24 02:26:21 +0000840 Diag(FD->getLocation(), diag::err_flexible_array_empty_struct,
841 FD->getName());
842 delete FD;
843 continue;
844 }
Chris Lattner720a0542007-01-25 00:44:24 +0000845
846 // Okay, we have a legal flexible array member at the end of the struct.
Chris Lattner41943152007-01-25 04:52:46 +0000847 Record->setHasFlexibleArrayMember(true);
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000848 }
Chris Lattner720a0542007-01-25 00:44:24 +0000849
850
851 /// C99 6.7.2.1p2 - a struct ending in a flexible array member cannot be the
852 /// field of another structure or the element of an array.
Chris Lattner0fd893e2007-07-31 21:33:24 +0000853 if (const RecordType *FDTTy = FDTy->getAsRecordType()) {
Chris Lattner720a0542007-01-25 00:44:24 +0000854 if (FDTTy->getDecl()->hasFlexibleArrayMember()) {
855 // If this is a member of a union, then entire union becomes "flexible".
856 if (Record->getKind() == Decl::Union) {
Chris Lattner41943152007-01-25 04:52:46 +0000857 Record->setHasFlexibleArrayMember(true);
Chris Lattner720a0542007-01-25 00:44:24 +0000858 } else {
859 // If this is a struct/class and this is not the last element, reject
860 // it. Note that GCC supports variable sized arrays in the middle of
861 // structures.
862 if (i != NumFields-1) {
863 Diag(FD->getLocation(), diag::err_variable_sized_type_in_struct,
864 FD->getName());
865 delete FD;
866 continue;
867 }
868
869 // We support flexible arrays at the end of structs in other structs
870 // as an extension.
871 Diag(FD->getLocation(), diag::ext_flexible_array_in_struct,
872 FD->getName());
Chris Lattner41943152007-01-25 04:52:46 +0000873 Record->setHasFlexibleArrayMember(true);
Chris Lattner720a0542007-01-25 00:44:24 +0000874 }
875 }
876 }
877
Chris Lattner82625602007-01-24 02:26:21 +0000878 // Keep track of the number of named members.
Chris Lattnere5a66562007-01-25 22:48:42 +0000879 if (IdentifierInfo *II = FD->getIdentifier()) {
880 // Detect duplicate member names.
Chris Lattnerbaf33662007-01-27 02:14:08 +0000881 if (!FieldIDs.insert(II)) {
Chris Lattnere5a66562007-01-25 22:48:42 +0000882 Diag(FD->getLocation(), diag::err_duplicate_member, II->getName());
883 // Find the previous decl.
884 SourceLocation PrevLoc;
885 for (unsigned i = 0, e = RecFields.size(); ; ++i) {
886 assert(i != e && "Didn't find previous def!");
887 if (RecFields[i]->getIdentifier() == II) {
888 PrevLoc = RecFields[i]->getLocation();
889 break;
890 }
891 }
892 Diag(PrevLoc, diag::err_previous_definition);
893 delete FD;
894 continue;
895 }
Chris Lattner82625602007-01-24 02:26:21 +0000896 ++NumNamedMembers;
Chris Lattnere5a66562007-01-25 22:48:42 +0000897 }
Chris Lattner41943152007-01-25 04:52:46 +0000898
899 // Remember good fields.
900 RecFields.push_back(FD);
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000901 }
Chris Lattner82625602007-01-24 02:26:21 +0000902
903
904 // Okay, we successfully defined 'Record'.
Chris Lattner41943152007-01-25 04:52:46 +0000905 Record->defineBody(&RecFields[0], RecFields.size());
Chris Lattner1300fb92007-01-23 23:42:53 +0000906}
907
Chris Lattner4ef40012007-06-11 01:28:17 +0000908Sema::DeclTy *Sema::ParseEnumConstant(Scope *S, DeclTy *theEnumDecl,
909 DeclTy *lastEnumConst,
Chris Lattnerc1915e22007-01-25 07:29:02 +0000910 SourceLocation IdLoc, IdentifierInfo *Id,
Chris Lattner4ef40012007-06-11 01:28:17 +0000911 SourceLocation EqualLoc, ExprTy *val) {
912 theEnumDecl = theEnumDecl; // silence unused warning.
913 EnumConstantDecl *LastEnumConst =
914 cast_or_null<EnumConstantDecl>(static_cast<Decl*>(lastEnumConst));
915 Expr *Val = static_cast<Expr*>(val);
Chris Lattner8116d1b2007-01-25 22:38:29 +0000916
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000917 // The scope passed in may not be a decl scope. Zip up the scope tree until
918 // we find one that is.
919 while ((S->getFlags() & Scope::DeclScope) == 0)
920 S = S->getParent();
921
Chris Lattner8116d1b2007-01-25 22:38:29 +0000922 // Verify that there isn't already something declared with this name in this
923 // scope.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000924 if (Decl *PrevDecl = LookupScopedDecl(Id, Decl::IDNS_Ordinary, IdLoc, S)) {
Chris Lattner8116d1b2007-01-25 22:38:29 +0000925 if (S->isDeclScope(PrevDecl)) {
926 if (isa<EnumConstantDecl>(PrevDecl))
927 Diag(IdLoc, diag::err_redefinition_of_enumerator, Id->getName());
928 else
929 Diag(IdLoc, diag::err_redefinition, Id->getName());
930 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
Chris Lattner4ef40012007-06-11 01:28:17 +0000931 // FIXME: Don't leak memory: delete Val;
Chris Lattner8116d1b2007-01-25 22:38:29 +0000932 return 0;
933 }
934 }
Chris Lattner4ef40012007-06-11 01:28:17 +0000935
Chris Lattner23b7eb62007-06-15 23:05:46 +0000936 llvm::APSInt EnumVal(32);
Chris Lattner4ef40012007-06-11 01:28:17 +0000937 QualType EltTy;
938 if (Val) {
939 // C99 6.7.2.2p2: Make sure we have an integer constant expression.
940 SourceLocation ExpLoc;
Chris Lattner0e9d6222007-07-15 23:26:56 +0000941 if (!Val->isIntegerConstantExpr(EnumVal, Context, &ExpLoc)) {
Chris Lattner4ef40012007-06-11 01:28:17 +0000942 Diag(ExpLoc, diag::err_enum_value_not_integer_constant_expr,
943 Id->getName());
944 // FIXME: Don't leak memory: delete Val;
945 return 0;
946 }
947 EltTy = Val->getType();
948 } else if (LastEnumConst) {
949 // Assign the last value + 1.
950 EnumVal = LastEnumConst->getInitVal();
951 ++EnumVal;
952 // FIXME: detect overflow!
953 EltTy = LastEnumConst->getType();
954 } else {
955 // First value, set to zero.
956 EltTy = Context.IntTy;
957 // FIXME: Resize EnumVal to the size of int.
Steve Naroff63969212007-05-07 21:22:42 +0000958 }
Chris Lattner4ef40012007-06-11 01:28:17 +0000959
960 // TODO: Default promotions to int/uint.
961
962 // TODO: If the result value doesn't fit in an int, it must be a long or long
963 // long value. ISO C does not support this, but GCC does as an extension,
964 // emit a warning.
965
966 EnumConstantDecl *New = new EnumConstantDecl(IdLoc, Id, EltTy, Val, EnumVal,
967 LastEnumConst);
Chris Lattner8116d1b2007-01-25 22:38:29 +0000968
969 // Register this decl in the current scope stack.
970 New->setNext(Id->getFETokenInfo<Decl>());
971 Id->setFETokenInfo(New);
972 S->AddDecl(New);
973 return New;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000974}
975
976void Sema::ParseEnumBody(SourceLocation EnumLoc, DeclTy *EnumDeclX,
977 DeclTy **Elements, unsigned NumElements) {
978 EnumDecl *Enum = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX));
979 assert(!Enum->isDefinition() && "Enum redefinitions can't reach here");
980
Chris Lattner4ef40012007-06-11 01:28:17 +0000981 // Verify that all the values are okay, and reverse the list.
982 EnumConstantDecl *EltList = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000983 for (unsigned i = 0; i != NumElements; ++i) {
984 EnumConstantDecl *ECD =
985 cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i]));
986 if (!ECD) continue; // Already issued a diagnostic.
Chris Lattner4ef40012007-06-11 01:28:17 +0000987
988 ECD->setNextDeclarator(EltList);
989 EltList = ECD;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000990 }
991
Chris Lattner4ef40012007-06-11 01:28:17 +0000992 Enum->defineElements(EltList);
Chris Lattnerc1915e22007-01-25 07:29:02 +0000993}
Chris Lattner1300fb92007-01-23 23:42:53 +0000994
Steve Naroff26c8ea52007-03-21 21:08:52 +0000995void Sema::AddTopLevelDecl(Decl *current, Decl *last) {
996 if (!current) return;
997
998 // If this is a top-level decl that is chained to some other (e.g. int A,B,C;)
999 // remember this in the LastInGroupList list.
Chris Lattner776fac82007-06-09 00:53:06 +00001000 if (last)
Steve Naroff26c8ea52007-03-21 21:08:52 +00001001 LastInGroupList.push_back((Decl*)last);
Steve Naroff26c8ea52007-03-21 21:08:52 +00001002}
Steve Naroffa8fd9732007-06-11 00:35:03 +00001003
1004void Sema::HandleDeclAttribute(Decl *New, AttributeList *rawAttr) {
1005 if (strcmp(rawAttr->getAttributeName()->getName(), "vector_size") == 0) {
1006 if (ValueDecl *vDecl = dyn_cast<ValueDecl>(New)) {
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001007 QualType newType = HandleVectorTypeAttribute(vDecl->getType(), rawAttr);
Steve Naroff4dddb612007-07-09 18:55:26 +00001008 if (!newType.isNull()) // install the new vector type into the decl
1009 vDecl->setType(newType);
Steve Naroffa8fd9732007-06-11 00:35:03 +00001010 }
1011 if (TypedefDecl *tDecl = dyn_cast<TypedefDecl>(New)) {
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001012 QualType newType = HandleVectorTypeAttribute(tDecl->getUnderlyingType(),
1013 rawAttr);
Steve Naroff4dddb612007-07-09 18:55:26 +00001014 if (!newType.isNull()) // install the new vector type into the decl
1015 tDecl->setUnderlyingType(newType);
Steve Naroffa8fd9732007-06-11 00:35:03 +00001016 }
1017 }
Steve Naroff91fcddb2007-07-18 18:00:27 +00001018 if (strcmp(rawAttr->getAttributeName()->getName(), "ocu_vector_type") == 0) {
Steve Naroffddf5a1d2007-07-29 16:33:31 +00001019 if (TypedefDecl *tDecl = dyn_cast<TypedefDecl>(New))
1020 HandleOCUVectorTypeAttribute(tDecl, rawAttr);
1021 else
Steve Naroff91fcddb2007-07-18 18:00:27 +00001022 Diag(rawAttr->getAttributeLoc(),
1023 diag::err_typecheck_ocu_vector_not_typedef);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001024 }
Steve Naroffa8fd9732007-06-11 00:35:03 +00001025 // FIXME: add other attributes...
1026}
1027
1028void Sema::HandleDeclAttributes(Decl *New, AttributeList *declspec_prefix,
1029 AttributeList *declarator_postfix) {
1030 while (declspec_prefix) {
1031 HandleDeclAttribute(New, declspec_prefix);
1032 declspec_prefix = declspec_prefix->getNext();
1033 }
1034 while (declarator_postfix) {
1035 HandleDeclAttribute(New, declarator_postfix);
1036 declarator_postfix = declarator_postfix->getNext();
1037 }
1038}
1039
Steve Naroffddf5a1d2007-07-29 16:33:31 +00001040void Sema::HandleOCUVectorTypeAttribute(TypedefDecl *tDecl,
1041 AttributeList *rawAttr) {
1042 QualType curType = tDecl->getUnderlyingType();
Steve Naroff91fcddb2007-07-18 18:00:27 +00001043 // check the attribute arugments.
1044 if (rawAttr->getNumArgs() != 1) {
1045 Diag(rawAttr->getAttributeLoc(), diag::err_attribute_wrong_number_arguments,
1046 std::string("1"));
Steve Naroffddf5a1d2007-07-29 16:33:31 +00001047 return;
Steve Naroff91fcddb2007-07-18 18:00:27 +00001048 }
1049 Expr *sizeExpr = static_cast<Expr *>(rawAttr->getArg(0));
1050 llvm::APSInt vecSize(32);
1051 if (!sizeExpr->isIntegerConstantExpr(vecSize, Context)) {
1052 Diag(rawAttr->getAttributeLoc(), diag::err_attribute_vector_size_not_int,
1053 sizeExpr->getSourceRange());
Steve Naroffddf5a1d2007-07-29 16:33:31 +00001054 return;
Steve Naroff91fcddb2007-07-18 18:00:27 +00001055 }
1056 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
1057 // in conjunction with complex types (pointers, arrays, functions, etc.).
1058 Type *canonType = curType.getCanonicalType().getTypePtr();
1059 if (!(canonType->isIntegerType() || canonType->isRealFloatingType())) {
1060 Diag(rawAttr->getAttributeLoc(), diag::err_attribute_invalid_vector_type,
1061 curType.getCanonicalType().getAsString());
Steve Naroffddf5a1d2007-07-29 16:33:31 +00001062 return;
Steve Naroff91fcddb2007-07-18 18:00:27 +00001063 }
1064 // unlike gcc's vector_size attribute, the size is specified as the
1065 // number of elements, not the number of bytes.
1066 unsigned vectorSize = vecSize.getZExtValue();
1067
1068 if (vectorSize == 0) {
1069 Diag(rawAttr->getAttributeLoc(), diag::err_attribute_zero_size,
1070 sizeExpr->getSourceRange());
Steve Naroffddf5a1d2007-07-29 16:33:31 +00001071 return;
Steve Naroff91fcddb2007-07-18 18:00:27 +00001072 }
Steve Naroffddf5a1d2007-07-29 16:33:31 +00001073 // Instantiate/Install the vector type, the number of elements is > 0.
1074 tDecl->setUnderlyingType(Context.getOCUVectorType(curType, vectorSize));
1075 // Remember this typedef decl, we will need it later for diagnostics.
1076 OCUVectorDecls.push_back(tDecl);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001077}
1078
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001079QualType Sema::HandleVectorTypeAttribute(QualType curType,
Chris Lattner983a8bb2007-07-13 22:13:22 +00001080 AttributeList *rawAttr) {
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001081 // check the attribute arugments.
Steve Naroffa8fd9732007-06-11 00:35:03 +00001082 if (rawAttr->getNumArgs() != 1) {
1083 Diag(rawAttr->getAttributeLoc(), diag::err_attribute_wrong_number_arguments,
1084 std::string("1"));
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001085 return QualType();
Steve Naroffa8fd9732007-06-11 00:35:03 +00001086 }
1087 Expr *sizeExpr = static_cast<Expr *>(rawAttr->getArg(0));
Chris Lattner23b7eb62007-06-15 23:05:46 +00001088 llvm::APSInt vecSize(32);
Chris Lattner0e9d6222007-07-15 23:26:56 +00001089 if (!sizeExpr->isIntegerConstantExpr(vecSize, Context)) {
Steve Naroffa8fd9732007-06-11 00:35:03 +00001090 Diag(rawAttr->getAttributeLoc(), diag::err_attribute_vector_size_not_int,
1091 sizeExpr->getSourceRange());
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001092 return QualType();
Steve Naroffa8fd9732007-06-11 00:35:03 +00001093 }
1094 // navigate to the base type - we need to provide for vector pointers,
1095 // vector arrays, and functions returning vectors.
1096 Type *canonType = curType.getCanonicalType().getTypePtr();
1097
Steve Naroff91fcddb2007-07-18 18:00:27 +00001098 if (canonType->isPointerType() || canonType->isArrayType() ||
1099 canonType->isFunctionType()) {
1100 assert(1 && "HandleVector(): Complex type construction unimplemented");
1101 /* FIXME: rebuild the type from the inside out, vectorizing the inner type.
1102 do {
1103 if (PointerType *PT = dyn_cast<PointerType>(canonType))
1104 canonType = PT->getPointeeType().getTypePtr();
1105 else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
1106 canonType = AT->getElementType().getTypePtr();
1107 else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
1108 canonType = FT->getResultType().getTypePtr();
1109 } while (canonType->isPointerType() || canonType->isArrayType() ||
1110 canonType->isFunctionType());
1111 */
Steve Naroffa8fd9732007-06-11 00:35:03 +00001112 }
1113 // the base type must be integer or float.
1114 if (!(canonType->isIntegerType() || canonType->isRealFloatingType())) {
1115 Diag(rawAttr->getAttributeLoc(), diag::err_attribute_invalid_vector_type,
1116 curType.getCanonicalType().getAsString());
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001117 return QualType();
Steve Naroffa8fd9732007-06-11 00:35:03 +00001118 }
Chris Lattner4481b422007-07-14 01:29:45 +00001119 unsigned typeSize = Context.getTypeSize(curType, rawAttr->getAttributeLoc());
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001120 // vecSize is specified in bytes - convert to bits.
1121 unsigned vectorSize = vecSize.getZExtValue() * 8;
1122
1123 // the vector size needs to be an integral multiple of the type size.
1124 if (vectorSize % typeSize) {
1125 Diag(rawAttr->getAttributeLoc(), diag::err_attribute_invalid_size,
1126 sizeExpr->getSourceRange());
1127 return QualType();
1128 }
1129 if (vectorSize == 0) {
1130 Diag(rawAttr->getAttributeLoc(), diag::err_attribute_zero_size,
1131 sizeExpr->getSourceRange());
1132 return QualType();
1133 }
1134 // Since OpenCU requires 3 element vectors (OpenCU 5.1.2), we don't restrict
1135 // the number of elements to be a power of two (unlike GCC).
1136 // Instantiate the vector type, the number of elements is > 0.
Steve Naroff91fcddb2007-07-18 18:00:27 +00001137 return Context.getVectorType(curType, vectorSize/typeSize);
Steve Naroffa8fd9732007-06-11 00:35:03 +00001138}
1139