blob: 41a9dda18511308a9dbe97ca25ebbdbb25774571 [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 Lattner200bdc32006-11-19 02:43:37 +0000104
Chris Lattner229ce602006-11-21 01:21:07 +0000105
106Sema::DeclTy *Sema::ParseStartOfFunctionDef(Scope *S, Declarator &D
107 /* TODO: FORMAL ARG INFO.*/) {
108 assert(CurFunctionDecl == 0 && "Function parsing confused");
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000109 assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
110 "Not a function declarator!");
111 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
112
113 // Verify 6.9.1p6: 'every identifier in the identifier list shall be declared'
114 // for a K&R function.
115 if (!FTI.hasPrototype) {
116 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
117 if (FTI.ArgInfo[i].TypeInfo == 0) {
118 Diag(FTI.ArgInfo[i].IdentLoc, diag::err_param_not_declared,
119 FTI.ArgInfo[i].Ident->getName());
120 // Implicitly declare the argument as type 'int' for lack of a better
121 // type.
122 FTI.ArgInfo[i].TypeInfo = Context.IntTy.getAsOpaquePtr();
123 }
124 }
125
126 // Since this is a function definition, act as though we have information
127 // about the arguments.
128 FTI.hasPrototype = true;
Chris Lattner2114d5e2006-12-04 07:40:24 +0000129 } else {
130 // FIXME: Diagnose arguments without names in C.
131
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000132 }
133
Chris Lattner229ce602006-11-21 01:21:07 +0000134 FunctionDecl *FD = static_cast<FunctionDecl*>(ParseDeclarator(S, D, 0, 0));
135 CurFunctionDecl = FD;
Chris Lattner2114d5e2006-12-04 07:40:24 +0000136
137 // Since this is a function definition, remember the names of the arguments in
138 // the FunctionDecl.
139
140 // FIXME: TODO. Add to FunctionDecl, install declarators into current scope.
141
142
Chris Lattnere168f762006-11-10 05:29:30 +0000143 return FD;
144}
145
Chris Lattner229ce602006-11-21 01:21:07 +0000146Sema::DeclTy *Sema::ParseFunctionDefBody(DeclTy *D, StmtTy *Body) {
147 FunctionDecl *FD = static_cast<FunctionDecl*>(D);
148 FD->setBody((Stmt*)Body);
149
150 assert(FD == CurFunctionDecl && "Function parsing confused");
151 CurFunctionDecl = 0;
152 return FD;
153}
154
155
Chris Lattnerac18be92006-11-20 06:49:47 +0000156/// ImplicitlyDefineFunction - An undeclared identifier was used in a function
157/// call, forming a call to an implicitly defined function (per C99 6.5.1p2).
158Decl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II,
159 Scope *S) {
160 if (getLangOptions().C99) // Extension in C99.
161 Diag(Loc, diag::ext_implicit_function_decl, II.getName());
162 else // Legal in C90, but warn about it.
163 Diag(Loc, diag::warn_implicit_function_decl, II.getName());
164
165 // FIXME: handle stuff like:
166 // void foo() { extern float X(); }
167 // void bar() { X(); } <-- implicit decl for X in another scope.
168
169 // Set a Declarator for the implicit definition: int foo();
Chris Lattner353f5742006-11-28 04:50:12 +0000170 const char *Dummy;
Chris Lattnerac18be92006-11-20 06:49:47 +0000171 DeclSpec DS;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000172 bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy);
Chris Lattner353f5742006-11-28 04:50:12 +0000173 assert(!Error && "Error setting up implicit decl!");
Chris Lattnerac18be92006-11-20 06:49:47 +0000174 Declarator D(DS, Declarator::BlockContext);
Chris Lattnercbc426d2006-12-02 06:43:02 +0000175 D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, Loc));
Chris Lattnerac18be92006-11-20 06:49:47 +0000176 D.SetIdentifier(&II, Loc);
177
178 Decl *Result = static_cast<Decl*>(ParseDeclarator(S, D, 0, 0));
179
180 // Visit this implicit declaration like any other top-level form.
181 LastInGroupList.push_back(Result);
182 return Result;
183}
184
Chris Lattner302b4be2006-11-19 02:31:38 +0000185
186Decl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, Decl *PrevDecl) {
Chris Lattnerda8aa7b2006-11-19 23:12:30 +0000187 assert(D.getIdentifier() && "Wrong callback for declspec withotu declarator");
Chris Lattner302b4be2006-11-19 02:31:38 +0000188
Chris Lattner5ca17df2006-11-19 23:32:49 +0000189 TypeRef T = GetTypeForDeclarator(D, S);
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000190 if (T.isNull()) return 0;
191
Chris Lattner5ca17df2006-11-19 23:32:49 +0000192 return new TypedefDecl(D.getIdentifier(), T, PrevDecl);
Chris Lattnere168f762006-11-10 05:29:30 +0000193}
194