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