blob: 22c69c9388ad7d485a15a4b08b85e1caabbcc8de [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 Lattner697e5d62006-11-09 06:32:27 +000020using namespace llvm;
21using namespace clang;
22
Chris Lattnere168f762006-11-10 05:29:30 +000023
Chris Lattnere168f762006-11-10 05:29:30 +000024bool Sema::isTypeName(const IdentifierInfo &II, Scope *S) const {
25 Decl *D = II.getFETokenInfo<Decl>();
Chris Lattnerda8aa7b2006-11-19 23:12:30 +000026 return D != 0 && isa<TypeDecl>(D);
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 Lattner302b4be2006-11-19 02:31:38 +0000108
109Decl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, Decl *PrevDecl) {
Chris Lattnerda8aa7b2006-11-19 23:12:30 +0000110 assert(D.getIdentifier() && "Wrong callback for declspec withotu declarator");
Chris Lattner302b4be2006-11-19 02:31:38 +0000111
Chris Lattner5ca17df2006-11-19 23:32:49 +0000112 TypeRef T = GetTypeForDeclarator(D, S);
113 return new TypedefDecl(D.getIdentifier(), T, PrevDecl);
Chris Lattnere168f762006-11-10 05:29:30 +0000114}
115