blob: 329ce987e69b7a0b0d2da000ad671fc5249bc8fe [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) {
33 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
34 Decl *D = II.getFETokenInfo<Decl>();
35 assert(D && "This decl didn't get pushed??");
36
Chris Lattner229ce602006-11-21 01:21:07 +000037 II.setFETokenInfo(D->getNext());
Chris Lattner302b4be2006-11-19 02:31:38 +000038
Chris Lattner740b2f32006-11-21 01:32:20 +000039 // This will have to be revisited for C++: there we want to nest stuff in
40 // namespace decls etc. Even for C, we might want a top-level translation
41 // unit decl or something.
42 if (!CurFunctionDecl)
43 continue;
44
45 // Chain this decl to the containing function, it now owns the memory for
46 // the decl.
47 D->setNext(CurFunctionDecl->getDeclChain());
48 CurFunctionDecl->setDeclChain(D);
Chris Lattner302b4be2006-11-19 02:31:38 +000049 }
50}
51
Chris Lattner200bdc32006-11-19 02:43:37 +000052/// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
53/// no declarator (e.g. "struct foo;") is parsed.
54Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) {
55 // TODO: emit error on 'int;' or 'const enum foo;'.
56 // TODO: emit error on 'typedef int;'
57 // if (!DS.isMissingDeclaratorOk()) Diag(...);
58
59 // TODO: Register 'struct foo;' with the type system as an opaque struct.
60
61 // TODO: Check that we don't already have 'union foo;' or something else
62 // that conflicts.
63 return 0;
64}
65
Chris Lattnere168f762006-11-10 05:29:30 +000066Action::DeclTy *
67Sema::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
68 DeclTy *LastInGroup) {
69 IdentifierInfo *II = D.getIdentifier();
Chris Lattner302b4be2006-11-19 02:31:38 +000070 Decl *PrevDecl = 0;
71
72 if (II) {
73 PrevDecl = II->getFETokenInfo<Decl>();
74
75 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
76 }
Chris Lattnere168f762006-11-10 05:29:30 +000077
78 Decl *New;
Chris Lattner2114d5e2006-12-04 07:40:24 +000079 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
Chris Lattner302b4be2006-11-19 02:31:38 +000080 New = ParseTypedefDecl(S, D, PrevDecl);
Chris Lattner2114d5e2006-12-04 07:40:24 +000081 else if (D.isFunctionDeclarator())
Chris Lattner5ca17df2006-11-19 23:32:49 +000082 New = new FunctionDecl(II, GetTypeForDeclarator(D, S), PrevDecl);
Chris Lattnere168f762006-11-10 05:29:30 +000083 else
Chris Lattner5ca17df2006-11-19 23:32:49 +000084 New = new VarDecl(II, GetTypeForDeclarator(D, S), PrevDecl);
Chris Lattnere168f762006-11-10 05:29:30 +000085
Chris Lattner302b4be2006-11-19 02:31:38 +000086 if (!New) return 0;
87
88
Chris Lattnere168f762006-11-10 05:29:30 +000089 // If this has an identifier, add it to the scope stack.
90 if (II) {
91 // If PrevDecl includes conflicting name here, emit a diagnostic.
92 II->setFETokenInfo(New);
93 S->AddDecl(II);
94 }
95
96 // If this is a top-level decl that is chained to some other (e.g. int A,B,C;)
97 // remember this in the LastInGroupList list.
98 if (LastInGroup && S->getParent() == 0)
99 LastInGroupList.push_back((Decl*)LastInGroup);
100
101 return New;
102}
103
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000104VarDecl *
105Sema::ParseParamDeclarator(DeclaratorChunk &FTI, unsigned ArgNo,
106 Scope *FnScope) {
107 const DeclaratorChunk::ParamInfo &PI = FTI.Fun.ArgInfo[ArgNo];
Chris Lattner200bdc32006-11-19 02:43:37 +0000108
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000109 IdentifierInfo *II = PI.Ident;
110 Decl *PrevDecl = 0;
111
112 if (II) {
113 PrevDecl = II->getFETokenInfo<Decl>();
114
115 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
116 }
117
118 VarDecl *New = new VarDecl(II, static_cast<Type*>(PI.TypeInfo), PrevDecl);
119
120 // If this has an identifier, add it to the scope stack.
121 if (II) {
122 // If PrevDecl includes conflicting name here, emit a diagnostic.
123 II->setFETokenInfo(New);
124 FnScope->AddDecl(II);
125 }
Chris Lattner229ce602006-11-21 01:21:07 +0000126
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000127 return New;
128}
129
130
131Sema::DeclTy *Sema::ParseStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) {
Chris Lattner229ce602006-11-21 01:21:07 +0000132 assert(CurFunctionDecl == 0 && "Function parsing confused");
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000133 assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
134 "Not a function declarator!");
135 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
136
137 // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared'
138 // for a K&R function.
139 if (!FTI.hasPrototype) {
140 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
141 if (FTI.ArgInfo[i].TypeInfo == 0) {
142 Diag(FTI.ArgInfo[i].IdentLoc, diag::err_param_not_declared,
143 FTI.ArgInfo[i].Ident->getName());
144 // Implicitly declare the argument as type 'int' for lack of a better
145 // type.
146 FTI.ArgInfo[i].TypeInfo = Context.IntTy.getAsOpaquePtr();
147 }
148 }
149
150 // Since this is a function definition, act as though we have information
151 // about the arguments.
152 FTI.hasPrototype = true;
Chris Lattner2114d5e2006-12-04 07:40:24 +0000153 } else {
154 // FIXME: Diagnose arguments without names in C.
155
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000156 }
157
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000158 Scope *GlobalScope = FnBodyScope->getParent();
159
160 FunctionDecl *FD =
161 static_cast<FunctionDecl*>(ParseDeclarator(GlobalScope, D, 0, 0));
Chris Lattner229ce602006-11-21 01:21:07 +0000162 CurFunctionDecl = FD;
Chris Lattner2114d5e2006-12-04 07:40:24 +0000163
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000164 // Create Decl objects for each parameter, adding them to the FunctionDecl.
165 SmallVector<VarDecl*, 16> Params;
Chris Lattnerf61c8a82007-01-21 19:04:43 +0000166
167 // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes
168 // no arguments, not a function that takes a single void argument.
169 if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 &&
170 FTI.ArgInfo[0].TypeInfo == Context.VoidTy.getAsOpaquePtr()) {
171
172 } else {
173 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i)
174 Params.push_back(ParseParamDeclarator(D.getTypeObject(0), i,FnBodyScope));
175 }
Chris Lattner2114d5e2006-12-04 07:40:24 +0000176
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000177 FD->setParams(&Params[0], Params.size());
Chris Lattner2114d5e2006-12-04 07:40:24 +0000178
Chris Lattnere168f762006-11-10 05:29:30 +0000179 return FD;
180}
181
Chris Lattner229ce602006-11-21 01:21:07 +0000182Sema::DeclTy *Sema::ParseFunctionDefBody(DeclTy *D, StmtTy *Body) {
183 FunctionDecl *FD = static_cast<FunctionDecl*>(D);
184 FD->setBody((Stmt*)Body);
185
186 assert(FD == CurFunctionDecl && "Function parsing confused");
187 CurFunctionDecl = 0;
188 return FD;
189}
190
191
Chris Lattnerac18be92006-11-20 06:49:47 +0000192/// ImplicitlyDefineFunction - An undeclared identifier was used in a function
193/// call, forming a call to an implicitly defined function (per C99 6.5.1p2).
194Decl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II,
195 Scope *S) {
196 if (getLangOptions().C99) // Extension in C99.
197 Diag(Loc, diag::ext_implicit_function_decl, II.getName());
198 else // Legal in C90, but warn about it.
199 Diag(Loc, diag::warn_implicit_function_decl, II.getName());
200
201 // FIXME: handle stuff like:
202 // void foo() { extern float X(); }
203 // void bar() { X(); } <-- implicit decl for X in another scope.
204
205 // Set a Declarator for the implicit definition: int foo();
Chris Lattner353f5742006-11-28 04:50:12 +0000206 const char *Dummy;
Chris Lattnerac18be92006-11-20 06:49:47 +0000207 DeclSpec DS;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000208 bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy);
Chris Lattner353f5742006-11-28 04:50:12 +0000209 assert(!Error && "Error setting up implicit decl!");
Chris Lattnerac18be92006-11-20 06:49:47 +0000210 Declarator D(DS, Declarator::BlockContext);
Chris Lattnercbc426d2006-12-02 06:43:02 +0000211 D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, Loc));
Chris Lattnerac18be92006-11-20 06:49:47 +0000212 D.SetIdentifier(&II, Loc);
213
214 Decl *Result = static_cast<Decl*>(ParseDeclarator(S, D, 0, 0));
215
216 // Visit this implicit declaration like any other top-level form.
217 LastInGroupList.push_back(Result);
218 return Result;
219}
220
Chris Lattner302b4be2006-11-19 02:31:38 +0000221
222Decl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, Decl *PrevDecl) {
Chris Lattnerda8aa7b2006-11-19 23:12:30 +0000223 assert(D.getIdentifier() && "Wrong callback for declspec withotu declarator");
Chris Lattner302b4be2006-11-19 02:31:38 +0000224
Chris Lattner5ca17df2006-11-19 23:32:49 +0000225 TypeRef T = GetTypeForDeclarator(D, S);
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000226 if (T.isNull()) return 0;
227
Chris Lattner5ca17df2006-11-19 23:32:49 +0000228 return new TypedefDecl(D.getIdentifier(), T, PrevDecl);
Chris Lattnere168f762006-11-10 05:29:30 +0000229}
230