2006-11-09 14:32:27 +08:00
|
|
|
//===--- SemaDecl.cpp - Semantic Analysis for Declarations ----------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Chris Lattner and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements semantic analysis for declarations.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-11-10 13:29:30 +08:00
|
|
|
#include "Sema.h"
|
|
|
|
#include "clang/AST/Decl.h"
|
2006-11-12 06:59:23 +08:00
|
|
|
#include "clang/AST/Type.h"
|
2006-11-20 07:16:18 +08:00
|
|
|
#include "clang/Parse/DeclSpec.h"
|
2006-11-10 13:29:30 +08:00
|
|
|
#include "clang/Parse/Scope.h"
|
|
|
|
#include "clang/Lex/IdentifierTable.h"
|
2006-11-09 14:32:27 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace clang;
|
|
|
|
|
2006-11-10 13:29:30 +08:00
|
|
|
|
|
|
|
bool Sema::isTypeName(const IdentifierInfo &II, Scope *S) const {
|
|
|
|
Decl *D = II.getFETokenInfo<Decl>();
|
2006-11-20 07:12:30 +08:00
|
|
|
return D != 0 && isa<TypeDecl>(D);
|
2006-11-10 13:29:30 +08:00
|
|
|
}
|
|
|
|
|
2006-11-19 10:31:38 +08:00
|
|
|
void Sema::PopScope(SourceLocation Loc, Scope *S) {
|
|
|
|
for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
|
|
|
|
Decl *D = II.getFETokenInfo<Decl>();
|
|
|
|
assert(D && "This decl didn't get pushed??");
|
|
|
|
|
|
|
|
Decl *Next = D->getNext();
|
|
|
|
|
|
|
|
// FIXME: Push the decl on the parent function list if in a function.
|
|
|
|
// FIXME: Don't delete the decl when it gets popped!
|
|
|
|
delete D;
|
|
|
|
|
|
|
|
II.setFETokenInfo(Next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-19 10:43:37 +08:00
|
|
|
/// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
|
|
|
|
/// no declarator (e.g. "struct foo;") is parsed.
|
|
|
|
Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) {
|
|
|
|
// TODO: emit error on 'int;' or 'const enum foo;'.
|
|
|
|
// TODO: emit error on 'typedef int;'
|
|
|
|
// if (!DS.isMissingDeclaratorOk()) Diag(...);
|
|
|
|
|
|
|
|
// TODO: Register 'struct foo;' with the type system as an opaque struct.
|
|
|
|
|
|
|
|
// TODO: Check that we don't already have 'union foo;' or something else
|
|
|
|
// that conflicts.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-11-10 13:29:30 +08:00
|
|
|
Action::DeclTy *
|
|
|
|
Sema::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
|
|
|
|
DeclTy *LastInGroup) {
|
|
|
|
IdentifierInfo *II = D.getIdentifier();
|
2006-11-19 10:31:38 +08:00
|
|
|
Decl *PrevDecl = 0;
|
|
|
|
|
|
|
|
if (II) {
|
|
|
|
PrevDecl = II->getFETokenInfo<Decl>();
|
|
|
|
|
|
|
|
// TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
|
|
|
|
}
|
2006-11-10 13:29:30 +08:00
|
|
|
|
|
|
|
Decl *New;
|
2006-11-19 10:31:38 +08:00
|
|
|
if (D.getDeclSpec().StorageClassSpec == DeclSpec::SCS_typedef) {
|
|
|
|
New = ParseTypedefDecl(S, D, PrevDecl);
|
|
|
|
} else if (D.isFunctionDeclarator())
|
2006-11-20 07:32:49 +08:00
|
|
|
New = new FunctionDecl(II, GetTypeForDeclarator(D, S), PrevDecl);
|
2006-11-10 13:29:30 +08:00
|
|
|
else
|
2006-11-20 07:32:49 +08:00
|
|
|
New = new VarDecl(II, GetTypeForDeclarator(D, S), PrevDecl);
|
2006-11-10 13:29:30 +08:00
|
|
|
|
2006-11-19 10:31:38 +08:00
|
|
|
if (!New) return 0;
|
|
|
|
|
|
|
|
|
2006-11-10 13:29:30 +08:00
|
|
|
// If this has an identifier, add it to the scope stack.
|
|
|
|
if (II) {
|
|
|
|
// If PrevDecl includes conflicting name here, emit a diagnostic.
|
|
|
|
II->setFETokenInfo(New);
|
|
|
|
S->AddDecl(II);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a top-level decl that is chained to some other (e.g. int A,B,C;)
|
|
|
|
// remember this in the LastInGroupList list.
|
|
|
|
if (LastInGroup && S->getParent() == 0)
|
|
|
|
LastInGroupList.push_back((Decl*)LastInGroup);
|
|
|
|
|
|
|
|
return New;
|
|
|
|
}
|
|
|
|
|
2006-11-19 10:43:37 +08:00
|
|
|
|
|
|
|
Sema::DeclTy *
|
2006-11-10 13:29:30 +08:00
|
|
|
Sema::ParseFunctionDefinition(Scope *S, Declarator &D, StmtTy *Body) {
|
|
|
|
FunctionDecl *FD = (FunctionDecl *)ParseDeclarator(S, D, 0, 0);
|
|
|
|
|
|
|
|
FD->setBody((Stmt*)Body);
|
|
|
|
|
|
|
|
return FD;
|
|
|
|
}
|
|
|
|
|
2006-11-19 10:31:38 +08:00
|
|
|
|
|
|
|
Decl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, Decl *PrevDecl) {
|
2006-11-20 07:12:30 +08:00
|
|
|
assert(D.getIdentifier() && "Wrong callback for declspec withotu declarator");
|
2006-11-19 10:31:38 +08:00
|
|
|
|
2006-11-20 07:32:49 +08:00
|
|
|
TypeRef T = GetTypeForDeclarator(D, S);
|
|
|
|
return new TypedefDecl(D.getIdentifier(), T, PrevDecl);
|
2006-11-10 13:29:30 +08:00
|
|
|
}
|
|
|
|
|