blob: bc7c210c1038d30b6dca1e999c9a352366150510 [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 Lattnerf84a79c2006-11-11 22:59:23 +000017#include "clang/AST/Type.h"
Chris Lattner591a6752006-11-19 23:16:18 +000018#include "clang/Parse/DeclSpec.h"
Chris Lattnere168f762006-11-10 05:29:30 +000019#include "clang/Parse/Scope.h"
20#include "clang/Lex/IdentifierTable.h"
Chris Lattnerac18be92006-11-20 06:49:47 +000021#include "clang/Basic/LangOptions.h"
Chris Lattner697e5d62006-11-09 06:32:27 +000022using namespace llvm;
23using namespace clang;
24
Chris Lattnere168f762006-11-10 05:29:30 +000025
Chris Lattner2ebe4bb2006-11-20 01:29:42 +000026Sema::DeclTy *Sema::isTypeName(const IdentifierInfo &II, Scope *S) const {
27 return dyn_cast_or_null<TypeDecl>(II.getFETokenInfo<Decl>());
Chris Lattnere168f762006-11-10 05:29:30 +000028}
29
Chris Lattner302b4be2006-11-19 02:31:38 +000030void Sema::PopScope(SourceLocation Loc, Scope *S) {
31 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
32 I != E; ++I) {
Chris Lattner99d31772007-01-21 22:37:37 +000033 Decl *D = static_cast<Decl*>(*I);
Chris Lattner302b4be2006-11-19 02:31:38 +000034 assert(D && "This decl didn't get pushed??");
Chris Lattnerff65b6b2007-01-23 01:33:16 +000035 IdentifierInfo *II = D->getIdentifier();
36 if (!II) continue;
Chris Lattner302b4be2006-11-19 02:31:38 +000037
Chris Lattnerff65b6b2007-01-23 01:33:16 +000038 // Unlink this decl from the identifier. Because the scope contains decls
39 // in an unordered collection, and because we have multiple identifier
40 // namespaces (e.g. tag, normal, label),the decl may not be the first entry.
41 if (II->getFETokenInfo<Decl>() == D) {
42 // Normal case, no multiple decls in different namespaces.
43 II->setFETokenInfo(D->getNext());
44 } else {
45 // Scan ahead. There are only three namespaces in C, so this loop can
46 // never execute more than 3 times.
47 Decl *SomeDecl = II->getFETokenInfo<Decl>();
48 while (SomeDecl->getNext() != D) {
49 SomeDecl = SomeDecl->getNext();
50 assert(SomeDecl && "Didn't find this decl on its identifier's chain!");
51 }
52 SomeDecl->setNext(D->getNext());
53 }
Chris Lattner302b4be2006-11-19 02:31:38 +000054
Chris Lattner740b2f32006-11-21 01:32:20 +000055 // This will have to be revisited for C++: there we want to nest stuff in
56 // namespace decls etc. Even for C, we might want a top-level translation
57 // unit decl or something.
58 if (!CurFunctionDecl)
59 continue;
60
61 // Chain this decl to the containing function, it now owns the memory for
62 // the decl.
63 D->setNext(CurFunctionDecl->getDeclChain());
64 CurFunctionDecl->setDeclChain(D);
Chris Lattner302b4be2006-11-19 02:31:38 +000065 }
66}
67
Chris Lattner200bdc32006-11-19 02:43:37 +000068/// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
69/// no declarator (e.g. "struct foo;") is parsed.
70Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) {
71 // TODO: emit error on 'int;' or 'const enum foo;'.
72 // TODO: emit error on 'typedef int;'
73 // if (!DS.isMissingDeclaratorOk()) Diag(...);
74
75 // TODO: Register 'struct foo;' with the type system as an opaque struct.
76
77 // TODO: Check that we don't already have 'union foo;' or something else
78 // that conflicts.
79 return 0;
80}
81
Chris Lattner18b19622007-01-22 07:39:13 +000082/// LookupScopedDecl - Look up the inner-most declaration in the specified
83/// namespace.
84static Decl *LookupScopedDecl(IdentifierInfo *II, Decl::IdentifierNamespace NS){
85 if (II == 0) return 0;
86
87 // Scan up the scope chain looking for a decl that matches this identifier
88 // that is in the appropriate namespace. This search should not take long, as
89 // shadowing of names is uncommon, and deep shadowing is extremely uncommon.
90 for (Decl *D = II->getFETokenInfo<Decl>(); D; D = D->getNext())
91 if (D->getIdentifierNamespace() == NS)
92 return D;
93 return 0;
94}
95
96
Chris Lattnere168f762006-11-10 05:29:30 +000097Action::DeclTy *
98Sema::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
99 DeclTy *LastInGroup) {
100 IdentifierInfo *II = D.getIdentifier();
Chris Lattner302b4be2006-11-19 02:31:38 +0000101
Chris Lattner18b19622007-01-22 07:39:13 +0000102 if (Decl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary)) {
Chris Lattner302b4be2006-11-19 02:31:38 +0000103 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
Chris Lattner99d31772007-01-21 22:37:37 +0000104 if (S->isDeclScope(PrevDecl)) {
105 // TODO: This is totally simplistic. It should handle merging functions
106 // together etc, merging extern int X; int X; ...
107 Diag(D.getIdentifierLoc(), diag::err_redefinition, II->getName());
108 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
109 }
Chris Lattner302b4be2006-11-19 02:31:38 +0000110 }
Chris Lattnere168f762006-11-10 05:29:30 +0000111
112 Decl *New;
Chris Lattner2114d5e2006-12-04 07:40:24 +0000113 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
Chris Lattner18b19622007-01-22 07:39:13 +0000114 New = ParseTypedefDecl(S, D);
Chris Lattner2114d5e2006-12-04 07:40:24 +0000115 else if (D.isFunctionDeclarator())
Chris Lattner18b19622007-01-22 07:39:13 +0000116 New = new FunctionDecl(D.getIdentifierLoc(), II, GetTypeForDeclarator(D,S));
Chris Lattnere168f762006-11-10 05:29:30 +0000117 else
Chris Lattner18b19622007-01-22 07:39:13 +0000118 New = new VarDecl(D.getIdentifierLoc(), II, GetTypeForDeclarator(D, S));
Chris Lattnere168f762006-11-10 05:29:30 +0000119
Chris Lattner302b4be2006-11-19 02:31:38 +0000120 if (!New) return 0;
121
122
Chris Lattnere168f762006-11-10 05:29:30 +0000123 // If this has an identifier, add it to the scope stack.
124 if (II) {
Chris Lattner18b19622007-01-22 07:39:13 +0000125 New->setNext(II->getFETokenInfo<Decl>());
Chris Lattnere168f762006-11-10 05:29:30 +0000126 II->setFETokenInfo(New);
Chris Lattner99d31772007-01-21 22:37:37 +0000127 S->AddDecl(New);
Chris Lattnere168f762006-11-10 05:29:30 +0000128 }
129
130 // If this is a top-level decl that is chained to some other (e.g. int A,B,C;)
131 // remember this in the LastInGroupList list.
132 if (LastInGroup && S->getParent() == 0)
133 LastInGroupList.push_back((Decl*)LastInGroup);
134
135 return New;
136}
137
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000138VarDecl *
139Sema::ParseParamDeclarator(DeclaratorChunk &FTI, unsigned ArgNo,
140 Scope *FnScope) {
141 const DeclaratorChunk::ParamInfo &PI = FTI.Fun.ArgInfo[ArgNo];
Chris Lattner200bdc32006-11-19 02:43:37 +0000142
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000143 IdentifierInfo *II = PI.Ident;
Chris Lattner18b19622007-01-22 07:39:13 +0000144 if (Decl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary)) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000145
146 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000147 assert(PrevDecl == 0 && "FIXME: Unimp!");
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000148 }
149
Chris Lattner18b19622007-01-22 07:39:13 +0000150 VarDecl *New = new VarDecl(PI.IdentLoc, II, static_cast<Type*>(PI.TypeInfo));
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000151
152 // If this has an identifier, add it to the scope stack.
153 if (II) {
Chris Lattner18b19622007-01-22 07:39:13 +0000154 New->setNext(II->getFETokenInfo<Decl>());
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000155 II->setFETokenInfo(New);
Chris Lattner99d31772007-01-21 22:37:37 +0000156 FnScope->AddDecl(New);
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000157 }
Chris Lattner229ce602006-11-21 01:21:07 +0000158
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000159 return New;
160}
161
162
163Sema::DeclTy *Sema::ParseStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) {
Chris Lattner229ce602006-11-21 01:21:07 +0000164 assert(CurFunctionDecl == 0 && "Function parsing confused");
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000165 assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
166 "Not a function declarator!");
167 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
168
169 // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared'
170 // for a K&R function.
171 if (!FTI.hasPrototype) {
172 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
173 if (FTI.ArgInfo[i].TypeInfo == 0) {
174 Diag(FTI.ArgInfo[i].IdentLoc, diag::err_param_not_declared,
175 FTI.ArgInfo[i].Ident->getName());
176 // Implicitly declare the argument as type 'int' for lack of a better
177 // type.
178 FTI.ArgInfo[i].TypeInfo = Context.IntTy.getAsOpaquePtr();
179 }
180 }
181
182 // Since this is a function definition, act as though we have information
183 // about the arguments.
184 FTI.hasPrototype = true;
Chris Lattner2114d5e2006-12-04 07:40:24 +0000185 } else {
186 // FIXME: Diagnose arguments without names in C.
187
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000188 }
189
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000190 Scope *GlobalScope = FnBodyScope->getParent();
191
192 FunctionDecl *FD =
193 static_cast<FunctionDecl*>(ParseDeclarator(GlobalScope, D, 0, 0));
Chris Lattner229ce602006-11-21 01:21:07 +0000194 CurFunctionDecl = FD;
Chris Lattner2114d5e2006-12-04 07:40:24 +0000195
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000196 // Create Decl objects for each parameter, adding them to the FunctionDecl.
197 SmallVector<VarDecl*, 16> Params;
Chris Lattnerf61c8a82007-01-21 19:04:43 +0000198
199 // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes
200 // no arguments, not a function that takes a single void argument.
201 if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 &&
202 FTI.ArgInfo[0].TypeInfo == Context.VoidTy.getAsOpaquePtr()) {
Chris Lattner99d31772007-01-21 22:37:37 +0000203 // empty arg list, don't push any params.
Chris Lattnerf61c8a82007-01-21 19:04:43 +0000204 } else {
205 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i)
206 Params.push_back(ParseParamDeclarator(D.getTypeObject(0), i,FnBodyScope));
207 }
Chris Lattner2114d5e2006-12-04 07:40:24 +0000208
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000209 FD->setParams(&Params[0], Params.size());
Chris Lattner2114d5e2006-12-04 07:40:24 +0000210
Chris Lattnere168f762006-11-10 05:29:30 +0000211 return FD;
212}
213
Chris Lattner229ce602006-11-21 01:21:07 +0000214Sema::DeclTy *Sema::ParseFunctionDefBody(DeclTy *D, StmtTy *Body) {
215 FunctionDecl *FD = static_cast<FunctionDecl*>(D);
216 FD->setBody((Stmt*)Body);
217
218 assert(FD == CurFunctionDecl && "Function parsing confused");
219 CurFunctionDecl = 0;
220 return FD;
221}
222
223
Chris Lattnerac18be92006-11-20 06:49:47 +0000224/// ImplicitlyDefineFunction - An undeclared identifier was used in a function
225/// call, forming a call to an implicitly defined function (per C99 6.5.1p2).
226Decl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II,
227 Scope *S) {
228 if (getLangOptions().C99) // Extension in C99.
229 Diag(Loc, diag::ext_implicit_function_decl, II.getName());
230 else // Legal in C90, but warn about it.
231 Diag(Loc, diag::warn_implicit_function_decl, II.getName());
232
233 // FIXME: handle stuff like:
234 // void foo() { extern float X(); }
235 // void bar() { X(); } <-- implicit decl for X in another scope.
236
237 // Set a Declarator for the implicit definition: int foo();
Chris Lattner353f5742006-11-28 04:50:12 +0000238 const char *Dummy;
Chris Lattnerac18be92006-11-20 06:49:47 +0000239 DeclSpec DS;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000240 bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy);
Chris Lattner353f5742006-11-28 04:50:12 +0000241 assert(!Error && "Error setting up implicit decl!");
Chris Lattnerac18be92006-11-20 06:49:47 +0000242 Declarator D(DS, Declarator::BlockContext);
Chris Lattnercbc426d2006-12-02 06:43:02 +0000243 D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, Loc));
Chris Lattnerac18be92006-11-20 06:49:47 +0000244 D.SetIdentifier(&II, Loc);
245
246 Decl *Result = static_cast<Decl*>(ParseDeclarator(S, D, 0, 0));
247
248 // Visit this implicit declaration like any other top-level form.
249 LastInGroupList.push_back(Result);
250 return Result;
251}
252
Chris Lattner302b4be2006-11-19 02:31:38 +0000253
Chris Lattner18b19622007-01-22 07:39:13 +0000254Decl *Sema::ParseTypedefDecl(Scope *S, Declarator &D) {
Chris Lattnerda8aa7b2006-11-19 23:12:30 +0000255 assert(D.getIdentifier() && "Wrong callback for declspec withotu declarator");
Chris Lattner302b4be2006-11-19 02:31:38 +0000256
Chris Lattner5ca17df2006-11-19 23:32:49 +0000257 TypeRef T = GetTypeForDeclarator(D, S);
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000258 if (T.isNull()) return 0;
259
Chris Lattner18b19622007-01-22 07:39:13 +0000260 // Scope manipulation handled by caller.
261 return new TypedefDecl(D.getIdentifierLoc(), D.getIdentifier(), T);
Chris Lattnere168f762006-11-10 05:29:30 +0000262}
263
Chris Lattner18b19622007-01-22 07:39:13 +0000264
265/// ParseStructUnionTag - This is invoked when we see 'struct foo' or
266/// 'struct {'. In the former case, Name will be non-null. In the later case,
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000267/// Name will be null. TagType indicates what kind of tag this is. isUse
268/// indicates whether this is a use of a preexisting struct tag, or if it is a
269/// definition or declaration of a new one.
270Sema::DeclTy *Sema::ParseTag(Scope *S, unsigned TagType, bool isUse,
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000271 SourceLocation KWLoc, IdentifierInfo *Name,
272 SourceLocation NameLoc) {
Chris Lattner8799cf22007-01-23 01:57:16 +0000273 // If this is a use of an existing tag, it must have a name.
274 assert((isUse || Name != 0) && "Nameless record must have a name!");
275
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000276 Decl::Kind Kind;
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000277 switch (TagType) {
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000278 default: assert(0 && "Unknown tag type!");
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000279 case DeclSpec::TST_struct: Kind = Decl::Struct; break;
280 case DeclSpec::TST_union: Kind = Decl::Union; break;
281//case DeclSpec::TST_class: Kind = Decl::Class; break;
282 case DeclSpec::TST_enum: Kind = Decl::Enum; break;
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000283 }
Chris Lattner7e783a12007-01-23 02:05:42 +0000284
Chris Lattner18b19622007-01-22 07:39:13 +0000285 // If this is a named struct, check to see if there was a previous forward
286 // declaration or definition.
287 if (Decl *PrevDecl = LookupScopedDecl(Name, Decl::IDNS_Tag)) {
Chris Lattner8799cf22007-01-23 01:57:16 +0000288
289 // If this is a use of a previous tag, or if the tag is already declared in
290 // the same scope (so that the definition/declaration completes or
291 // rementions the tag), reuse the decl.
292 if (isUse || S->isDeclScope(PrevDecl)) {
Chris Lattner7e783a12007-01-23 02:05:42 +0000293 // Make sure that this wasn't declared as an enum and now used as a struct
294 // or something similar.
295 if (PrevDecl->getKind() != Kind) {
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000296 Diag(KWLoc, diag::err_use_with_wrong_tag, Name->getName());
Chris Lattner7e783a12007-01-23 02:05:42 +0000297 Diag(PrevDecl->getLocation(), diag::err_previous_use);
298 }
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000299
300 // Okay, this is a reference to the old decl, return it.
301 return PrevDecl;
Chris Lattner8799cf22007-01-23 01:57:16 +0000302 }
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000303 // If we get here, this is a definition of a new struct type in a nested
304 // scope, e.g. "struct foo; void bar() { struct foo; }", just create a new
305 // type.
Chris Lattner18b19622007-01-22 07:39:13 +0000306 }
307
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000308 // If there is an identifier, use the location of the identifier as the
309 // location of the decl, otherwise use the location of the struct/union
310 // keyword.
311 SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc;
312
Chris Lattner18b19622007-01-22 07:39:13 +0000313 // Otherwise, if this is the first time we've seen this tag, create the decl.
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000314 Decl *New;
315 if (Kind != Decl::Enum)
316 New = new RecordDecl(Kind, Loc, Name);
317 else
318 assert(0 && "Enum tags not implemented yet!");
Chris Lattner18b19622007-01-22 07:39:13 +0000319
320 // If this has an identifier, add it to the scope stack.
321 if (Name) {
322 New->setNext(Name->getFETokenInfo<Decl>());
323 Name->setFETokenInfo(New);
324 S->AddDecl(New);
325 }
326
327 return New;
328}