blob: fbfa0fc6adebaee4ed5e04daadc037ecb1100c52 [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"
15#include "clang/AST/Decl.h"
Chris Lattnerf84a79c2006-11-11 22:59:23 +000016#include "clang/AST/Type.h"
Chris Lattner591a6752006-11-19 23:16:18 +000017#include "clang/Parse/DeclSpec.h"
Chris Lattnere168f762006-11-10 05:29:30 +000018#include "clang/Parse/Scope.h"
19#include "clang/Lex/IdentifierTable.h"
Chris Lattnerac18be92006-11-20 06:49:47 +000020#include "clang/Basic/LangOptions.h"
Chris Lattner697e5d62006-11-09 06:32:27 +000021using namespace llvm;
22using namespace clang;
23
Chris Lattnere168f762006-11-10 05:29:30 +000024
Chris Lattner2ebe4bb2006-11-20 01:29:42 +000025Sema::DeclTy *Sema::isTypeName(const IdentifierInfo &II, Scope *S) const {
26 return dyn_cast_or_null<TypeDecl>(II.getFETokenInfo<Decl>());
Chris Lattnere168f762006-11-10 05:29:30 +000027}
28
Chris Lattner302b4be2006-11-19 02:31:38 +000029void Sema::PopScope(SourceLocation Loc, Scope *S) {
30 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
31 I != E; ++I) {
32 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
33 Decl *D = II.getFETokenInfo<Decl>();
34 assert(D && "This decl didn't get pushed??");
35
Chris Lattner229ce602006-11-21 01:21:07 +000036 II.setFETokenInfo(D->getNext());
Chris Lattner302b4be2006-11-19 02:31:38 +000037
Chris Lattner740b2f32006-11-21 01:32:20 +000038 // This will have to be revisited for C++: there we want to nest stuff in
39 // namespace decls etc. Even for C, we might want a top-level translation
40 // unit decl or something.
41 if (!CurFunctionDecl)
42 continue;
43
44 // Chain this decl to the containing function, it now owns the memory for
45 // the decl.
46 D->setNext(CurFunctionDecl->getDeclChain());
47 CurFunctionDecl->setDeclChain(D);
Chris Lattner302b4be2006-11-19 02:31:38 +000048 }
49}
50
Chris Lattner200bdc32006-11-19 02:43:37 +000051/// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
52/// no declarator (e.g. "struct foo;") is parsed.
53Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) {
54 // TODO: emit error on 'int;' or 'const enum foo;'.
55 // TODO: emit error on 'typedef int;'
56 // if (!DS.isMissingDeclaratorOk()) Diag(...);
57
58 // TODO: Register 'struct foo;' with the type system as an opaque struct.
59
60 // TODO: Check that we don't already have 'union foo;' or something else
61 // that conflicts.
62 return 0;
63}
64
Chris Lattnere168f762006-11-10 05:29:30 +000065Action::DeclTy *
66Sema::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
67 DeclTy *LastInGroup) {
68 IdentifierInfo *II = D.getIdentifier();
Chris Lattner302b4be2006-11-19 02:31:38 +000069 Decl *PrevDecl = 0;
70
71 if (II) {
72 PrevDecl = II->getFETokenInfo<Decl>();
73
74 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
75 }
Chris Lattnere168f762006-11-10 05:29:30 +000076
77 Decl *New;
Chris Lattner302b4be2006-11-19 02:31:38 +000078 if (D.getDeclSpec().StorageClassSpec == DeclSpec::SCS_typedef) {
79 New = ParseTypedefDecl(S, D, PrevDecl);
80 } else if (D.isFunctionDeclarator())
Chris Lattner5ca17df2006-11-19 23:32:49 +000081 New = new FunctionDecl(II, GetTypeForDeclarator(D, S), PrevDecl);
Chris Lattnere168f762006-11-10 05:29:30 +000082 else
Chris Lattner5ca17df2006-11-19 23:32:49 +000083 New = new VarDecl(II, GetTypeForDeclarator(D, S), PrevDecl);
Chris Lattnere168f762006-11-10 05:29:30 +000084
Chris Lattner302b4be2006-11-19 02:31:38 +000085 if (!New) return 0;
86
87
Chris Lattnere168f762006-11-10 05:29:30 +000088 // If this has an identifier, add it to the scope stack.
89 if (II) {
90 // If PrevDecl includes conflicting name here, emit a diagnostic.
91 II->setFETokenInfo(New);
92 S->AddDecl(II);
93 }
94
95 // If this is a top-level decl that is chained to some other (e.g. int A,B,C;)
96 // remember this in the LastInGroupList list.
97 if (LastInGroup && S->getParent() == 0)
98 LastInGroupList.push_back((Decl*)LastInGroup);
99
100 return New;
101}
102
Chris Lattner200bdc32006-11-19 02:43:37 +0000103
Chris Lattner229ce602006-11-21 01:21:07 +0000104
105Sema::DeclTy *Sema::ParseStartOfFunctionDef(Scope *S, Declarator &D
106 /* TODO: FORMAL ARG INFO.*/) {
107 assert(CurFunctionDecl == 0 && "Function parsing confused");
Chris Lattnere168f762006-11-10 05:29:30 +0000108
Chris Lattner229ce602006-11-21 01:21:07 +0000109 FunctionDecl *FD = static_cast<FunctionDecl*>(ParseDeclarator(S, D, 0, 0));
110 CurFunctionDecl = FD;
Chris Lattnere168f762006-11-10 05:29:30 +0000111 return FD;
112}
113
Chris Lattner229ce602006-11-21 01:21:07 +0000114Sema::DeclTy *Sema::ParseFunctionDefBody(DeclTy *D, StmtTy *Body) {
115 FunctionDecl *FD = static_cast<FunctionDecl*>(D);
116 FD->setBody((Stmt*)Body);
117
118 assert(FD == CurFunctionDecl && "Function parsing confused");
119 CurFunctionDecl = 0;
120 return FD;
121}
122
123
Chris Lattnerac18be92006-11-20 06:49:47 +0000124/// ImplicitlyDefineFunction - An undeclared identifier was used in a function
125/// call, forming a call to an implicitly defined function (per C99 6.5.1p2).
126Decl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II,
127 Scope *S) {
128 if (getLangOptions().C99) // Extension in C99.
129 Diag(Loc, diag::ext_implicit_function_decl, II.getName());
130 else // Legal in C90, but warn about it.
131 Diag(Loc, diag::warn_implicit_function_decl, II.getName());
132
133 // FIXME: handle stuff like:
134 // void foo() { extern float X(); }
135 // void bar() { X(); } <-- implicit decl for X in another scope.
136
137 // Set a Declarator for the implicit definition: int foo();
138 DeclSpec DS;
139 DS.TypeSpecType = DeclSpec::TST_int;
140 Declarator D(DS, Declarator::BlockContext);
141 D.AddTypeInfo(DeclaratorTypeInfo::getFunction(false, false, true, Loc));
142 D.SetIdentifier(&II, Loc);
143
144 Decl *Result = static_cast<Decl*>(ParseDeclarator(S, D, 0, 0));
145
146 // Visit this implicit declaration like any other top-level form.
147 LastInGroupList.push_back(Result);
148 return Result;
149}
150
Chris Lattner302b4be2006-11-19 02:31:38 +0000151
152Decl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, Decl *PrevDecl) {
Chris Lattnerda8aa7b2006-11-19 23:12:30 +0000153 assert(D.getIdentifier() && "Wrong callback for declspec withotu declarator");
Chris Lattner302b4be2006-11-19 02:31:38 +0000154
Chris Lattner5ca17df2006-11-19 23:32:49 +0000155 TypeRef T = GetTypeForDeclarator(D, S);
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000156 if (T.isNull()) return 0;
157
Chris Lattner5ca17df2006-11-19 23:32:49 +0000158 return new TypedefDecl(D.getIdentifier(), T, PrevDecl);
Chris Lattnere168f762006-11-10 05:29:30 +0000159}
160