| Chris Lattner | 697e5d6 | 2006-11-09 06:32:27 +0000 | [diff] [blame] | 1 | //===--- 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 Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 14 | #include "Sema.h" | 
|  | 15 | #include "clang/AST/Decl.h" | 
| Chris Lattner | f84a79c | 2006-11-11 22:59:23 +0000 | [diff] [blame] | 16 | #include "clang/AST/Type.h" | 
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 17 | #include "clang/Parse/Scope.h" | 
|  | 18 | #include "clang/Lex/IdentifierTable.h" | 
| Chris Lattner | 697e5d6 | 2006-11-09 06:32:27 +0000 | [diff] [blame] | 19 | using namespace llvm; | 
|  | 20 | using namespace clang; | 
|  | 21 |  | 
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 22 |  | 
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 23 | bool 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 Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 28 | void 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 Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 45 | Action::DeclTy * | 
|  | 46 | Sema::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init, | 
|  | 47 | DeclTy *LastInGroup) { | 
| Chris Lattner | f84a79c | 2006-11-11 22:59:23 +0000 | [diff] [blame] | 48 | TypeRef DeclaratorType = GetTypeForDeclarator(D, S); | 
|  | 49 |  | 
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 50 | IdentifierInfo *II = D.getIdentifier(); | 
| Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 51 | 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 Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 58 |  | 
|  | 59 | Decl *New; | 
| Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 60 | if (D.getDeclSpec().StorageClassSpec == DeclSpec::SCS_typedef) { | 
|  | 61 | New = ParseTypedefDecl(S, D, PrevDecl); | 
|  | 62 | } else if (D.isFunctionDeclarator()) | 
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 63 | New = new FunctionDecl(II, D, PrevDecl); | 
|  | 64 | else | 
|  | 65 | New = new VarDecl(II, D, PrevDecl); | 
|  | 66 |  | 
| Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 67 | if (!New) return 0; | 
|  | 68 |  | 
|  | 69 |  | 
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 70 | // 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 |  | 
|  | 85 | Action::DeclTy * | 
|  | 86 | Sema::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 Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 94 |  | 
|  | 95 | Decl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, Decl *PrevDecl) { | 
|  | 96 | assert(D.getIdentifier()); | 
|  | 97 |  | 
|  | 98 | return new TypedefDecl(D.getIdentifier(), D, PrevDecl); | 
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 99 | } | 
|  | 100 |  |