blob: 8e57fa78734077933337252103aeb234e1d2cd44 [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
36 Decl *Next = D->getNext();
37
38 // FIXME: Push the decl on the parent function list if in a function.
39 // FIXME: Don't delete the decl when it gets popped!
40 delete D;
41
42 II.setFETokenInfo(Next);
43 }
44}
45
Chris Lattner200bdc32006-11-19 02:43:37 +000046/// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
47/// no declarator (e.g. "struct foo;") is parsed.
48Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) {
49 // TODO: emit error on 'int;' or 'const enum foo;'.
50 // TODO: emit error on 'typedef int;'
51 // if (!DS.isMissingDeclaratorOk()) Diag(...);
52
53 // TODO: Register 'struct foo;' with the type system as an opaque struct.
54
55 // TODO: Check that we don't already have 'union foo;' or something else
56 // that conflicts.
57 return 0;
58}
59
Chris Lattnere168f762006-11-10 05:29:30 +000060Action::DeclTy *
61Sema::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
62 DeclTy *LastInGroup) {
63 IdentifierInfo *II = D.getIdentifier();
Chris Lattner302b4be2006-11-19 02:31:38 +000064 Decl *PrevDecl = 0;
65
66 if (II) {
67 PrevDecl = II->getFETokenInfo<Decl>();
68
69 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
70 }
Chris Lattnere168f762006-11-10 05:29:30 +000071
72 Decl *New;
Chris Lattner302b4be2006-11-19 02:31:38 +000073 if (D.getDeclSpec().StorageClassSpec == DeclSpec::SCS_typedef) {
74 New = ParseTypedefDecl(S, D, PrevDecl);
75 } else if (D.isFunctionDeclarator())
Chris Lattner5ca17df2006-11-19 23:32:49 +000076 New = new FunctionDecl(II, GetTypeForDeclarator(D, S), PrevDecl);
Chris Lattnere168f762006-11-10 05:29:30 +000077 else
Chris Lattner5ca17df2006-11-19 23:32:49 +000078 New = new VarDecl(II, GetTypeForDeclarator(D, S), PrevDecl);
Chris Lattnere168f762006-11-10 05:29:30 +000079
Chris Lattner302b4be2006-11-19 02:31:38 +000080 if (!New) return 0;
81
82
Chris Lattnere168f762006-11-10 05:29:30 +000083 // If this has an identifier, add it to the scope stack.
84 if (II) {
85 // If PrevDecl includes conflicting name here, emit a diagnostic.
86 II->setFETokenInfo(New);
87 S->AddDecl(II);
88 }
89
90 // If this is a top-level decl that is chained to some other (e.g. int A,B,C;)
91 // remember this in the LastInGroupList list.
92 if (LastInGroup && S->getParent() == 0)
93 LastInGroupList.push_back((Decl*)LastInGroup);
94
95 return New;
96}
97
Chris Lattner200bdc32006-11-19 02:43:37 +000098
99Sema::DeclTy *
Chris Lattnere168f762006-11-10 05:29:30 +0000100Sema::ParseFunctionDefinition(Scope *S, Declarator &D, StmtTy *Body) {
101 FunctionDecl *FD = (FunctionDecl *)ParseDeclarator(S, D, 0, 0);
102
103 FD->setBody((Stmt*)Body);
104
105 return FD;
106}
107
Chris Lattnerac18be92006-11-20 06:49:47 +0000108/// ImplicitlyDefineFunction - An undeclared identifier was used in a function
109/// call, forming a call to an implicitly defined function (per C99 6.5.1p2).
110Decl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II,
111 Scope *S) {
112 if (getLangOptions().C99) // Extension in C99.
113 Diag(Loc, diag::ext_implicit_function_decl, II.getName());
114 else // Legal in C90, but warn about it.
115 Diag(Loc, diag::warn_implicit_function_decl, II.getName());
116
117 // FIXME: handle stuff like:
118 // void foo() { extern float X(); }
119 // void bar() { X(); } <-- implicit decl for X in another scope.
120
121 // Set a Declarator for the implicit definition: int foo();
122 DeclSpec DS;
123 DS.TypeSpecType = DeclSpec::TST_int;
124 Declarator D(DS, Declarator::BlockContext);
125 D.AddTypeInfo(DeclaratorTypeInfo::getFunction(false, false, true, Loc));
126 D.SetIdentifier(&II, Loc);
127
128 Decl *Result = static_cast<Decl*>(ParseDeclarator(S, D, 0, 0));
129
130 // Visit this implicit declaration like any other top-level form.
131 LastInGroupList.push_back(Result);
132 return Result;
133}
134
Chris Lattner302b4be2006-11-19 02:31:38 +0000135
136Decl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, Decl *PrevDecl) {
Chris Lattnerda8aa7b2006-11-19 23:12:30 +0000137 assert(D.getIdentifier() && "Wrong callback for declspec withotu declarator");
Chris Lattner302b4be2006-11-19 02:31:38 +0000138
Chris Lattner5ca17df2006-11-19 23:32:49 +0000139 TypeRef T = GetTypeForDeclarator(D, S);
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000140 if (T.isNull()) return 0;
141
Chris Lattner5ca17df2006-11-19 23:32:49 +0000142 return new TypedefDecl(D.getIdentifier(), T, PrevDecl);
Chris Lattnere168f762006-11-10 05:29:30 +0000143}
144