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