blob: 39d90b6e6a5380324c5eb9d0010dff23734ab3cb [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
Chris Lattner302b4be2006-11-19 02:31:38 +000028void Sema::PopScope(SourceLocation Loc, Scope *S) {
29 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
30 I != E; ++I) {
31 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
32 Decl *D = II.getFETokenInfo<Decl>();
33 assert(D && "This decl didn't get pushed??");
34
35 Decl *Next = D->getNext();
36
37 // FIXME: Push the decl on the parent function list if in a function.
38 // FIXME: Don't delete the decl when it gets popped!
39 delete D;
40
41 II.setFETokenInfo(Next);
42 }
43}
44
Chris Lattnere168f762006-11-10 05:29:30 +000045Action::DeclTy *
46Sema::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
47 DeclTy *LastInGroup) {
Chris Lattnerf84a79c2006-11-11 22:59:23 +000048 TypeRef DeclaratorType = GetTypeForDeclarator(D, S);
49
Chris Lattnere168f762006-11-10 05:29:30 +000050 IdentifierInfo *II = D.getIdentifier();
Chris Lattner302b4be2006-11-19 02:31:38 +000051 Decl *PrevDecl = 0;
52
53 if (II) {
54 PrevDecl = II->getFETokenInfo<Decl>();
55
56 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
57 }
Chris Lattnere168f762006-11-10 05:29:30 +000058
59 Decl *New;
Chris Lattner302b4be2006-11-19 02:31:38 +000060 if (D.getDeclSpec().StorageClassSpec == DeclSpec::SCS_typedef) {
61 New = ParseTypedefDecl(S, D, PrevDecl);
62 } else if (D.isFunctionDeclarator())
Chris Lattnere168f762006-11-10 05:29:30 +000063 New = new FunctionDecl(II, D, PrevDecl);
64 else
65 New = new VarDecl(II, D, PrevDecl);
66
Chris Lattner302b4be2006-11-19 02:31:38 +000067 if (!New) return 0;
68
69
Chris Lattnere168f762006-11-10 05:29:30 +000070 // If this has an identifier, add it to the scope stack.
71 if (II) {
72 // If PrevDecl includes conflicting name here, emit a diagnostic.
73 II->setFETokenInfo(New);
74 S->AddDecl(II);
75 }
76
77 // If this is a top-level decl that is chained to some other (e.g. int A,B,C;)
78 // remember this in the LastInGroupList list.
79 if (LastInGroup && S->getParent() == 0)
80 LastInGroupList.push_back((Decl*)LastInGroup);
81
82 return New;
83}
84
85Action::DeclTy *
86Sema::ParseFunctionDefinition(Scope *S, Declarator &D, StmtTy *Body) {
87 FunctionDecl *FD = (FunctionDecl *)ParseDeclarator(S, D, 0, 0);
88
89 FD->setBody((Stmt*)Body);
90
91 return FD;
92}
93
Chris Lattner302b4be2006-11-19 02:31:38 +000094
95Decl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, Decl *PrevDecl) {
96 assert(D.getIdentifier());
97
98 return new TypedefDecl(D.getIdentifier(), D, PrevDecl);
Chris Lattnere168f762006-11-10 05:29:30 +000099}
100