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"
|
2006-12-03 16:41:30 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2006-11-10 13:29:30 +08:00
|
|
|
#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-20 14:49:47 +08:00
|
|
|
#include "clang/Basic/LangOptions.h"
|
2006-11-09 14:32:27 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace clang;
|
|
|
|
|
2006-11-10 13:29:30 +08:00
|
|
|
|
2006-11-20 09:29:42 +08:00
|
|
|
Sema::DeclTy *Sema::isTypeName(const IdentifierInfo &II, Scope *S) const {
|
|
|
|
return dyn_cast_or_null<TypeDecl>(II.getFETokenInfo<Decl>());
|
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) {
|
2007-01-22 06:37:37 +08:00
|
|
|
Decl *D = static_cast<Decl*>(*I);
|
2006-11-19 10:31:38 +08:00
|
|
|
assert(D && "This decl didn't get pushed??");
|
|
|
|
|
2007-01-22 06:37:37 +08:00
|
|
|
D->getIdentifier()->setFETokenInfo(D->getNext());
|
2006-11-19 10:31:38 +08:00
|
|
|
|
2006-11-21 09:32:20 +08:00
|
|
|
// This will have to be revisited for C++: there we want to nest stuff in
|
|
|
|
// namespace decls etc. Even for C, we might want a top-level translation
|
|
|
|
// unit decl or something.
|
|
|
|
if (!CurFunctionDecl)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Chain this decl to the containing function, it now owns the memory for
|
|
|
|
// the decl.
|
|
|
|
D->setNext(CurFunctionDecl->getDeclChain());
|
|
|
|
CurFunctionDecl->setDeclChain(D);
|
2006-11-19 10:31:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2007-01-22 06:37:37 +08:00
|
|
|
if (II)
|
2006-11-19 10:31:38 +08:00
|
|
|
PrevDecl = II->getFETokenInfo<Decl>();
|
2007-01-22 06:37:37 +08:00
|
|
|
if (PrevDecl) {
|
2006-11-19 10:31:38 +08:00
|
|
|
// TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
|
2007-01-22 06:37:37 +08:00
|
|
|
if (S->isDeclScope(PrevDecl)) {
|
|
|
|
// TODO: This is totally simplistic. It should handle merging functions
|
|
|
|
// together etc, merging extern int X; int X; ...
|
|
|
|
Diag(D.getIdentifierLoc(), diag::err_redefinition, II->getName());
|
|
|
|
Diag(PrevDecl->getLocation(), diag::err_previous_definition);
|
|
|
|
}
|
2006-11-19 10:31:38 +08:00
|
|
|
}
|
2006-11-10 13:29:30 +08:00
|
|
|
|
|
|
|
Decl *New;
|
2006-12-04 15:40:24 +08:00
|
|
|
if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
|
2006-11-19 10:31:38 +08:00
|
|
|
New = ParseTypedefDecl(S, D, PrevDecl);
|
2006-12-04 15:40:24 +08:00
|
|
|
else if (D.isFunctionDeclarator())
|
2007-01-22 06:37:37 +08:00
|
|
|
New = new FunctionDecl(D.getIdentifierLoc(), II, GetTypeForDeclarator(D, S),
|
|
|
|
PrevDecl);
|
2006-11-10 13:29:30 +08:00
|
|
|
else
|
2007-01-22 06:37:37 +08:00
|
|
|
New = new VarDecl(D.getIdentifierLoc(), 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);
|
2007-01-22 06:37:37 +08:00
|
|
|
S->AddDecl(New);
|
2006-11-10 13:29:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2007-01-21 15:42:07 +08:00
|
|
|
VarDecl *
|
|
|
|
Sema::ParseParamDeclarator(DeclaratorChunk &FTI, unsigned ArgNo,
|
|
|
|
Scope *FnScope) {
|
|
|
|
const DeclaratorChunk::ParamInfo &PI = FTI.Fun.ArgInfo[ArgNo];
|
2006-11-19 10:43:37 +08:00
|
|
|
|
2007-01-21 15:42:07 +08:00
|
|
|
IdentifierInfo *II = PI.Ident;
|
|
|
|
Decl *PrevDecl = 0;
|
|
|
|
|
2007-01-22 06:37:37 +08:00
|
|
|
if (II)
|
2007-01-21 15:42:07 +08:00
|
|
|
PrevDecl = II->getFETokenInfo<Decl>();
|
2007-01-22 06:37:37 +08:00
|
|
|
if (PrevDecl) {
|
2007-01-21 15:42:07 +08:00
|
|
|
|
|
|
|
// TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
|
|
|
|
}
|
|
|
|
|
2007-01-22 06:37:37 +08:00
|
|
|
VarDecl *New = new VarDecl(PI.IdentLoc, II, static_cast<Type*>(PI.TypeInfo),
|
|
|
|
PrevDecl);
|
2007-01-21 15:42:07 +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);
|
2007-01-22 06:37:37 +08:00
|
|
|
FnScope->AddDecl(New);
|
2007-01-21 15:42:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return New;
|
|
|
|
}
|
|
|
|
|
2006-11-21 09:21:07 +08:00
|
|
|
|
2007-01-21 15:42:07 +08:00
|
|
|
Sema::DeclTy *Sema::ParseStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) {
|
2006-11-21 09:21:07 +08:00
|
|
|
assert(CurFunctionDecl == 0 && "Function parsing confused");
|
2006-12-03 16:41:30 +08:00
|
|
|
assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
|
|
|
|
"Not a function declarator!");
|
|
|
|
DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
|
|
|
|
|
|
|
|
// Verify 6.9.1p6: 'every identifier in the identifier list shall be declared'
|
|
|
|
// for a K&R function.
|
|
|
|
if (!FTI.hasPrototype) {
|
|
|
|
for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
|
|
|
|
if (FTI.ArgInfo[i].TypeInfo == 0) {
|
|
|
|
Diag(FTI.ArgInfo[i].IdentLoc, diag::err_param_not_declared,
|
|
|
|
FTI.ArgInfo[i].Ident->getName());
|
|
|
|
// Implicitly declare the argument as type 'int' for lack of a better
|
|
|
|
// type.
|
|
|
|
FTI.ArgInfo[i].TypeInfo = Context.IntTy.getAsOpaquePtr();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since this is a function definition, act as though we have information
|
|
|
|
// about the arguments.
|
|
|
|
FTI.hasPrototype = true;
|
2006-12-04 15:40:24 +08:00
|
|
|
} else {
|
|
|
|
// FIXME: Diagnose arguments without names in C.
|
|
|
|
|
2006-12-03 16:41:30 +08:00
|
|
|
}
|
|
|
|
|
2007-01-21 15:42:07 +08:00
|
|
|
Scope *GlobalScope = FnBodyScope->getParent();
|
2006-12-04 15:40:24 +08:00
|
|
|
|
2007-01-21 15:42:07 +08:00
|
|
|
FunctionDecl *FD =
|
|
|
|
static_cast<FunctionDecl*>(ParseDeclarator(GlobalScope, D, 0, 0));
|
|
|
|
CurFunctionDecl = FD;
|
2006-12-04 15:40:24 +08:00
|
|
|
|
2007-01-21 15:42:07 +08:00
|
|
|
// Create Decl objects for each parameter, adding them to the FunctionDecl.
|
|
|
|
SmallVector<VarDecl*, 16> Params;
|
2007-01-22 03:04:43 +08:00
|
|
|
|
|
|
|
// Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes
|
|
|
|
// no arguments, not a function that takes a single void argument.
|
|
|
|
if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 &&
|
|
|
|
FTI.ArgInfo[0].TypeInfo == Context.VoidTy.getAsOpaquePtr()) {
|
2007-01-22 06:37:37 +08:00
|
|
|
// empty arg list, don't push any params.
|
2007-01-22 03:04:43 +08:00
|
|
|
} else {
|
|
|
|
for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i)
|
|
|
|
Params.push_back(ParseParamDeclarator(D.getTypeObject(0), i,FnBodyScope));
|
|
|
|
}
|
2006-12-04 15:40:24 +08:00
|
|
|
|
2007-01-21 15:42:07 +08:00
|
|
|
FD->setParams(&Params[0], Params.size());
|
2006-12-04 15:40:24 +08:00
|
|
|
|
2006-11-21 09:21:07 +08:00
|
|
|
return FD;
|
|
|
|
}
|
|
|
|
|
|
|
|
Sema::DeclTy *Sema::ParseFunctionDefBody(DeclTy *D, StmtTy *Body) {
|
|
|
|
FunctionDecl *FD = static_cast<FunctionDecl*>(D);
|
2006-11-10 13:29:30 +08:00
|
|
|
FD->setBody((Stmt*)Body);
|
|
|
|
|
2006-11-21 09:21:07 +08:00
|
|
|
assert(FD == CurFunctionDecl && "Function parsing confused");
|
|
|
|
CurFunctionDecl = 0;
|
2006-11-10 13:29:30 +08:00
|
|
|
return FD;
|
|
|
|
}
|
|
|
|
|
2006-11-21 09:21:07 +08:00
|
|
|
|
2006-11-20 14:49:47 +08:00
|
|
|
/// ImplicitlyDefineFunction - An undeclared identifier was used in a function
|
|
|
|
/// call, forming a call to an implicitly defined function (per C99 6.5.1p2).
|
|
|
|
Decl *Sema::ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II,
|
|
|
|
Scope *S) {
|
|
|
|
if (getLangOptions().C99) // Extension in C99.
|
|
|
|
Diag(Loc, diag::ext_implicit_function_decl, II.getName());
|
|
|
|
else // Legal in C90, but warn about it.
|
|
|
|
Diag(Loc, diag::warn_implicit_function_decl, II.getName());
|
|
|
|
|
|
|
|
// FIXME: handle stuff like:
|
|
|
|
// void foo() { extern float X(); }
|
|
|
|
// void bar() { X(); } <-- implicit decl for X in another scope.
|
|
|
|
|
|
|
|
// Set a Declarator for the implicit definition: int foo();
|
2006-11-28 12:50:12 +08:00
|
|
|
const char *Dummy;
|
2006-11-20 14:49:47 +08:00
|
|
|
DeclSpec DS;
|
2006-11-28 13:30:29 +08:00
|
|
|
bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy);
|
2006-11-28 12:50:12 +08:00
|
|
|
assert(!Error && "Error setting up implicit decl!");
|
2006-11-20 14:49:47 +08:00
|
|
|
Declarator D(DS, Declarator::BlockContext);
|
2006-12-02 14:43:02 +08:00
|
|
|
D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, Loc));
|
2006-11-20 14:49:47 +08:00
|
|
|
D.SetIdentifier(&II, Loc);
|
|
|
|
|
|
|
|
Decl *Result = static_cast<Decl*>(ParseDeclarator(S, D, 0, 0));
|
|
|
|
|
|
|
|
// Visit this implicit declaration like any other top-level form.
|
|
|
|
LastInGroupList.push_back(Result);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
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);
|
2006-11-20 12:34:45 +08:00
|
|
|
if (T.isNull()) return 0;
|
|
|
|
|
2007-01-22 06:37:37 +08:00
|
|
|
return new TypedefDecl(D.getIdentifierLoc(), D.getIdentifier(), T, PrevDecl);
|
2006-11-10 13:29:30 +08:00
|
|
|
}
|
|
|
|
|