blob: 7104bf78808a8e38a9c965c9444842576e09c465 [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;
166 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i)
167 Params.push_back(ParseParamDeclarator(D.getTypeObject(0), i, FnBodyScope));
Chris Lattner2114d5e2006-12-04 07:40:24 +0000168
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000169 FD->setParams(&Params[0], Params.size());
Chris Lattner2114d5e2006-12-04 07:40:24 +0000170
Chris Lattnere168f762006-11-10 05:29:30 +0000171 return FD;
172}
173
Chris Lattner229ce602006-11-21 01:21:07 +0000174Sema::DeclTy *Sema::ParseFunctionDefBody(DeclTy *D, StmtTy *Body) {
175 FunctionDecl *FD = static_cast<FunctionDecl*>(D);
176 FD->setBody((Stmt*)Body);
177
178 assert(FD == CurFunctionDecl && "Function parsing confused");
179 CurFunctionDecl = 0;
180 return FD;
181}
182
183
Chris Lattnerac18be92006-11-20 06:49:47 +0000184/// ImplicitlyDefineFunction - An undeclared identifier was used in a function
185/// call, forming a call to an implicitly defined function (per C99 6.5.1p2).
186Decl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II,
187 Scope *S) {
188 if (getLangOptions().C99) // Extension in C99.
189 Diag(Loc, diag::ext_implicit_function_decl, II.getName());
190 else // Legal in C90, but warn about it.
191 Diag(Loc, diag::warn_implicit_function_decl, II.getName());
192
193 // FIXME: handle stuff like:
194 // void foo() { extern float X(); }
195 // void bar() { X(); } <-- implicit decl for X in another scope.
196
197 // Set a Declarator for the implicit definition: int foo();
Chris Lattner353f5742006-11-28 04:50:12 +0000198 const char *Dummy;
Chris Lattnerac18be92006-11-20 06:49:47 +0000199 DeclSpec DS;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000200 bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy);
Chris Lattner353f5742006-11-28 04:50:12 +0000201 assert(!Error && "Error setting up implicit decl!");
Chris Lattnerac18be92006-11-20 06:49:47 +0000202 Declarator D(DS, Declarator::BlockContext);
Chris Lattnercbc426d2006-12-02 06:43:02 +0000203 D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, Loc));
Chris Lattnerac18be92006-11-20 06:49:47 +0000204 D.SetIdentifier(&II, Loc);
205
206 Decl *Result = static_cast<Decl*>(ParseDeclarator(S, D, 0, 0));
207
208 // Visit this implicit declaration like any other top-level form.
209 LastInGroupList.push_back(Result);
210 return Result;
211}
212
Chris Lattner302b4be2006-11-19 02:31:38 +0000213
214Decl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, Decl *PrevDecl) {
Chris Lattnerda8aa7b2006-11-19 23:12:30 +0000215 assert(D.getIdentifier() && "Wrong callback for declspec withotu declarator");
Chris Lattner302b4be2006-11-19 02:31:38 +0000216
Chris Lattner5ca17df2006-11-19 23:32:49 +0000217 TypeRef T = GetTypeForDeclarator(D, S);
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000218 if (T.isNull()) return 0;
219
Chris Lattner5ca17df2006-11-19 23:32:49 +0000220 return new TypedefDecl(D.getIdentifier(), T, PrevDecl);
Chris Lattnere168f762006-11-10 05:29:30 +0000221}
222