blob: 1a353aa698dc153af1974c7dd64ad07bea7f4407 [file] [log] [blame]
Chris Lattner697e5d62006-11-09 06:32:27 +00001//===--- SemaDecl.cpp - Semantic Analysis for Declarations ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for declarations.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattnere168f762006-11-10 05:29:30 +000014#include "Sema.h"
Chris Lattner5c5fbcc2006-12-03 08:41:30 +000015#include "clang/AST/ASTContext.h"
Chris Lattner9561a0b2007-01-28 08:20:04 +000016#include "clang/AST/Builtins.h"
Chris Lattnere168f762006-11-10 05:29:30 +000017#include "clang/AST/Decl.h"
Chris Lattner1300fb92007-01-23 23:42:53 +000018#include "clang/AST/Expr.h"
Chris Lattnerf84a79c2006-11-11 22:59:23 +000019#include "clang/AST/Type.h"
Chris Lattner591a6752006-11-19 23:16:18 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattnere168f762006-11-10 05:29:30 +000021#include "clang/Parse/Scope.h"
22#include "clang/Lex/IdentifierTable.h"
Chris Lattnerac18be92006-11-20 06:49:47 +000023#include "clang/Basic/LangOptions.h"
Chris Lattner9561a0b2007-01-28 08:20:04 +000024#include "clang/Basic/TargetInfo.h"
Chris Lattner38047f92007-01-27 06:24:01 +000025#include "llvm/ADT/SmallSet.h"
Steve Naroffd50c88e2007-04-05 21:15:20 +000026
Chris Lattner697e5d62006-11-09 06:32:27 +000027using namespace llvm;
28using namespace clang;
29
Steve Naroff8eeeb132007-05-08 21:09:37 +000030// C99: 6.7.5p3: Used by ParseDeclarator/ParseField to make sure we have
31// a constant expression of type int with a value greater than zero.
32bool Sema::isConstantArrayType(ArrayType *ary, SourceLocation loc) {
33 if (Expr *size = ary->getSize()) {
34 if (!size->isConstantExpr()) {
35 Diag(loc, diag::err_typecheck_illegal_vla);
36 return false;
37 }
38 if (!size->getType()->isIntegerType()) {
39 Diag(loc, diag::err_array_size_non_int, size->getType());
40 return false;
41 }
42 // We have a constant expression with an integer type, now make sure
43 // value greater than zero (C99 6.7.5.2p1).
44 // FIXME: evaluate constant expression.
45 }
46 return true;
47}
Chris Lattnere168f762006-11-10 05:29:30 +000048
Chris Lattner2ebe4bb2006-11-20 01:29:42 +000049Sema::DeclTy *Sema::isTypeName(const IdentifierInfo &II, Scope *S) const {
Chris Lattner32d920b2007-01-26 02:01:53 +000050 return dyn_cast_or_null<TypedefDecl>(II.getFETokenInfo<Decl>());
Chris Lattnere168f762006-11-10 05:29:30 +000051}
52
Chris Lattner302b4be2006-11-19 02:31:38 +000053void Sema::PopScope(SourceLocation Loc, Scope *S) {
54 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
55 I != E; ++I) {
Chris Lattner99d31772007-01-21 22:37:37 +000056 Decl *D = static_cast<Decl*>(*I);
Chris Lattner302b4be2006-11-19 02:31:38 +000057 assert(D && "This decl didn't get pushed??");
Chris Lattnerff65b6b2007-01-23 01:33:16 +000058 IdentifierInfo *II = D->getIdentifier();
59 if (!II) continue;
Chris Lattner302b4be2006-11-19 02:31:38 +000060
Chris Lattnerff65b6b2007-01-23 01:33:16 +000061 // Unlink this decl from the identifier. Because the scope contains decls
62 // in an unordered collection, and because we have multiple identifier
63 // namespaces (e.g. tag, normal, label),the decl may not be the first entry.
64 if (II->getFETokenInfo<Decl>() == D) {
65 // Normal case, no multiple decls in different namespaces.
66 II->setFETokenInfo(D->getNext());
67 } else {
68 // Scan ahead. There are only three namespaces in C, so this loop can
69 // never execute more than 3 times.
70 Decl *SomeDecl = II->getFETokenInfo<Decl>();
71 while (SomeDecl->getNext() != D) {
72 SomeDecl = SomeDecl->getNext();
73 assert(SomeDecl && "Didn't find this decl on its identifier's chain!");
74 }
75 SomeDecl->setNext(D->getNext());
76 }
Chris Lattner302b4be2006-11-19 02:31:38 +000077
Chris Lattner740b2f32006-11-21 01:32:20 +000078 // This will have to be revisited for C++: there we want to nest stuff in
79 // namespace decls etc. Even for C, we might want a top-level translation
80 // unit decl or something.
81 if (!CurFunctionDecl)
82 continue;
83
84 // Chain this decl to the containing function, it now owns the memory for
85 // the decl.
86 D->setNext(CurFunctionDecl->getDeclChain());
87 CurFunctionDecl->setDeclChain(D);
Chris Lattner302b4be2006-11-19 02:31:38 +000088 }
89}
90
Chris Lattner18b19622007-01-22 07:39:13 +000091/// LookupScopedDecl - Look up the inner-most declaration in the specified
92/// namespace.
Chris Lattner9561a0b2007-01-28 08:20:04 +000093Decl *Sema::LookupScopedDecl(IdentifierInfo *II, unsigned NSI,
94 SourceLocation IdLoc, Scope *S) {
Chris Lattner18b19622007-01-22 07:39:13 +000095 if (II == 0) return 0;
Chris Lattnerb6738ec2007-01-28 00:38:24 +000096 Decl::IdentifierNamespace NS = (Decl::IdentifierNamespace)NSI;
Chris Lattner18b19622007-01-22 07:39:13 +000097
98 // Scan up the scope chain looking for a decl that matches this identifier
99 // that is in the appropriate namespace. This search should not take long, as
100 // shadowing of names is uncommon, and deep shadowing is extremely uncommon.
101 for (Decl *D = II->getFETokenInfo<Decl>(); D; D = D->getNext())
102 if (D->getIdentifierNamespace() == NS)
103 return D;
Chris Lattnerb6738ec2007-01-28 00:38:24 +0000104
Chris Lattner9561a0b2007-01-28 08:20:04 +0000105 // If we didn't find a use of this identifier, and if the identifier
106 // corresponds to a compiler builtin, create the decl object for the builtin
107 // now, injecting it into translation unit scope, and return it.
108 if (NS == Decl::IDNS_Ordinary) {
109 // If this is a builtin on some other target, or if this builtin varies
110 // across targets (e.g. in type), emit a diagnostic and mark the translation
111 // unit non-portable for using it.
112 if (II->isNonPortableBuiltin()) {
113 // Only emit this diagnostic once for this builtin.
114 II->setNonPortableBuiltin(false);
115 Context.Target.DiagnoseNonPortability(IdLoc,
116 diag::port_target_builtin_use);
117 }
Chris Lattner9561a0b2007-01-28 08:20:04 +0000118 // If this is a builtin on this (or all) targets, create the decl.
119 if (unsigned BuiltinID = II->getBuiltinID())
120 return LazilyCreateBuiltin(II, BuiltinID, S);
121 }
Chris Lattner18b19622007-01-22 07:39:13 +0000122 return 0;
123}
124
Chris Lattner9561a0b2007-01-28 08:20:04 +0000125/// LazilyCreateBuiltin - The specified Builtin-ID was first used at file scope.
126/// lazily create a decl for it.
127Decl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid, Scope *S) {
128 Builtin::ID BID = (Builtin::ID)bid;
129
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000130 QualType R = Context.BuiltinInfo.GetBuiltinType(BID, Context);
Chris Lattner9561a0b2007-01-28 08:20:04 +0000131 FunctionDecl *New = new FunctionDecl(SourceLocation(), II, R);
132
133 // Find translation-unit scope to insert this function into.
134 while (S->getParent())
135 S = S->getParent();
136 S->AddDecl(New);
137
138 // Add this decl to the end of the identifier info.
139 if (Decl *LastDecl = II->getFETokenInfo<Decl>()) {
140 // Scan until we find the last (outermost) decl in the id chain.
141 while (LastDecl->getNext())
142 LastDecl = LastDecl->getNext();
143 // Insert before (outside) it.
144 LastDecl->setNext(New);
145 } else {
146 II->setFETokenInfo(New);
147 }
148 // Make sure clients iterating over decls see this.
149 LastInGroupList.push_back(New);
150
151 return New;
152}
153
Chris Lattner01564d92007-01-27 19:27:06 +0000154/// MergeTypeDefDecl - We just parsed a typedef 'New' which has the same name
155/// and scope as a previous declaration 'Old'. Figure out how to resolve this
156/// situation, merging decls or emitting diagnostics as appropriate.
157///
Chris Lattnerc511efb2007-01-27 19:32:14 +0000158TypedefDecl *Sema::MergeTypeDefDecl(TypedefDecl *New, Decl *OldD) {
159 // Verify the old decl was also a typedef.
160 TypedefDecl *Old = dyn_cast<TypedefDecl>(OldD);
161 if (!Old) {
162 Diag(New->getLocation(), diag::err_redefinition_different_kind,
163 New->getName());
164 Diag(OldD->getLocation(), diag::err_previous_definition);
165 return New;
166 }
167
Chris Lattner01564d92007-01-27 19:27:06 +0000168 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
169 // TODO: This is totally simplistic. It should handle merging functions
170 // together etc, merging extern int X; int X; ...
171 Diag(New->getLocation(), diag::err_redefinition, New->getName());
172 Diag(Old->getLocation(), diag::err_previous_definition);
173 return New;
174}
175
176/// MergeFunctionDecl - We just parsed a function 'New' which has the same name
177/// and scope as a previous declaration 'Old'. Figure out how to resolve this
178/// situation, merging decls or emitting diagnostics as appropriate.
179///
Chris Lattnerc511efb2007-01-27 19:32:14 +0000180FunctionDecl *Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD) {
181 // Verify the old decl was also a function.
182 FunctionDecl *Old = dyn_cast<FunctionDecl>(OldD);
183 if (!Old) {
184 Diag(New->getLocation(), diag::err_redefinition_different_kind,
185 New->getName());
186 Diag(OldD->getLocation(), diag::err_previous_definition);
187 return New;
188 }
189
Chris Lattnerefe4aea2007-01-27 19:35:39 +0000190 // This is not right, but it's a start. If 'Old' is a function prototype with
191 // the same type as 'New', silently allow this. FIXME: We should link up decl
192 // objects here.
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000193 if (Old->getBody() == 0 &&
194 Old->getCanonicalType() == New->getCanonicalType()) {
Chris Lattnerefe4aea2007-01-27 19:35:39 +0000195 return New;
196 }
Chris Lattnerc511efb2007-01-27 19:32:14 +0000197
Chris Lattner01564d92007-01-27 19:27:06 +0000198 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
199 // TODO: This is totally simplistic. It should handle merging functions
200 // together etc, merging extern int X; int X; ...
201 Diag(New->getLocation(), diag::err_redefinition, New->getName());
202 Diag(Old->getLocation(), diag::err_previous_definition);
203 return New;
204}
205
206/// MergeVarDecl - We just parsed a variable 'New' which has the same name
207/// and scope as a previous declaration 'Old'. Figure out how to resolve this
208/// situation, merging decls or emitting diagnostics as appropriate.
209///
Steve Narofffc49d672007-04-01 21:27:45 +0000210/// FIXME: Need to carefully consider tentative definition rules (C99 6.9.2p2).
211/// For example, we incorrectly complain about i1, i4 from C99 6.9.2p4.
212///
Chris Lattnerc511efb2007-01-27 19:32:14 +0000213VarDecl *Sema::MergeVarDecl(VarDecl *New, Decl *OldD) {
214 // Verify the old decl was also a variable.
215 VarDecl *Old = dyn_cast<VarDecl>(OldD);
216 if (!Old) {
217 Diag(New->getLocation(), diag::err_redefinition_different_kind,
218 New->getName());
219 Diag(OldD->getLocation(), diag::err_previous_definition);
220 return New;
221 }
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000222 // Verify the types match.
223 if (Old->getCanonicalType() != New->getCanonicalType()) {
224 Diag(New->getLocation(), diag::err_redefinition, New->getName());
225 Diag(Old->getLocation(), diag::err_previous_definition);
226 return New;
227 }
228 // We've verified the types match, now check if Old is "extern".
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000229 if (Old->getStorageClass() != VarDecl::Extern) {
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000230 Diag(New->getLocation(), diag::err_redefinition, New->getName());
231 Diag(Old->getLocation(), diag::err_previous_definition);
232 }
Chris Lattner01564d92007-01-27 19:27:06 +0000233 return New;
234}
235
Chris Lattnerb6738ec2007-01-28 00:38:24 +0000236/// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
237/// no declarator (e.g. "struct foo;") is parsed.
238Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) {
239 // TODO: emit error on 'int;' or 'const enum foo;'.
240 // TODO: emit error on 'typedef int;'
241 // if (!DS.isMissingDeclaratorOk()) Diag(...);
242
243 return 0;
244}
245
Chris Lattnere168f762006-11-10 05:29:30 +0000246Action::DeclTy *
247Sema::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
248 DeclTy *LastInGroup) {
249 IdentifierInfo *II = D.getIdentifier();
Chris Lattner302b4be2006-11-19 02:31:38 +0000250
Chris Lattner01564d92007-01-27 19:27:06 +0000251 // See if this is a redefinition of a variable in the same scope.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000252 Decl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary,
253 D.getIdentifierLoc(), S);
Chris Lattner01564d92007-01-27 19:27:06 +0000254 if (!S->isDeclScope(PrevDecl))
255 PrevDecl = 0; // If in outer scope, it isn't the same thing.
256
Chris Lattnere168f762006-11-10 05:29:30 +0000257 Decl *New;
Chris Lattner01a7c532007-01-25 23:09:03 +0000258 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Chris Lattner01564d92007-01-27 19:27:06 +0000259 TypedefDecl *NewTD = ParseTypedefDecl(S, D);
260 if (!NewTD) return 0;
261
262 // Merge the decl with the existing one if appropriate.
263 if (PrevDecl) {
264 NewTD = MergeTypeDefDecl(NewTD, PrevDecl);
265 if (NewTD == 0) return 0;
266 }
267 New = NewTD;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000268 if (S->getParent() == 0) {
269 // C99 6.7.7p2: If a typedef name specifies a variably modified type
270 // then it shall have block scope.
271 if (ArrayType *ary = dyn_cast<ArrayType>(NewTD->getUnderlyingType())) {
272 if (!isConstantArrayType(ary, D.getIdentifierLoc()))
273 return 0;
274 }
275 }
Chris Lattner01a7c532007-01-25 23:09:03 +0000276 } else if (D.isFunctionDeclarator()) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000277 QualType R = GetTypeForDeclarator(D, S);
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000278 if (R.isNull()) return 0; // FIXME: "auto func();" passes through...
Chris Lattner01564d92007-01-27 19:27:06 +0000279
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000280 FunctionDecl::StorageClass SC;
281 switch (D.getDeclSpec().getStorageClassSpec()) {
282 default: assert(0 && "Unknown storage class!");
283 case DeclSpec::SCS_auto:
284 case DeclSpec::SCS_register:
285 Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_func, R);
286 return 0;
287 case DeclSpec::SCS_unspecified: SC = FunctionDecl::None; break;
288 case DeclSpec::SCS_extern: SC = FunctionDecl::Extern; break;
289 case DeclSpec::SCS_static: SC = FunctionDecl::Static; break;
290 }
291
292 FunctionDecl *NewFD = new FunctionDecl(D.getIdentifierLoc(), II, R, SC);
Chris Lattner01564d92007-01-27 19:27:06 +0000293
294 // Merge the decl with the existing one if appropriate.
295 if (PrevDecl) {
296 NewFD = MergeFunctionDecl(NewFD, PrevDecl);
297 if (NewFD == 0) return 0;
298 }
299 New = NewFD;
Chris Lattner01a7c532007-01-25 23:09:03 +0000300 } else {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000301 QualType R = GetTypeForDeclarator(D, S);
Chris Lattner01a7c532007-01-25 23:09:03 +0000302 if (R.isNull()) return 0;
Chris Lattner01564d92007-01-27 19:27:06 +0000303
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000304 VarDecl *NewVD;
305 VarDecl::StorageClass SC;
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000306 switch (D.getDeclSpec().getStorageClassSpec()) {
307 default: assert(0 && "Unknown storage class!");
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000308 case DeclSpec::SCS_unspecified: SC = VarDecl::None; break;
309 case DeclSpec::SCS_extern: SC = VarDecl::Extern; break;
310 case DeclSpec::SCS_static: SC = VarDecl::Static; break;
311 case DeclSpec::SCS_auto: SC = VarDecl::Auto; break;
312 case DeclSpec::SCS_register: SC = VarDecl::Register; break;
313 }
Steve Narofffc49d672007-04-01 21:27:45 +0000314 if (S->getParent() == 0) {
315 // File scope. C99 6.9.2p2: A declaration of an identifier for and
316 // object that has file scope without an initializer, and without a
317 // storage-class specifier or with the storage-class specifier "static",
318 // constitutes a tentative definition. Note: A tentative definition with
319 // external linkage is valid (C99 6.2.2p5).
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000320 if (!Init && SC == VarDecl::Static) {
Steve Narofffc49d672007-04-01 21:27:45 +0000321 // C99 6.9.2p3: If the declaration of an identifier for an object is
322 // a tentative definition and has internal linkage (C99 6.2.2p3), the
323 // declared type shall not be an incomplete type.
324 if (R->isIncompleteType()) {
325 Diag(D.getIdentifierLoc(), diag::err_typecheck_decl_incomplete_type, R);
326 return 0;
327 }
Steve Naroffca8f7122007-04-01 01:41:35 +0000328 }
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000329 // FIXME: Find C99 spec reference
330 if (SC == VarDecl::Auto || SC == VarDecl::Register) {
331 Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope, R);
332 return 0;
333 }
Steve Naroff8eeeb132007-05-08 21:09:37 +0000334 // C99 6.7.5.2p2: If an identifier is declared to be an object with
335 // static storage duration, it shall not have a variable length array.
336 if (ArrayType *ary = dyn_cast<ArrayType>(R)) {
337 if (!isConstantArrayType(ary, D.getIdentifierLoc()))
338 return 0;
339 }
Steve Naroffef2ab6a2007-04-02 20:50:54 +0000340 NewVD = new FileVarDecl(D.getIdentifierLoc(), II, R, SC);
Steve Narofffc49d672007-04-01 21:27:45 +0000341 } else {
342 // Block scope. C99 6.7p7: If an identifier for an object is declared with
343 // no linkage (C99 6.2.2p6), the type for the object shall be complete...
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000344 if (SC != VarDecl::Extern) {
Steve Narofffc49d672007-04-01 21:27:45 +0000345 if (R->isIncompleteType()) {
346 Diag(D.getIdentifierLoc(), diag::err_typecheck_decl_incomplete_type, R);
347 return 0;
348 }
349 }
Steve Naroff8eeeb132007-05-08 21:09:37 +0000350 if (SC == VarDecl::Static) {
351 // C99 6.7.5.2p2: If an identifier is declared to be an object with
352 // static storage duration, it shall not have a variable length array.
353 if (ArrayType *ary = dyn_cast<ArrayType>(R)) {
354 if (!isConstantArrayType(ary, D.getIdentifierLoc()))
355 return 0;
356 }
357 }
Steve Naroffef2ab6a2007-04-02 20:50:54 +0000358 NewVD = new BlockVarDecl(D.getIdentifierLoc(), II, R, SC);
359 }
Chris Lattner01564d92007-01-27 19:27:06 +0000360 // Merge the decl with the existing one if appropriate.
361 if (PrevDecl) {
362 NewVD = MergeVarDecl(NewVD, PrevDecl);
363 if (NewVD == 0) return 0;
364 }
365 New = NewVD;
Chris Lattner01a7c532007-01-25 23:09:03 +0000366 }
Chris Lattner302b4be2006-11-19 02:31:38 +0000367
368
Chris Lattnere168f762006-11-10 05:29:30 +0000369 // If this has an identifier, add it to the scope stack.
370 if (II) {
Chris Lattner18b19622007-01-22 07:39:13 +0000371 New->setNext(II->getFETokenInfo<Decl>());
Chris Lattnere168f762006-11-10 05:29:30 +0000372 II->setFETokenInfo(New);
Chris Lattner99d31772007-01-21 22:37:37 +0000373 S->AddDecl(New);
Chris Lattnere168f762006-11-10 05:29:30 +0000374 }
375
Steve Naroff26c8ea52007-03-21 21:08:52 +0000376 if (S->getParent() == 0)
377 AddTopLevelDecl(New, (Decl *)LastInGroup);
Chris Lattnere168f762006-11-10 05:29:30 +0000378
379 return New;
380}
381
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000382VarDecl *
383Sema::ParseParamDeclarator(DeclaratorChunk &FTI, unsigned ArgNo,
384 Scope *FnScope) {
385 const DeclaratorChunk::ParamInfo &PI = FTI.Fun.ArgInfo[ArgNo];
Chris Lattner200bdc32006-11-19 02:43:37 +0000386
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000387 IdentifierInfo *II = PI.Ident;
Chris Lattnerc284e9b2007-01-23 05:14:32 +0000388 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
389 // Can this happen for params? We already checked that they don't conflict
390 // among each other. Here they can only shadow globals, which is ok.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000391 if (Decl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary,
392 PI.IdentLoc, FnScope)) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000393
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000394 }
395
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000396 // FIXME: Handle storage class (auto, register). No declarator?
Steve Naroffef2ab6a2007-04-02 20:50:54 +0000397 VarDecl *New = new ParmVarDecl(PI.IdentLoc, II,
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000398 QualType::getFromOpaquePtr(PI.TypeInfo),
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000399 VarDecl::None);
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000400
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +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 Lattnerc5cdf4d2007-01-21 07:42:07 +0000404 II->setFETokenInfo(New);
Chris Lattner99d31772007-01-21 22:37:37 +0000405 FnScope->AddDecl(New);
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000406 }
Chris Lattner229ce602006-11-21 01:21:07 +0000407
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000408 return New;
409}
410
411
412Sema::DeclTy *Sema::ParseStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) {
Chris Lattner229ce602006-11-21 01:21:07 +0000413 assert(CurFunctionDecl == 0 && "Function parsing confused");
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000414 assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
415 "Not a function declarator!");
416 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
417
418 // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared'
419 // for a K&R function.
420 if (!FTI.hasPrototype) {
421 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
422 if (FTI.ArgInfo[i].TypeInfo == 0) {
423 Diag(FTI.ArgInfo[i].IdentLoc, diag::err_param_not_declared,
424 FTI.ArgInfo[i].Ident->getName());
425 // Implicitly declare the argument as type 'int' for lack of a better
426 // type.
427 FTI.ArgInfo[i].TypeInfo = Context.IntTy.getAsOpaquePtr();
428 }
429 }
430
431 // Since this is a function definition, act as though we have information
432 // about the arguments.
433 FTI.hasPrototype = true;
Chris Lattner2114d5e2006-12-04 07:40:24 +0000434 } else {
435 // FIXME: Diagnose arguments without names in C.
436
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000437 }
438
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000439 Scope *GlobalScope = FnBodyScope->getParent();
440
441 FunctionDecl *FD =
442 static_cast<FunctionDecl*>(ParseDeclarator(GlobalScope, D, 0, 0));
Chris Lattner229ce602006-11-21 01:21:07 +0000443 CurFunctionDecl = FD;
Chris Lattner2114d5e2006-12-04 07:40:24 +0000444
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000445 // Create Decl objects for each parameter, adding them to the FunctionDecl.
446 SmallVector<VarDecl*, 16> Params;
Chris Lattnerf61c8a82007-01-21 19:04:43 +0000447
448 // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes
449 // no arguments, not a function that takes a single void argument.
450 if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 &&
451 FTI.ArgInfo[0].TypeInfo == Context.VoidTy.getAsOpaquePtr()) {
Chris Lattner99d31772007-01-21 22:37:37 +0000452 // empty arg list, don't push any params.
Chris Lattnerf61c8a82007-01-21 19:04:43 +0000453 } else {
454 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i)
455 Params.push_back(ParseParamDeclarator(D.getTypeObject(0), i,FnBodyScope));
456 }
Chris Lattner2114d5e2006-12-04 07:40:24 +0000457
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000458 FD->setParams(&Params[0], Params.size());
Chris Lattner2114d5e2006-12-04 07:40:24 +0000459
Chris Lattnere168f762006-11-10 05:29:30 +0000460 return FD;
461}
462
Chris Lattner229ce602006-11-21 01:21:07 +0000463Sema::DeclTy *Sema::ParseFunctionDefBody(DeclTy *D, StmtTy *Body) {
464 FunctionDecl *FD = static_cast<FunctionDecl*>(D);
465 FD->setBody((Stmt*)Body);
466
467 assert(FD == CurFunctionDecl && "Function parsing confused");
468 CurFunctionDecl = 0;
469 return FD;
470}
471
472
Chris Lattnerac18be92006-11-20 06:49:47 +0000473/// ImplicitlyDefineFunction - An undeclared identifier was used in a function
474/// call, forming a call to an implicitly defined function (per C99 6.5.1p2).
475Decl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II,
476 Scope *S) {
477 if (getLangOptions().C99) // Extension in C99.
478 Diag(Loc, diag::ext_implicit_function_decl, II.getName());
479 else // Legal in C90, but warn about it.
480 Diag(Loc, diag::warn_implicit_function_decl, II.getName());
481
482 // FIXME: handle stuff like:
483 // void foo() { extern float X(); }
484 // void bar() { X(); } <-- implicit decl for X in another scope.
485
486 // Set a Declarator for the implicit definition: int foo();
Chris Lattner353f5742006-11-28 04:50:12 +0000487 const char *Dummy;
Chris Lattnerac18be92006-11-20 06:49:47 +0000488 DeclSpec DS;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000489 bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy);
Chris Lattnerb055f2d2007-02-11 08:19:57 +0000490 Error = Error; // Silence warning.
Chris Lattner353f5742006-11-28 04:50:12 +0000491 assert(!Error && "Error setting up implicit decl!");
Chris Lattnerac18be92006-11-20 06:49:47 +0000492 Declarator D(DS, Declarator::BlockContext);
Chris Lattnercbc426d2006-12-02 06:43:02 +0000493 D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, Loc));
Chris Lattnerac18be92006-11-20 06:49:47 +0000494 D.SetIdentifier(&II, Loc);
495
Chris Lattner62d2e662007-01-28 00:21:37 +0000496 // Find translation-unit scope to insert this function into.
497 while (S->getParent())
498 S = S->getParent();
Chris Lattnerac18be92006-11-20 06:49:47 +0000499
Chris Lattner62d2e662007-01-28 00:21:37 +0000500 return static_cast<Decl*>(ParseDeclarator(S, D, 0, 0));
Chris Lattnerac18be92006-11-20 06:49:47 +0000501}
502
Chris Lattner302b4be2006-11-19 02:31:38 +0000503
Chris Lattner01564d92007-01-27 19:27:06 +0000504TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D) {
Chris Lattnerda8aa7b2006-11-19 23:12:30 +0000505 assert(D.getIdentifier() && "Wrong callback for declspec withotu declarator");
Chris Lattner302b4be2006-11-19 02:31:38 +0000506
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000507 QualType T = GetTypeForDeclarator(D, S);
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000508 if (T.isNull()) return 0;
509
Chris Lattner18b19622007-01-22 07:39:13 +0000510 // Scope manipulation handled by caller.
511 return new TypedefDecl(D.getIdentifierLoc(), D.getIdentifier(), T);
Chris Lattnere168f762006-11-10 05:29:30 +0000512}
513
Chris Lattner18b19622007-01-22 07:39:13 +0000514
Chris Lattner1300fb92007-01-23 23:42:53 +0000515/// ParseTag - This is invoked when we see 'struct foo' or 'struct {'. In the
516/// former case, Name will be non-null. In the later case, Name will be null.
517/// TagType indicates what kind of tag this is. TK indicates whether this is a
518/// reference/declaration/definition of a tag.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000519Sema::DeclTy *Sema::ParseTag(Scope *S, unsigned TagType, TagKind TK,
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000520 SourceLocation KWLoc, IdentifierInfo *Name,
521 SourceLocation NameLoc) {
Chris Lattner8799cf22007-01-23 01:57:16 +0000522 // If this is a use of an existing tag, it must have a name.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000523 assert((Name != 0 || TK == TK_Definition) &&
524 "Nameless record must be a definition!");
Chris Lattner8799cf22007-01-23 01:57:16 +0000525
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000526 Decl::Kind Kind;
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000527 switch (TagType) {
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000528 default: assert(0 && "Unknown tag type!");
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000529 case DeclSpec::TST_struct: Kind = Decl::Struct; break;
530 case DeclSpec::TST_union: Kind = Decl::Union; break;
531//case DeclSpec::TST_class: Kind = Decl::Class; break;
532 case DeclSpec::TST_enum: Kind = Decl::Enum; break;
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000533 }
Chris Lattner7e783a12007-01-23 02:05:42 +0000534
Chris Lattner18b19622007-01-22 07:39:13 +0000535 // If this is a named struct, check to see if there was a previous forward
536 // declaration or definition.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000537 if (TagDecl *PrevDecl =
Chris Lattner9561a0b2007-01-28 08:20:04 +0000538 dyn_cast_or_null<TagDecl>(LookupScopedDecl(Name, Decl::IDNS_Tag,
539 NameLoc, S))) {
Chris Lattner8799cf22007-01-23 01:57:16 +0000540
541 // If this is a use of a previous tag, or if the tag is already declared in
542 // the same scope (so that the definition/declaration completes or
543 // rementions the tag), reuse the decl.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000544 if (TK == TK_Reference || S->isDeclScope(PrevDecl)) {
Chris Lattner7e783a12007-01-23 02:05:42 +0000545 // Make sure that this wasn't declared as an enum and now used as a struct
546 // or something similar.
547 if (PrevDecl->getKind() != Kind) {
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000548 Diag(KWLoc, diag::err_use_with_wrong_tag, Name->getName());
Chris Lattner7e783a12007-01-23 02:05:42 +0000549 Diag(PrevDecl->getLocation(), diag::err_previous_use);
550 }
Chris Lattner7b9ace62007-01-23 20:11:08 +0000551
552 // If this is a use or a forward declaration, we're good.
553 if (TK != TK_Definition)
554 return PrevDecl;
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000555
Chris Lattner7b9ace62007-01-23 20:11:08 +0000556 // Diagnose attempts to redefine a tag.
557 if (PrevDecl->isDefinition()) {
558 Diag(NameLoc, diag::err_redefinition, Name->getName());
559 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
560 // If this is a redefinition, recover by making this struct be
561 // anonymous, which will make any later references get the previous
562 // definition.
563 Name = 0;
564 } else {
565 // Okay, this is definition of a previously declared or referenced tag.
566 // Move the location of the decl to be the definition site.
567 PrevDecl->setLocation(NameLoc);
Chris Lattner7b9ace62007-01-23 20:11:08 +0000568 return PrevDecl;
569 }
Chris Lattner8799cf22007-01-23 01:57:16 +0000570 }
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000571 // If we get here, this is a definition of a new struct type in a nested
572 // scope, e.g. "struct foo; void bar() { struct foo; }", just create a new
573 // type.
Chris Lattner18b19622007-01-22 07:39:13 +0000574 }
575
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000576 // If there is an identifier, use the location of the identifier as the
577 // location of the decl, otherwise use the location of the struct/union
578 // keyword.
579 SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc;
580
Chris Lattner18b19622007-01-22 07:39:13 +0000581 // Otherwise, if this is the first time we've seen this tag, create the decl.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000582 TagDecl *New;
Chris Lattner720a0542007-01-25 00:44:24 +0000583 switch (Kind) {
584 default: assert(0 && "Unknown tag kind!");
Chris Lattner5f521502007-01-25 06:27:24 +0000585 case Decl::Enum:
586 New = new EnumDecl(Loc, Name);
587 // If this is an undefined enum, warn.
Chris Lattnerc1915e22007-01-25 07:29:02 +0000588 if (TK != TK_Definition) Diag(Loc, diag::ext_forward_ref_enum);
Chris Lattner5f521502007-01-25 06:27:24 +0000589 break;
Chris Lattner720a0542007-01-25 00:44:24 +0000590 case Decl::Union:
591 case Decl::Struct:
592 case Decl::Class:
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000593 New = new RecordDecl(Kind, Loc, Name);
Chris Lattner720a0542007-01-25 00:44:24 +0000594 break;
595 }
Chris Lattner18b19622007-01-22 07:39:13 +0000596
597 // If this has an identifier, add it to the scope stack.
598 if (Name) {
599 New->setNext(Name->getFETokenInfo<Decl>());
600 Name->setFETokenInfo(New);
601 S->AddDecl(New);
602 }
603
604 return New;
605}
Chris Lattner1300fb92007-01-23 23:42:53 +0000606
607/// ParseField - Each field of a struct/union/class is passed into this in order
608/// to create a FieldDecl object for it.
609Sema::DeclTy *Sema::ParseField(Scope *S, DeclTy *TagDecl,
610 SourceLocation DeclStart,
611 Declarator &D, ExprTy *BitfieldWidth) {
612 IdentifierInfo *II = D.getIdentifier();
613 Expr *BitWidth = (Expr*)BitfieldWidth;
614
615 SourceLocation Loc = DeclStart;
616 if (II) Loc = D.getIdentifierLoc();
617
Chris Lattner62d2e662007-01-28 00:21:37 +0000618 // FIXME: Unnamed fields can be handled in various different ways, for
619 // example, unnamed unions inject all members into the struct namespace!
620
621
Chris Lattner1300fb92007-01-23 23:42:53 +0000622 if (BitWidth) {
623 // TODO: Validate.
Chris Lattner01a7c532007-01-25 23:09:03 +0000624 printf("WARNING: BITFIELDS IGNORED!\n");
Chris Lattner1300fb92007-01-23 23:42:53 +0000625
626 // 6.7.2.1p3
627 // 6.7.2.1p4
628
629 } else {
630 // Not a bitfield.
631
632 // validate II.
633
634 }
635
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000636 QualType T = GetTypeForDeclarator(D, S);
Chris Lattner01a7c532007-01-25 23:09:03 +0000637 if (T.isNull()) return 0;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000638
639 // C99 6.7.2.1p8: A member of a structure or union may have any type other
640 // than a variably modified type.
641 if (ArrayType *ary = dyn_cast<ArrayType>(T)) {
642 if (!isConstantArrayType(ary, Loc))
643 return 0;
644 }
Chris Lattner01a7c532007-01-25 23:09:03 +0000645 return new FieldDecl(Loc, II, T);
Chris Lattner1300fb92007-01-23 23:42:53 +0000646}
647
648void Sema::ParseRecordBody(SourceLocation RecLoc, DeclTy *RecDecl,
649 DeclTy **Fields, unsigned NumFields) {
Chris Lattnerc1915e22007-01-25 07:29:02 +0000650 RecordDecl *Record = cast<RecordDecl>(static_cast<Decl*>(RecDecl));
Chris Lattner1300fb92007-01-23 23:42:53 +0000651 if (Record->isDefinition()) {
652 // Diagnose code like:
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000653 // struct S { struct S {} X; };
Chris Lattner1300fb92007-01-23 23:42:53 +0000654 // We discover this when we complete the outer S. Reject and ignore the
655 // outer S.
656 Diag(Record->getLocation(), diag::err_nested_redefinition,
657 Record->getKindName());
658 Diag(RecLoc, diag::err_previous_definition);
659 return;
660 }
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000661
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000662 // Verify that all the fields are okay.
Chris Lattner82625602007-01-24 02:26:21 +0000663 unsigned NumNamedMembers = 0;
Steve Naroffcc321422007-03-26 23:09:51 +0000664 SmallVector<FieldDecl*, 32> RecFields;
Chris Lattnere5a66562007-01-25 22:48:42 +0000665 SmallSet<const IdentifierInfo*, 32> FieldIDs;
666
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000667 for (unsigned i = 0; i != NumFields; ++i) {
668 FieldDecl *FD = cast_or_null<FieldDecl>(static_cast<Decl*>(Fields[i]));
669 if (!FD) continue; // Already issued a diagnostic.
Chris Lattner720a0542007-01-25 00:44:24 +0000670
671 // Get the type for the field.
Steve Naroffd50c88e2007-04-05 21:15:20 +0000672 Type *FDTy = FD->getType().getCanonicalType().getTypePtr();
Chris Lattner720a0542007-01-25 00:44:24 +0000673
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000674 // C99 6.7.2.1p2 - A field may not be a function type.
Chris Lattner720a0542007-01-25 00:44:24 +0000675 if (isa<FunctionType>(FDTy)) {
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000676 Diag(FD->getLocation(), diag::err_field_declared_as_function,
677 FD->getName());
Chris Lattner82625602007-01-24 02:26:21 +0000678 delete FD;
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000679 continue;
680 }
681
Chris Lattner82625602007-01-24 02:26:21 +0000682 // C99 6.7.2.1p2 - A field may not be an incomplete type except...
Chris Lattner720a0542007-01-25 00:44:24 +0000683 if (FDTy->isIncompleteType()) {
Chris Lattner82625602007-01-24 02:26:21 +0000684 if (i != NumFields-1 || // ... that the last member ...
685 Record->getKind() != Decl::Struct || // ... of a structure ...
Chris Lattner720a0542007-01-25 00:44:24 +0000686 !isa<ArrayType>(FDTy)) { //... may have incomplete array type.
Chris Lattner82625602007-01-24 02:26:21 +0000687 Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName());
688 delete FD;
689 continue;
690 }
Chris Lattner720a0542007-01-25 00:44:24 +0000691 if (NumNamedMembers < 1) { //... must have more than named member ...
Chris Lattner82625602007-01-24 02:26:21 +0000692 Diag(FD->getLocation(), diag::err_flexible_array_empty_struct,
693 FD->getName());
694 delete FD;
695 continue;
696 }
Chris Lattner720a0542007-01-25 00:44:24 +0000697
698 // Okay, we have a legal flexible array member at the end of the struct.
Chris Lattner41943152007-01-25 04:52:46 +0000699 Record->setHasFlexibleArrayMember(true);
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000700 }
Chris Lattner720a0542007-01-25 00:44:24 +0000701
702
703 /// C99 6.7.2.1p2 - a struct ending in a flexible array member cannot be the
704 /// field of another structure or the element of an array.
705 if (RecordType *FDTTy = dyn_cast<RecordType>(FDTy)) {
706 if (FDTTy->getDecl()->hasFlexibleArrayMember()) {
707 // If this is a member of a union, then entire union becomes "flexible".
708 if (Record->getKind() == Decl::Union) {
Chris Lattner41943152007-01-25 04:52:46 +0000709 Record->setHasFlexibleArrayMember(true);
Chris Lattner720a0542007-01-25 00:44:24 +0000710 } else {
711 // If this is a struct/class and this is not the last element, reject
712 // it. Note that GCC supports variable sized arrays in the middle of
713 // structures.
714 if (i != NumFields-1) {
715 Diag(FD->getLocation(), diag::err_variable_sized_type_in_struct,
716 FD->getName());
717 delete FD;
718 continue;
719 }
720
721 // We support flexible arrays at the end of structs in other structs
722 // as an extension.
723 Diag(FD->getLocation(), diag::ext_flexible_array_in_struct,
724 FD->getName());
Chris Lattner41943152007-01-25 04:52:46 +0000725 Record->setHasFlexibleArrayMember(true);
Chris Lattner720a0542007-01-25 00:44:24 +0000726 }
727 }
728 }
729
Chris Lattner82625602007-01-24 02:26:21 +0000730 // Keep track of the number of named members.
Chris Lattnere5a66562007-01-25 22:48:42 +0000731 if (IdentifierInfo *II = FD->getIdentifier()) {
732 // Detect duplicate member names.
Chris Lattnerbaf33662007-01-27 02:14:08 +0000733 if (!FieldIDs.insert(II)) {
Chris Lattnere5a66562007-01-25 22:48:42 +0000734 Diag(FD->getLocation(), diag::err_duplicate_member, II->getName());
735 // Find the previous decl.
736 SourceLocation PrevLoc;
737 for (unsigned i = 0, e = RecFields.size(); ; ++i) {
738 assert(i != e && "Didn't find previous def!");
739 if (RecFields[i]->getIdentifier() == II) {
740 PrevLoc = RecFields[i]->getLocation();
741 break;
742 }
743 }
744 Diag(PrevLoc, diag::err_previous_definition);
745 delete FD;
746 continue;
747 }
Chris Lattner82625602007-01-24 02:26:21 +0000748 ++NumNamedMembers;
Chris Lattnere5a66562007-01-25 22:48:42 +0000749 }
Chris Lattner41943152007-01-25 04:52:46 +0000750
751 // Remember good fields.
752 RecFields.push_back(FD);
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000753 }
Chris Lattner82625602007-01-24 02:26:21 +0000754
755
756 // Okay, we successfully defined 'Record'.
Chris Lattner41943152007-01-25 04:52:46 +0000757 Record->defineBody(&RecFields[0], RecFields.size());
Chris Lattner1300fb92007-01-23 23:42:53 +0000758}
759
Chris Lattnerc1915e22007-01-25 07:29:02 +0000760Sema::DeclTy *Sema::ParseEnumConstant(Scope *S, DeclTy *EnumDeclX,
761 SourceLocation IdLoc, IdentifierInfo *Id,
762 SourceLocation EqualLoc, ExprTy *Val) {
763 EnumDecl *TheEnumDecl = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX));
Chris Lattner8116d1b2007-01-25 22:38:29 +0000764
765 // Verify that there isn't already something declared with this name in this
766 // scope.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000767 if (Decl *PrevDecl = LookupScopedDecl(Id, Decl::IDNS_Ordinary, IdLoc, S)) {
Chris Lattner8116d1b2007-01-25 22:38:29 +0000768 if (S->isDeclScope(PrevDecl)) {
769 if (isa<EnumConstantDecl>(PrevDecl))
770 Diag(IdLoc, diag::err_redefinition_of_enumerator, Id->getName());
771 else
772 Diag(IdLoc, diag::err_redefinition, Id->getName());
773 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
774 return 0;
775 }
776 }
Steve Naroff63969212007-05-07 21:22:42 +0000777 // C99 6.7.2.2p2: Make sure we have an integer constant expression.
778 if (Val && !((Expr *)Val)->isIntegerConstantExpr()) {
779 Diag(IdLoc, diag::err_enum_value_not_integer_constant_expr, Id->getName());
780 return 0;
781 }
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000782 QualType Ty = Context.getTagDeclType(TheEnumDecl);
Steve Naroff63969212007-05-07 21:22:42 +0000783 EnumConstantDecl *New = new EnumConstantDecl(IdLoc, Id, Ty, (Expr *)Val);
Chris Lattner8116d1b2007-01-25 22:38:29 +0000784
785 // Register this decl in the current scope stack.
786 New->setNext(Id->getFETokenInfo<Decl>());
787 Id->setFETokenInfo(New);
788 S->AddDecl(New);
789 return New;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000790}
791
792void Sema::ParseEnumBody(SourceLocation EnumLoc, DeclTy *EnumDeclX,
793 DeclTy **Elements, unsigned NumElements) {
794 EnumDecl *Enum = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX));
795 assert(!Enum->isDefinition() && "Enum redefinitions can't reach here");
796
797 // Verify that all the values are okay.
798 SmallVector<EnumConstantDecl*, 32> Values;
799 for (unsigned i = 0; i != NumElements; ++i) {
800 EnumConstantDecl *ECD =
801 cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i]));
802 if (!ECD) continue; // Already issued a diagnostic.
803
804 Values.push_back(ECD);
805 }
806
807 Enum->defineElements(&Values[0], Values.size());
808}
Chris Lattner1300fb92007-01-23 23:42:53 +0000809
Steve Naroff26c8ea52007-03-21 21:08:52 +0000810void Sema::AddTopLevelDecl(Decl *current, Decl *last) {
811 if (!current) return;
812
813 // If this is a top-level decl that is chained to some other (e.g. int A,B,C;)
814 // remember this in the LastInGroupList list.
815 if (last) {
816 LastInGroupList.push_back((Decl*)last);
817 }
818}