blob: 972ba56242a6dd15f6c34e0b60abcbb092ff7ec9 [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 Lattner200bdc32006-11-19 02:43:37 +000045/// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
46/// no declarator (e.g. "struct foo;") is parsed.
47Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) {
48 // TODO: emit error on 'int;' or 'const enum foo;'.
49 // TODO: emit error on 'typedef int;'
50 // if (!DS.isMissingDeclaratorOk()) Diag(...);
51
52 // TODO: Register 'struct foo;' with the type system as an opaque struct.
53
54 // TODO: Check that we don't already have 'union foo;' or something else
55 // that conflicts.
56 return 0;
57}
58
Chris Lattnere168f762006-11-10 05:29:30 +000059Action::DeclTy *
60Sema::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
61 DeclTy *LastInGroup) {
Chris Lattnerf84a79c2006-11-11 22:59:23 +000062 TypeRef DeclaratorType = GetTypeForDeclarator(D, S);
63
Chris Lattnere168f762006-11-10 05:29:30 +000064 IdentifierInfo *II = D.getIdentifier();
Chris Lattner302b4be2006-11-19 02:31:38 +000065 Decl *PrevDecl = 0;
66
67 if (II) {
68 PrevDecl = II->getFETokenInfo<Decl>();
69
70 // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
71 }
Chris Lattnere168f762006-11-10 05:29:30 +000072
73 Decl *New;
Chris Lattner302b4be2006-11-19 02:31:38 +000074 if (D.getDeclSpec().StorageClassSpec == DeclSpec::SCS_typedef) {
75 New = ParseTypedefDecl(S, D, PrevDecl);
76 } else if (D.isFunctionDeclarator())
Chris Lattnere168f762006-11-10 05:29:30 +000077 New = new FunctionDecl(II, D, PrevDecl);
78 else
79 New = new VarDecl(II, D, PrevDecl);
80
Chris Lattner302b4be2006-11-19 02:31:38 +000081 if (!New) return 0;
82
83
Chris Lattnere168f762006-11-10 05:29:30 +000084 // If this has an identifier, add it to the scope stack.
85 if (II) {
86 // If PrevDecl includes conflicting name here, emit a diagnostic.
87 II->setFETokenInfo(New);
88 S->AddDecl(II);
89 }
90
91 // If this is a top-level decl that is chained to some other (e.g. int A,B,C;)
92 // remember this in the LastInGroupList list.
93 if (LastInGroup && S->getParent() == 0)
94 LastInGroupList.push_back((Decl*)LastInGroup);
95
96 return New;
97}
98
Chris Lattner200bdc32006-11-19 02:43:37 +000099
100Sema::DeclTy *
Chris Lattnere168f762006-11-10 05:29:30 +0000101Sema::ParseFunctionDefinition(Scope *S, Declarator &D, StmtTy *Body) {
102 FunctionDecl *FD = (FunctionDecl *)ParseDeclarator(S, D, 0, 0);
103
104 FD->setBody((Stmt*)Body);
105
106 return FD;
107}
108
Chris Lattner302b4be2006-11-19 02:31:38 +0000109
110Decl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, Decl *PrevDecl) {
111 assert(D.getIdentifier());
112
113 return new TypedefDecl(D.getIdentifier(), D, PrevDecl);
Chris Lattnere168f762006-11-10 05:29:30 +0000114}
115