| 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 | 200bdc3 | 2006-11-19 02:43:37 +0000 | [diff] [blame^] | 45 | /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with |
| 46 | /// no declarator (e.g. "struct foo;") is parsed. |
| 47 | Sema::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 Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 59 | Action::DeclTy * |
| 60 | Sema::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init, |
| 61 | DeclTy *LastInGroup) { |
| Chris Lattner | f84a79c | 2006-11-11 22:59:23 +0000 | [diff] [blame] | 62 | TypeRef DeclaratorType = GetTypeForDeclarator(D, S); |
| 63 | |
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 64 | IdentifierInfo *II = D.getIdentifier(); |
| Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 65 | 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 Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 72 | |
| 73 | Decl *New; |
| Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 74 | if (D.getDeclSpec().StorageClassSpec == DeclSpec::SCS_typedef) { |
| 75 | New = ParseTypedefDecl(S, D, PrevDecl); |
| 76 | } else if (D.isFunctionDeclarator()) |
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 77 | New = new FunctionDecl(II, D, PrevDecl); |
| 78 | else |
| 79 | New = new VarDecl(II, D, PrevDecl); |
| 80 | |
| Chris Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 81 | if (!New) return 0; |
| 82 | |
| 83 | |
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 84 | // 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 Lattner | 200bdc3 | 2006-11-19 02:43:37 +0000 | [diff] [blame^] | 99 | |
| 100 | Sema::DeclTy * |
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 101 | Sema::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 Lattner | 302b4be | 2006-11-19 02:31:38 +0000 | [diff] [blame] | 109 | |
| 110 | Decl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, Decl *PrevDecl) { |
| 111 | assert(D.getIdentifier()); |
| 112 | |
| 113 | return new TypedefDecl(D.getIdentifier(), D, PrevDecl); |
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 114 | } |
| 115 | |