blob: c1264050d856fbc59942112a09aab061b9e6c06e [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"
Steve Naroffd50c88e2007-04-05 21:15:20 +000027
Chris Lattner697e5d62006-11-09 06:32:27 +000028using namespace llvm;
29using namespace clang;
30
Steve Naroff8eeeb132007-05-08 21:09:37 +000031// C99: 6.7.5p3: Used by ParseDeclarator/ParseField to make sure we have
32// a constant expression of type int with a value greater than zero.
Chris Lattner6046e002007-06-05 06:39:23 +000033bool Sema::VerifyConstantArrayType(const ArrayType *Array,
34 SourceLocation DeclLoc) {
35 const Expr *Size = Array->getSize();
36 if (Size == 0) return false; // incomplete type.
37
38 if (!Size->getType()->isIntegerType()) {
39 Diag(Size->getLocStart(), diag::err_array_size_non_int,
40 Size->getType().getAsString(), Size->getSourceRange());
41 return true;
Steve Naroff8eeeb132007-05-08 21:09:37 +000042 }
Chris Lattner6046e002007-06-05 06:39:23 +000043
44 // Verify that the size of the array is an integer constant expr.
45 SourceLocation Loc;
46 APSInt SizeVal(32);
47 if (!Size->isIntegerConstantExpr(SizeVal, &Loc)) {
48 // FIXME: This emits the diagnostic to enforce 6.7.2.1p8, but the message
49 // is wrong. It is also wrong for static variables.
50 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()) {
61 APSInt Zero(SizeVal.getBitWidth());
62 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) {
81 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
82 I != E; ++I) {
Chris Lattner99d31772007-01-21 22:37:37 +000083 Decl *D = static_cast<Decl*>(*I);
Chris Lattner302b4be2006-11-19 02:31:38 +000084 assert(D && "This decl didn't get pushed??");
Chris Lattnerff65b6b2007-01-23 01:33:16 +000085 IdentifierInfo *II = D->getIdentifier();
86 if (!II) continue;
Chris Lattner302b4be2006-11-19 02:31:38 +000087
Chris Lattnerff65b6b2007-01-23 01:33:16 +000088 // Unlink this decl from the identifier. Because the scope contains decls
89 // in an unordered collection, and because we have multiple identifier
90 // namespaces (e.g. tag, normal, label),the decl may not be the first entry.
91 if (II->getFETokenInfo<Decl>() == D) {
92 // Normal case, no multiple decls in different namespaces.
93 II->setFETokenInfo(D->getNext());
94 } else {
95 // Scan ahead. There are only three namespaces in C, so this loop can
96 // never execute more than 3 times.
97 Decl *SomeDecl = II->getFETokenInfo<Decl>();
98 while (SomeDecl->getNext() != D) {
99 SomeDecl = SomeDecl->getNext();
100 assert(SomeDecl && "Didn't find this decl on its identifier's chain!");
101 }
102 SomeDecl->setNext(D->getNext());
103 }
Chris Lattner302b4be2006-11-19 02:31:38 +0000104
Chris Lattner740b2f32006-11-21 01:32:20 +0000105 // This will have to be revisited for C++: there we want to nest stuff in
106 // namespace decls etc. Even for C, we might want a top-level translation
107 // unit decl or something.
108 if (!CurFunctionDecl)
109 continue;
110
111 // Chain this decl to the containing function, it now owns the memory for
112 // the decl.
113 D->setNext(CurFunctionDecl->getDeclChain());
114 CurFunctionDecl->setDeclChain(D);
Chris Lattner302b4be2006-11-19 02:31:38 +0000115 }
116}
117
Chris Lattner18b19622007-01-22 07:39:13 +0000118/// LookupScopedDecl - Look up the inner-most declaration in the specified
119/// namespace.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000120Decl *Sema::LookupScopedDecl(IdentifierInfo *II, unsigned NSI,
121 SourceLocation IdLoc, Scope *S) {
Chris Lattner18b19622007-01-22 07:39:13 +0000122 if (II == 0) return 0;
Chris Lattnerb6738ec2007-01-28 00:38:24 +0000123 Decl::IdentifierNamespace NS = (Decl::IdentifierNamespace)NSI;
Chris Lattner18b19622007-01-22 07:39:13 +0000124
125 // Scan up the scope chain looking for a decl that matches this identifier
126 // that is in the appropriate namespace. This search should not take long, as
127 // shadowing of names is uncommon, and deep shadowing is extremely uncommon.
128 for (Decl *D = II->getFETokenInfo<Decl>(); D; D = D->getNext())
129 if (D->getIdentifierNamespace() == NS)
130 return D;
Chris Lattnerb6738ec2007-01-28 00:38:24 +0000131
Chris Lattner9561a0b2007-01-28 08:20:04 +0000132 // If we didn't find a use of this identifier, and if the identifier
133 // corresponds to a compiler builtin, create the decl object for the builtin
134 // now, injecting it into translation unit scope, and return it.
135 if (NS == Decl::IDNS_Ordinary) {
136 // If this is a builtin on some other target, or if this builtin varies
137 // across targets (e.g. in type), emit a diagnostic and mark the translation
138 // unit non-portable for using it.
139 if (II->isNonPortableBuiltin()) {
140 // Only emit this diagnostic once for this builtin.
141 II->setNonPortableBuiltin(false);
142 Context.Target.DiagnoseNonPortability(IdLoc,
143 diag::port_target_builtin_use);
144 }
Chris Lattner9561a0b2007-01-28 08:20:04 +0000145 // If this is a builtin on this (or all) targets, create the decl.
146 if (unsigned BuiltinID = II->getBuiltinID())
147 return LazilyCreateBuiltin(II, BuiltinID, S);
148 }
Chris Lattner18b19622007-01-22 07:39:13 +0000149 return 0;
150}
151
Chris Lattner9561a0b2007-01-28 08:20:04 +0000152/// LazilyCreateBuiltin - The specified Builtin-ID was first used at file scope.
153/// lazily create a decl for it.
154Decl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid, Scope *S) {
155 Builtin::ID BID = (Builtin::ID)bid;
156
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000157 QualType R = Context.BuiltinInfo.GetBuiltinType(BID, Context);
Chris Lattner9561a0b2007-01-28 08:20:04 +0000158 FunctionDecl *New = new FunctionDecl(SourceLocation(), II, R);
159
160 // Find translation-unit scope to insert this function into.
161 while (S->getParent())
162 S = S->getParent();
163 S->AddDecl(New);
164
165 // Add this decl to the end of the identifier info.
166 if (Decl *LastDecl = II->getFETokenInfo<Decl>()) {
167 // Scan until we find the last (outermost) decl in the id chain.
168 while (LastDecl->getNext())
169 LastDecl = LastDecl->getNext();
170 // Insert before (outside) it.
171 LastDecl->setNext(New);
172 } else {
173 II->setFETokenInfo(New);
174 }
175 // Make sure clients iterating over decls see this.
176 LastInGroupList.push_back(New);
177
178 return New;
179}
180
Chris Lattner01564d92007-01-27 19:27:06 +0000181/// MergeTypeDefDecl - We just parsed a typedef 'New' which has the same name
182/// and scope as a previous declaration 'Old'. Figure out how to resolve this
183/// situation, merging decls or emitting diagnostics as appropriate.
184///
Chris Lattnerc511efb2007-01-27 19:32:14 +0000185TypedefDecl *Sema::MergeTypeDefDecl(TypedefDecl *New, Decl *OldD) {
186 // Verify the old decl was also a typedef.
187 TypedefDecl *Old = dyn_cast<TypedefDecl>(OldD);
188 if (!Old) {
189 Diag(New->getLocation(), diag::err_redefinition_different_kind,
190 New->getName());
191 Diag(OldD->getLocation(), diag::err_previous_definition);
192 return New;
193 }
194
Chris Lattner01564d92007-01-27 19:27:06 +0000195 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
196 // TODO: This is totally simplistic. It should handle merging functions
197 // together etc, merging extern int X; int X; ...
198 Diag(New->getLocation(), diag::err_redefinition, New->getName());
199 Diag(Old->getLocation(), diag::err_previous_definition);
200 return New;
201}
202
203/// MergeFunctionDecl - We just parsed a function 'New' which has the same name
204/// and scope as a previous declaration 'Old'. Figure out how to resolve this
205/// situation, merging decls or emitting diagnostics as appropriate.
206///
Chris Lattnerc511efb2007-01-27 19:32:14 +0000207FunctionDecl *Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD) {
208 // Verify the old decl was also a function.
209 FunctionDecl *Old = dyn_cast<FunctionDecl>(OldD);
210 if (!Old) {
211 Diag(New->getLocation(), diag::err_redefinition_different_kind,
212 New->getName());
213 Diag(OldD->getLocation(), diag::err_previous_definition);
214 return New;
215 }
216
Chris Lattnerefe4aea2007-01-27 19:35:39 +0000217 // This is not right, but it's a start. If 'Old' is a function prototype with
218 // the same type as 'New', silently allow this. FIXME: We should link up decl
219 // objects here.
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000220 if (Old->getBody() == 0 &&
221 Old->getCanonicalType() == New->getCanonicalType()) {
Chris Lattnerefe4aea2007-01-27 19:35:39 +0000222 return New;
223 }
Chris Lattnerc511efb2007-01-27 19:32:14 +0000224
Chris Lattner01564d92007-01-27 19:27:06 +0000225 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
226 // TODO: This is totally simplistic. It should handle merging functions
227 // together etc, merging extern int X; int X; ...
228 Diag(New->getLocation(), diag::err_redefinition, New->getName());
229 Diag(Old->getLocation(), diag::err_previous_definition);
230 return New;
231}
232
233/// MergeVarDecl - We just parsed a variable 'New' which has the same name
234/// and scope as a previous declaration 'Old'. Figure out how to resolve this
235/// situation, merging decls or emitting diagnostics as appropriate.
236///
Steve Narofffc49d672007-04-01 21:27:45 +0000237/// FIXME: Need to carefully consider tentative definition rules (C99 6.9.2p2).
238/// For example, we incorrectly complain about i1, i4 from C99 6.9.2p4.
239///
Chris Lattnerc511efb2007-01-27 19:32:14 +0000240VarDecl *Sema::MergeVarDecl(VarDecl *New, Decl *OldD) {
241 // Verify the old decl was also a variable.
242 VarDecl *Old = dyn_cast<VarDecl>(OldD);
243 if (!Old) {
244 Diag(New->getLocation(), diag::err_redefinition_different_kind,
245 New->getName());
246 Diag(OldD->getLocation(), diag::err_previous_definition);
247 return New;
248 }
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000249 // Verify the types match.
250 if (Old->getCanonicalType() != New->getCanonicalType()) {
251 Diag(New->getLocation(), diag::err_redefinition, New->getName());
252 Diag(Old->getLocation(), diag::err_previous_definition);
253 return New;
254 }
255 // We've verified the types match, now check if Old is "extern".
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000256 if (Old->getStorageClass() != VarDecl::Extern) {
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000257 Diag(New->getLocation(), diag::err_redefinition, New->getName());
258 Diag(Old->getLocation(), diag::err_previous_definition);
259 }
Chris Lattner01564d92007-01-27 19:27:06 +0000260 return New;
261}
262
Chris Lattnerb6738ec2007-01-28 00:38:24 +0000263/// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
264/// no declarator (e.g. "struct foo;") is parsed.
265Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) {
266 // TODO: emit error on 'int;' or 'const enum foo;'.
267 // TODO: emit error on 'typedef int;'
268 // if (!DS.isMissingDeclaratorOk()) Diag(...);
269
270 return 0;
271}
272
Chris Lattnere168f762006-11-10 05:29:30 +0000273Action::DeclTy *
274Sema::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
275 DeclTy *LastInGroup) {
276 IdentifierInfo *II = D.getIdentifier();
Chris Lattner302b4be2006-11-19 02:31:38 +0000277
Chris Lattner01564d92007-01-27 19:27:06 +0000278 // See if this is a redefinition of a variable in the same scope.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000279 Decl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary,
280 D.getIdentifierLoc(), S);
Chris Lattner01564d92007-01-27 19:27:06 +0000281 if (!S->isDeclScope(PrevDecl))
282 PrevDecl = 0; // If in outer scope, it isn't the same thing.
283
Chris Lattnere168f762006-11-10 05:29:30 +0000284 Decl *New;
Chris Lattner01a7c532007-01-25 23:09:03 +0000285 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Chris Lattner01564d92007-01-27 19:27:06 +0000286 TypedefDecl *NewTD = ParseTypedefDecl(S, D);
287 if (!NewTD) return 0;
288
289 // Merge the decl with the existing one if appropriate.
290 if (PrevDecl) {
291 NewTD = MergeTypeDefDecl(NewTD, PrevDecl);
292 if (NewTD == 0) return 0;
293 }
294 New = NewTD;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000295 if (S->getParent() == 0) {
296 // C99 6.7.7p2: If a typedef name specifies a variably modified type
297 // then it shall have block scope.
298 if (ArrayType *ary = dyn_cast<ArrayType>(NewTD->getUnderlyingType())) {
Chris Lattner6046e002007-06-05 06:39:23 +0000299 if (VerifyConstantArrayType(ary, D.getIdentifierLoc()))
Steve Naroff8eeeb132007-05-08 21:09:37 +0000300 return 0;
301 }
302 }
Chris Lattner01a7c532007-01-25 23:09:03 +0000303 } else if (D.isFunctionDeclarator()) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000304 QualType R = GetTypeForDeclarator(D, S);
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000305 if (R.isNull()) return 0; // FIXME: "auto func();" passes through...
Chris Lattner01564d92007-01-27 19:27:06 +0000306
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000307 FunctionDecl::StorageClass SC;
308 switch (D.getDeclSpec().getStorageClassSpec()) {
309 default: assert(0 && "Unknown storage class!");
310 case DeclSpec::SCS_auto:
311 case DeclSpec::SCS_register:
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000312 Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_func,
313 R.getAsString());
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000314 return 0;
315 case DeclSpec::SCS_unspecified: SC = FunctionDecl::None; break;
316 case DeclSpec::SCS_extern: SC = FunctionDecl::Extern; break;
317 case DeclSpec::SCS_static: SC = FunctionDecl::Static; break;
318 }
319
320 FunctionDecl *NewFD = new FunctionDecl(D.getIdentifierLoc(), II, R, SC);
Chris Lattner01564d92007-01-27 19:27:06 +0000321
322 // Merge the decl with the existing one if appropriate.
323 if (PrevDecl) {
324 NewFD = MergeFunctionDecl(NewFD, PrevDecl);
325 if (NewFD == 0) return 0;
326 }
327 New = NewFD;
Chris Lattner01a7c532007-01-25 23:09:03 +0000328 } else {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000329 QualType R = GetTypeForDeclarator(D, S);
Chris Lattner01a7c532007-01-25 23:09:03 +0000330 if (R.isNull()) return 0;
Chris Lattner01564d92007-01-27 19:27:06 +0000331
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000332 VarDecl *NewVD;
333 VarDecl::StorageClass SC;
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000334 switch (D.getDeclSpec().getStorageClassSpec()) {
335 default: assert(0 && "Unknown storage class!");
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000336 case DeclSpec::SCS_unspecified: SC = VarDecl::None; break;
337 case DeclSpec::SCS_extern: SC = VarDecl::Extern; break;
338 case DeclSpec::SCS_static: SC = VarDecl::Static; break;
339 case DeclSpec::SCS_auto: SC = VarDecl::Auto; break;
340 case DeclSpec::SCS_register: SC = VarDecl::Register; break;
341 }
Steve Narofffc49d672007-04-01 21:27:45 +0000342 if (S->getParent() == 0) {
343 // File scope. C99 6.9.2p2: A declaration of an identifier for and
344 // object that has file scope without an initializer, and without a
345 // storage-class specifier or with the storage-class specifier "static",
346 // constitutes a tentative definition. Note: A tentative definition with
347 // external linkage is valid (C99 6.2.2p5).
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000348 if (!Init && SC == VarDecl::Static) {
Steve Narofffc49d672007-04-01 21:27:45 +0000349 // C99 6.9.2p3: If the declaration of an identifier for an object is
350 // a tentative definition and has internal linkage (C99 6.2.2p3), the
351 // declared type shall not be an incomplete type.
352 if (R->isIncompleteType()) {
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000353 Diag(D.getIdentifierLoc(), diag::err_typecheck_decl_incomplete_type,
354 R.getAsString());
Steve Narofffc49d672007-04-01 21:27:45 +0000355 return 0;
356 }
Steve Naroffca8f7122007-04-01 01:41:35 +0000357 }
Bill Wendlingd6de6572007-06-02 09:40:07 +0000358 // C99 6.9p2: The storage-class specifiers auto and register shall not
359 // appear in the declaration specifiers in an external declaration.
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000360 if (SC == VarDecl::Auto || SC == VarDecl::Register) {
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000361 Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope,
362 R.getAsString());
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000363 return 0;
364 }
Steve Naroff8eeeb132007-05-08 21:09:37 +0000365 // C99 6.7.5.2p2: If an identifier is declared to be an object with
366 // static storage duration, it shall not have a variable length array.
Chris Lattner6046e002007-06-05 06:39:23 +0000367 if (ArrayType *ary = dyn_cast<ArrayType>(R.getCanonicalType())) {
368 if (VerifyConstantArrayType(ary, D.getIdentifierLoc()))
Steve Naroff8eeeb132007-05-08 21:09:37 +0000369 return 0;
370 }
Steve Naroffef2ab6a2007-04-02 20:50:54 +0000371 NewVD = new FileVarDecl(D.getIdentifierLoc(), II, R, SC);
Steve Narofffc49d672007-04-01 21:27:45 +0000372 } else {
373 // Block scope. C99 6.7p7: If an identifier for an object is declared with
374 // no linkage (C99 6.2.2p6), the type for the object shall be complete...
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000375 if (SC != VarDecl::Extern) {
Steve Narofffc49d672007-04-01 21:27:45 +0000376 if (R->isIncompleteType()) {
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000377 Diag(D.getIdentifierLoc(), diag::err_typecheck_decl_incomplete_type,
378 R.getAsString());
Steve Narofffc49d672007-04-01 21:27:45 +0000379 return 0;
380 }
381 }
Steve Naroff8eeeb132007-05-08 21:09:37 +0000382 if (SC == VarDecl::Static) {
383 // C99 6.7.5.2p2: If an identifier is declared to be an object with
384 // static storage duration, it shall not have a variable length array.
Chris Lattner6046e002007-06-05 06:39:23 +0000385 if (ArrayType *ary = dyn_cast<ArrayType>(R.getCanonicalType())) {
386 if (VerifyConstantArrayType(ary, D.getIdentifierLoc()))
Steve Naroff8eeeb132007-05-08 21:09:37 +0000387 return 0;
388 }
389 }
Steve Naroffef2ab6a2007-04-02 20:50:54 +0000390 NewVD = new BlockVarDecl(D.getIdentifierLoc(), II, R, SC);
391 }
Chris Lattner01564d92007-01-27 19:27:06 +0000392 // Merge the decl with the existing one if appropriate.
393 if (PrevDecl) {
394 NewVD = MergeVarDecl(NewVD, PrevDecl);
395 if (NewVD == 0) return 0;
396 }
397 New = NewVD;
Chris Lattner01a7c532007-01-25 23:09:03 +0000398 }
Chris Lattner302b4be2006-11-19 02:31:38 +0000399
400
Chris Lattnere168f762006-11-10 05:29:30 +0000401 // If this has an identifier, add it to the scope stack.
402 if (II) {
Chris Lattner18b19622007-01-22 07:39:13 +0000403 New->setNext(II->getFETokenInfo<Decl>());
Chris Lattnere168f762006-11-10 05:29:30 +0000404 II->setFETokenInfo(New);
Chris Lattner99d31772007-01-21 22:37:37 +0000405 S->AddDecl(New);
Chris Lattnere168f762006-11-10 05:29:30 +0000406 }
407
Steve Naroff26c8ea52007-03-21 21:08:52 +0000408 if (S->getParent() == 0)
409 AddTopLevelDecl(New, (Decl *)LastInGroup);
Chris Lattnere168f762006-11-10 05:29:30 +0000410
411 return New;
412}
413
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000414VarDecl *
415Sema::ParseParamDeclarator(DeclaratorChunk &FTI, unsigned ArgNo,
416 Scope *FnScope) {
417 const DeclaratorChunk::ParamInfo &PI = FTI.Fun.ArgInfo[ArgNo];
Chris Lattner200bdc32006-11-19 02:43:37 +0000418
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000419 IdentifierInfo *II = PI.Ident;
Chris Lattnerc284e9b2007-01-23 05:14:32 +0000420 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
421 // Can this happen for params? We already checked that they don't conflict
422 // among each other. Here they can only shadow globals, which is ok.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000423 if (Decl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary,
424 PI.IdentLoc, FnScope)) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000425
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000426 }
427
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000428 // FIXME: Handle storage class (auto, register). No declarator?
Steve Naroffef2ab6a2007-04-02 20:50:54 +0000429 VarDecl *New = new ParmVarDecl(PI.IdentLoc, II,
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000430 QualType::getFromOpaquePtr(PI.TypeInfo),
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000431 VarDecl::None);
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000432
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000433 // If this has an identifier, add it to the scope stack.
434 if (II) {
Chris Lattner18b19622007-01-22 07:39:13 +0000435 New->setNext(II->getFETokenInfo<Decl>());
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000436 II->setFETokenInfo(New);
Chris Lattner99d31772007-01-21 22:37:37 +0000437 FnScope->AddDecl(New);
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000438 }
Chris Lattner229ce602006-11-21 01:21:07 +0000439
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000440 return New;
441}
442
443
444Sema::DeclTy *Sema::ParseStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) {
Chris Lattner229ce602006-11-21 01:21:07 +0000445 assert(CurFunctionDecl == 0 && "Function parsing confused");
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000446 assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
447 "Not a function declarator!");
448 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
449
450 // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared'
451 // for a K&R function.
452 if (!FTI.hasPrototype) {
453 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
454 if (FTI.ArgInfo[i].TypeInfo == 0) {
455 Diag(FTI.ArgInfo[i].IdentLoc, diag::err_param_not_declared,
456 FTI.ArgInfo[i].Ident->getName());
457 // Implicitly declare the argument as type 'int' for lack of a better
458 // type.
459 FTI.ArgInfo[i].TypeInfo = Context.IntTy.getAsOpaquePtr();
460 }
461 }
462
463 // Since this is a function definition, act as though we have information
464 // about the arguments.
465 FTI.hasPrototype = true;
Chris Lattner2114d5e2006-12-04 07:40:24 +0000466 } else {
467 // FIXME: Diagnose arguments without names in C.
468
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000469 }
470
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000471 Scope *GlobalScope = FnBodyScope->getParent();
472
473 FunctionDecl *FD =
474 static_cast<FunctionDecl*>(ParseDeclarator(GlobalScope, D, 0, 0));
Chris Lattner229ce602006-11-21 01:21:07 +0000475 CurFunctionDecl = FD;
Chris Lattner2114d5e2006-12-04 07:40:24 +0000476
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000477 // Create Decl objects for each parameter, adding them to the FunctionDecl.
478 SmallVector<VarDecl*, 16> Params;
Chris Lattnerf61c8a82007-01-21 19:04:43 +0000479
480 // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes
481 // no arguments, not a function that takes a single void argument.
482 if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 &&
483 FTI.ArgInfo[0].TypeInfo == Context.VoidTy.getAsOpaquePtr()) {
Chris Lattner99d31772007-01-21 22:37:37 +0000484 // empty arg list, don't push any params.
Chris Lattnerf61c8a82007-01-21 19:04:43 +0000485 } else {
486 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i)
487 Params.push_back(ParseParamDeclarator(D.getTypeObject(0), i,FnBodyScope));
488 }
Chris Lattner2114d5e2006-12-04 07:40:24 +0000489
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000490 FD->setParams(&Params[0], Params.size());
Chris Lattner2114d5e2006-12-04 07:40:24 +0000491
Chris Lattnere168f762006-11-10 05:29:30 +0000492 return FD;
493}
494
Chris Lattner229ce602006-11-21 01:21:07 +0000495Sema::DeclTy *Sema::ParseFunctionDefBody(DeclTy *D, StmtTy *Body) {
496 FunctionDecl *FD = static_cast<FunctionDecl*>(D);
497 FD->setBody((Stmt*)Body);
498
499 assert(FD == CurFunctionDecl && "Function parsing confused");
500 CurFunctionDecl = 0;
Chris Lattnere2473062007-05-28 06:28:18 +0000501
502 // Verify and clean out per-function state.
503
504 // Check goto/label use.
505 for (DenseMap<IdentifierInfo*, LabelStmt*>::iterator I = LabelMap.begin(),
506 E = LabelMap.end(); I != E; ++I) {
507 // Verify that we have no forward references left. If so, there was a goto
508 // or address of a label taken, but no definition of it. Label fwd
509 // definitions are indicated with a null substmt.
510 if (I->second->getSubStmt() == 0) {
511 LabelStmt *L = I->second;
512 // Emit error.
Chris Lattnereefa10e2007-05-28 06:56:27 +0000513 Diag(L->getIdentLoc(), diag::err_undeclared_label_use, L->getName());
Chris Lattnere2473062007-05-28 06:28:18 +0000514
515 // At this point, we have gotos that use the bogus label. Stitch it into
516 // the function body so that they aren't leaked and that the AST is well
517 // formed.
518 L->setSubStmt(new NullStmt(L->getIdentLoc()));
519 cast<CompoundStmt>((Stmt*)Body)->push_back(L);
520 }
521 }
522 LabelMap.clear();
523
Chris Lattner229ce602006-11-21 01:21:07 +0000524 return FD;
525}
526
527
Chris Lattnerac18be92006-11-20 06:49:47 +0000528/// ImplicitlyDefineFunction - An undeclared identifier was used in a function
529/// call, forming a call to an implicitly defined function (per C99 6.5.1p2).
530Decl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II,
531 Scope *S) {
532 if (getLangOptions().C99) // Extension in C99.
533 Diag(Loc, diag::ext_implicit_function_decl, II.getName());
534 else // Legal in C90, but warn about it.
535 Diag(Loc, diag::warn_implicit_function_decl, II.getName());
536
537 // FIXME: handle stuff like:
538 // void foo() { extern float X(); }
539 // void bar() { X(); } <-- implicit decl for X in another scope.
540
541 // Set a Declarator for the implicit definition: int foo();
Chris Lattner353f5742006-11-28 04:50:12 +0000542 const char *Dummy;
Chris Lattnerac18be92006-11-20 06:49:47 +0000543 DeclSpec DS;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000544 bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy);
Chris Lattnerb055f2d2007-02-11 08:19:57 +0000545 Error = Error; // Silence warning.
Chris Lattner353f5742006-11-28 04:50:12 +0000546 assert(!Error && "Error setting up implicit decl!");
Chris Lattnerac18be92006-11-20 06:49:47 +0000547 Declarator D(DS, Declarator::BlockContext);
Chris Lattnercbc426d2006-12-02 06:43:02 +0000548 D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, Loc));
Chris Lattnerac18be92006-11-20 06:49:47 +0000549 D.SetIdentifier(&II, Loc);
550
Chris Lattner62d2e662007-01-28 00:21:37 +0000551 // Find translation-unit scope to insert this function into.
552 while (S->getParent())
553 S = S->getParent();
Chris Lattnerac18be92006-11-20 06:49:47 +0000554
Chris Lattner62d2e662007-01-28 00:21:37 +0000555 return static_cast<Decl*>(ParseDeclarator(S, D, 0, 0));
Chris Lattnerac18be92006-11-20 06:49:47 +0000556}
557
Chris Lattner302b4be2006-11-19 02:31:38 +0000558
Chris Lattner01564d92007-01-27 19:27:06 +0000559TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D) {
Chris Lattnerda8aa7b2006-11-19 23:12:30 +0000560 assert(D.getIdentifier() && "Wrong callback for declspec withotu declarator");
Chris Lattner302b4be2006-11-19 02:31:38 +0000561
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000562 QualType T = GetTypeForDeclarator(D, S);
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000563 if (T.isNull()) return 0;
564
Chris Lattner18b19622007-01-22 07:39:13 +0000565 // Scope manipulation handled by caller.
566 return new TypedefDecl(D.getIdentifierLoc(), D.getIdentifier(), T);
Chris Lattnere168f762006-11-10 05:29:30 +0000567}
568
Chris Lattner18b19622007-01-22 07:39:13 +0000569
Chris Lattner1300fb92007-01-23 23:42:53 +0000570/// ParseTag - This is invoked when we see 'struct foo' or 'struct {'. In the
571/// former case, Name will be non-null. In the later case, Name will be null.
572/// TagType indicates what kind of tag this is. TK indicates whether this is a
573/// reference/declaration/definition of a tag.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000574Sema::DeclTy *Sema::ParseTag(Scope *S, unsigned TagType, TagKind TK,
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000575 SourceLocation KWLoc, IdentifierInfo *Name,
576 SourceLocation NameLoc) {
Chris Lattner8799cf22007-01-23 01:57:16 +0000577 // If this is a use of an existing tag, it must have a name.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000578 assert((Name != 0 || TK == TK_Definition) &&
579 "Nameless record must be a definition!");
Chris Lattner8799cf22007-01-23 01:57:16 +0000580
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000581 Decl::Kind Kind;
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000582 switch (TagType) {
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000583 default: assert(0 && "Unknown tag type!");
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000584 case DeclSpec::TST_struct: Kind = Decl::Struct; break;
585 case DeclSpec::TST_union: Kind = Decl::Union; break;
586//case DeclSpec::TST_class: Kind = Decl::Class; break;
587 case DeclSpec::TST_enum: Kind = Decl::Enum; break;
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000588 }
Chris Lattner7e783a12007-01-23 02:05:42 +0000589
Chris Lattner18b19622007-01-22 07:39:13 +0000590 // If this is a named struct, check to see if there was a previous forward
591 // declaration or definition.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000592 if (TagDecl *PrevDecl =
Chris Lattner9561a0b2007-01-28 08:20:04 +0000593 dyn_cast_or_null<TagDecl>(LookupScopedDecl(Name, Decl::IDNS_Tag,
594 NameLoc, S))) {
Chris Lattner8799cf22007-01-23 01:57:16 +0000595
596 // If this is a use of a previous tag, or if the tag is already declared in
597 // the same scope (so that the definition/declaration completes or
598 // rementions the tag), reuse the decl.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000599 if (TK == TK_Reference || S->isDeclScope(PrevDecl)) {
Chris Lattner7e783a12007-01-23 02:05:42 +0000600 // Make sure that this wasn't declared as an enum and now used as a struct
601 // or something similar.
602 if (PrevDecl->getKind() != Kind) {
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000603 Diag(KWLoc, diag::err_use_with_wrong_tag, Name->getName());
Chris Lattner7e783a12007-01-23 02:05:42 +0000604 Diag(PrevDecl->getLocation(), diag::err_previous_use);
605 }
Chris Lattner7b9ace62007-01-23 20:11:08 +0000606
607 // If this is a use or a forward declaration, we're good.
608 if (TK != TK_Definition)
609 return PrevDecl;
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000610
Chris Lattner7b9ace62007-01-23 20:11:08 +0000611 // Diagnose attempts to redefine a tag.
612 if (PrevDecl->isDefinition()) {
613 Diag(NameLoc, diag::err_redefinition, Name->getName());
614 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
615 // If this is a redefinition, recover by making this struct be
616 // anonymous, which will make any later references get the previous
617 // definition.
618 Name = 0;
619 } else {
620 // Okay, this is definition of a previously declared or referenced tag.
621 // Move the location of the decl to be the definition site.
622 PrevDecl->setLocation(NameLoc);
Chris Lattner7b9ace62007-01-23 20:11:08 +0000623 return PrevDecl;
624 }
Chris Lattner8799cf22007-01-23 01:57:16 +0000625 }
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000626 // If we get here, this is a definition of a new struct type in a nested
627 // scope, e.g. "struct foo; void bar() { struct foo; }", just create a new
628 // type.
Chris Lattner18b19622007-01-22 07:39:13 +0000629 }
630
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000631 // If there is an identifier, use the location of the identifier as the
632 // location of the decl, otherwise use the location of the struct/union
633 // keyword.
634 SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc;
635
Chris Lattner18b19622007-01-22 07:39:13 +0000636 // Otherwise, if this is the first time we've seen this tag, create the decl.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000637 TagDecl *New;
Chris Lattner720a0542007-01-25 00:44:24 +0000638 switch (Kind) {
639 default: assert(0 && "Unknown tag kind!");
Chris Lattner5f521502007-01-25 06:27:24 +0000640 case Decl::Enum:
641 New = new EnumDecl(Loc, Name);
642 // If this is an undefined enum, warn.
Chris Lattnerc1915e22007-01-25 07:29:02 +0000643 if (TK != TK_Definition) Diag(Loc, diag::ext_forward_ref_enum);
Chris Lattner5f521502007-01-25 06:27:24 +0000644 break;
Chris Lattner720a0542007-01-25 00:44:24 +0000645 case Decl::Union:
646 case Decl::Struct:
647 case Decl::Class:
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000648 New = new RecordDecl(Kind, Loc, Name);
Chris Lattner720a0542007-01-25 00:44:24 +0000649 break;
650 }
Chris Lattner18b19622007-01-22 07:39:13 +0000651
652 // If this has an identifier, add it to the scope stack.
653 if (Name) {
654 New->setNext(Name->getFETokenInfo<Decl>());
655 Name->setFETokenInfo(New);
656 S->AddDecl(New);
657 }
658
659 return New;
660}
Chris Lattner1300fb92007-01-23 23:42:53 +0000661
662/// ParseField - Each field of a struct/union/class is passed into this in order
663/// to create a FieldDecl object for it.
664Sema::DeclTy *Sema::ParseField(Scope *S, DeclTy *TagDecl,
665 SourceLocation DeclStart,
666 Declarator &D, ExprTy *BitfieldWidth) {
667 IdentifierInfo *II = D.getIdentifier();
668 Expr *BitWidth = (Expr*)BitfieldWidth;
669
670 SourceLocation Loc = DeclStart;
671 if (II) Loc = D.getIdentifierLoc();
672
Chris Lattner62d2e662007-01-28 00:21:37 +0000673 // FIXME: Unnamed fields can be handled in various different ways, for
674 // example, unnamed unions inject all members into the struct namespace!
675
676
Chris Lattner1300fb92007-01-23 23:42:53 +0000677 if (BitWidth) {
678 // TODO: Validate.
Steve Narofff84d11f2007-05-23 21:48:04 +0000679 //printf("WARNING: BITFIELDS IGNORED!\n");
Chris Lattner1300fb92007-01-23 23:42:53 +0000680
681 // 6.7.2.1p3
682 // 6.7.2.1p4
683
684 } else {
685 // Not a bitfield.
686
687 // validate II.
688
689 }
690
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000691 QualType T = GetTypeForDeclarator(D, S);
Chris Lattner01a7c532007-01-25 23:09:03 +0000692 if (T.isNull()) return 0;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000693
694 // C99 6.7.2.1p8: A member of a structure or union may have any type other
695 // than a variably modified type.
Chris Lattner6046e002007-06-05 06:39:23 +0000696 if (ArrayType *ary = dyn_cast<ArrayType>(T.getCanonicalType())) {
697 if (VerifyConstantArrayType(ary, Loc))
Steve Naroff8eeeb132007-05-08 21:09:37 +0000698 return 0;
699 }
Chris Lattner01a7c532007-01-25 23:09:03 +0000700 return new FieldDecl(Loc, II, T);
Chris Lattner1300fb92007-01-23 23:42:53 +0000701}
702
703void Sema::ParseRecordBody(SourceLocation RecLoc, DeclTy *RecDecl,
704 DeclTy **Fields, unsigned NumFields) {
Chris Lattnerc1915e22007-01-25 07:29:02 +0000705 RecordDecl *Record = cast<RecordDecl>(static_cast<Decl*>(RecDecl));
Chris Lattner1300fb92007-01-23 23:42:53 +0000706 if (Record->isDefinition()) {
707 // Diagnose code like:
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000708 // struct S { struct S {} X; };
Chris Lattner1300fb92007-01-23 23:42:53 +0000709 // We discover this when we complete the outer S. Reject and ignore the
710 // outer S.
711 Diag(Record->getLocation(), diag::err_nested_redefinition,
712 Record->getKindName());
713 Diag(RecLoc, diag::err_previous_definition);
714 return;
715 }
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000716
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000717 // Verify that all the fields are okay.
Chris Lattner82625602007-01-24 02:26:21 +0000718 unsigned NumNamedMembers = 0;
Steve Naroffcc321422007-03-26 23:09:51 +0000719 SmallVector<FieldDecl*, 32> RecFields;
Chris Lattnere5a66562007-01-25 22:48:42 +0000720 SmallSet<const IdentifierInfo*, 32> FieldIDs;
721
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000722 for (unsigned i = 0; i != NumFields; ++i) {
723 FieldDecl *FD = cast_or_null<FieldDecl>(static_cast<Decl*>(Fields[i]));
724 if (!FD) continue; // Already issued a diagnostic.
Chris Lattner720a0542007-01-25 00:44:24 +0000725
726 // Get the type for the field.
Steve Naroffd50c88e2007-04-05 21:15:20 +0000727 Type *FDTy = FD->getType().getCanonicalType().getTypePtr();
Chris Lattner720a0542007-01-25 00:44:24 +0000728
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000729 // C99 6.7.2.1p2 - A field may not be a function type.
Chris Lattner720a0542007-01-25 00:44:24 +0000730 if (isa<FunctionType>(FDTy)) {
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000731 Diag(FD->getLocation(), diag::err_field_declared_as_function,
732 FD->getName());
Chris Lattner82625602007-01-24 02:26:21 +0000733 delete FD;
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000734 continue;
735 }
736
Chris Lattner82625602007-01-24 02:26:21 +0000737 // C99 6.7.2.1p2 - A field may not be an incomplete type except...
Chris Lattner720a0542007-01-25 00:44:24 +0000738 if (FDTy->isIncompleteType()) {
Chris Lattner82625602007-01-24 02:26:21 +0000739 if (i != NumFields-1 || // ... that the last member ...
740 Record->getKind() != Decl::Struct || // ... of a structure ...
Chris Lattner720a0542007-01-25 00:44:24 +0000741 !isa<ArrayType>(FDTy)) { //... may have incomplete array type.
Chris Lattner82625602007-01-24 02:26:21 +0000742 Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName());
743 delete FD;
744 continue;
745 }
Chris Lattner720a0542007-01-25 00:44:24 +0000746 if (NumNamedMembers < 1) { //... must have more than named member ...
Chris Lattner82625602007-01-24 02:26:21 +0000747 Diag(FD->getLocation(), diag::err_flexible_array_empty_struct,
748 FD->getName());
749 delete FD;
750 continue;
751 }
Chris Lattner720a0542007-01-25 00:44:24 +0000752
753 // Okay, we have a legal flexible array member at the end of the struct.
Chris Lattner41943152007-01-25 04:52:46 +0000754 Record->setHasFlexibleArrayMember(true);
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000755 }
Chris Lattner720a0542007-01-25 00:44:24 +0000756
757
758 /// C99 6.7.2.1p2 - a struct ending in a flexible array member cannot be the
759 /// field of another structure or the element of an array.
760 if (RecordType *FDTTy = dyn_cast<RecordType>(FDTy)) {
761 if (FDTTy->getDecl()->hasFlexibleArrayMember()) {
762 // If this is a member of a union, then entire union becomes "flexible".
763 if (Record->getKind() == Decl::Union) {
Chris Lattner41943152007-01-25 04:52:46 +0000764 Record->setHasFlexibleArrayMember(true);
Chris Lattner720a0542007-01-25 00:44:24 +0000765 } else {
766 // If this is a struct/class and this is not the last element, reject
767 // it. Note that GCC supports variable sized arrays in the middle of
768 // structures.
769 if (i != NumFields-1) {
770 Diag(FD->getLocation(), diag::err_variable_sized_type_in_struct,
771 FD->getName());
772 delete FD;
773 continue;
774 }
775
776 // We support flexible arrays at the end of structs in other structs
777 // as an extension.
778 Diag(FD->getLocation(), diag::ext_flexible_array_in_struct,
779 FD->getName());
Chris Lattner41943152007-01-25 04:52:46 +0000780 Record->setHasFlexibleArrayMember(true);
Chris Lattner720a0542007-01-25 00:44:24 +0000781 }
782 }
783 }
784
Chris Lattner82625602007-01-24 02:26:21 +0000785 // Keep track of the number of named members.
Chris Lattnere5a66562007-01-25 22:48:42 +0000786 if (IdentifierInfo *II = FD->getIdentifier()) {
787 // Detect duplicate member names.
Chris Lattnerbaf33662007-01-27 02:14:08 +0000788 if (!FieldIDs.insert(II)) {
Chris Lattnere5a66562007-01-25 22:48:42 +0000789 Diag(FD->getLocation(), diag::err_duplicate_member, II->getName());
790 // Find the previous decl.
791 SourceLocation PrevLoc;
792 for (unsigned i = 0, e = RecFields.size(); ; ++i) {
793 assert(i != e && "Didn't find previous def!");
794 if (RecFields[i]->getIdentifier() == II) {
795 PrevLoc = RecFields[i]->getLocation();
796 break;
797 }
798 }
799 Diag(PrevLoc, diag::err_previous_definition);
800 delete FD;
801 continue;
802 }
Chris Lattner82625602007-01-24 02:26:21 +0000803 ++NumNamedMembers;
Chris Lattnere5a66562007-01-25 22:48:42 +0000804 }
Chris Lattner41943152007-01-25 04:52:46 +0000805
806 // Remember good fields.
807 RecFields.push_back(FD);
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000808 }
Chris Lattner82625602007-01-24 02:26:21 +0000809
810
811 // Okay, we successfully defined 'Record'.
Chris Lattner41943152007-01-25 04:52:46 +0000812 Record->defineBody(&RecFields[0], RecFields.size());
Chris Lattner1300fb92007-01-23 23:42:53 +0000813}
814
Chris Lattnerc1915e22007-01-25 07:29:02 +0000815Sema::DeclTy *Sema::ParseEnumConstant(Scope *S, DeclTy *EnumDeclX,
816 SourceLocation IdLoc, IdentifierInfo *Id,
817 SourceLocation EqualLoc, ExprTy *Val) {
818 EnumDecl *TheEnumDecl = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX));
Chris Lattner8116d1b2007-01-25 22:38:29 +0000819
820 // Verify that there isn't already something declared with this name in this
821 // scope.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000822 if (Decl *PrevDecl = LookupScopedDecl(Id, Decl::IDNS_Ordinary, IdLoc, S)) {
Chris Lattner8116d1b2007-01-25 22:38:29 +0000823 if (S->isDeclScope(PrevDecl)) {
824 if (isa<EnumConstantDecl>(PrevDecl))
825 Diag(IdLoc, diag::err_redefinition_of_enumerator, Id->getName());
826 else
827 Diag(IdLoc, diag::err_redefinition, Id->getName());
828 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
829 return 0;
830 }
831 }
Steve Naroff72cada02007-05-18 00:18:52 +0000832 SourceLocation expLoc;
Steve Naroff63969212007-05-07 21:22:42 +0000833 // C99 6.7.2.2p2: Make sure we have an integer constant expression.
Chris Lattner6046e002007-06-05 06:39:23 +0000834 // FIXME: Capture this value in the enumconstantdecl.
Chris Lattner238cbc52007-06-02 04:48:48 +0000835 if (Val && !((Expr *)Val)->isIntegerConstantExpr(&expLoc)) {
Steve Naroff72cada02007-05-18 00:18:52 +0000836 Diag(expLoc, diag::err_enum_value_not_integer_constant_expr, Id->getName());
Steve Naroff63969212007-05-07 21:22:42 +0000837 return 0;
838 }
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000839 QualType Ty = Context.getTagDeclType(TheEnumDecl);
Steve Naroff63969212007-05-07 21:22:42 +0000840 EnumConstantDecl *New = new EnumConstantDecl(IdLoc, Id, Ty, (Expr *)Val);
Chris Lattner8116d1b2007-01-25 22:38:29 +0000841
842 // Register this decl in the current scope stack.
843 New->setNext(Id->getFETokenInfo<Decl>());
844 Id->setFETokenInfo(New);
845 S->AddDecl(New);
846 return New;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000847}
848
849void Sema::ParseEnumBody(SourceLocation EnumLoc, DeclTy *EnumDeclX,
850 DeclTy **Elements, unsigned NumElements) {
851 EnumDecl *Enum = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX));
852 assert(!Enum->isDefinition() && "Enum redefinitions can't reach here");
853
854 // Verify that all the values are okay.
855 SmallVector<EnumConstantDecl*, 32> Values;
856 for (unsigned i = 0; i != NumElements; ++i) {
857 EnumConstantDecl *ECD =
858 cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i]));
859 if (!ECD) continue; // Already issued a diagnostic.
860
861 Values.push_back(ECD);
862 }
863
864 Enum->defineElements(&Values[0], Values.size());
865}
Chris Lattner1300fb92007-01-23 23:42:53 +0000866
Steve Naroff26c8ea52007-03-21 21:08:52 +0000867void Sema::AddTopLevelDecl(Decl *current, Decl *last) {
868 if (!current) return;
869
870 // If this is a top-level decl that is chained to some other (e.g. int A,B,C;)
871 // remember this in the LastInGroupList list.
872 if (last) {
873 LastInGroupList.push_back((Decl*)last);
874 }
875}
Steve Naroff0f2fe172007-06-01 17:11:19 +0000876
877/// ParseAttribute GCC __attribute__
Steve Naroff45552922007-06-01 21:56:17 +0000878Sema::AttrTy *Sema::ParseAttribute(
879 IdentifierInfo *AttrName, SourceLocation AttrNameLoc, AttrTy *PrevAttr,
Steve Naroff0f2fe172007-06-01 17:11:19 +0000880 IdentifierInfo *ParmName, SourceLocation ParmNameLoc,
881 ExprTy **Args, unsigned NumArgs,
882 SourceLocation LParenLoc, SourceLocation RParenLoc) {
Steve Naroff45552922007-06-01 21:56:17 +0000883 Attr *attrib = new Attr(AttrNameLoc, AttrName, ParmName, (Expr **)Args,
884 NumArgs);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000885 if (PrevAttr)
886 // reuse Decl's "Next" pointer for chaining the attribute list
Steve Naroff45552922007-06-01 21:56:17 +0000887 attrib->setNext(static_cast<Attr *>(PrevAttr));
Steve Naroff0f2fe172007-06-01 17:11:19 +0000888 return attrib;
889}