blob: bef5c674296d95eb000f4d7ca08f84d8d5618547 [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"
Steve Naroff45552922007-06-01 21:56:17 +000020#include "clang/AST/Attr.h"
Chris Lattner591a6752006-11-19 23:16:18 +000021#include "clang/Parse/DeclSpec.h"
Chris Lattnere168f762006-11-10 05:29:30 +000022#include "clang/Parse/Scope.h"
23#include "clang/Lex/IdentifierTable.h"
Chris Lattnerac18be92006-11-20 06:49:47 +000024#include "clang/Basic/LangOptions.h"
Chris Lattner9561a0b2007-01-28 08:20:04 +000025#include "clang/Basic/TargetInfo.h"
Chris Lattner38047f92007-01-27 06:24:01 +000026#include "llvm/ADT/SmallSet.h"
Chris Lattner697e5d62006-11-09 06:32:27 +000027using namespace clang;
28
Steve Naroff8eeeb132007-05-08 21:09:37 +000029// C99: 6.7.5p3: Used by ParseDeclarator/ParseField to make sure we have
30// a constant expression of type int with a value greater than zero.
Chris Lattner6046e002007-06-05 06:39:23 +000031bool Sema::VerifyConstantArrayType(const ArrayType *Array,
32 SourceLocation DeclLoc) {
33 const Expr *Size = Array->getSize();
34 if (Size == 0) return false; // incomplete type.
35
36 if (!Size->getType()->isIntegerType()) {
37 Diag(Size->getLocStart(), diag::err_array_size_non_int,
38 Size->getType().getAsString(), Size->getSourceRange());
39 return true;
Steve Naroff8eeeb132007-05-08 21:09:37 +000040 }
Chris Lattner6046e002007-06-05 06:39:23 +000041
42 // Verify that the size of the array is an integer constant expr.
43 SourceLocation Loc;
Chris Lattner23b7eb62007-06-15 23:05:46 +000044 llvm::APSInt SizeVal(32);
Chris Lattner6046e002007-06-05 06:39:23 +000045 if (!Size->isIntegerConstantExpr(SizeVal, &Loc)) {
46 // FIXME: This emits the diagnostic to enforce 6.7.2.1p8, but the message
47 // is wrong. It is also wrong for static variables.
Chris Lattner6d9b49a2007-06-09 05:56:19 +000048 // FIXME: This is also wrong for:
49 // int sub1(int i, char *pi) { typedef int foo[i];
50 // struct bar {foo f1; int f2:3; int f3:4} *p; }
Chris Lattner6046e002007-06-05 06:39:23 +000051 Diag(DeclLoc, diag::err_typecheck_illegal_vla, Size->getSourceRange());
52 return true;
53 }
54
55 // We have a constant expression with an integer type, now make sure
56 // value greater than zero (C99 6.7.5.2p1).
57
58 // FIXME: This check isn't specific to static VLAs, this should be moved
59 // elsewhere or replicated. 'int X[-1];' inside a function should emit an
60 // error.
61 if (SizeVal.isSigned()) {
Chris Lattner23b7eb62007-06-15 23:05:46 +000062 llvm::APSInt Zero(SizeVal.getBitWidth());
Chris Lattner6046e002007-06-05 06:39:23 +000063 Zero.setIsUnsigned(false);
64 if (SizeVal < Zero) {
65 Diag(DeclLoc, diag::err_typecheck_negative_array_size,
66 Size->getSourceRange());
67 return true;
68 } else if (SizeVal == 0) {
69 // GCC accepts zero sized static arrays.
70 Diag(DeclLoc, diag::err_typecheck_zero_array_size,
71 Size->getSourceRange());
72 }
73 }
74 return false;
Steve Naroff8eeeb132007-05-08 21:09:37 +000075}
Chris Lattnere168f762006-11-10 05:29:30 +000076
Chris Lattner2ebe4bb2006-11-20 01:29:42 +000077Sema::DeclTy *Sema::isTypeName(const IdentifierInfo &II, Scope *S) const {
Chris Lattner32d920b2007-01-26 02:01:53 +000078 return dyn_cast_or_null<TypedefDecl>(II.getFETokenInfo<Decl>());
Chris Lattnere168f762006-11-10 05:29:30 +000079}
80
Chris Lattner302b4be2006-11-19 02:31:38 +000081void Sema::PopScope(SourceLocation Loc, Scope *S) {
82 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
83 I != E; ++I) {
Chris Lattner99d31772007-01-21 22:37:37 +000084 Decl *D = static_cast<Decl*>(*I);
Chris Lattner302b4be2006-11-19 02:31:38 +000085 assert(D && "This decl didn't get pushed??");
Chris Lattnerff65b6b2007-01-23 01:33:16 +000086 IdentifierInfo *II = D->getIdentifier();
87 if (!II) continue;
Chris Lattner302b4be2006-11-19 02:31:38 +000088
Chris Lattnerff65b6b2007-01-23 01:33:16 +000089 // Unlink this decl from the identifier. Because the scope contains decls
90 // in an unordered collection, and because we have multiple identifier
91 // namespaces (e.g. tag, normal, label),the decl may not be the first entry.
92 if (II->getFETokenInfo<Decl>() == D) {
93 // Normal case, no multiple decls in different namespaces.
94 II->setFETokenInfo(D->getNext());
95 } else {
96 // Scan ahead. There are only three namespaces in C, so this loop can
97 // never execute more than 3 times.
98 Decl *SomeDecl = II->getFETokenInfo<Decl>();
99 while (SomeDecl->getNext() != D) {
100 SomeDecl = SomeDecl->getNext();
101 assert(SomeDecl && "Didn't find this decl on its identifier's chain!");
102 }
103 SomeDecl->setNext(D->getNext());
104 }
Chris Lattner302b4be2006-11-19 02:31:38 +0000105
Chris Lattner740b2f32006-11-21 01:32:20 +0000106 // This will have to be revisited for C++: there we want to nest stuff in
107 // namespace decls etc. Even for C, we might want a top-level translation
108 // unit decl or something.
109 if (!CurFunctionDecl)
110 continue;
111
112 // Chain this decl to the containing function, it now owns the memory for
113 // the decl.
114 D->setNext(CurFunctionDecl->getDeclChain());
115 CurFunctionDecl->setDeclChain(D);
Chris Lattner302b4be2006-11-19 02:31:38 +0000116 }
117}
118
Chris Lattner18b19622007-01-22 07:39:13 +0000119/// LookupScopedDecl - Look up the inner-most declaration in the specified
120/// namespace.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000121Decl *Sema::LookupScopedDecl(IdentifierInfo *II, unsigned NSI,
122 SourceLocation IdLoc, Scope *S) {
Chris Lattner18b19622007-01-22 07:39:13 +0000123 if (II == 0) return 0;
Chris Lattnerb6738ec2007-01-28 00:38:24 +0000124 Decl::IdentifierNamespace NS = (Decl::IdentifierNamespace)NSI;
Chris Lattner18b19622007-01-22 07:39:13 +0000125
126 // Scan up the scope chain looking for a decl that matches this identifier
127 // that is in the appropriate namespace. This search should not take long, as
128 // shadowing of names is uncommon, and deep shadowing is extremely uncommon.
129 for (Decl *D = II->getFETokenInfo<Decl>(); D; D = D->getNext())
130 if (D->getIdentifierNamespace() == NS)
131 return D;
Chris Lattnerb6738ec2007-01-28 00:38:24 +0000132
Chris Lattner9561a0b2007-01-28 08:20:04 +0000133 // If we didn't find a use of this identifier, and if the identifier
134 // corresponds to a compiler builtin, create the decl object for the builtin
135 // now, injecting it into translation unit scope, and return it.
136 if (NS == Decl::IDNS_Ordinary) {
137 // If this is a builtin on some other target, or if this builtin varies
138 // across targets (e.g. in type), emit a diagnostic and mark the translation
139 // unit non-portable for using it.
140 if (II->isNonPortableBuiltin()) {
141 // Only emit this diagnostic once for this builtin.
142 II->setNonPortableBuiltin(false);
143 Context.Target.DiagnoseNonPortability(IdLoc,
144 diag::port_target_builtin_use);
145 }
Chris Lattner9561a0b2007-01-28 08:20:04 +0000146 // If this is a builtin on this (or all) targets, create the decl.
147 if (unsigned BuiltinID = II->getBuiltinID())
148 return LazilyCreateBuiltin(II, BuiltinID, S);
149 }
Chris Lattner18b19622007-01-22 07:39:13 +0000150 return 0;
151}
152
Chris Lattner9561a0b2007-01-28 08:20:04 +0000153/// LazilyCreateBuiltin - The specified Builtin-ID was first used at file scope.
154/// lazily create a decl for it.
155Decl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid, Scope *S) {
156 Builtin::ID BID = (Builtin::ID)bid;
157
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000158 QualType R = Context.BuiltinInfo.GetBuiltinType(BID, Context);
Chris Lattner776fac82007-06-09 00:53:06 +0000159 FunctionDecl *New = new FunctionDecl(SourceLocation(), II, R,
160 FunctionDecl::Extern, 0);
Chris Lattner9561a0b2007-01-28 08:20:04 +0000161
162 // Find translation-unit scope to insert this function into.
163 while (S->getParent())
164 S = S->getParent();
165 S->AddDecl(New);
166
167 // Add this decl to the end of the identifier info.
168 if (Decl *LastDecl = II->getFETokenInfo<Decl>()) {
169 // Scan until we find the last (outermost) decl in the id chain.
170 while (LastDecl->getNext())
171 LastDecl = LastDecl->getNext();
172 // Insert before (outside) it.
173 LastDecl->setNext(New);
174 } else {
175 II->setFETokenInfo(New);
176 }
177 // Make sure clients iterating over decls see this.
178 LastInGroupList.push_back(New);
179
180 return New;
181}
182
Chris Lattner01564d92007-01-27 19:27:06 +0000183/// MergeTypeDefDecl - We just parsed a typedef 'New' which has the same name
184/// and scope as a previous declaration 'Old'. Figure out how to resolve this
185/// situation, merging decls or emitting diagnostics as appropriate.
186///
Chris Lattnerc511efb2007-01-27 19:32:14 +0000187TypedefDecl *Sema::MergeTypeDefDecl(TypedefDecl *New, Decl *OldD) {
188 // Verify the old decl was also a typedef.
189 TypedefDecl *Old = dyn_cast<TypedefDecl>(OldD);
190 if (!Old) {
191 Diag(New->getLocation(), diag::err_redefinition_different_kind,
192 New->getName());
193 Diag(OldD->getLocation(), diag::err_previous_definition);
194 return New;
195 }
196
Chris Lattner01564d92007-01-27 19:27:06 +0000197 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
198 // TODO: This is totally simplistic. It should handle merging functions
199 // together etc, merging extern int X; int X; ...
200 Diag(New->getLocation(), diag::err_redefinition, New->getName());
201 Diag(Old->getLocation(), diag::err_previous_definition);
202 return New;
203}
204
205/// MergeFunctionDecl - We just parsed a function 'New' which has the same name
206/// and scope as a previous declaration 'Old'. Figure out how to resolve this
207/// situation, merging decls or emitting diagnostics as appropriate.
208///
Chris Lattnerc511efb2007-01-27 19:32:14 +0000209FunctionDecl *Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD) {
210 // Verify the old decl was also a function.
211 FunctionDecl *Old = dyn_cast<FunctionDecl>(OldD);
212 if (!Old) {
213 Diag(New->getLocation(), diag::err_redefinition_different_kind,
214 New->getName());
215 Diag(OldD->getLocation(), diag::err_previous_definition);
216 return New;
217 }
218
Chris Lattnerefe4aea2007-01-27 19:35:39 +0000219 // This is not right, but it's a start. If 'Old' is a function prototype with
220 // the same type as 'New', silently allow this. FIXME: We should link up decl
221 // objects here.
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000222 if (Old->getBody() == 0 &&
223 Old->getCanonicalType() == New->getCanonicalType()) {
Chris Lattnerefe4aea2007-01-27 19:35:39 +0000224 return New;
225 }
Chris Lattnerc511efb2007-01-27 19:32:14 +0000226
Chris Lattner01564d92007-01-27 19:27:06 +0000227 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
228 // TODO: This is totally simplistic. It should handle merging functions
229 // together etc, merging extern int X; int X; ...
230 Diag(New->getLocation(), diag::err_redefinition, New->getName());
231 Diag(Old->getLocation(), diag::err_previous_definition);
232 return New;
233}
234
235/// MergeVarDecl - We just parsed a variable 'New' which has the same name
236/// and scope as a previous declaration 'Old'. Figure out how to resolve this
237/// situation, merging decls or emitting diagnostics as appropriate.
238///
Steve Narofffc49d672007-04-01 21:27:45 +0000239/// FIXME: Need to carefully consider tentative definition rules (C99 6.9.2p2).
240/// For example, we incorrectly complain about i1, i4 from C99 6.9.2p4.
241///
Chris Lattnerc511efb2007-01-27 19:32:14 +0000242VarDecl *Sema::MergeVarDecl(VarDecl *New, Decl *OldD) {
243 // Verify the old decl was also a variable.
244 VarDecl *Old = dyn_cast<VarDecl>(OldD);
245 if (!Old) {
246 Diag(New->getLocation(), diag::err_redefinition_different_kind,
247 New->getName());
248 Diag(OldD->getLocation(), diag::err_previous_definition);
249 return New;
250 }
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000251 // Verify the types match.
252 if (Old->getCanonicalType() != New->getCanonicalType()) {
253 Diag(New->getLocation(), diag::err_redefinition, New->getName());
254 Diag(Old->getLocation(), diag::err_previous_definition);
255 return New;
256 }
257 // We've verified the types match, now check if Old is "extern".
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000258 if (Old->getStorageClass() != VarDecl::Extern) {
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000259 Diag(New->getLocation(), diag::err_redefinition, New->getName());
260 Diag(Old->getLocation(), diag::err_previous_definition);
261 }
Chris Lattner01564d92007-01-27 19:27:06 +0000262 return New;
263}
264
Chris Lattnerb6738ec2007-01-28 00:38:24 +0000265/// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
266/// no declarator (e.g. "struct foo;") is parsed.
267Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) {
268 // TODO: emit error on 'int;' or 'const enum foo;'.
269 // TODO: emit error on 'typedef int;'
270 // if (!DS.isMissingDeclaratorOk()) Diag(...);
271
272 return 0;
273}
274
Chris Lattner776fac82007-06-09 00:53:06 +0000275Sema::DeclTy *
Chris Lattnere168f762006-11-10 05:29:30 +0000276Sema::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
Chris Lattner776fac82007-06-09 00:53:06 +0000277 DeclTy *lastDeclarator) {
278 Decl *LastDeclarator = (Decl*)lastDeclarator;
Chris Lattnere168f762006-11-10 05:29:30 +0000279 IdentifierInfo *II = D.getIdentifier();
Chris Lattner302b4be2006-11-19 02:31:38 +0000280
Chris Lattner01564d92007-01-27 19:27:06 +0000281 // See if this is a redefinition of a variable in the same scope.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000282 Decl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary,
283 D.getIdentifierLoc(), S);
Chris Lattner776fac82007-06-09 00:53:06 +0000284 if (PrevDecl && !S->isDeclScope(PrevDecl))
Chris Lattner01564d92007-01-27 19:27:06 +0000285 PrevDecl = 0; // If in outer scope, it isn't the same thing.
286
Chris Lattnere168f762006-11-10 05:29:30 +0000287 Decl *New;
Chris Lattner01a7c532007-01-25 23:09:03 +0000288 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Chris Lattner776fac82007-06-09 00:53:06 +0000289 TypedefDecl *NewTD = ParseTypedefDecl(S, D, LastDeclarator);
Chris Lattner01564d92007-01-27 19:27:06 +0000290 if (!NewTD) return 0;
Steve Naroffa8fd9732007-06-11 00:35:03 +0000291
292 // Handle attributes prior to checking for duplicates in MergeVarDecl
293 HandleDeclAttributes(NewTD, D.getDeclSpec().getAttributes(),
294 D.getAttributes());
Chris Lattner01564d92007-01-27 19:27:06 +0000295 // Merge the decl with the existing one if appropriate.
296 if (PrevDecl) {
297 NewTD = MergeTypeDefDecl(NewTD, PrevDecl);
298 if (NewTD == 0) return 0;
299 }
300 New = NewTD;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000301 if (S->getParent() == 0) {
302 // C99 6.7.7p2: If a typedef name specifies a variably modified type
303 // then it shall have block scope.
304 if (ArrayType *ary = dyn_cast<ArrayType>(NewTD->getUnderlyingType())) {
Chris Lattner6046e002007-06-05 06:39:23 +0000305 if (VerifyConstantArrayType(ary, D.getIdentifierLoc()))
Steve Naroff8eeeb132007-05-08 21:09:37 +0000306 return 0;
307 }
308 }
Chris Lattner01a7c532007-01-25 23:09:03 +0000309 } else if (D.isFunctionDeclarator()) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000310 QualType R = GetTypeForDeclarator(D, S);
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000311 if (R.isNull()) return 0; // FIXME: "auto func();" passes through...
Chris Lattner01564d92007-01-27 19:27:06 +0000312
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000313 FunctionDecl::StorageClass SC;
314 switch (D.getDeclSpec().getStorageClassSpec()) {
315 default: assert(0 && "Unknown storage class!");
316 case DeclSpec::SCS_auto:
317 case DeclSpec::SCS_register:
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000318 Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_func,
319 R.getAsString());
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000320 return 0;
321 case DeclSpec::SCS_unspecified: SC = FunctionDecl::None; break;
322 case DeclSpec::SCS_extern: SC = FunctionDecl::Extern; break;
323 case DeclSpec::SCS_static: SC = FunctionDecl::Static; break;
324 }
325
Chris Lattner776fac82007-06-09 00:53:06 +0000326 FunctionDecl *NewFD = new FunctionDecl(D.getIdentifierLoc(), II, R, SC,
327 LastDeclarator);
Chris Lattner01564d92007-01-27 19:27:06 +0000328
329 // Merge the decl with the existing one if appropriate.
330 if (PrevDecl) {
331 NewFD = MergeFunctionDecl(NewFD, PrevDecl);
332 if (NewFD == 0) return 0;
333 }
334 New = NewFD;
Chris Lattner01a7c532007-01-25 23:09:03 +0000335 } else {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000336 QualType R = GetTypeForDeclarator(D, S);
Chris Lattner01a7c532007-01-25 23:09:03 +0000337 if (R.isNull()) return 0;
Chris Lattner01564d92007-01-27 19:27:06 +0000338
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000339 VarDecl *NewVD;
340 VarDecl::StorageClass SC;
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000341 switch (D.getDeclSpec().getStorageClassSpec()) {
342 default: assert(0 && "Unknown storage class!");
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000343 case DeclSpec::SCS_unspecified: SC = VarDecl::None; break;
344 case DeclSpec::SCS_extern: SC = VarDecl::Extern; break;
345 case DeclSpec::SCS_static: SC = VarDecl::Static; break;
346 case DeclSpec::SCS_auto: SC = VarDecl::Auto; break;
347 case DeclSpec::SCS_register: SC = VarDecl::Register; break;
348 }
Steve Narofffc49d672007-04-01 21:27:45 +0000349 if (S->getParent() == 0) {
350 // File scope. C99 6.9.2p2: A declaration of an identifier for and
351 // object that has file scope without an initializer, and without a
352 // storage-class specifier or with the storage-class specifier "static",
353 // constitutes a tentative definition. Note: A tentative definition with
354 // external linkage is valid (C99 6.2.2p5).
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000355 if (!Init && SC == VarDecl::Static) {
Steve Narofffc49d672007-04-01 21:27:45 +0000356 // C99 6.9.2p3: If the declaration of an identifier for an object is
357 // a tentative definition and has internal linkage (C99 6.2.2p3), the
358 // declared type shall not be an incomplete type.
359 if (R->isIncompleteType()) {
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000360 Diag(D.getIdentifierLoc(), diag::err_typecheck_decl_incomplete_type,
361 R.getAsString());
Steve Narofffc49d672007-04-01 21:27:45 +0000362 return 0;
363 }
Steve Naroffca8f7122007-04-01 01:41:35 +0000364 }
Bill Wendlingd6de6572007-06-02 09:40:07 +0000365 // C99 6.9p2: The storage-class specifiers auto and register shall not
366 // appear in the declaration specifiers in an external declaration.
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000367 if (SC == VarDecl::Auto || SC == VarDecl::Register) {
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000368 Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope,
369 R.getAsString());
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000370 return 0;
371 }
Steve Naroff8eeeb132007-05-08 21:09:37 +0000372 // C99 6.7.5.2p2: If an identifier is declared to be an object with
373 // static storage duration, it shall not have a variable length array.
Chris Lattner6046e002007-06-05 06:39:23 +0000374 if (ArrayType *ary = dyn_cast<ArrayType>(R.getCanonicalType())) {
375 if (VerifyConstantArrayType(ary, D.getIdentifierLoc()))
Steve Naroff8eeeb132007-05-08 21:09:37 +0000376 return 0;
377 }
Chris Lattner776fac82007-06-09 00:53:06 +0000378 NewVD = new FileVarDecl(D.getIdentifierLoc(), II, R, SC, LastDeclarator);
Steve Narofffc49d672007-04-01 21:27:45 +0000379 } else {
380 // Block scope. C99 6.7p7: If an identifier for an object is declared with
381 // no linkage (C99 6.2.2p6), the type for the object shall be complete...
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000382 if (SC != VarDecl::Extern) {
Steve Narofffc49d672007-04-01 21:27:45 +0000383 if (R->isIncompleteType()) {
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000384 Diag(D.getIdentifierLoc(), diag::err_typecheck_decl_incomplete_type,
385 R.getAsString());
Steve Narofffc49d672007-04-01 21:27:45 +0000386 return 0;
387 }
388 }
Steve Naroff8eeeb132007-05-08 21:09:37 +0000389 if (SC == VarDecl::Static) {
390 // C99 6.7.5.2p2: If an identifier is declared to be an object with
391 // static storage duration, it shall not have a variable length array.
Chris Lattner6046e002007-06-05 06:39:23 +0000392 if (ArrayType *ary = dyn_cast<ArrayType>(R.getCanonicalType())) {
393 if (VerifyConstantArrayType(ary, D.getIdentifierLoc()))
Steve Naroff8eeeb132007-05-08 21:09:37 +0000394 return 0;
395 }
396 }
Chris Lattner776fac82007-06-09 00:53:06 +0000397 NewVD = new BlockVarDecl(D.getIdentifierLoc(), II, R, SC, LastDeclarator);
Steve Naroffef2ab6a2007-04-02 20:50:54 +0000398 }
Steve Naroffa8fd9732007-06-11 00:35:03 +0000399 // Handle attributes prior to checking for duplicates in MergeVarDecl
400 HandleDeclAttributes(NewVD, D.getDeclSpec().getAttributes(),
401 D.getAttributes());
402
Chris Lattner01564d92007-01-27 19:27:06 +0000403 // Merge the decl with the existing one if appropriate.
404 if (PrevDecl) {
405 NewVD = MergeVarDecl(NewVD, PrevDecl);
406 if (NewVD == 0) return 0;
407 }
408 New = NewVD;
Chris Lattner01a7c532007-01-25 23:09:03 +0000409 }
Chris Lattner302b4be2006-11-19 02:31:38 +0000410
Chris Lattnere168f762006-11-10 05:29:30 +0000411 // If this has an identifier, add it to the scope stack.
412 if (II) {
Chris Lattner18b19622007-01-22 07:39:13 +0000413 New->setNext(II->getFETokenInfo<Decl>());
Chris Lattnere168f762006-11-10 05:29:30 +0000414 II->setFETokenInfo(New);
Chris Lattner99d31772007-01-21 22:37:37 +0000415 S->AddDecl(New);
Chris Lattnere168f762006-11-10 05:29:30 +0000416 }
417
Steve Naroff26c8ea52007-03-21 21:08:52 +0000418 if (S->getParent() == 0)
Chris Lattner776fac82007-06-09 00:53:06 +0000419 AddTopLevelDecl(New, LastDeclarator);
Chris Lattnere168f762006-11-10 05:29:30 +0000420
421 return New;
422}
423
Chris Lattner776fac82007-06-09 00:53:06 +0000424/// The declarators are chained together backwards, reverse the list.
425Sema::DeclTy *Sema::FinalizeDeclaratorGroup(Scope *S, DeclTy *group) {
426 // Often we have single declarators, handle them quickly.
427 Decl *Group = static_cast<Decl*>(group);
Chris Lattnerb2dd2412007-06-09 06:16:32 +0000428 if (Group == 0 || Group->getNextDeclarator() == 0) return Group;
Chris Lattner776fac82007-06-09 00:53:06 +0000429
430 Decl *NewGroup = 0;
431 while (Group) {
432 Decl *Next = Group->getNextDeclarator();
433 Group->setNextDeclarator(NewGroup);
434 NewGroup = Group;
435 Group = Next;
436 }
437 return NewGroup;
438}
439
Chris Lattner53621a52007-06-13 20:44:40 +0000440ParmVarDecl *
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000441Sema::ParseParamDeclarator(DeclaratorChunk &FTI, unsigned ArgNo,
442 Scope *FnScope) {
443 const DeclaratorChunk::ParamInfo &PI = FTI.Fun.ArgInfo[ArgNo];
Chris Lattner200bdc32006-11-19 02:43:37 +0000444
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000445 IdentifierInfo *II = PI.Ident;
Chris Lattnerc284e9b2007-01-23 05:14:32 +0000446 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
447 // Can this happen for params? We already checked that they don't conflict
448 // among each other. Here they can only shadow globals, which is ok.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000449 if (Decl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary,
450 PI.IdentLoc, FnScope)) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000451
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000452 }
453
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000454 // FIXME: Handle storage class (auto, register). No declarator?
Chris Lattner776fac82007-06-09 00:53:06 +0000455 // TODO: Chain to previous parameter with the prevdeclarator chain?
Chris Lattner53621a52007-06-13 20:44:40 +0000456 ParmVarDecl *New = new ParmVarDecl(PI.IdentLoc, II,
457 QualType::getFromOpaquePtr(PI.TypeInfo),
458 VarDecl::None, 0);
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000459
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000460 // If this has an identifier, add it to the scope stack.
461 if (II) {
Chris Lattner18b19622007-01-22 07:39:13 +0000462 New->setNext(II->getFETokenInfo<Decl>());
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000463 II->setFETokenInfo(New);
Chris Lattner99d31772007-01-21 22:37:37 +0000464 FnScope->AddDecl(New);
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000465 }
Chris Lattner229ce602006-11-21 01:21:07 +0000466
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000467 return New;
468}
469
470
471Sema::DeclTy *Sema::ParseStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) {
Chris Lattner229ce602006-11-21 01:21:07 +0000472 assert(CurFunctionDecl == 0 && "Function parsing confused");
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000473 assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
474 "Not a function declarator!");
475 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
476
477 // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared'
478 // for a K&R function.
479 if (!FTI.hasPrototype) {
480 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
481 if (FTI.ArgInfo[i].TypeInfo == 0) {
Chris Lattner843c5922007-06-10 23:40:34 +0000482 Diag(FTI.ArgInfo[i].IdentLoc, diag::ext_param_not_declared,
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000483 FTI.ArgInfo[i].Ident->getName());
484 // Implicitly declare the argument as type 'int' for lack of a better
485 // type.
486 FTI.ArgInfo[i].TypeInfo = Context.IntTy.getAsOpaquePtr();
487 }
488 }
489
490 // Since this is a function definition, act as though we have information
491 // about the arguments.
492 FTI.hasPrototype = true;
Chris Lattner2114d5e2006-12-04 07:40:24 +0000493 } else {
494 // FIXME: Diagnose arguments without names in C.
495
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000496 }
497
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000498 Scope *GlobalScope = FnBodyScope->getParent();
499
500 FunctionDecl *FD =
501 static_cast<FunctionDecl*>(ParseDeclarator(GlobalScope, D, 0, 0));
Chris Lattner229ce602006-11-21 01:21:07 +0000502 CurFunctionDecl = FD;
Chris Lattner2114d5e2006-12-04 07:40:24 +0000503
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000504 // Create Decl objects for each parameter, adding them to the FunctionDecl.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000505 llvm::SmallVector<ParmVarDecl*, 16> Params;
Chris Lattnerf61c8a82007-01-21 19:04:43 +0000506
507 // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes
508 // no arguments, not a function that takes a single void argument.
509 if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 &&
510 FTI.ArgInfo[0].TypeInfo == Context.VoidTy.getAsOpaquePtr()) {
Chris Lattner99d31772007-01-21 22:37:37 +0000511 // empty arg list, don't push any params.
Chris Lattnerf61c8a82007-01-21 19:04:43 +0000512 } else {
513 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i)
514 Params.push_back(ParseParamDeclarator(D.getTypeObject(0), i,FnBodyScope));
515 }
Chris Lattner2114d5e2006-12-04 07:40:24 +0000516
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000517 FD->setParams(&Params[0], Params.size());
Chris Lattner2114d5e2006-12-04 07:40:24 +0000518
Chris Lattnere168f762006-11-10 05:29:30 +0000519 return FD;
520}
521
Chris Lattner229ce602006-11-21 01:21:07 +0000522Sema::DeclTy *Sema::ParseFunctionDefBody(DeclTy *D, StmtTy *Body) {
523 FunctionDecl *FD = static_cast<FunctionDecl*>(D);
524 FD->setBody((Stmt*)Body);
525
526 assert(FD == CurFunctionDecl && "Function parsing confused");
527 CurFunctionDecl = 0;
Chris Lattnere2473062007-05-28 06:28:18 +0000528
529 // Verify and clean out per-function state.
530
531 // Check goto/label use.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000532 for (llvm::DenseMap<IdentifierInfo*, LabelStmt*>::iterator
533 I = LabelMap.begin(), E = LabelMap.end(); I != E; ++I) {
Chris Lattnere2473062007-05-28 06:28:18 +0000534 // Verify that we have no forward references left. If so, there was a goto
535 // or address of a label taken, but no definition of it. Label fwd
536 // definitions are indicated with a null substmt.
537 if (I->second->getSubStmt() == 0) {
538 LabelStmt *L = I->second;
539 // Emit error.
Chris Lattnereefa10e2007-05-28 06:56:27 +0000540 Diag(L->getIdentLoc(), diag::err_undeclared_label_use, L->getName());
Chris Lattnere2473062007-05-28 06:28:18 +0000541
542 // At this point, we have gotos that use the bogus label. Stitch it into
543 // the function body so that they aren't leaked and that the AST is well
544 // formed.
545 L->setSubStmt(new NullStmt(L->getIdentLoc()));
546 cast<CompoundStmt>((Stmt*)Body)->push_back(L);
547 }
548 }
549 LabelMap.clear();
550
Chris Lattner229ce602006-11-21 01:21:07 +0000551 return FD;
552}
553
554
Chris Lattnerac18be92006-11-20 06:49:47 +0000555/// ImplicitlyDefineFunction - An undeclared identifier was used in a function
556/// call, forming a call to an implicitly defined function (per C99 6.5.1p2).
557Decl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II,
558 Scope *S) {
559 if (getLangOptions().C99) // Extension in C99.
560 Diag(Loc, diag::ext_implicit_function_decl, II.getName());
561 else // Legal in C90, but warn about it.
562 Diag(Loc, diag::warn_implicit_function_decl, II.getName());
563
564 // FIXME: handle stuff like:
565 // void foo() { extern float X(); }
566 // void bar() { X(); } <-- implicit decl for X in another scope.
567
568 // Set a Declarator for the implicit definition: int foo();
Chris Lattner353f5742006-11-28 04:50:12 +0000569 const char *Dummy;
Chris Lattnerac18be92006-11-20 06:49:47 +0000570 DeclSpec DS;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000571 bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy);
Chris Lattnerb055f2d2007-02-11 08:19:57 +0000572 Error = Error; // Silence warning.
Chris Lattner353f5742006-11-28 04:50:12 +0000573 assert(!Error && "Error setting up implicit decl!");
Chris Lattnerac18be92006-11-20 06:49:47 +0000574 Declarator D(DS, Declarator::BlockContext);
Chris Lattnercbc426d2006-12-02 06:43:02 +0000575 D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, Loc));
Chris Lattnerac18be92006-11-20 06:49:47 +0000576 D.SetIdentifier(&II, Loc);
577
Chris Lattner62d2e662007-01-28 00:21:37 +0000578 // Find translation-unit scope to insert this function into.
579 while (S->getParent())
580 S = S->getParent();
Chris Lattnerac18be92006-11-20 06:49:47 +0000581
Chris Lattner62d2e662007-01-28 00:21:37 +0000582 return static_cast<Decl*>(ParseDeclarator(S, D, 0, 0));
Chris Lattnerac18be92006-11-20 06:49:47 +0000583}
584
Chris Lattner302b4be2006-11-19 02:31:38 +0000585
Chris Lattner776fac82007-06-09 00:53:06 +0000586TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D,
587 Decl *LastDeclarator) {
588 assert(D.getIdentifier() && "Wrong callback for declspec without declarator");
Chris Lattner302b4be2006-11-19 02:31:38 +0000589
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000590 QualType T = GetTypeForDeclarator(D, S);
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000591 if (T.isNull()) return 0;
592
Chris Lattner18b19622007-01-22 07:39:13 +0000593 // Scope manipulation handled by caller.
Chris Lattner776fac82007-06-09 00:53:06 +0000594 return new TypedefDecl(D.getIdentifierLoc(), D.getIdentifier(), T,
595 LastDeclarator);
Chris Lattnere168f762006-11-10 05:29:30 +0000596}
597
Chris Lattner18b19622007-01-22 07:39:13 +0000598
Chris Lattner1300fb92007-01-23 23:42:53 +0000599/// ParseTag - This is invoked when we see 'struct foo' or 'struct {'. In the
600/// former case, Name will be non-null. In the later case, Name will be null.
601/// TagType indicates what kind of tag this is. TK indicates whether this is a
602/// reference/declaration/definition of a tag.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000603Sema::DeclTy *Sema::ParseTag(Scope *S, unsigned TagType, TagKind TK,
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000604 SourceLocation KWLoc, IdentifierInfo *Name,
Steve Naroffb3096442007-06-09 03:47:53 +0000605 SourceLocation NameLoc, AttributeList *Attr) {
Chris Lattner8799cf22007-01-23 01:57:16 +0000606 // If this is a use of an existing tag, it must have a name.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000607 assert((Name != 0 || TK == TK_Definition) &&
608 "Nameless record must be a definition!");
Chris Lattner8799cf22007-01-23 01:57:16 +0000609
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000610 Decl::Kind Kind;
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000611 switch (TagType) {
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000612 default: assert(0 && "Unknown tag type!");
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000613 case DeclSpec::TST_struct: Kind = Decl::Struct; break;
614 case DeclSpec::TST_union: Kind = Decl::Union; break;
615//case DeclSpec::TST_class: Kind = Decl::Class; break;
616 case DeclSpec::TST_enum: Kind = Decl::Enum; break;
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000617 }
Chris Lattner7e783a12007-01-23 02:05:42 +0000618
Chris Lattner18b19622007-01-22 07:39:13 +0000619 // If this is a named struct, check to see if there was a previous forward
620 // declaration or definition.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000621 if (TagDecl *PrevDecl =
Chris Lattner9561a0b2007-01-28 08:20:04 +0000622 dyn_cast_or_null<TagDecl>(LookupScopedDecl(Name, Decl::IDNS_Tag,
623 NameLoc, S))) {
Chris Lattner8799cf22007-01-23 01:57:16 +0000624
625 // If this is a use of a previous tag, or if the tag is already declared in
626 // the same scope (so that the definition/declaration completes or
627 // rementions the tag), reuse the decl.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000628 if (TK == TK_Reference || S->isDeclScope(PrevDecl)) {
Chris Lattner7e783a12007-01-23 02:05:42 +0000629 // Make sure that this wasn't declared as an enum and now used as a struct
630 // or something similar.
631 if (PrevDecl->getKind() != Kind) {
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000632 Diag(KWLoc, diag::err_use_with_wrong_tag, Name->getName());
Chris Lattner7e783a12007-01-23 02:05:42 +0000633 Diag(PrevDecl->getLocation(), diag::err_previous_use);
634 }
Chris Lattner7b9ace62007-01-23 20:11:08 +0000635
636 // If this is a use or a forward declaration, we're good.
637 if (TK != TK_Definition)
638 return PrevDecl;
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000639
Chris Lattner7b9ace62007-01-23 20:11:08 +0000640 // Diagnose attempts to redefine a tag.
641 if (PrevDecl->isDefinition()) {
642 Diag(NameLoc, diag::err_redefinition, Name->getName());
643 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
644 // If this is a redefinition, recover by making this struct be
645 // anonymous, which will make any later references get the previous
646 // definition.
647 Name = 0;
648 } else {
649 // Okay, this is definition of a previously declared or referenced tag.
650 // Move the location of the decl to be the definition site.
651 PrevDecl->setLocation(NameLoc);
Chris Lattner7b9ace62007-01-23 20:11:08 +0000652 return PrevDecl;
653 }
Chris Lattner8799cf22007-01-23 01:57:16 +0000654 }
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000655 // If we get here, this is a definition of a new struct type in a nested
656 // scope, e.g. "struct foo; void bar() { struct foo; }", just create a new
657 // type.
Chris Lattner18b19622007-01-22 07:39:13 +0000658 }
659
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000660 // If there is an identifier, use the location of the identifier as the
661 // location of the decl, otherwise use the location of the struct/union
662 // keyword.
663 SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc;
664
Chris Lattner18b19622007-01-22 07:39:13 +0000665 // Otherwise, if this is the first time we've seen this tag, create the decl.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000666 TagDecl *New;
Chris Lattner720a0542007-01-25 00:44:24 +0000667 switch (Kind) {
668 default: assert(0 && "Unknown tag kind!");
Chris Lattner5f521502007-01-25 06:27:24 +0000669 case Decl::Enum:
Chris Lattner776fac82007-06-09 00:53:06 +0000670 // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.:
671 // enum X { A, B, C } D; D should chain to X.
672 New = new EnumDecl(Loc, Name, 0);
Chris Lattner5f521502007-01-25 06:27:24 +0000673 // If this is an undefined enum, warn.
Chris Lattnerc1915e22007-01-25 07:29:02 +0000674 if (TK != TK_Definition) Diag(Loc, diag::ext_forward_ref_enum);
Chris Lattner5f521502007-01-25 06:27:24 +0000675 break;
Chris Lattner720a0542007-01-25 00:44:24 +0000676 case Decl::Union:
677 case Decl::Struct:
678 case Decl::Class:
Chris Lattner776fac82007-06-09 00:53:06 +0000679 // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.:
680 // struct X { int A; } D; D should chain to X.
681 New = new RecordDecl(Kind, Loc, Name, 0);
Chris Lattner720a0542007-01-25 00:44:24 +0000682 break;
683 }
Chris Lattner18b19622007-01-22 07:39:13 +0000684
685 // If this has an identifier, add it to the scope stack.
686 if (Name) {
687 New->setNext(Name->getFETokenInfo<Decl>());
688 Name->setFETokenInfo(New);
689 S->AddDecl(New);
690 }
691
692 return New;
693}
Chris Lattner1300fb92007-01-23 23:42:53 +0000694
695/// ParseField - Each field of a struct/union/class is passed into this in order
696/// to create a FieldDecl object for it.
697Sema::DeclTy *Sema::ParseField(Scope *S, DeclTy *TagDecl,
698 SourceLocation DeclStart,
699 Declarator &D, ExprTy *BitfieldWidth) {
700 IdentifierInfo *II = D.getIdentifier();
701 Expr *BitWidth = (Expr*)BitfieldWidth;
702
703 SourceLocation Loc = DeclStart;
704 if (II) Loc = D.getIdentifierLoc();
705
Chris Lattner62d2e662007-01-28 00:21:37 +0000706 // FIXME: Unnamed fields can be handled in various different ways, for
707 // example, unnamed unions inject all members into the struct namespace!
708
709
Chris Lattner1300fb92007-01-23 23:42:53 +0000710 if (BitWidth) {
711 // TODO: Validate.
Steve Narofff84d11f2007-05-23 21:48:04 +0000712 //printf("WARNING: BITFIELDS IGNORED!\n");
Chris Lattner1300fb92007-01-23 23:42:53 +0000713
714 // 6.7.2.1p3
715 // 6.7.2.1p4
716
717 } else {
718 // Not a bitfield.
719
720 // validate II.
721
722 }
723
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000724 QualType T = GetTypeForDeclarator(D, S);
Chris Lattner01a7c532007-01-25 23:09:03 +0000725 if (T.isNull()) return 0;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000726
727 // C99 6.7.2.1p8: A member of a structure or union may have any type other
728 // than a variably modified type.
Chris Lattner6046e002007-06-05 06:39:23 +0000729 if (ArrayType *ary = dyn_cast<ArrayType>(T.getCanonicalType())) {
730 if (VerifyConstantArrayType(ary, Loc))
Steve Naroff8eeeb132007-05-08 21:09:37 +0000731 return 0;
732 }
Chris Lattner776fac82007-06-09 00:53:06 +0000733
734 // FIXME: Chain fielddecls together.
735 return new FieldDecl(Loc, II, T, 0);
Chris Lattner1300fb92007-01-23 23:42:53 +0000736}
737
738void Sema::ParseRecordBody(SourceLocation RecLoc, DeclTy *RecDecl,
739 DeclTy **Fields, unsigned NumFields) {
Chris Lattnerc1915e22007-01-25 07:29:02 +0000740 RecordDecl *Record = cast<RecordDecl>(static_cast<Decl*>(RecDecl));
Chris Lattner1300fb92007-01-23 23:42:53 +0000741 if (Record->isDefinition()) {
742 // Diagnose code like:
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000743 // struct S { struct S {} X; };
Chris Lattner1300fb92007-01-23 23:42:53 +0000744 // We discover this when we complete the outer S. Reject and ignore the
745 // outer S.
746 Diag(Record->getLocation(), diag::err_nested_redefinition,
747 Record->getKindName());
748 Diag(RecLoc, diag::err_previous_definition);
749 return;
750 }
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000751
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000752 // Verify that all the fields are okay.
Chris Lattner82625602007-01-24 02:26:21 +0000753 unsigned NumNamedMembers = 0;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000754 llvm::SmallVector<FieldDecl*, 32> RecFields;
755 llvm::SmallSet<const IdentifierInfo*, 32> FieldIDs;
Chris Lattnere5a66562007-01-25 22:48:42 +0000756
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000757 for (unsigned i = 0; i != NumFields; ++i) {
758 FieldDecl *FD = cast_or_null<FieldDecl>(static_cast<Decl*>(Fields[i]));
759 if (!FD) continue; // Already issued a diagnostic.
Chris Lattner720a0542007-01-25 00:44:24 +0000760
761 // Get the type for the field.
Steve Naroffd50c88e2007-04-05 21:15:20 +0000762 Type *FDTy = FD->getType().getCanonicalType().getTypePtr();
Chris Lattner720a0542007-01-25 00:44:24 +0000763
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000764 // C99 6.7.2.1p2 - A field may not be a function type.
Chris Lattner720a0542007-01-25 00:44:24 +0000765 if (isa<FunctionType>(FDTy)) {
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000766 Diag(FD->getLocation(), diag::err_field_declared_as_function,
767 FD->getName());
Chris Lattner82625602007-01-24 02:26:21 +0000768 delete FD;
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000769 continue;
770 }
771
Chris Lattner82625602007-01-24 02:26:21 +0000772 // C99 6.7.2.1p2 - A field may not be an incomplete type except...
Chris Lattner720a0542007-01-25 00:44:24 +0000773 if (FDTy->isIncompleteType()) {
Chris Lattner82625602007-01-24 02:26:21 +0000774 if (i != NumFields-1 || // ... that the last member ...
775 Record->getKind() != Decl::Struct || // ... of a structure ...
Chris Lattner720a0542007-01-25 00:44:24 +0000776 !isa<ArrayType>(FDTy)) { //... may have incomplete array type.
Chris Lattner82625602007-01-24 02:26:21 +0000777 Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName());
778 delete FD;
779 continue;
780 }
Chris Lattner720a0542007-01-25 00:44:24 +0000781 if (NumNamedMembers < 1) { //... must have more than named member ...
Chris Lattner82625602007-01-24 02:26:21 +0000782 Diag(FD->getLocation(), diag::err_flexible_array_empty_struct,
783 FD->getName());
784 delete FD;
785 continue;
786 }
Chris Lattner720a0542007-01-25 00:44:24 +0000787
788 // Okay, we have a legal flexible array member at the end of the struct.
Chris Lattner41943152007-01-25 04:52:46 +0000789 Record->setHasFlexibleArrayMember(true);
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000790 }
Chris Lattner720a0542007-01-25 00:44:24 +0000791
792
793 /// C99 6.7.2.1p2 - a struct ending in a flexible array member cannot be the
794 /// field of another structure or the element of an array.
795 if (RecordType *FDTTy = dyn_cast<RecordType>(FDTy)) {
796 if (FDTTy->getDecl()->hasFlexibleArrayMember()) {
797 // If this is a member of a union, then entire union becomes "flexible".
798 if (Record->getKind() == Decl::Union) {
Chris Lattner41943152007-01-25 04:52:46 +0000799 Record->setHasFlexibleArrayMember(true);
Chris Lattner720a0542007-01-25 00:44:24 +0000800 } else {
801 // If this is a struct/class and this is not the last element, reject
802 // it. Note that GCC supports variable sized arrays in the middle of
803 // structures.
804 if (i != NumFields-1) {
805 Diag(FD->getLocation(), diag::err_variable_sized_type_in_struct,
806 FD->getName());
807 delete FD;
808 continue;
809 }
810
811 // We support flexible arrays at the end of structs in other structs
812 // as an extension.
813 Diag(FD->getLocation(), diag::ext_flexible_array_in_struct,
814 FD->getName());
Chris Lattner41943152007-01-25 04:52:46 +0000815 Record->setHasFlexibleArrayMember(true);
Chris Lattner720a0542007-01-25 00:44:24 +0000816 }
817 }
818 }
819
Chris Lattner82625602007-01-24 02:26:21 +0000820 // Keep track of the number of named members.
Chris Lattnere5a66562007-01-25 22:48:42 +0000821 if (IdentifierInfo *II = FD->getIdentifier()) {
822 // Detect duplicate member names.
Chris Lattnerbaf33662007-01-27 02:14:08 +0000823 if (!FieldIDs.insert(II)) {
Chris Lattnere5a66562007-01-25 22:48:42 +0000824 Diag(FD->getLocation(), diag::err_duplicate_member, II->getName());
825 // Find the previous decl.
826 SourceLocation PrevLoc;
827 for (unsigned i = 0, e = RecFields.size(); ; ++i) {
828 assert(i != e && "Didn't find previous def!");
829 if (RecFields[i]->getIdentifier() == II) {
830 PrevLoc = RecFields[i]->getLocation();
831 break;
832 }
833 }
834 Diag(PrevLoc, diag::err_previous_definition);
835 delete FD;
836 continue;
837 }
Chris Lattner82625602007-01-24 02:26:21 +0000838 ++NumNamedMembers;
Chris Lattnere5a66562007-01-25 22:48:42 +0000839 }
Chris Lattner41943152007-01-25 04:52:46 +0000840
841 // Remember good fields.
842 RecFields.push_back(FD);
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000843 }
Chris Lattner82625602007-01-24 02:26:21 +0000844
845
846 // Okay, we successfully defined 'Record'.
Chris Lattner41943152007-01-25 04:52:46 +0000847 Record->defineBody(&RecFields[0], RecFields.size());
Chris Lattner1300fb92007-01-23 23:42:53 +0000848}
849
Chris Lattner4ef40012007-06-11 01:28:17 +0000850Sema::DeclTy *Sema::ParseEnumConstant(Scope *S, DeclTy *theEnumDecl,
851 DeclTy *lastEnumConst,
Chris Lattnerc1915e22007-01-25 07:29:02 +0000852 SourceLocation IdLoc, IdentifierInfo *Id,
Chris Lattner4ef40012007-06-11 01:28:17 +0000853 SourceLocation EqualLoc, ExprTy *val) {
854 theEnumDecl = theEnumDecl; // silence unused warning.
855 EnumConstantDecl *LastEnumConst =
856 cast_or_null<EnumConstantDecl>(static_cast<Decl*>(lastEnumConst));
857 Expr *Val = static_cast<Expr*>(val);
Chris Lattner8116d1b2007-01-25 22:38:29 +0000858
859 // Verify that there isn't already something declared with this name in this
860 // scope.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000861 if (Decl *PrevDecl = LookupScopedDecl(Id, Decl::IDNS_Ordinary, IdLoc, S)) {
Chris Lattner8116d1b2007-01-25 22:38:29 +0000862 if (S->isDeclScope(PrevDecl)) {
863 if (isa<EnumConstantDecl>(PrevDecl))
864 Diag(IdLoc, diag::err_redefinition_of_enumerator, Id->getName());
865 else
866 Diag(IdLoc, diag::err_redefinition, Id->getName());
867 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
Chris Lattner4ef40012007-06-11 01:28:17 +0000868 // FIXME: Don't leak memory: delete Val;
Chris Lattner8116d1b2007-01-25 22:38:29 +0000869 return 0;
870 }
871 }
Chris Lattner4ef40012007-06-11 01:28:17 +0000872
Chris Lattner23b7eb62007-06-15 23:05:46 +0000873 llvm::APSInt EnumVal(32);
Chris Lattner4ef40012007-06-11 01:28:17 +0000874 QualType EltTy;
875 if (Val) {
876 // C99 6.7.2.2p2: Make sure we have an integer constant expression.
877 SourceLocation ExpLoc;
878 if (!Val->isIntegerConstantExpr(EnumVal, &ExpLoc)) {
879 Diag(ExpLoc, diag::err_enum_value_not_integer_constant_expr,
880 Id->getName());
881 // FIXME: Don't leak memory: delete Val;
882 return 0;
883 }
884 EltTy = Val->getType();
885 } else if (LastEnumConst) {
886 // Assign the last value + 1.
887 EnumVal = LastEnumConst->getInitVal();
888 ++EnumVal;
889 // FIXME: detect overflow!
890 EltTy = LastEnumConst->getType();
891 } else {
892 // First value, set to zero.
893 EltTy = Context.IntTy;
894 // FIXME: Resize EnumVal to the size of int.
Steve Naroff63969212007-05-07 21:22:42 +0000895 }
Chris Lattner4ef40012007-06-11 01:28:17 +0000896
897 // TODO: Default promotions to int/uint.
898
899 // TODO: If the result value doesn't fit in an int, it must be a long or long
900 // long value. ISO C does not support this, but GCC does as an extension,
901 // emit a warning.
902
903 EnumConstantDecl *New = new EnumConstantDecl(IdLoc, Id, EltTy, Val, EnumVal,
904 LastEnumConst);
Chris Lattner8116d1b2007-01-25 22:38:29 +0000905
906 // Register this decl in the current scope stack.
907 New->setNext(Id->getFETokenInfo<Decl>());
908 Id->setFETokenInfo(New);
909 S->AddDecl(New);
910 return New;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000911}
912
913void Sema::ParseEnumBody(SourceLocation EnumLoc, DeclTy *EnumDeclX,
914 DeclTy **Elements, unsigned NumElements) {
915 EnumDecl *Enum = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX));
916 assert(!Enum->isDefinition() && "Enum redefinitions can't reach here");
917
Chris Lattner4ef40012007-06-11 01:28:17 +0000918 // Verify that all the values are okay, and reverse the list.
919 EnumConstantDecl *EltList = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000920 for (unsigned i = 0; i != NumElements; ++i) {
921 EnumConstantDecl *ECD =
922 cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i]));
923 if (!ECD) continue; // Already issued a diagnostic.
Chris Lattner4ef40012007-06-11 01:28:17 +0000924
925 ECD->setNextDeclarator(EltList);
926 EltList = ECD;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000927 }
928
Chris Lattner4ef40012007-06-11 01:28:17 +0000929 Enum->defineElements(EltList);
Chris Lattnerc1915e22007-01-25 07:29:02 +0000930}
Chris Lattner1300fb92007-01-23 23:42:53 +0000931
Steve Naroff26c8ea52007-03-21 21:08:52 +0000932void Sema::AddTopLevelDecl(Decl *current, Decl *last) {
933 if (!current) return;
934
935 // If this is a top-level decl that is chained to some other (e.g. int A,B,C;)
936 // remember this in the LastInGroupList list.
Chris Lattner776fac82007-06-09 00:53:06 +0000937 if (last)
Steve Naroff26c8ea52007-03-21 21:08:52 +0000938 LastInGroupList.push_back((Decl*)last);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000939}
Steve Naroffa8fd9732007-06-11 00:35:03 +0000940
941void Sema::HandleDeclAttribute(Decl *New, AttributeList *rawAttr) {
942 if (strcmp(rawAttr->getAttributeName()->getName(), "vector_size") == 0) {
943 if (ValueDecl *vDecl = dyn_cast<ValueDecl>(New)) {
Steve Naroff4ae0ac62007-07-06 23:09:18 +0000944 QualType newType = HandleVectorTypeAttribute(vDecl->getType(), rawAttr);
Steve Naroff4dddb612007-07-09 18:55:26 +0000945 if (!newType.isNull()) // install the new vector type into the decl
946 vDecl->setType(newType);
Steve Naroffa8fd9732007-06-11 00:35:03 +0000947 }
948 if (TypedefDecl *tDecl = dyn_cast<TypedefDecl>(New)) {
Steve Naroff4ae0ac62007-07-06 23:09:18 +0000949 QualType newType = HandleVectorTypeAttribute(tDecl->getUnderlyingType(),
950 rawAttr);
Steve Naroff4dddb612007-07-09 18:55:26 +0000951 if (!newType.isNull()) // install the new vector type into the decl
952 tDecl->setUnderlyingType(newType);
Steve Naroffa8fd9732007-06-11 00:35:03 +0000953 }
954 }
955 // FIXME: add other attributes...
956}
957
958void Sema::HandleDeclAttributes(Decl *New, AttributeList *declspec_prefix,
959 AttributeList *declarator_postfix) {
960 while (declspec_prefix) {
961 HandleDeclAttribute(New, declspec_prefix);
962 declspec_prefix = declspec_prefix->getNext();
963 }
964 while (declarator_postfix) {
965 HandleDeclAttribute(New, declarator_postfix);
966 declarator_postfix = declarator_postfix->getNext();
967 }
968}
969
Steve Naroff4ae0ac62007-07-06 23:09:18 +0000970QualType Sema::HandleVectorTypeAttribute(QualType curType,
Steve Naroffa8fd9732007-06-11 00:35:03 +0000971 AttributeList *rawAttr) {
Steve Naroff4ae0ac62007-07-06 23:09:18 +0000972 // check the attribute arugments.
Steve Naroffa8fd9732007-06-11 00:35:03 +0000973 if (rawAttr->getNumArgs() != 1) {
974 Diag(rawAttr->getAttributeLoc(), diag::err_attribute_wrong_number_arguments,
975 std::string("1"));
Steve Naroff4ae0ac62007-07-06 23:09:18 +0000976 return QualType();
Steve Naroffa8fd9732007-06-11 00:35:03 +0000977 }
978 Expr *sizeExpr = static_cast<Expr *>(rawAttr->getArg(0));
Chris Lattner23b7eb62007-06-15 23:05:46 +0000979 llvm::APSInt vecSize(32);
Steve Naroffa8fd9732007-06-11 00:35:03 +0000980 if (!sizeExpr->isIntegerConstantExpr(vecSize)) {
981 Diag(rawAttr->getAttributeLoc(), diag::err_attribute_vector_size_not_int,
982 sizeExpr->getSourceRange());
Steve Naroff4ae0ac62007-07-06 23:09:18 +0000983 return QualType();
Steve Naroffa8fd9732007-06-11 00:35:03 +0000984 }
985 // navigate to the base type - we need to provide for vector pointers,
986 // vector arrays, and functions returning vectors.
987 Type *canonType = curType.getCanonicalType().getTypePtr();
988
989 while (canonType->isPointerType() || canonType->isArrayType() ||
990 canonType->isFunctionType()) {
991 if (PointerType *PT = dyn_cast<PointerType>(canonType))
992 canonType = PT->getPointeeType().getTypePtr();
993 else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
994 canonType = AT->getElementType().getTypePtr();
995 else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
996 canonType = FT->getResultType().getTypePtr();
997 }
998 // the base type must be integer or float.
999 if (!(canonType->isIntegerType() || canonType->isRealFloatingType())) {
1000 Diag(rawAttr->getAttributeLoc(), diag::err_attribute_invalid_vector_type,
1001 curType.getCanonicalType().getAsString());
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001002 return QualType();
Steve Naroffa8fd9732007-06-11 00:35:03 +00001003 }
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001004 BuiltinType *baseType = cast<BuiltinType>(canonType);
1005 unsigned typeSize = baseType->getSize();
1006 // vecSize is specified in bytes - convert to bits.
1007 unsigned vectorSize = vecSize.getZExtValue() * 8;
1008
1009 // the vector size needs to be an integral multiple of the type size.
1010 if (vectorSize % typeSize) {
1011 Diag(rawAttr->getAttributeLoc(), diag::err_attribute_invalid_size,
1012 sizeExpr->getSourceRange());
1013 return QualType();
1014 }
1015 if (vectorSize == 0) {
1016 Diag(rawAttr->getAttributeLoc(), diag::err_attribute_zero_size,
1017 sizeExpr->getSourceRange());
1018 return QualType();
1019 }
1020 // Since OpenCU requires 3 element vectors (OpenCU 5.1.2), we don't restrict
1021 // the number of elements to be a power of two (unlike GCC).
1022 // Instantiate the vector type, the number of elements is > 0.
1023 return Context.convertToVectorType(curType, vectorSize/typeSize);
Steve Naroffa8fd9732007-06-11 00:35:03 +00001024}
1025