blob: b2be165ceb4b77b17698290a49c7e1f867db0ef3 [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 Lattnere168f762006-11-10 05:29:30 +000017#include "clang/Parse/Scope.h"
18#include "clang/Lex/IdentifierTable.h"
Chris Lattner697e5d62006-11-09 06:32:27 +000019using namespace llvm;
20using namespace clang;
21
Chris Lattnere168f762006-11-10 05:29:30 +000022
Chris Lattnere168f762006-11-10 05:29:30 +000023bool Sema::isTypeName(const IdentifierInfo &II, Scope *S) const {
24 Decl *D = II.getFETokenInfo<Decl>();
25 return D != 0 && D->getDeclSpec().StorageClassSpec == DeclSpec::SCS_typedef;
26}
27
28Action::DeclTy *
29Sema::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
30 DeclTy *LastInGroup) {
Chris Lattnerf84a79c2006-11-11 22:59:23 +000031 TypeRef DeclaratorType = GetTypeForDeclarator(D, S);
32
33 // FIXME: Temporary.
34 if (!DeclaratorType.isNull())
35 DeclaratorType->dump();
36
37
Chris Lattnere168f762006-11-10 05:29:30 +000038 IdentifierInfo *II = D.getIdentifier();
39 Decl *PrevDecl = II ? II->getFETokenInfo<Decl>() : 0;
40
41 Decl *New;
42 if (D.isFunctionDeclarator())
43 New = new FunctionDecl(II, D, PrevDecl);
44 else
45 New = new VarDecl(II, D, PrevDecl);
46
47 // If this has an identifier, add it to the scope stack.
48 if (II) {
49 // If PrevDecl includes conflicting name here, emit a diagnostic.
50 II->setFETokenInfo(New);
51 S->AddDecl(II);
52 }
53
54 // If this is a top-level decl that is chained to some other (e.g. int A,B,C;)
55 // remember this in the LastInGroupList list.
56 if (LastInGroup && S->getParent() == 0)
57 LastInGroupList.push_back((Decl*)LastInGroup);
58
59 return New;
60}
61
62Action::DeclTy *
63Sema::ParseFunctionDefinition(Scope *S, Declarator &D, StmtTy *Body) {
64 FunctionDecl *FD = (FunctionDecl *)ParseDeclarator(S, D, 0, 0);
65
66 FD->setBody((Stmt*)Body);
67
68 return FD;
69}
70
71void Sema::PopScope(SourceLocation Loc, Scope *S) {
72 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
73 I != E; ++I) {
74 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
75 Decl *D = II.getFETokenInfo<Decl>();
76 assert(D && "This decl didn't get pushed??");
77
78 Decl *Next = D->getNext();
79
80 // FIXME: Push the decl on the parent function list if in a function.
81 delete D;
82
83 II.setFETokenInfo(Next);
84 }
85}
86