blob: acc2c004671ba08c116365797ad3bbebcdffe101 [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
22
23bool 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) {
31 IdentifierInfo *II = D.getIdentifier();
32 Decl *PrevDecl = II ? II->getFETokenInfo<Decl>() : 0;
33
34 Decl *New;
35 if (D.isFunctionDeclarator())
36 New = new FunctionDecl(II, D, PrevDecl);
37 else
38 New = new VarDecl(II, D, PrevDecl);
39
40 // If this has an identifier, add it to the scope stack.
41 if (II) {
42 // If PrevDecl includes conflicting name here, emit a diagnostic.
43 II->setFETokenInfo(New);
44 S->AddDecl(II);
45 }
46
47 // If this is a top-level decl that is chained to some other (e.g. int A,B,C;)
48 // remember this in the LastInGroupList list.
49 if (LastInGroup && S->getParent() == 0)
50 LastInGroupList.push_back((Decl*)LastInGroup);
51
52 return New;
53}
54
55Action::DeclTy *
56Sema::ParseFunctionDefinition(Scope *S, Declarator &D, StmtTy *Body) {
57 FunctionDecl *FD = (FunctionDecl *)ParseDeclarator(S, D, 0, 0);
58
59 FD->setBody((Stmt*)Body);
60
61 return FD;
62}
63
64void Sema::PopScope(SourceLocation Loc, Scope *S) {
65 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
66 I != E; ++I) {
67 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
68 Decl *D = II.getFETokenInfo<Decl>();
69 assert(D && "This decl didn't get pushed??");
70
71 Decl *Next = D->getNext();
72
73 // FIXME: Push the decl on the parent function list if in a function.
74 delete D;
75
76 II.setFETokenInfo(Next);
77 }
78}
79