blob: 49c6347cf116bc60c5a76c3f262226300b503d8e [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.
Chris Lattner6d9b49a2007-06-09 05:56:19 +000050 // FIXME: This is also wrong for:
51 // int sub1(int i, char *pi) { typedef int foo[i];
52 // struct bar {foo f1; int f2:3; int f3:4} *p; }
Chris Lattner6046e002007-06-05 06:39:23 +000053 Diag(DeclLoc, diag::err_typecheck_illegal_vla, Size->getSourceRange());
54 return true;
55 }
56
57 // We have a constant expression with an integer type, now make sure
58 // value greater than zero (C99 6.7.5.2p1).
59
60 // FIXME: This check isn't specific to static VLAs, this should be moved
61 // elsewhere or replicated. 'int X[-1];' inside a function should emit an
62 // error.
63 if (SizeVal.isSigned()) {
64 APSInt Zero(SizeVal.getBitWidth());
65 Zero.setIsUnsigned(false);
66 if (SizeVal < Zero) {
67 Diag(DeclLoc, diag::err_typecheck_negative_array_size,
68 Size->getSourceRange());
69 return true;
70 } else if (SizeVal == 0) {
71 // GCC accepts zero sized static arrays.
72 Diag(DeclLoc, diag::err_typecheck_zero_array_size,
73 Size->getSourceRange());
74 }
75 }
76 return false;
Steve Naroff8eeeb132007-05-08 21:09:37 +000077}
Chris Lattnere168f762006-11-10 05:29:30 +000078
Chris Lattner2ebe4bb2006-11-20 01:29:42 +000079Sema::DeclTy *Sema::isTypeName(const IdentifierInfo &II, Scope *S) const {
Chris Lattner32d920b2007-01-26 02:01:53 +000080 return dyn_cast_or_null<TypedefDecl>(II.getFETokenInfo<Decl>());
Chris Lattnere168f762006-11-10 05:29:30 +000081}
82
Chris Lattner302b4be2006-11-19 02:31:38 +000083void Sema::PopScope(SourceLocation Loc, Scope *S) {
84 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
85 I != E; ++I) {
Chris Lattner99d31772007-01-21 22:37:37 +000086 Decl *D = static_cast<Decl*>(*I);
Chris Lattner302b4be2006-11-19 02:31:38 +000087 assert(D && "This decl didn't get pushed??");
Chris Lattnerff65b6b2007-01-23 01:33:16 +000088 IdentifierInfo *II = D->getIdentifier();
89 if (!II) continue;
Chris Lattner302b4be2006-11-19 02:31:38 +000090
Chris Lattnerff65b6b2007-01-23 01:33:16 +000091 // Unlink this decl from the identifier. Because the scope contains decls
92 // in an unordered collection, and because we have multiple identifier
93 // namespaces (e.g. tag, normal, label),the decl may not be the first entry.
94 if (II->getFETokenInfo<Decl>() == D) {
95 // Normal case, no multiple decls in different namespaces.
96 II->setFETokenInfo(D->getNext());
97 } else {
98 // Scan ahead. There are only three namespaces in C, so this loop can
99 // never execute more than 3 times.
100 Decl *SomeDecl = II->getFETokenInfo<Decl>();
101 while (SomeDecl->getNext() != D) {
102 SomeDecl = SomeDecl->getNext();
103 assert(SomeDecl && "Didn't find this decl on its identifier's chain!");
104 }
105 SomeDecl->setNext(D->getNext());
106 }
Chris Lattner302b4be2006-11-19 02:31:38 +0000107
Chris Lattner740b2f32006-11-21 01:32:20 +0000108 // This will have to be revisited for C++: there we want to nest stuff in
109 // namespace decls etc. Even for C, we might want a top-level translation
110 // unit decl or something.
111 if (!CurFunctionDecl)
112 continue;
113
114 // Chain this decl to the containing function, it now owns the memory for
115 // the decl.
116 D->setNext(CurFunctionDecl->getDeclChain());
117 CurFunctionDecl->setDeclChain(D);
Chris Lattner302b4be2006-11-19 02:31:38 +0000118 }
119}
120
Chris Lattner18b19622007-01-22 07:39:13 +0000121/// LookupScopedDecl - Look up the inner-most declaration in the specified
122/// namespace.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000123Decl *Sema::LookupScopedDecl(IdentifierInfo *II, unsigned NSI,
124 SourceLocation IdLoc, Scope *S) {
Chris Lattner18b19622007-01-22 07:39:13 +0000125 if (II == 0) return 0;
Chris Lattnerb6738ec2007-01-28 00:38:24 +0000126 Decl::IdentifierNamespace NS = (Decl::IdentifierNamespace)NSI;
Chris Lattner18b19622007-01-22 07:39:13 +0000127
128 // Scan up the scope chain looking for a decl that matches this identifier
129 // that is in the appropriate namespace. This search should not take long, as
130 // shadowing of names is uncommon, and deep shadowing is extremely uncommon.
131 for (Decl *D = II->getFETokenInfo<Decl>(); D; D = D->getNext())
132 if (D->getIdentifierNamespace() == NS)
133 return D;
Chris Lattnerb6738ec2007-01-28 00:38:24 +0000134
Chris Lattner9561a0b2007-01-28 08:20:04 +0000135 // If we didn't find a use of this identifier, and if the identifier
136 // corresponds to a compiler builtin, create the decl object for the builtin
137 // now, injecting it into translation unit scope, and return it.
138 if (NS == Decl::IDNS_Ordinary) {
139 // If this is a builtin on some other target, or if this builtin varies
140 // across targets (e.g. in type), emit a diagnostic and mark the translation
141 // unit non-portable for using it.
142 if (II->isNonPortableBuiltin()) {
143 // Only emit this diagnostic once for this builtin.
144 II->setNonPortableBuiltin(false);
145 Context.Target.DiagnoseNonPortability(IdLoc,
146 diag::port_target_builtin_use);
147 }
Chris Lattner9561a0b2007-01-28 08:20:04 +0000148 // If this is a builtin on this (or all) targets, create the decl.
149 if (unsigned BuiltinID = II->getBuiltinID())
150 return LazilyCreateBuiltin(II, BuiltinID, S);
151 }
Chris Lattner18b19622007-01-22 07:39:13 +0000152 return 0;
153}
154
Chris Lattner9561a0b2007-01-28 08:20:04 +0000155/// LazilyCreateBuiltin - The specified Builtin-ID was first used at file scope.
156/// lazily create a decl for it.
157Decl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid, Scope *S) {
158 Builtin::ID BID = (Builtin::ID)bid;
159
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000160 QualType R = Context.BuiltinInfo.GetBuiltinType(BID, Context);
Chris Lattner776fac82007-06-09 00:53:06 +0000161 FunctionDecl *New = new FunctionDecl(SourceLocation(), II, R,
162 FunctionDecl::Extern, 0);
Chris Lattner9561a0b2007-01-28 08:20:04 +0000163
164 // Find translation-unit scope to insert this function into.
165 while (S->getParent())
166 S = S->getParent();
167 S->AddDecl(New);
168
169 // Add this decl to the end of the identifier info.
170 if (Decl *LastDecl = II->getFETokenInfo<Decl>()) {
171 // Scan until we find the last (outermost) decl in the id chain.
172 while (LastDecl->getNext())
173 LastDecl = LastDecl->getNext();
174 // Insert before (outside) it.
175 LastDecl->setNext(New);
176 } else {
177 II->setFETokenInfo(New);
178 }
179 // Make sure clients iterating over decls see this.
180 LastInGroupList.push_back(New);
181
182 return New;
183}
184
Chris Lattner01564d92007-01-27 19:27:06 +0000185/// MergeTypeDefDecl - We just parsed a typedef 'New' which has the same name
186/// and scope as a previous declaration 'Old'. Figure out how to resolve this
187/// situation, merging decls or emitting diagnostics as appropriate.
188///
Chris Lattnerc511efb2007-01-27 19:32:14 +0000189TypedefDecl *Sema::MergeTypeDefDecl(TypedefDecl *New, Decl *OldD) {
190 // Verify the old decl was also a typedef.
191 TypedefDecl *Old = dyn_cast<TypedefDecl>(OldD);
192 if (!Old) {
193 Diag(New->getLocation(), diag::err_redefinition_different_kind,
194 New->getName());
195 Diag(OldD->getLocation(), diag::err_previous_definition);
196 return New;
197 }
198
Chris Lattner01564d92007-01-27 19:27:06 +0000199 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
200 // TODO: This is totally simplistic. It should handle merging functions
201 // together etc, merging extern int X; int X; ...
202 Diag(New->getLocation(), diag::err_redefinition, New->getName());
203 Diag(Old->getLocation(), diag::err_previous_definition);
204 return New;
205}
206
207/// MergeFunctionDecl - We just parsed a function 'New' which has the same name
208/// and scope as a previous declaration 'Old'. Figure out how to resolve this
209/// situation, merging decls or emitting diagnostics as appropriate.
210///
Chris Lattnerc511efb2007-01-27 19:32:14 +0000211FunctionDecl *Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD) {
212 // Verify the old decl was also a function.
213 FunctionDecl *Old = dyn_cast<FunctionDecl>(OldD);
214 if (!Old) {
215 Diag(New->getLocation(), diag::err_redefinition_different_kind,
216 New->getName());
217 Diag(OldD->getLocation(), diag::err_previous_definition);
218 return New;
219 }
220
Chris Lattnerefe4aea2007-01-27 19:35:39 +0000221 // This is not right, but it's a start. If 'Old' is a function prototype with
222 // the same type as 'New', silently allow this. FIXME: We should link up decl
223 // objects here.
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000224 if (Old->getBody() == 0 &&
225 Old->getCanonicalType() == New->getCanonicalType()) {
Chris Lattnerefe4aea2007-01-27 19:35:39 +0000226 return New;
227 }
Chris Lattnerc511efb2007-01-27 19:32:14 +0000228
Chris Lattner01564d92007-01-27 19:27:06 +0000229 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
230 // TODO: This is totally simplistic. It should handle merging functions
231 // together etc, merging extern int X; int X; ...
232 Diag(New->getLocation(), diag::err_redefinition, New->getName());
233 Diag(Old->getLocation(), diag::err_previous_definition);
234 return New;
235}
236
237/// MergeVarDecl - We just parsed a variable 'New' which has the same name
238/// and scope as a previous declaration 'Old'. Figure out how to resolve this
239/// situation, merging decls or emitting diagnostics as appropriate.
240///
Steve Narofffc49d672007-04-01 21:27:45 +0000241/// FIXME: Need to carefully consider tentative definition rules (C99 6.9.2p2).
242/// For example, we incorrectly complain about i1, i4 from C99 6.9.2p4.
243///
Chris Lattnerc511efb2007-01-27 19:32:14 +0000244VarDecl *Sema::MergeVarDecl(VarDecl *New, Decl *OldD) {
245 // Verify the old decl was also a variable.
246 VarDecl *Old = dyn_cast<VarDecl>(OldD);
247 if (!Old) {
248 Diag(New->getLocation(), diag::err_redefinition_different_kind,
249 New->getName());
250 Diag(OldD->getLocation(), diag::err_previous_definition);
251 return New;
252 }
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000253 // Verify the types match.
254 if (Old->getCanonicalType() != New->getCanonicalType()) {
255 Diag(New->getLocation(), diag::err_redefinition, New->getName());
256 Diag(Old->getLocation(), diag::err_previous_definition);
257 return New;
258 }
259 // We've verified the types match, now check if Old is "extern".
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000260 if (Old->getStorageClass() != VarDecl::Extern) {
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000261 Diag(New->getLocation(), diag::err_redefinition, New->getName());
262 Diag(Old->getLocation(), diag::err_previous_definition);
263 }
Chris Lattner01564d92007-01-27 19:27:06 +0000264 return New;
265}
266
Chris Lattnerb6738ec2007-01-28 00:38:24 +0000267/// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
268/// no declarator (e.g. "struct foo;") is parsed.
269Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) {
270 // TODO: emit error on 'int;' or 'const enum foo;'.
271 // TODO: emit error on 'typedef int;'
272 // if (!DS.isMissingDeclaratorOk()) Diag(...);
273
274 return 0;
275}
276
Chris Lattner776fac82007-06-09 00:53:06 +0000277Sema::DeclTy *
Chris Lattnere168f762006-11-10 05:29:30 +0000278Sema::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
Chris Lattner776fac82007-06-09 00:53:06 +0000279 DeclTy *lastDeclarator) {
280 Decl *LastDeclarator = (Decl*)lastDeclarator;
Chris Lattnere168f762006-11-10 05:29:30 +0000281 IdentifierInfo *II = D.getIdentifier();
Chris Lattner302b4be2006-11-19 02:31:38 +0000282
Chris Lattner01564d92007-01-27 19:27:06 +0000283 // See if this is a redefinition of a variable in the same scope.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000284 Decl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary,
285 D.getIdentifierLoc(), S);
Chris Lattner776fac82007-06-09 00:53:06 +0000286 if (PrevDecl && !S->isDeclScope(PrevDecl))
Chris Lattner01564d92007-01-27 19:27:06 +0000287 PrevDecl = 0; // If in outer scope, it isn't the same thing.
288
Chris Lattnere168f762006-11-10 05:29:30 +0000289 Decl *New;
Chris Lattner01a7c532007-01-25 23:09:03 +0000290 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Chris Lattner776fac82007-06-09 00:53:06 +0000291 TypedefDecl *NewTD = ParseTypedefDecl(S, D, LastDeclarator);
Chris Lattner01564d92007-01-27 19:27:06 +0000292 if (!NewTD) return 0;
Steve Naroffa8fd9732007-06-11 00:35:03 +0000293
294 // Handle attributes prior to checking for duplicates in MergeVarDecl
295 HandleDeclAttributes(NewTD, D.getDeclSpec().getAttributes(),
296 D.getAttributes());
Chris Lattner01564d92007-01-27 19:27:06 +0000297 // Merge the decl with the existing one if appropriate.
298 if (PrevDecl) {
299 NewTD = MergeTypeDefDecl(NewTD, PrevDecl);
300 if (NewTD == 0) return 0;
301 }
302 New = NewTD;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000303 if (S->getParent() == 0) {
304 // C99 6.7.7p2: If a typedef name specifies a variably modified type
305 // then it shall have block scope.
306 if (ArrayType *ary = dyn_cast<ArrayType>(NewTD->getUnderlyingType())) {
Chris Lattner6046e002007-06-05 06:39:23 +0000307 if (VerifyConstantArrayType(ary, D.getIdentifierLoc()))
Steve Naroff8eeeb132007-05-08 21:09:37 +0000308 return 0;
309 }
310 }
Chris Lattner01a7c532007-01-25 23:09:03 +0000311 } else if (D.isFunctionDeclarator()) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000312 QualType R = GetTypeForDeclarator(D, S);
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000313 if (R.isNull()) return 0; // FIXME: "auto func();" passes through...
Chris Lattner01564d92007-01-27 19:27:06 +0000314
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000315 FunctionDecl::StorageClass SC;
316 switch (D.getDeclSpec().getStorageClassSpec()) {
317 default: assert(0 && "Unknown storage class!");
318 case DeclSpec::SCS_auto:
319 case DeclSpec::SCS_register:
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000320 Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_func,
321 R.getAsString());
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000322 return 0;
323 case DeclSpec::SCS_unspecified: SC = FunctionDecl::None; break;
324 case DeclSpec::SCS_extern: SC = FunctionDecl::Extern; break;
325 case DeclSpec::SCS_static: SC = FunctionDecl::Static; break;
326 }
327
Chris Lattner776fac82007-06-09 00:53:06 +0000328 FunctionDecl *NewFD = new FunctionDecl(D.getIdentifierLoc(), II, R, SC,
329 LastDeclarator);
Chris Lattner01564d92007-01-27 19:27:06 +0000330
331 // Merge the decl with the existing one if appropriate.
332 if (PrevDecl) {
333 NewFD = MergeFunctionDecl(NewFD, PrevDecl);
334 if (NewFD == 0) return 0;
335 }
336 New = NewFD;
Chris Lattner01a7c532007-01-25 23:09:03 +0000337 } else {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000338 QualType R = GetTypeForDeclarator(D, S);
Chris Lattner01a7c532007-01-25 23:09:03 +0000339 if (R.isNull()) return 0;
Chris Lattner01564d92007-01-27 19:27:06 +0000340
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000341 VarDecl *NewVD;
342 VarDecl::StorageClass SC;
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000343 switch (D.getDeclSpec().getStorageClassSpec()) {
344 default: assert(0 && "Unknown storage class!");
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000345 case DeclSpec::SCS_unspecified: SC = VarDecl::None; break;
346 case DeclSpec::SCS_extern: SC = VarDecl::Extern; break;
347 case DeclSpec::SCS_static: SC = VarDecl::Static; break;
348 case DeclSpec::SCS_auto: SC = VarDecl::Auto; break;
349 case DeclSpec::SCS_register: SC = VarDecl::Register; break;
350 }
Steve Narofffc49d672007-04-01 21:27:45 +0000351 if (S->getParent() == 0) {
352 // File scope. C99 6.9.2p2: A declaration of an identifier for and
353 // object that has file scope without an initializer, and without a
354 // storage-class specifier or with the storage-class specifier "static",
355 // constitutes a tentative definition. Note: A tentative definition with
356 // external linkage is valid (C99 6.2.2p5).
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000357 if (!Init && SC == VarDecl::Static) {
Steve Narofffc49d672007-04-01 21:27:45 +0000358 // C99 6.9.2p3: If the declaration of an identifier for an object is
359 // a tentative definition and has internal linkage (C99 6.2.2p3), the
360 // declared type shall not be an incomplete type.
361 if (R->isIncompleteType()) {
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000362 Diag(D.getIdentifierLoc(), diag::err_typecheck_decl_incomplete_type,
363 R.getAsString());
Steve Narofffc49d672007-04-01 21:27:45 +0000364 return 0;
365 }
Steve Naroffca8f7122007-04-01 01:41:35 +0000366 }
Bill Wendlingd6de6572007-06-02 09:40:07 +0000367 // C99 6.9p2: The storage-class specifiers auto and register shall not
368 // appear in the declaration specifiers in an external declaration.
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000369 if (SC == VarDecl::Auto || SC == VarDecl::Register) {
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000370 Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope,
371 R.getAsString());
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000372 return 0;
373 }
Steve Naroff8eeeb132007-05-08 21:09:37 +0000374 // C99 6.7.5.2p2: If an identifier is declared to be an object with
375 // static storage duration, it shall not have a variable length array.
Chris Lattner6046e002007-06-05 06:39:23 +0000376 if (ArrayType *ary = dyn_cast<ArrayType>(R.getCanonicalType())) {
377 if (VerifyConstantArrayType(ary, D.getIdentifierLoc()))
Steve Naroff8eeeb132007-05-08 21:09:37 +0000378 return 0;
379 }
Chris Lattner776fac82007-06-09 00:53:06 +0000380 NewVD = new FileVarDecl(D.getIdentifierLoc(), II, R, SC, LastDeclarator);
Steve Narofffc49d672007-04-01 21:27:45 +0000381 } else {
382 // Block scope. C99 6.7p7: If an identifier for an object is declared with
383 // no linkage (C99 6.2.2p6), the type for the object shall be complete...
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000384 if (SC != VarDecl::Extern) {
Steve Narofffc49d672007-04-01 21:27:45 +0000385 if (R->isIncompleteType()) {
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000386 Diag(D.getIdentifierLoc(), diag::err_typecheck_decl_incomplete_type,
387 R.getAsString());
Steve Narofffc49d672007-04-01 21:27:45 +0000388 return 0;
389 }
390 }
Steve Naroff8eeeb132007-05-08 21:09:37 +0000391 if (SC == VarDecl::Static) {
392 // C99 6.7.5.2p2: If an identifier is declared to be an object with
393 // static storage duration, it shall not have a variable length array.
Chris Lattner6046e002007-06-05 06:39:23 +0000394 if (ArrayType *ary = dyn_cast<ArrayType>(R.getCanonicalType())) {
395 if (VerifyConstantArrayType(ary, D.getIdentifierLoc()))
Steve Naroff8eeeb132007-05-08 21:09:37 +0000396 return 0;
397 }
398 }
Chris Lattner776fac82007-06-09 00:53:06 +0000399 NewVD = new BlockVarDecl(D.getIdentifierLoc(), II, R, SC, LastDeclarator);
Steve Naroffef2ab6a2007-04-02 20:50:54 +0000400 }
Steve Naroffa8fd9732007-06-11 00:35:03 +0000401 // Handle attributes prior to checking for duplicates in MergeVarDecl
402 HandleDeclAttributes(NewVD, D.getDeclSpec().getAttributes(),
403 D.getAttributes());
404
Chris Lattner01564d92007-01-27 19:27:06 +0000405 // Merge the decl with the existing one if appropriate.
406 if (PrevDecl) {
407 NewVD = MergeVarDecl(NewVD, PrevDecl);
408 if (NewVD == 0) return 0;
409 }
410 New = NewVD;
Chris Lattner01a7c532007-01-25 23:09:03 +0000411 }
Chris Lattner302b4be2006-11-19 02:31:38 +0000412
Chris Lattnere168f762006-11-10 05:29:30 +0000413 // If this has an identifier, add it to the scope stack.
414 if (II) {
Chris Lattner18b19622007-01-22 07:39:13 +0000415 New->setNext(II->getFETokenInfo<Decl>());
Chris Lattnere168f762006-11-10 05:29:30 +0000416 II->setFETokenInfo(New);
Chris Lattner99d31772007-01-21 22:37:37 +0000417 S->AddDecl(New);
Chris Lattnere168f762006-11-10 05:29:30 +0000418 }
419
Steve Naroff26c8ea52007-03-21 21:08:52 +0000420 if (S->getParent() == 0)
Chris Lattner776fac82007-06-09 00:53:06 +0000421 AddTopLevelDecl(New, LastDeclarator);
Chris Lattnere168f762006-11-10 05:29:30 +0000422
423 return New;
424}
425
Chris Lattner776fac82007-06-09 00:53:06 +0000426/// The declarators are chained together backwards, reverse the list.
427Sema::DeclTy *Sema::FinalizeDeclaratorGroup(Scope *S, DeclTy *group) {
428 // Often we have single declarators, handle them quickly.
429 Decl *Group = static_cast<Decl*>(group);
Chris Lattnerb2dd2412007-06-09 06:16:32 +0000430 if (Group == 0 || Group->getNextDeclarator() == 0) return Group;
Chris Lattner776fac82007-06-09 00:53:06 +0000431
432 Decl *NewGroup = 0;
433 while (Group) {
434 Decl *Next = Group->getNextDeclarator();
435 Group->setNextDeclarator(NewGroup);
436 NewGroup = Group;
437 Group = Next;
438 }
439 return NewGroup;
440}
441
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000442VarDecl *
443Sema::ParseParamDeclarator(DeclaratorChunk &FTI, unsigned ArgNo,
444 Scope *FnScope) {
445 const DeclaratorChunk::ParamInfo &PI = FTI.Fun.ArgInfo[ArgNo];
Chris Lattner200bdc32006-11-19 02:43:37 +0000446
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000447 IdentifierInfo *II = PI.Ident;
Chris Lattnerc284e9b2007-01-23 05:14:32 +0000448 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
449 // Can this happen for params? We already checked that they don't conflict
450 // among each other. Here they can only shadow globals, which is ok.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000451 if (Decl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary,
452 PI.IdentLoc, FnScope)) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000453
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000454 }
455
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000456 // FIXME: Handle storage class (auto, register). No declarator?
Chris Lattner776fac82007-06-09 00:53:06 +0000457 // TODO: Chain to previous parameter with the prevdeclarator chain?
Steve Naroffef2ab6a2007-04-02 20:50:54 +0000458 VarDecl *New = new ParmVarDecl(PI.IdentLoc, II,
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000459 QualType::getFromOpaquePtr(PI.TypeInfo),
Chris Lattner776fac82007-06-09 00:53:06 +0000460 VarDecl::None, 0);
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000461
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000462 // If this has an identifier, add it to the scope stack.
463 if (II) {
Chris Lattner18b19622007-01-22 07:39:13 +0000464 New->setNext(II->getFETokenInfo<Decl>());
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000465 II->setFETokenInfo(New);
Chris Lattner99d31772007-01-21 22:37:37 +0000466 FnScope->AddDecl(New);
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000467 }
Chris Lattner229ce602006-11-21 01:21:07 +0000468
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000469 return New;
470}
471
472
473Sema::DeclTy *Sema::ParseStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) {
Chris Lattner229ce602006-11-21 01:21:07 +0000474 assert(CurFunctionDecl == 0 && "Function parsing confused");
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000475 assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
476 "Not a function declarator!");
477 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
478
479 // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared'
480 // for a K&R function.
481 if (!FTI.hasPrototype) {
482 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
483 if (FTI.ArgInfo[i].TypeInfo == 0) {
Chris Lattner843c5922007-06-10 23:40:34 +0000484 Diag(FTI.ArgInfo[i].IdentLoc, diag::ext_param_not_declared,
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000485 FTI.ArgInfo[i].Ident->getName());
486 // Implicitly declare the argument as type 'int' for lack of a better
487 // type.
488 FTI.ArgInfo[i].TypeInfo = Context.IntTy.getAsOpaquePtr();
489 }
490 }
491
492 // Since this is a function definition, act as though we have information
493 // about the arguments.
494 FTI.hasPrototype = true;
Chris Lattner2114d5e2006-12-04 07:40:24 +0000495 } else {
496 // FIXME: Diagnose arguments without names in C.
497
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000498 }
499
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000500 Scope *GlobalScope = FnBodyScope->getParent();
501
502 FunctionDecl *FD =
503 static_cast<FunctionDecl*>(ParseDeclarator(GlobalScope, D, 0, 0));
Chris Lattner229ce602006-11-21 01:21:07 +0000504 CurFunctionDecl = FD;
Chris Lattner2114d5e2006-12-04 07:40:24 +0000505
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000506 // Create Decl objects for each parameter, adding them to the FunctionDecl.
507 SmallVector<VarDecl*, 16> Params;
Chris Lattnerf61c8a82007-01-21 19:04:43 +0000508
509 // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes
510 // no arguments, not a function that takes a single void argument.
511 if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 &&
512 FTI.ArgInfo[0].TypeInfo == Context.VoidTy.getAsOpaquePtr()) {
Chris Lattner99d31772007-01-21 22:37:37 +0000513 // empty arg list, don't push any params.
Chris Lattnerf61c8a82007-01-21 19:04:43 +0000514 } else {
515 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i)
516 Params.push_back(ParseParamDeclarator(D.getTypeObject(0), i,FnBodyScope));
517 }
Chris Lattner2114d5e2006-12-04 07:40:24 +0000518
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000519 FD->setParams(&Params[0], Params.size());
Chris Lattner2114d5e2006-12-04 07:40:24 +0000520
Chris Lattnere168f762006-11-10 05:29:30 +0000521 return FD;
522}
523
Chris Lattner229ce602006-11-21 01:21:07 +0000524Sema::DeclTy *Sema::ParseFunctionDefBody(DeclTy *D, StmtTy *Body) {
525 FunctionDecl *FD = static_cast<FunctionDecl*>(D);
526 FD->setBody((Stmt*)Body);
527
528 assert(FD == CurFunctionDecl && "Function parsing confused");
529 CurFunctionDecl = 0;
Chris Lattnere2473062007-05-28 06:28:18 +0000530
531 // Verify and clean out per-function state.
532
533 // Check goto/label use.
534 for (DenseMap<IdentifierInfo*, LabelStmt*>::iterator I = LabelMap.begin(),
535 E = LabelMap.end(); I != E; ++I) {
536 // Verify that we have no forward references left. If so, there was a goto
537 // or address of a label taken, but no definition of it. Label fwd
538 // definitions are indicated with a null substmt.
539 if (I->second->getSubStmt() == 0) {
540 LabelStmt *L = I->second;
541 // Emit error.
Chris Lattnereefa10e2007-05-28 06:56:27 +0000542 Diag(L->getIdentLoc(), diag::err_undeclared_label_use, L->getName());
Chris Lattnere2473062007-05-28 06:28:18 +0000543
544 // At this point, we have gotos that use the bogus label. Stitch it into
545 // the function body so that they aren't leaked and that the AST is well
546 // formed.
547 L->setSubStmt(new NullStmt(L->getIdentLoc()));
548 cast<CompoundStmt>((Stmt*)Body)->push_back(L);
549 }
550 }
551 LabelMap.clear();
552
Chris Lattner229ce602006-11-21 01:21:07 +0000553 return FD;
554}
555
556
Chris Lattnerac18be92006-11-20 06:49:47 +0000557/// ImplicitlyDefineFunction - An undeclared identifier was used in a function
558/// call, forming a call to an implicitly defined function (per C99 6.5.1p2).
559Decl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II,
560 Scope *S) {
561 if (getLangOptions().C99) // Extension in C99.
562 Diag(Loc, diag::ext_implicit_function_decl, II.getName());
563 else // Legal in C90, but warn about it.
564 Diag(Loc, diag::warn_implicit_function_decl, II.getName());
565
566 // FIXME: handle stuff like:
567 // void foo() { extern float X(); }
568 // void bar() { X(); } <-- implicit decl for X in another scope.
569
570 // Set a Declarator for the implicit definition: int foo();
Chris Lattner353f5742006-11-28 04:50:12 +0000571 const char *Dummy;
Chris Lattnerac18be92006-11-20 06:49:47 +0000572 DeclSpec DS;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000573 bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy);
Chris Lattnerb055f2d2007-02-11 08:19:57 +0000574 Error = Error; // Silence warning.
Chris Lattner353f5742006-11-28 04:50:12 +0000575 assert(!Error && "Error setting up implicit decl!");
Chris Lattnerac18be92006-11-20 06:49:47 +0000576 Declarator D(DS, Declarator::BlockContext);
Chris Lattnercbc426d2006-12-02 06:43:02 +0000577 D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, Loc));
Chris Lattnerac18be92006-11-20 06:49:47 +0000578 D.SetIdentifier(&II, Loc);
579
Chris Lattner62d2e662007-01-28 00:21:37 +0000580 // Find translation-unit scope to insert this function into.
581 while (S->getParent())
582 S = S->getParent();
Chris Lattnerac18be92006-11-20 06:49:47 +0000583
Chris Lattner62d2e662007-01-28 00:21:37 +0000584 return static_cast<Decl*>(ParseDeclarator(S, D, 0, 0));
Chris Lattnerac18be92006-11-20 06:49:47 +0000585}
586
Chris Lattner302b4be2006-11-19 02:31:38 +0000587
Chris Lattner776fac82007-06-09 00:53:06 +0000588TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D,
589 Decl *LastDeclarator) {
590 assert(D.getIdentifier() && "Wrong callback for declspec without declarator");
Chris Lattner302b4be2006-11-19 02:31:38 +0000591
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000592 QualType T = GetTypeForDeclarator(D, S);
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000593 if (T.isNull()) return 0;
594
Chris Lattner18b19622007-01-22 07:39:13 +0000595 // Scope manipulation handled by caller.
Chris Lattner776fac82007-06-09 00:53:06 +0000596 return new TypedefDecl(D.getIdentifierLoc(), D.getIdentifier(), T,
597 LastDeclarator);
Chris Lattnere168f762006-11-10 05:29:30 +0000598}
599
Chris Lattner18b19622007-01-22 07:39:13 +0000600
Chris Lattner1300fb92007-01-23 23:42:53 +0000601/// ParseTag - This is invoked when we see 'struct foo' or 'struct {'. In the
602/// former case, Name will be non-null. In the later case, Name will be null.
603/// TagType indicates what kind of tag this is. TK indicates whether this is a
604/// reference/declaration/definition of a tag.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000605Sema::DeclTy *Sema::ParseTag(Scope *S, unsigned TagType, TagKind TK,
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000606 SourceLocation KWLoc, IdentifierInfo *Name,
Steve Naroffb3096442007-06-09 03:47:53 +0000607 SourceLocation NameLoc, AttributeList *Attr) {
Chris Lattner8799cf22007-01-23 01:57:16 +0000608 // If this is a use of an existing tag, it must have a name.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000609 assert((Name != 0 || TK == TK_Definition) &&
610 "Nameless record must be a definition!");
Chris Lattner8799cf22007-01-23 01:57:16 +0000611
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000612 Decl::Kind Kind;
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000613 switch (TagType) {
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000614 default: assert(0 && "Unknown tag type!");
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000615 case DeclSpec::TST_struct: Kind = Decl::Struct; break;
616 case DeclSpec::TST_union: Kind = Decl::Union; break;
617//case DeclSpec::TST_class: Kind = Decl::Class; break;
618 case DeclSpec::TST_enum: Kind = Decl::Enum; break;
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000619 }
Chris Lattner7e783a12007-01-23 02:05:42 +0000620
Chris Lattner18b19622007-01-22 07:39:13 +0000621 // If this is a named struct, check to see if there was a previous forward
622 // declaration or definition.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000623 if (TagDecl *PrevDecl =
Chris Lattner9561a0b2007-01-28 08:20:04 +0000624 dyn_cast_or_null<TagDecl>(LookupScopedDecl(Name, Decl::IDNS_Tag,
625 NameLoc, S))) {
Chris Lattner8799cf22007-01-23 01:57:16 +0000626
627 // If this is a use of a previous tag, or if the tag is already declared in
628 // the same scope (so that the definition/declaration completes or
629 // rementions the tag), reuse the decl.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000630 if (TK == TK_Reference || S->isDeclScope(PrevDecl)) {
Chris Lattner7e783a12007-01-23 02:05:42 +0000631 // Make sure that this wasn't declared as an enum and now used as a struct
632 // or something similar.
633 if (PrevDecl->getKind() != Kind) {
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000634 Diag(KWLoc, diag::err_use_with_wrong_tag, Name->getName());
Chris Lattner7e783a12007-01-23 02:05:42 +0000635 Diag(PrevDecl->getLocation(), diag::err_previous_use);
636 }
Chris Lattner7b9ace62007-01-23 20:11:08 +0000637
638 // If this is a use or a forward declaration, we're good.
639 if (TK != TK_Definition)
640 return PrevDecl;
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000641
Chris Lattner7b9ace62007-01-23 20:11:08 +0000642 // Diagnose attempts to redefine a tag.
643 if (PrevDecl->isDefinition()) {
644 Diag(NameLoc, diag::err_redefinition, Name->getName());
645 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
646 // If this is a redefinition, recover by making this struct be
647 // anonymous, which will make any later references get the previous
648 // definition.
649 Name = 0;
650 } else {
651 // Okay, this is definition of a previously declared or referenced tag.
652 // Move the location of the decl to be the definition site.
653 PrevDecl->setLocation(NameLoc);
Chris Lattner7b9ace62007-01-23 20:11:08 +0000654 return PrevDecl;
655 }
Chris Lattner8799cf22007-01-23 01:57:16 +0000656 }
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000657 // If we get here, this is a definition of a new struct type in a nested
658 // scope, e.g. "struct foo; void bar() { struct foo; }", just create a new
659 // type.
Chris Lattner18b19622007-01-22 07:39:13 +0000660 }
661
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000662 // If there is an identifier, use the location of the identifier as the
663 // location of the decl, otherwise use the location of the struct/union
664 // keyword.
665 SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc;
666
Chris Lattner18b19622007-01-22 07:39:13 +0000667 // Otherwise, if this is the first time we've seen this tag, create the decl.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000668 TagDecl *New;
Chris Lattner720a0542007-01-25 00:44:24 +0000669 switch (Kind) {
670 default: assert(0 && "Unknown tag kind!");
Chris Lattner5f521502007-01-25 06:27:24 +0000671 case Decl::Enum:
Chris Lattner776fac82007-06-09 00:53:06 +0000672 // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.:
673 // enum X { A, B, C } D; D should chain to X.
674 New = new EnumDecl(Loc, Name, 0);
Chris Lattner5f521502007-01-25 06:27:24 +0000675 // If this is an undefined enum, warn.
Chris Lattnerc1915e22007-01-25 07:29:02 +0000676 if (TK != TK_Definition) Diag(Loc, diag::ext_forward_ref_enum);
Chris Lattner5f521502007-01-25 06:27:24 +0000677 break;
Chris Lattner720a0542007-01-25 00:44:24 +0000678 case Decl::Union:
679 case Decl::Struct:
680 case Decl::Class:
Chris Lattner776fac82007-06-09 00:53:06 +0000681 // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.:
682 // struct X { int A; } D; D should chain to X.
683 New = new RecordDecl(Kind, Loc, Name, 0);
Chris Lattner720a0542007-01-25 00:44:24 +0000684 break;
685 }
Chris Lattner18b19622007-01-22 07:39:13 +0000686
687 // If this has an identifier, add it to the scope stack.
688 if (Name) {
689 New->setNext(Name->getFETokenInfo<Decl>());
690 Name->setFETokenInfo(New);
691 S->AddDecl(New);
692 }
693
694 return New;
695}
Chris Lattner1300fb92007-01-23 23:42:53 +0000696
697/// ParseField - Each field of a struct/union/class is passed into this in order
698/// to create a FieldDecl object for it.
699Sema::DeclTy *Sema::ParseField(Scope *S, DeclTy *TagDecl,
700 SourceLocation DeclStart,
701 Declarator &D, ExprTy *BitfieldWidth) {
702 IdentifierInfo *II = D.getIdentifier();
703 Expr *BitWidth = (Expr*)BitfieldWidth;
704
705 SourceLocation Loc = DeclStart;
706 if (II) Loc = D.getIdentifierLoc();
707
Chris Lattner62d2e662007-01-28 00:21:37 +0000708 // FIXME: Unnamed fields can be handled in various different ways, for
709 // example, unnamed unions inject all members into the struct namespace!
710
711
Chris Lattner1300fb92007-01-23 23:42:53 +0000712 if (BitWidth) {
713 // TODO: Validate.
Steve Narofff84d11f2007-05-23 21:48:04 +0000714 //printf("WARNING: BITFIELDS IGNORED!\n");
Chris Lattner1300fb92007-01-23 23:42:53 +0000715
716 // 6.7.2.1p3
717 // 6.7.2.1p4
718
719 } else {
720 // Not a bitfield.
721
722 // validate II.
723
724 }
725
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000726 QualType T = GetTypeForDeclarator(D, S);
Chris Lattner01a7c532007-01-25 23:09:03 +0000727 if (T.isNull()) return 0;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000728
729 // C99 6.7.2.1p8: A member of a structure or union may have any type other
730 // than a variably modified type.
Chris Lattner6046e002007-06-05 06:39:23 +0000731 if (ArrayType *ary = dyn_cast<ArrayType>(T.getCanonicalType())) {
732 if (VerifyConstantArrayType(ary, Loc))
Steve Naroff8eeeb132007-05-08 21:09:37 +0000733 return 0;
734 }
Chris Lattner776fac82007-06-09 00:53:06 +0000735
736 // FIXME: Chain fielddecls together.
737 return new FieldDecl(Loc, II, T, 0);
Chris Lattner1300fb92007-01-23 23:42:53 +0000738}
739
740void Sema::ParseRecordBody(SourceLocation RecLoc, DeclTy *RecDecl,
741 DeclTy **Fields, unsigned NumFields) {
Chris Lattnerc1915e22007-01-25 07:29:02 +0000742 RecordDecl *Record = cast<RecordDecl>(static_cast<Decl*>(RecDecl));
Chris Lattner1300fb92007-01-23 23:42:53 +0000743 if (Record->isDefinition()) {
744 // Diagnose code like:
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000745 // struct S { struct S {} X; };
Chris Lattner1300fb92007-01-23 23:42:53 +0000746 // We discover this when we complete the outer S. Reject and ignore the
747 // outer S.
748 Diag(Record->getLocation(), diag::err_nested_redefinition,
749 Record->getKindName());
750 Diag(RecLoc, diag::err_previous_definition);
751 return;
752 }
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000753
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000754 // Verify that all the fields are okay.
Chris Lattner82625602007-01-24 02:26:21 +0000755 unsigned NumNamedMembers = 0;
Steve Naroffcc321422007-03-26 23:09:51 +0000756 SmallVector<FieldDecl*, 32> RecFields;
Chris Lattnere5a66562007-01-25 22:48:42 +0000757 SmallSet<const IdentifierInfo*, 32> FieldIDs;
758
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000759 for (unsigned i = 0; i != NumFields; ++i) {
760 FieldDecl *FD = cast_or_null<FieldDecl>(static_cast<Decl*>(Fields[i]));
761 if (!FD) continue; // Already issued a diagnostic.
Chris Lattner720a0542007-01-25 00:44:24 +0000762
763 // Get the type for the field.
Steve Naroffd50c88e2007-04-05 21:15:20 +0000764 Type *FDTy = FD->getType().getCanonicalType().getTypePtr();
Chris Lattner720a0542007-01-25 00:44:24 +0000765
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000766 // C99 6.7.2.1p2 - A field may not be a function type.
Chris Lattner720a0542007-01-25 00:44:24 +0000767 if (isa<FunctionType>(FDTy)) {
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000768 Diag(FD->getLocation(), diag::err_field_declared_as_function,
769 FD->getName());
Chris Lattner82625602007-01-24 02:26:21 +0000770 delete FD;
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000771 continue;
772 }
773
Chris Lattner82625602007-01-24 02:26:21 +0000774 // C99 6.7.2.1p2 - A field may not be an incomplete type except...
Chris Lattner720a0542007-01-25 00:44:24 +0000775 if (FDTy->isIncompleteType()) {
Chris Lattner82625602007-01-24 02:26:21 +0000776 if (i != NumFields-1 || // ... that the last member ...
777 Record->getKind() != Decl::Struct || // ... of a structure ...
Chris Lattner720a0542007-01-25 00:44:24 +0000778 !isa<ArrayType>(FDTy)) { //... may have incomplete array type.
Chris Lattner82625602007-01-24 02:26:21 +0000779 Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName());
780 delete FD;
781 continue;
782 }
Chris Lattner720a0542007-01-25 00:44:24 +0000783 if (NumNamedMembers < 1) { //... must have more than named member ...
Chris Lattner82625602007-01-24 02:26:21 +0000784 Diag(FD->getLocation(), diag::err_flexible_array_empty_struct,
785 FD->getName());
786 delete FD;
787 continue;
788 }
Chris Lattner720a0542007-01-25 00:44:24 +0000789
790 // Okay, we have a legal flexible array member at the end of the struct.
Chris Lattner41943152007-01-25 04:52:46 +0000791 Record->setHasFlexibleArrayMember(true);
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000792 }
Chris Lattner720a0542007-01-25 00:44:24 +0000793
794
795 /// C99 6.7.2.1p2 - a struct ending in a flexible array member cannot be the
796 /// field of another structure or the element of an array.
797 if (RecordType *FDTTy = dyn_cast<RecordType>(FDTy)) {
798 if (FDTTy->getDecl()->hasFlexibleArrayMember()) {
799 // If this is a member of a union, then entire union becomes "flexible".
800 if (Record->getKind() == Decl::Union) {
Chris Lattner41943152007-01-25 04:52:46 +0000801 Record->setHasFlexibleArrayMember(true);
Chris Lattner720a0542007-01-25 00:44:24 +0000802 } else {
803 // If this is a struct/class and this is not the last element, reject
804 // it. Note that GCC supports variable sized arrays in the middle of
805 // structures.
806 if (i != NumFields-1) {
807 Diag(FD->getLocation(), diag::err_variable_sized_type_in_struct,
808 FD->getName());
809 delete FD;
810 continue;
811 }
812
813 // We support flexible arrays at the end of structs in other structs
814 // as an extension.
815 Diag(FD->getLocation(), diag::ext_flexible_array_in_struct,
816 FD->getName());
Chris Lattner41943152007-01-25 04:52:46 +0000817 Record->setHasFlexibleArrayMember(true);
Chris Lattner720a0542007-01-25 00:44:24 +0000818 }
819 }
820 }
821
Chris Lattner82625602007-01-24 02:26:21 +0000822 // Keep track of the number of named members.
Chris Lattnere5a66562007-01-25 22:48:42 +0000823 if (IdentifierInfo *II = FD->getIdentifier()) {
824 // Detect duplicate member names.
Chris Lattnerbaf33662007-01-27 02:14:08 +0000825 if (!FieldIDs.insert(II)) {
Chris Lattnere5a66562007-01-25 22:48:42 +0000826 Diag(FD->getLocation(), diag::err_duplicate_member, II->getName());
827 // Find the previous decl.
828 SourceLocation PrevLoc;
829 for (unsigned i = 0, e = RecFields.size(); ; ++i) {
830 assert(i != e && "Didn't find previous def!");
831 if (RecFields[i]->getIdentifier() == II) {
832 PrevLoc = RecFields[i]->getLocation();
833 break;
834 }
835 }
836 Diag(PrevLoc, diag::err_previous_definition);
837 delete FD;
838 continue;
839 }
Chris Lattner82625602007-01-24 02:26:21 +0000840 ++NumNamedMembers;
Chris Lattnere5a66562007-01-25 22:48:42 +0000841 }
Chris Lattner41943152007-01-25 04:52:46 +0000842
843 // Remember good fields.
844 RecFields.push_back(FD);
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000845 }
Chris Lattner82625602007-01-24 02:26:21 +0000846
847
848 // Okay, we successfully defined 'Record'.
Chris Lattner41943152007-01-25 04:52:46 +0000849 Record->defineBody(&RecFields[0], RecFields.size());
Chris Lattner1300fb92007-01-23 23:42:53 +0000850}
851
Chris Lattnerc1915e22007-01-25 07:29:02 +0000852Sema::DeclTy *Sema::ParseEnumConstant(Scope *S, DeclTy *EnumDeclX,
853 SourceLocation IdLoc, IdentifierInfo *Id,
854 SourceLocation EqualLoc, ExprTy *Val) {
855 EnumDecl *TheEnumDecl = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX));
Chris Lattner8116d1b2007-01-25 22:38:29 +0000856
857 // Verify that there isn't already something declared with this name in this
858 // scope.
Chris Lattner9561a0b2007-01-28 08:20:04 +0000859 if (Decl *PrevDecl = LookupScopedDecl(Id, Decl::IDNS_Ordinary, IdLoc, S)) {
Chris Lattner8116d1b2007-01-25 22:38:29 +0000860 if (S->isDeclScope(PrevDecl)) {
861 if (isa<EnumConstantDecl>(PrevDecl))
862 Diag(IdLoc, diag::err_redefinition_of_enumerator, Id->getName());
863 else
864 Diag(IdLoc, diag::err_redefinition, Id->getName());
865 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
866 return 0;
867 }
868 }
Steve Naroff72cada02007-05-18 00:18:52 +0000869 SourceLocation expLoc;
Steve Naroff63969212007-05-07 21:22:42 +0000870 // C99 6.7.2.2p2: Make sure we have an integer constant expression.
Chris Lattner6046e002007-06-05 06:39:23 +0000871 // FIXME: Capture this value in the enumconstantdecl.
Chris Lattner238cbc52007-06-02 04:48:48 +0000872 if (Val && !((Expr *)Val)->isIntegerConstantExpr(&expLoc)) {
Steve Naroff72cada02007-05-18 00:18:52 +0000873 Diag(expLoc, diag::err_enum_value_not_integer_constant_expr, Id->getName());
Steve Naroff63969212007-05-07 21:22:42 +0000874 return 0;
875 }
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000876 QualType Ty = Context.getTagDeclType(TheEnumDecl);
Chris Lattner776fac82007-06-09 00:53:06 +0000877 // FIXME: Chain EnumConstantDecl's together.
878 EnumConstantDecl *New = new EnumConstantDecl(IdLoc, Id, Ty, (Expr *)Val, 0);
Chris Lattner8116d1b2007-01-25 22:38:29 +0000879
880 // Register this decl in the current scope stack.
881 New->setNext(Id->getFETokenInfo<Decl>());
882 Id->setFETokenInfo(New);
883 S->AddDecl(New);
884 return New;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000885}
886
887void Sema::ParseEnumBody(SourceLocation EnumLoc, DeclTy *EnumDeclX,
888 DeclTy **Elements, unsigned NumElements) {
889 EnumDecl *Enum = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX));
890 assert(!Enum->isDefinition() && "Enum redefinitions can't reach here");
891
892 // Verify that all the values are okay.
893 SmallVector<EnumConstantDecl*, 32> Values;
894 for (unsigned i = 0; i != NumElements; ++i) {
895 EnumConstantDecl *ECD =
896 cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i]));
897 if (!ECD) continue; // Already issued a diagnostic.
898
899 Values.push_back(ECD);
900 }
901
902 Enum->defineElements(&Values[0], Values.size());
903}
Chris Lattner1300fb92007-01-23 23:42:53 +0000904
Steve Naroff26c8ea52007-03-21 21:08:52 +0000905void Sema::AddTopLevelDecl(Decl *current, Decl *last) {
906 if (!current) return;
907
908 // If this is a top-level decl that is chained to some other (e.g. int A,B,C;)
909 // remember this in the LastInGroupList list.
Chris Lattner776fac82007-06-09 00:53:06 +0000910 if (last)
Steve Naroff26c8ea52007-03-21 21:08:52 +0000911 LastInGroupList.push_back((Decl*)last);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000912}
Steve Naroffa8fd9732007-06-11 00:35:03 +0000913
914void Sema::HandleDeclAttribute(Decl *New, AttributeList *rawAttr) {
915 if (strcmp(rawAttr->getAttributeName()->getName(), "vector_size") == 0) {
916 if (ValueDecl *vDecl = dyn_cast<ValueDecl>(New)) {
917 HandleVectorTypeAttribute(vDecl->getType(), rawAttr);
918 // install the new vector type into the decl
919 }
920 if (TypedefDecl *tDecl = dyn_cast<TypedefDecl>(New)) {
921 HandleVectorTypeAttribute(tDecl->getUnderlyingType(), rawAttr);
922 // install the new vector type into the decl
923 }
924 }
925 // FIXME: add other attributes...
926}
927
928void Sema::HandleDeclAttributes(Decl *New, AttributeList *declspec_prefix,
929 AttributeList *declarator_postfix) {
930 while (declspec_prefix) {
931 HandleDeclAttribute(New, declspec_prefix);
932 declspec_prefix = declspec_prefix->getNext();
933 }
934 while (declarator_postfix) {
935 HandleDeclAttribute(New, declarator_postfix);
936 declarator_postfix = declarator_postfix->getNext();
937 }
938}
939
940void *Sema::HandleVectorTypeAttribute(QualType curType,
941 AttributeList *rawAttr) {
942 // check the attribute arugments
943 if (rawAttr->getNumArgs() != 1) {
944 Diag(rawAttr->getAttributeLoc(), diag::err_attribute_wrong_number_arguments,
945 std::string("1"));
946 return 0;
947 }
948 Expr *sizeExpr = static_cast<Expr *>(rawAttr->getArg(0));
949 APSInt vecSize(32);
950 if (!sizeExpr->isIntegerConstantExpr(vecSize)) {
951 Diag(rawAttr->getAttributeLoc(), diag::err_attribute_vector_size_not_int,
952 sizeExpr->getSourceRange());
953 return 0;
954 }
955 // navigate to the base type - we need to provide for vector pointers,
956 // vector arrays, and functions returning vectors.
957 Type *canonType = curType.getCanonicalType().getTypePtr();
958
959 while (canonType->isPointerType() || canonType->isArrayType() ||
960 canonType->isFunctionType()) {
961 if (PointerType *PT = dyn_cast<PointerType>(canonType))
962 canonType = PT->getPointeeType().getTypePtr();
963 else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
964 canonType = AT->getElementType().getTypePtr();
965 else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
966 canonType = FT->getResultType().getTypePtr();
967 }
968 // the base type must be integer or float.
969 if (!(canonType->isIntegerType() || canonType->isRealFloatingType())) {
970 Diag(rawAttr->getAttributeLoc(), diag::err_attribute_invalid_vector_type,
971 curType.getCanonicalType().getAsString());
972 return 0;
973 }
974 // FIXME: check that the vector size is a multiple of the type size (and
975 // not 0). check that the vector components are a power of two. Last, and
976 // certainly not least, instantiate a vector type AST node!
977 return 0;
978}
979