blob: cad75848ab3431aad62e2bd29a942ebd218efc25 [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 Lattnere168f762006-11-10 05:29:30 +000016#include "clang/AST/Decl.h"
Chris Lattner1300fb92007-01-23 23:42:53 +000017#include "clang/AST/Expr.h"
Chris Lattnerf84a79c2006-11-11 22:59:23 +000018#include "clang/AST/Type.h"
Chris Lattner591a6752006-11-19 23:16:18 +000019#include "clang/Parse/DeclSpec.h"
Chris Lattnere168f762006-11-10 05:29:30 +000020#include "clang/Parse/Scope.h"
21#include "clang/Lex/IdentifierTable.h"
Chris Lattnerac18be92006-11-20 06:49:47 +000022#include "clang/Basic/LangOptions.h"
Chris Lattner38047f92007-01-27 06:24:01 +000023#include "llvm/ADT/SmallSet.h"
Chris Lattner697e5d62006-11-09 06:32:27 +000024using namespace llvm;
25using namespace clang;
26
Chris Lattnere168f762006-11-10 05:29:30 +000027
Chris Lattner2ebe4bb2006-11-20 01:29:42 +000028Sema::DeclTy *Sema::isTypeName(const IdentifierInfo &II, Scope *S) const {
Chris Lattner32d920b2007-01-26 02:01:53 +000029 return dyn_cast_or_null<TypedefDecl>(II.getFETokenInfo<Decl>());
Chris Lattnere168f762006-11-10 05:29:30 +000030}
31
Chris Lattner302b4be2006-11-19 02:31:38 +000032void Sema::PopScope(SourceLocation Loc, Scope *S) {
33 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
34 I != E; ++I) {
Chris Lattner99d31772007-01-21 22:37:37 +000035 Decl *D = static_cast<Decl*>(*I);
Chris Lattner302b4be2006-11-19 02:31:38 +000036 assert(D && "This decl didn't get pushed??");
Chris Lattnerff65b6b2007-01-23 01:33:16 +000037 IdentifierInfo *II = D->getIdentifier();
38 if (!II) continue;
Chris Lattner302b4be2006-11-19 02:31:38 +000039
Chris Lattnerff65b6b2007-01-23 01:33:16 +000040 // Unlink this decl from the identifier. Because the scope contains decls
41 // in an unordered collection, and because we have multiple identifier
42 // namespaces (e.g. tag, normal, label),the decl may not be the first entry.
43 if (II->getFETokenInfo<Decl>() == D) {
44 // Normal case, no multiple decls in different namespaces.
45 II->setFETokenInfo(D->getNext());
46 } else {
47 // Scan ahead. There are only three namespaces in C, so this loop can
48 // never execute more than 3 times.
49 Decl *SomeDecl = II->getFETokenInfo<Decl>();
50 while (SomeDecl->getNext() != D) {
51 SomeDecl = SomeDecl->getNext();
52 assert(SomeDecl && "Didn't find this decl on its identifier's chain!");
53 }
54 SomeDecl->setNext(D->getNext());
55 }
Chris Lattner302b4be2006-11-19 02:31:38 +000056
Chris Lattner740b2f32006-11-21 01:32:20 +000057 // This will have to be revisited for C++: there we want to nest stuff in
58 // namespace decls etc. Even for C, we might want a top-level translation
59 // unit decl or something.
60 if (!CurFunctionDecl)
61 continue;
62
63 // Chain this decl to the containing function, it now owns the memory for
64 // the decl.
65 D->setNext(CurFunctionDecl->getDeclChain());
66 CurFunctionDecl->setDeclChain(D);
Chris Lattner302b4be2006-11-19 02:31:38 +000067 }
68}
69
Chris Lattner200bdc32006-11-19 02:43:37 +000070/// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
71/// no declarator (e.g. "struct foo;") is parsed.
72Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) {
73 // TODO: emit error on 'int;' or 'const enum foo;'.
74 // TODO: emit error on 'typedef int;'
75 // if (!DS.isMissingDeclaratorOk()) Diag(...);
76
Chris Lattner200bdc32006-11-19 02:43:37 +000077 return 0;
78}
79
Chris Lattner18b19622007-01-22 07:39:13 +000080/// LookupScopedDecl - Look up the inner-most declaration in the specified
81/// namespace.
82static Decl *LookupScopedDecl(IdentifierInfo *II, Decl::IdentifierNamespace NS){
83 if (II == 0) return 0;
84
85 // Scan up the scope chain looking for a decl that matches this identifier
86 // that is in the appropriate namespace. This search should not take long, as
87 // shadowing of names is uncommon, and deep shadowing is extremely uncommon.
88 for (Decl *D = II->getFETokenInfo<Decl>(); D; D = D->getNext())
89 if (D->getIdentifierNamespace() == NS)
90 return D;
91 return 0;
92}
93
Chris Lattner01564d92007-01-27 19:27:06 +000094/// MergeTypeDefDecl - We just parsed a typedef 'New' which has the same name
95/// and scope as a previous declaration 'Old'. Figure out how to resolve this
96/// situation, merging decls or emitting diagnostics as appropriate.
97///
Chris Lattnerc511efb2007-01-27 19:32:14 +000098TypedefDecl *Sema::MergeTypeDefDecl(TypedefDecl *New, Decl *OldD) {
99 // Verify the old decl was also a typedef.
100 TypedefDecl *Old = dyn_cast<TypedefDecl>(OldD);
101 if (!Old) {
102 Diag(New->getLocation(), diag::err_redefinition_different_kind,
103 New->getName());
104 Diag(OldD->getLocation(), diag::err_previous_definition);
105 return New;
106 }
107
Chris Lattner01564d92007-01-27 19:27:06 +0000108 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
109 // TODO: This is totally simplistic. It should handle merging functions
110 // together etc, merging extern int X; int X; ...
111 Diag(New->getLocation(), diag::err_redefinition, New->getName());
112 Diag(Old->getLocation(), diag::err_previous_definition);
113 return New;
114}
115
116/// MergeFunctionDecl - We just parsed a function 'New' which has the same name
117/// and scope as a previous declaration 'Old'. Figure out how to resolve this
118/// situation, merging decls or emitting diagnostics as appropriate.
119///
Chris Lattnerc511efb2007-01-27 19:32:14 +0000120FunctionDecl *Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD) {
121 // Verify the old decl was also a function.
122 FunctionDecl *Old = dyn_cast<FunctionDecl>(OldD);
123 if (!Old) {
124 Diag(New->getLocation(), diag::err_redefinition_different_kind,
125 New->getName());
126 Diag(OldD->getLocation(), diag::err_previous_definition);
127 return New;
128 }
129
Chris Lattnerefe4aea2007-01-27 19:35:39 +0000130 // This is not right, but it's a start. If 'Old' is a function prototype with
131 // the same type as 'New', silently allow this. FIXME: We should link up decl
132 // objects here.
133 if (Old->getBody() == 0 && Old->getType() == New->getType()) {
134 return New;
135 }
Chris Lattnerc511efb2007-01-27 19:32:14 +0000136
Chris Lattner01564d92007-01-27 19:27:06 +0000137 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
138 // TODO: This is totally simplistic. It should handle merging functions
139 // together etc, merging extern int X; int X; ...
140 Diag(New->getLocation(), diag::err_redefinition, New->getName());
141 Diag(Old->getLocation(), diag::err_previous_definition);
142 return New;
143}
144
145/// MergeVarDecl - We just parsed a variable 'New' which has the same name
146/// and scope as a previous declaration 'Old'. Figure out how to resolve this
147/// situation, merging decls or emitting diagnostics as appropriate.
148///
Chris Lattnerc511efb2007-01-27 19:32:14 +0000149VarDecl *Sema::MergeVarDecl(VarDecl *New, Decl *OldD) {
150 // Verify the old decl was also a variable.
151 VarDecl *Old = dyn_cast<VarDecl>(OldD);
152 if (!Old) {
153 Diag(New->getLocation(), diag::err_redefinition_different_kind,
154 New->getName());
155 Diag(OldD->getLocation(), diag::err_previous_definition);
156 return New;
157 }
158
Chris Lattner01564d92007-01-27 19:27:06 +0000159 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
160 // TODO: This is totally simplistic. It should handle merging functions
161 // together etc, merging extern int X; int X; ...
162 Diag(New->getLocation(), diag::err_redefinition, New->getName());
163 Diag(Old->getLocation(), diag::err_previous_definition);
164 return New;
165}
166
Chris Lattner18b19622007-01-22 07:39:13 +0000167
Chris Lattnere168f762006-11-10 05:29:30 +0000168Action::DeclTy *
169Sema::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
170 DeclTy *LastInGroup) {
171 IdentifierInfo *II = D.getIdentifier();
Chris Lattner302b4be2006-11-19 02:31:38 +0000172
Chris Lattner01564d92007-01-27 19:27:06 +0000173 // See if this is a redefinition of a variable in the same scope.
174 Decl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary);
175 if (!S->isDeclScope(PrevDecl))
176 PrevDecl = 0; // If in outer scope, it isn't the same thing.
177
Chris Lattnere168f762006-11-10 05:29:30 +0000178 Decl *New;
Chris Lattner01a7c532007-01-25 23:09:03 +0000179 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Chris Lattner01564d92007-01-27 19:27:06 +0000180 TypedefDecl *NewTD = ParseTypedefDecl(S, D);
181 if (!NewTD) return 0;
182
183 // Merge the decl with the existing one if appropriate.
184 if (PrevDecl) {
185 NewTD = MergeTypeDefDecl(NewTD, PrevDecl);
186 if (NewTD == 0) return 0;
187 }
188 New = NewTD;
Chris Lattner01a7c532007-01-25 23:09:03 +0000189 } else if (D.isFunctionDeclarator()) {
190 TypeRef R = GetTypeForDeclarator(D, S);
191 if (R.isNull()) return 0;
Chris Lattner01564d92007-01-27 19:27:06 +0000192
193 FunctionDecl *NewFD = new FunctionDecl(D.getIdentifierLoc(), II, R);
194
195 // Merge the decl with the existing one if appropriate.
196 if (PrevDecl) {
197 NewFD = MergeFunctionDecl(NewFD, PrevDecl);
198 if (NewFD == 0) return 0;
199 }
200 New = NewFD;
Chris Lattner01a7c532007-01-25 23:09:03 +0000201 } else {
202 TypeRef R = GetTypeForDeclarator(D, S);
203 if (R.isNull()) return 0;
Chris Lattner01564d92007-01-27 19:27:06 +0000204 VarDecl *NewVD = new VarDecl(D.getIdentifierLoc(), II, R);
205
206 // Merge the decl with the existing one if appropriate.
207 if (PrevDecl) {
208 NewVD = MergeVarDecl(NewVD, PrevDecl);
209 if (NewVD == 0) return 0;
210 }
211 New = NewVD;
Chris Lattner01a7c532007-01-25 23:09:03 +0000212 }
Chris Lattner302b4be2006-11-19 02:31:38 +0000213
214
Chris Lattnere168f762006-11-10 05:29:30 +0000215 // If this has an identifier, add it to the scope stack.
216 if (II) {
Chris Lattner18b19622007-01-22 07:39:13 +0000217 New->setNext(II->getFETokenInfo<Decl>());
Chris Lattnere168f762006-11-10 05:29:30 +0000218 II->setFETokenInfo(New);
Chris Lattner99d31772007-01-21 22:37:37 +0000219 S->AddDecl(New);
Chris Lattnere168f762006-11-10 05:29:30 +0000220 }
221
222 // If this is a top-level decl that is chained to some other (e.g. int A,B,C;)
223 // remember this in the LastInGroupList list.
224 if (LastInGroup && S->getParent() == 0)
225 LastInGroupList.push_back((Decl*)LastInGroup);
226
227 return New;
228}
229
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000230VarDecl *
231Sema::ParseParamDeclarator(DeclaratorChunk &FTI, unsigned ArgNo,
232 Scope *FnScope) {
233 const DeclaratorChunk::ParamInfo &PI = FTI.Fun.ArgInfo[ArgNo];
Chris Lattner200bdc32006-11-19 02:43:37 +0000234
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000235 IdentifierInfo *II = PI.Ident;
Chris Lattnerc284e9b2007-01-23 05:14:32 +0000236 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
237 // Can this happen for params? We already checked that they don't conflict
238 // among each other. Here they can only shadow globals, which is ok.
Chris Lattner18b19622007-01-22 07:39:13 +0000239 if (Decl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary)) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000240
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000241 }
242
Chris Lattner38047f92007-01-27 06:24:01 +0000243 VarDecl *New = new VarDecl(PI.IdentLoc, II,
244 TypeRef::getFromOpaquePtr(PI.TypeInfo));
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000245
246 // If this has an identifier, add it to the scope stack.
247 if (II) {
Chris Lattner18b19622007-01-22 07:39:13 +0000248 New->setNext(II->getFETokenInfo<Decl>());
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000249 II->setFETokenInfo(New);
Chris Lattner99d31772007-01-21 22:37:37 +0000250 FnScope->AddDecl(New);
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000251 }
Chris Lattner229ce602006-11-21 01:21:07 +0000252
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000253 return New;
254}
255
256
257Sema::DeclTy *Sema::ParseStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) {
Chris Lattner229ce602006-11-21 01:21:07 +0000258 assert(CurFunctionDecl == 0 && "Function parsing confused");
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000259 assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
260 "Not a function declarator!");
261 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
262
263 // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared'
264 // for a K&R function.
265 if (!FTI.hasPrototype) {
266 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
267 if (FTI.ArgInfo[i].TypeInfo == 0) {
268 Diag(FTI.ArgInfo[i].IdentLoc, diag::err_param_not_declared,
269 FTI.ArgInfo[i].Ident->getName());
270 // Implicitly declare the argument as type 'int' for lack of a better
271 // type.
272 FTI.ArgInfo[i].TypeInfo = Context.IntTy.getAsOpaquePtr();
273 }
274 }
275
276 // Since this is a function definition, act as though we have information
277 // about the arguments.
278 FTI.hasPrototype = true;
Chris Lattner2114d5e2006-12-04 07:40:24 +0000279 } else {
280 // FIXME: Diagnose arguments without names in C.
281
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000282 }
283
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000284 Scope *GlobalScope = FnBodyScope->getParent();
285
286 FunctionDecl *FD =
287 static_cast<FunctionDecl*>(ParseDeclarator(GlobalScope, D, 0, 0));
Chris Lattner229ce602006-11-21 01:21:07 +0000288 CurFunctionDecl = FD;
Chris Lattner2114d5e2006-12-04 07:40:24 +0000289
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000290 // Create Decl objects for each parameter, adding them to the FunctionDecl.
291 SmallVector<VarDecl*, 16> Params;
Chris Lattnerf61c8a82007-01-21 19:04:43 +0000292
293 // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes
294 // no arguments, not a function that takes a single void argument.
295 if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 &&
296 FTI.ArgInfo[0].TypeInfo == Context.VoidTy.getAsOpaquePtr()) {
Chris Lattner99d31772007-01-21 22:37:37 +0000297 // empty arg list, don't push any params.
Chris Lattnerf61c8a82007-01-21 19:04:43 +0000298 } else {
299 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i)
300 Params.push_back(ParseParamDeclarator(D.getTypeObject(0), i,FnBodyScope));
301 }
Chris Lattner2114d5e2006-12-04 07:40:24 +0000302
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000303 FD->setParams(&Params[0], Params.size());
Chris Lattner2114d5e2006-12-04 07:40:24 +0000304
Chris Lattnere168f762006-11-10 05:29:30 +0000305 return FD;
306}
307
Chris Lattner229ce602006-11-21 01:21:07 +0000308Sema::DeclTy *Sema::ParseFunctionDefBody(DeclTy *D, StmtTy *Body) {
309 FunctionDecl *FD = static_cast<FunctionDecl*>(D);
310 FD->setBody((Stmt*)Body);
311
312 assert(FD == CurFunctionDecl && "Function parsing confused");
313 CurFunctionDecl = 0;
314 return FD;
315}
316
317
Chris Lattnerac18be92006-11-20 06:49:47 +0000318/// ImplicitlyDefineFunction - An undeclared identifier was used in a function
319/// call, forming a call to an implicitly defined function (per C99 6.5.1p2).
320Decl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II,
321 Scope *S) {
322 if (getLangOptions().C99) // Extension in C99.
323 Diag(Loc, diag::ext_implicit_function_decl, II.getName());
324 else // Legal in C90, but warn about it.
325 Diag(Loc, diag::warn_implicit_function_decl, II.getName());
326
327 // FIXME: handle stuff like:
328 // void foo() { extern float X(); }
329 // void bar() { X(); } <-- implicit decl for X in another scope.
330
331 // Set a Declarator for the implicit definition: int foo();
Chris Lattner353f5742006-11-28 04:50:12 +0000332 const char *Dummy;
Chris Lattnerac18be92006-11-20 06:49:47 +0000333 DeclSpec DS;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000334 bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy);
Chris Lattner353f5742006-11-28 04:50:12 +0000335 assert(!Error && "Error setting up implicit decl!");
Chris Lattnerac18be92006-11-20 06:49:47 +0000336 Declarator D(DS, Declarator::BlockContext);
Chris Lattnercbc426d2006-12-02 06:43:02 +0000337 D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, Loc));
Chris Lattnerac18be92006-11-20 06:49:47 +0000338 D.SetIdentifier(&II, Loc);
339
Chris Lattner62d2e662007-01-28 00:21:37 +0000340 // Find translation-unit scope to insert this function into.
341 while (S->getParent())
342 S = S->getParent();
Chris Lattnerac18be92006-11-20 06:49:47 +0000343
Chris Lattner62d2e662007-01-28 00:21:37 +0000344 return static_cast<Decl*>(ParseDeclarator(S, D, 0, 0));
Chris Lattnerac18be92006-11-20 06:49:47 +0000345}
346
Chris Lattner302b4be2006-11-19 02:31:38 +0000347
Chris Lattner01564d92007-01-27 19:27:06 +0000348TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D) {
Chris Lattnerda8aa7b2006-11-19 23:12:30 +0000349 assert(D.getIdentifier() && "Wrong callback for declspec withotu declarator");
Chris Lattner302b4be2006-11-19 02:31:38 +0000350
Chris Lattner5ca17df2006-11-19 23:32:49 +0000351 TypeRef T = GetTypeForDeclarator(D, S);
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000352 if (T.isNull()) return 0;
353
Chris Lattner18b19622007-01-22 07:39:13 +0000354 // Scope manipulation handled by caller.
355 return new TypedefDecl(D.getIdentifierLoc(), D.getIdentifier(), T);
Chris Lattnere168f762006-11-10 05:29:30 +0000356}
357
Chris Lattner18b19622007-01-22 07:39:13 +0000358
Chris Lattner1300fb92007-01-23 23:42:53 +0000359/// ParseTag - This is invoked when we see 'struct foo' or 'struct {'. In the
360/// former case, Name will be non-null. In the later case, Name will be null.
361/// TagType indicates what kind of tag this is. TK indicates whether this is a
362/// reference/declaration/definition of a tag.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000363Sema::DeclTy *Sema::ParseTag(Scope *S, unsigned TagType, TagKind TK,
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000364 SourceLocation KWLoc, IdentifierInfo *Name,
365 SourceLocation NameLoc) {
Chris Lattner8799cf22007-01-23 01:57:16 +0000366 // If this is a use of an existing tag, it must have a name.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000367 assert((Name != 0 || TK == TK_Definition) &&
368 "Nameless record must be a definition!");
Chris Lattner8799cf22007-01-23 01:57:16 +0000369
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000370 Decl::Kind Kind;
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000371 switch (TagType) {
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000372 default: assert(0 && "Unknown tag type!");
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000373 case DeclSpec::TST_struct: Kind = Decl::Struct; break;
374 case DeclSpec::TST_union: Kind = Decl::Union; break;
375//case DeclSpec::TST_class: Kind = Decl::Class; break;
376 case DeclSpec::TST_enum: Kind = Decl::Enum; break;
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000377 }
Chris Lattner7e783a12007-01-23 02:05:42 +0000378
Chris Lattner18b19622007-01-22 07:39:13 +0000379 // If this is a named struct, check to see if there was a previous forward
380 // declaration or definition.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000381 if (TagDecl *PrevDecl =
382 dyn_cast_or_null<TagDecl>(LookupScopedDecl(Name, Decl::IDNS_Tag))) {
Chris Lattner8799cf22007-01-23 01:57:16 +0000383
384 // If this is a use of a previous tag, or if the tag is already declared in
385 // the same scope (so that the definition/declaration completes or
386 // rementions the tag), reuse the decl.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000387 if (TK == TK_Reference || S->isDeclScope(PrevDecl)) {
Chris Lattner7e783a12007-01-23 02:05:42 +0000388 // Make sure that this wasn't declared as an enum and now used as a struct
389 // or something similar.
390 if (PrevDecl->getKind() != Kind) {
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000391 Diag(KWLoc, diag::err_use_with_wrong_tag, Name->getName());
Chris Lattner7e783a12007-01-23 02:05:42 +0000392 Diag(PrevDecl->getLocation(), diag::err_previous_use);
393 }
Chris Lattner7b9ace62007-01-23 20:11:08 +0000394
395 // If this is a use or a forward declaration, we're good.
396 if (TK != TK_Definition)
397 return PrevDecl;
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000398
Chris Lattner7b9ace62007-01-23 20:11:08 +0000399 // Diagnose attempts to redefine a tag.
400 if (PrevDecl->isDefinition()) {
401 Diag(NameLoc, diag::err_redefinition, Name->getName());
402 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
403 // If this is a redefinition, recover by making this struct be
404 // anonymous, which will make any later references get the previous
405 // definition.
406 Name = 0;
407 } else {
408 // Okay, this is definition of a previously declared or referenced tag.
409 // Move the location of the decl to be the definition site.
410 PrevDecl->setLocation(NameLoc);
Chris Lattner7b9ace62007-01-23 20:11:08 +0000411 return PrevDecl;
412 }
Chris Lattner8799cf22007-01-23 01:57:16 +0000413 }
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000414 // If we get here, this is a definition of a new struct type in a nested
415 // scope, e.g. "struct foo; void bar() { struct foo; }", just create a new
416 // type.
Chris Lattner18b19622007-01-22 07:39:13 +0000417 }
418
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000419 // If there is an identifier, use the location of the identifier as the
420 // location of the decl, otherwise use the location of the struct/union
421 // keyword.
422 SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc;
423
Chris Lattner18b19622007-01-22 07:39:13 +0000424 // Otherwise, if this is the first time we've seen this tag, create the decl.
Chris Lattner7b9ace62007-01-23 20:11:08 +0000425 TagDecl *New;
Chris Lattner720a0542007-01-25 00:44:24 +0000426 switch (Kind) {
427 default: assert(0 && "Unknown tag kind!");
Chris Lattner5f521502007-01-25 06:27:24 +0000428 case Decl::Enum:
429 New = new EnumDecl(Loc, Name);
430 // If this is an undefined enum, warn.
Chris Lattnerc1915e22007-01-25 07:29:02 +0000431 if (TK != TK_Definition) Diag(Loc, diag::ext_forward_ref_enum);
Chris Lattner5f521502007-01-25 06:27:24 +0000432 break;
Chris Lattner720a0542007-01-25 00:44:24 +0000433 case Decl::Union:
434 case Decl::Struct:
435 case Decl::Class:
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000436 New = new RecordDecl(Kind, Loc, Name);
Chris Lattner720a0542007-01-25 00:44:24 +0000437 break;
438 }
Chris Lattner18b19622007-01-22 07:39:13 +0000439
440 // If this has an identifier, add it to the scope stack.
441 if (Name) {
442 New->setNext(Name->getFETokenInfo<Decl>());
443 Name->setFETokenInfo(New);
444 S->AddDecl(New);
445 }
446
447 return New;
448}
Chris Lattner1300fb92007-01-23 23:42:53 +0000449
450/// ParseField - Each field of a struct/union/class is passed into this in order
451/// to create a FieldDecl object for it.
452Sema::DeclTy *Sema::ParseField(Scope *S, DeclTy *TagDecl,
453 SourceLocation DeclStart,
454 Declarator &D, ExprTy *BitfieldWidth) {
455 IdentifierInfo *II = D.getIdentifier();
456 Expr *BitWidth = (Expr*)BitfieldWidth;
457
458 SourceLocation Loc = DeclStart;
459 if (II) Loc = D.getIdentifierLoc();
460
Chris Lattner62d2e662007-01-28 00:21:37 +0000461 // FIXME: Unnamed fields can be handled in various different ways, for
462 // example, unnamed unions inject all members into the struct namespace!
463
464
Chris Lattner1300fb92007-01-23 23:42:53 +0000465 if (BitWidth) {
466 // TODO: Validate.
Chris Lattner01a7c532007-01-25 23:09:03 +0000467 printf("WARNING: BITFIELDS IGNORED!\n");
Chris Lattner1300fb92007-01-23 23:42:53 +0000468
469 // 6.7.2.1p3
470 // 6.7.2.1p4
471
472 } else {
473 // Not a bitfield.
474
475 // validate II.
476
477 }
478
Chris Lattner01a7c532007-01-25 23:09:03 +0000479 TypeRef T = GetTypeForDeclarator(D, S);
480 if (T.isNull()) return 0;
481
482 return new FieldDecl(Loc, II, T);
Chris Lattner1300fb92007-01-23 23:42:53 +0000483}
484
485void Sema::ParseRecordBody(SourceLocation RecLoc, DeclTy *RecDecl,
486 DeclTy **Fields, unsigned NumFields) {
Chris Lattnerc1915e22007-01-25 07:29:02 +0000487 RecordDecl *Record = cast<RecordDecl>(static_cast<Decl*>(RecDecl));
Chris Lattner1300fb92007-01-23 23:42:53 +0000488 if (Record->isDefinition()) {
489 // Diagnose code like:
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000490 // struct S { struct S {} X; };
Chris Lattner1300fb92007-01-23 23:42:53 +0000491 // We discover this when we complete the outer S. Reject and ignore the
492 // outer S.
493 Diag(Record->getLocation(), diag::err_nested_redefinition,
494 Record->getKindName());
495 Diag(RecLoc, diag::err_previous_definition);
496 return;
497 }
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000498
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000499 // Verify that all the fields are okay.
Chris Lattner82625602007-01-24 02:26:21 +0000500 unsigned NumNamedMembers = 0;
Chris Lattner41943152007-01-25 04:52:46 +0000501 SmallVector<Decl*, 32> RecFields;
Chris Lattnere5a66562007-01-25 22:48:42 +0000502 SmallSet<const IdentifierInfo*, 32> FieldIDs;
503
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000504 for (unsigned i = 0; i != NumFields; ++i) {
505 FieldDecl *FD = cast_or_null<FieldDecl>(static_cast<Decl*>(Fields[i]));
506 if (!FD) continue; // Already issued a diagnostic.
Chris Lattner720a0542007-01-25 00:44:24 +0000507
508 // Get the type for the field.
509 Type *FDTy = FD->getType()->getCanonicalType();
510
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000511 // C99 6.7.2.1p2 - A field may not be a function type.
Chris Lattner720a0542007-01-25 00:44:24 +0000512 if (isa<FunctionType>(FDTy)) {
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000513 Diag(FD->getLocation(), diag::err_field_declared_as_function,
514 FD->getName());
Chris Lattner82625602007-01-24 02:26:21 +0000515 delete FD;
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000516 continue;
517 }
518
Chris Lattner82625602007-01-24 02:26:21 +0000519 // C99 6.7.2.1p2 - A field may not be an incomplete type except...
Chris Lattner720a0542007-01-25 00:44:24 +0000520 if (FDTy->isIncompleteType()) {
Chris Lattner82625602007-01-24 02:26:21 +0000521 if (i != NumFields-1 || // ... that the last member ...
522 Record->getKind() != Decl::Struct || // ... of a structure ...
Chris Lattner720a0542007-01-25 00:44:24 +0000523 !isa<ArrayType>(FDTy)) { //... may have incomplete array type.
Chris Lattner82625602007-01-24 02:26:21 +0000524 Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName());
525 delete FD;
526 continue;
527 }
Chris Lattner720a0542007-01-25 00:44:24 +0000528 if (NumNamedMembers < 1) { //... must have more than named member ...
Chris Lattner82625602007-01-24 02:26:21 +0000529 Diag(FD->getLocation(), diag::err_flexible_array_empty_struct,
530 FD->getName());
531 delete FD;
532 continue;
533 }
Chris Lattner720a0542007-01-25 00:44:24 +0000534
535 // Okay, we have a legal flexible array member at the end of the struct.
Chris Lattner41943152007-01-25 04:52:46 +0000536 Record->setHasFlexibleArrayMember(true);
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000537 }
Chris Lattner720a0542007-01-25 00:44:24 +0000538
539
540 /// C99 6.7.2.1p2 - a struct ending in a flexible array member cannot be the
541 /// field of another structure or the element of an array.
542 if (RecordType *FDTTy = dyn_cast<RecordType>(FDTy)) {
543 if (FDTTy->getDecl()->hasFlexibleArrayMember()) {
544 // If this is a member of a union, then entire union becomes "flexible".
545 if (Record->getKind() == Decl::Union) {
Chris Lattner41943152007-01-25 04:52:46 +0000546 Record->setHasFlexibleArrayMember(true);
Chris Lattner720a0542007-01-25 00:44:24 +0000547 } else {
548 // If this is a struct/class and this is not the last element, reject
549 // it. Note that GCC supports variable sized arrays in the middle of
550 // structures.
551 if (i != NumFields-1) {
552 Diag(FD->getLocation(), diag::err_variable_sized_type_in_struct,
553 FD->getName());
554 delete FD;
555 continue;
556 }
557
558 // We support flexible arrays at the end of structs in other structs
559 // as an extension.
560 Diag(FD->getLocation(), diag::ext_flexible_array_in_struct,
561 FD->getName());
Chris Lattner41943152007-01-25 04:52:46 +0000562 Record->setHasFlexibleArrayMember(true);
Chris Lattner720a0542007-01-25 00:44:24 +0000563 }
564 }
565 }
566
Chris Lattner82625602007-01-24 02:26:21 +0000567 // Keep track of the number of named members.
Chris Lattnere5a66562007-01-25 22:48:42 +0000568 if (IdentifierInfo *II = FD->getIdentifier()) {
569 // Detect duplicate member names.
Chris Lattnerbaf33662007-01-27 02:14:08 +0000570 if (!FieldIDs.insert(II)) {
Chris Lattnere5a66562007-01-25 22:48:42 +0000571 Diag(FD->getLocation(), diag::err_duplicate_member, II->getName());
572 // Find the previous decl.
573 SourceLocation PrevLoc;
574 for (unsigned i = 0, e = RecFields.size(); ; ++i) {
575 assert(i != e && "Didn't find previous def!");
576 if (RecFields[i]->getIdentifier() == II) {
577 PrevLoc = RecFields[i]->getLocation();
578 break;
579 }
580 }
581 Diag(PrevLoc, diag::err_previous_definition);
582 delete FD;
583 continue;
584 }
Chris Lattner82625602007-01-24 02:26:21 +0000585 ++NumNamedMembers;
Chris Lattnere5a66562007-01-25 22:48:42 +0000586 }
Chris Lattner41943152007-01-25 04:52:46 +0000587
588 // Remember good fields.
589 RecFields.push_back(FD);
Chris Lattnerbdf8b8d2007-01-24 02:11:17 +0000590 }
Chris Lattner82625602007-01-24 02:26:21 +0000591
592
593 // Okay, we successfully defined 'Record'.
Chris Lattner41943152007-01-25 04:52:46 +0000594 Record->defineBody(&RecFields[0], RecFields.size());
Chris Lattner1300fb92007-01-23 23:42:53 +0000595}
596
Chris Lattnerc1915e22007-01-25 07:29:02 +0000597Sema::DeclTy *Sema::ParseEnumConstant(Scope *S, DeclTy *EnumDeclX,
598 SourceLocation IdLoc, IdentifierInfo *Id,
599 SourceLocation EqualLoc, ExprTy *Val) {
600 EnumDecl *TheEnumDecl = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX));
Chris Lattner8116d1b2007-01-25 22:38:29 +0000601
602 // Verify that there isn't already something declared with this name in this
603 // scope.
604 if (Decl *PrevDecl = LookupScopedDecl(Id, Decl::IDNS_Ordinary)) {
605 if (S->isDeclScope(PrevDecl)) {
606 if (isa<EnumConstantDecl>(PrevDecl))
607 Diag(IdLoc, diag::err_redefinition_of_enumerator, Id->getName());
608 else
609 Diag(IdLoc, diag::err_redefinition, Id->getName());
610 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
611 return 0;
612 }
613 }
614
Chris Lattnerc1915e22007-01-25 07:29:02 +0000615 TypeRef Ty = Context.getTagDeclType(TheEnumDecl);
Chris Lattner8116d1b2007-01-25 22:38:29 +0000616 EnumConstantDecl *New = new EnumConstantDecl(IdLoc, Id, Ty);
617
618 // Register this decl in the current scope stack.
619 New->setNext(Id->getFETokenInfo<Decl>());
620 Id->setFETokenInfo(New);
621 S->AddDecl(New);
622 return New;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000623}
624
625void Sema::ParseEnumBody(SourceLocation EnumLoc, DeclTy *EnumDeclX,
626 DeclTy **Elements, unsigned NumElements) {
627 EnumDecl *Enum = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX));
628 assert(!Enum->isDefinition() && "Enum redefinitions can't reach here");
629
630 // Verify that all the values are okay.
631 SmallVector<EnumConstantDecl*, 32> Values;
632 for (unsigned i = 0; i != NumElements; ++i) {
633 EnumConstantDecl *ECD =
634 cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i]));
635 if (!ECD) continue; // Already issued a diagnostic.
636
637 Values.push_back(ECD);
638 }
639
640 Enum->defineElements(&Values[0], Values.size());
641}
Chris Lattner1300fb92007-01-23 23:42:53 +0000642