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"
|
2007-01-24 07:42:53 +08:00
|
|
|
#include "clang/AST/Expr.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"
|
2007-01-27 14:24:01 +08:00
|
|
|
#include "llvm/ADT/SmallSet.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 {
|
2007-01-26 10:01:53 +08:00
|
|
|
return dyn_cast_or_null<TypedefDecl>(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-23 09:33:16 +08:00
|
|
|
IdentifierInfo *II = D->getIdentifier();
|
|
|
|
if (!II) continue;
|
2006-11-19 10:31:38 +08:00
|
|
|
|
2007-01-23 09:33:16 +08:00
|
|
|
// Unlink this decl from the identifier. Because the scope contains decls
|
|
|
|
// in an unordered collection, and because we have multiple identifier
|
|
|
|
// namespaces (e.g. tag, normal, label),the decl may not be the first entry.
|
|
|
|
if (II->getFETokenInfo<Decl>() == D) {
|
|
|
|
// Normal case, no multiple decls in different namespaces.
|
|
|
|
II->setFETokenInfo(D->getNext());
|
|
|
|
} else {
|
|
|
|
// Scan ahead. There are only three namespaces in C, so this loop can
|
|
|
|
// never execute more than 3 times.
|
|
|
|
Decl *SomeDecl = II->getFETokenInfo<Decl>();
|
|
|
|
while (SomeDecl->getNext() != D) {
|
|
|
|
SomeDecl = SomeDecl->getNext();
|
|
|
|
assert(SomeDecl && "Didn't find this decl on its identifier's chain!");
|
|
|
|
}
|
|
|
|
SomeDecl->setNext(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;
|
|
|
|
}
|
|
|
|
|
2007-01-22 15:39:13 +08:00
|
|
|
/// LookupScopedDecl - Look up the inner-most declaration in the specified
|
|
|
|
/// namespace.
|
|
|
|
static Decl *LookupScopedDecl(IdentifierInfo *II, Decl::IdentifierNamespace NS){
|
|
|
|
if (II == 0) return 0;
|
|
|
|
|
|
|
|
// Scan up the scope chain looking for a decl that matches this identifier
|
|
|
|
// that is in the appropriate namespace. This search should not take long, as
|
|
|
|
// shadowing of names is uncommon, and deep shadowing is extremely uncommon.
|
|
|
|
for (Decl *D = II->getFETokenInfo<Decl>(); D; D = D->getNext())
|
|
|
|
if (D->getIdentifierNamespace() == NS)
|
|
|
|
return D;
|
|
|
|
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
|
|
|
|
2007-01-22 15:39:13 +08:00
|
|
|
if (Decl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary)) {
|
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;
|
2007-01-26 07:09:03 +08:00
|
|
|
if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
|
2007-01-22 15:39:13 +08:00
|
|
|
New = ParseTypedefDecl(S, D);
|
2007-01-26 07:09:03 +08:00
|
|
|
if (!New) return 0;
|
|
|
|
} else if (D.isFunctionDeclarator()) {
|
|
|
|
TypeRef R = GetTypeForDeclarator(D, S);
|
|
|
|
if (R.isNull()) return 0;
|
|
|
|
New = new FunctionDecl(D.getIdentifierLoc(), II, R);
|
|
|
|
} else {
|
|
|
|
TypeRef R = GetTypeForDeclarator(D, S);
|
|
|
|
if (R.isNull()) return 0;
|
|
|
|
New = new VarDecl(D.getIdentifierLoc(), II, R);
|
|
|
|
}
|
2006-11-19 10:31:38 +08:00
|
|
|
|
|
|
|
|
2006-11-10 13:29:30 +08:00
|
|
|
// If this has an identifier, add it to the scope stack.
|
|
|
|
if (II) {
|
2007-01-22 15:39:13 +08:00
|
|
|
New->setNext(II->getFETokenInfo<Decl>());
|
2006-11-10 13:29:30 +08:00
|
|
|
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;
|
2007-01-23 13:14:32 +08:00
|
|
|
// TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
|
|
|
|
// Can this happen for params? We already checked that they don't conflict
|
|
|
|
// among each other. Here they can only shadow globals, which is ok.
|
2007-01-22 15:39:13 +08:00
|
|
|
if (Decl *PrevDecl = LookupScopedDecl(II, Decl::IDNS_Ordinary)) {
|
2007-01-21 15:42:07 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-01-27 14:24:01 +08:00
|
|
|
VarDecl *New = new VarDecl(PI.IdentLoc, II,
|
|
|
|
TypeRef::getFromOpaquePtr(PI.TypeInfo));
|
2007-01-21 15:42:07 +08:00
|
|
|
|
|
|
|
// If this has an identifier, add it to the scope stack.
|
|
|
|
if (II) {
|
2007-01-22 15:39:13 +08:00
|
|
|
New->setNext(II->getFETokenInfo<Decl>());
|
2007-01-21 15:42:07 +08:00
|
|
|
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
|
|
|
|
2007-01-22 15:39:13 +08:00
|
|
|
Decl *Sema::ParseTypedefDecl(Scope *S, Declarator &D) {
|
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 15:39:13 +08:00
|
|
|
// Scope manipulation handled by caller.
|
|
|
|
return new TypedefDecl(D.getIdentifierLoc(), D.getIdentifier(), T);
|
2006-11-10 13:29:30 +08:00
|
|
|
}
|
|
|
|
|
2007-01-22 15:39:13 +08:00
|
|
|
|
2007-01-24 07:42:53 +08:00
|
|
|
/// ParseTag - This is invoked when we see 'struct foo' or 'struct {'. In the
|
|
|
|
/// former case, Name will be non-null. In the later case, Name will be null.
|
|
|
|
/// TagType indicates what kind of tag this is. TK indicates whether this is a
|
|
|
|
/// reference/declaration/definition of a tag.
|
2007-01-24 04:11:08 +08:00
|
|
|
Sema::DeclTy *Sema::ParseTag(Scope *S, unsigned TagType, TagKind TK,
|
2007-01-23 12:08:05 +08:00
|
|
|
SourceLocation KWLoc, IdentifierInfo *Name,
|
|
|
|
SourceLocation NameLoc) {
|
2007-01-23 09:57:16 +08:00
|
|
|
// If this is a use of an existing tag, it must have a name.
|
2007-01-24 04:11:08 +08:00
|
|
|
assert((Name != 0 || TK == TK_Definition) &&
|
|
|
|
"Nameless record must be a definition!");
|
2007-01-23 09:57:16 +08:00
|
|
|
|
2007-01-23 12:08:05 +08:00
|
|
|
Decl::Kind Kind;
|
2007-01-23 12:27:41 +08:00
|
|
|
switch (TagType) {
|
2007-01-23 12:08:05 +08:00
|
|
|
default: assert(0 && "Unknown tag type!");
|
2007-01-23 12:27:41 +08:00
|
|
|
case DeclSpec::TST_struct: Kind = Decl::Struct; break;
|
|
|
|
case DeclSpec::TST_union: Kind = Decl::Union; break;
|
|
|
|
//case DeclSpec::TST_class: Kind = Decl::Class; break;
|
|
|
|
case DeclSpec::TST_enum: Kind = Decl::Enum; break;
|
2007-01-23 12:08:05 +08:00
|
|
|
}
|
2007-01-23 10:05:42 +08:00
|
|
|
|
2007-01-22 15:39:13 +08:00
|
|
|
// If this is a named struct, check to see if there was a previous forward
|
|
|
|
// declaration or definition.
|
2007-01-24 04:11:08 +08:00
|
|
|
if (TagDecl *PrevDecl =
|
|
|
|
dyn_cast_or_null<TagDecl>(LookupScopedDecl(Name, Decl::IDNS_Tag))) {
|
2007-01-23 09:57:16 +08:00
|
|
|
|
|
|
|
// If this is a use of a previous tag, or if the tag is already declared in
|
|
|
|
// the same scope (so that the definition/declaration completes or
|
|
|
|
// rementions the tag), reuse the decl.
|
2007-01-24 04:11:08 +08:00
|
|
|
if (TK == TK_Reference || S->isDeclScope(PrevDecl)) {
|
2007-01-23 10:05:42 +08:00
|
|
|
// Make sure that this wasn't declared as an enum and now used as a struct
|
|
|
|
// or something similar.
|
|
|
|
if (PrevDecl->getKind() != Kind) {
|
2007-01-23 12:08:05 +08:00
|
|
|
Diag(KWLoc, diag::err_use_with_wrong_tag, Name->getName());
|
2007-01-23 10:05:42 +08:00
|
|
|
Diag(PrevDecl->getLocation(), diag::err_previous_use);
|
|
|
|
}
|
2007-01-24 04:11:08 +08:00
|
|
|
|
|
|
|
// If this is a use or a forward declaration, we're good.
|
|
|
|
if (TK != TK_Definition)
|
|
|
|
return PrevDecl;
|
2007-01-23 12:08:05 +08:00
|
|
|
|
2007-01-24 04:11:08 +08:00
|
|
|
// Diagnose attempts to redefine a tag.
|
|
|
|
if (PrevDecl->isDefinition()) {
|
|
|
|
Diag(NameLoc, diag::err_redefinition, Name->getName());
|
|
|
|
Diag(PrevDecl->getLocation(), diag::err_previous_definition);
|
|
|
|
// If this is a redefinition, recover by making this struct be
|
|
|
|
// anonymous, which will make any later references get the previous
|
|
|
|
// definition.
|
|
|
|
Name = 0;
|
|
|
|
} else {
|
|
|
|
// Okay, this is definition of a previously declared or referenced tag.
|
|
|
|
// Move the location of the decl to be the definition site.
|
|
|
|
PrevDecl->setLocation(NameLoc);
|
|
|
|
return PrevDecl;
|
|
|
|
}
|
2007-01-23 09:57:16 +08:00
|
|
|
}
|
2007-01-23 12:08:05 +08:00
|
|
|
// If we get here, this is a definition of a new struct type in a nested
|
|
|
|
// scope, e.g. "struct foo; void bar() { struct foo; }", just create a new
|
|
|
|
// type.
|
2007-01-22 15:39:13 +08:00
|
|
|
}
|
|
|
|
|
2007-01-23 12:27:41 +08:00
|
|
|
// If there is an identifier, use the location of the identifier as the
|
|
|
|
// location of the decl, otherwise use the location of the struct/union
|
|
|
|
// keyword.
|
|
|
|
SourceLocation Loc = NameLoc.isValid() ? NameLoc : KWLoc;
|
|
|
|
|
2007-01-22 15:39:13 +08:00
|
|
|
// Otherwise, if this is the first time we've seen this tag, create the decl.
|
2007-01-24 04:11:08 +08:00
|
|
|
TagDecl *New;
|
2007-01-25 08:44:24 +08:00
|
|
|
switch (Kind) {
|
|
|
|
default: assert(0 && "Unknown tag kind!");
|
2007-01-25 14:27:24 +08:00
|
|
|
case Decl::Enum:
|
|
|
|
New = new EnumDecl(Loc, Name);
|
|
|
|
// If this is an undefined enum, warn.
|
2007-01-25 15:29:02 +08:00
|
|
|
if (TK != TK_Definition) Diag(Loc, diag::ext_forward_ref_enum);
|
2007-01-25 14:27:24 +08:00
|
|
|
break;
|
2007-01-25 08:44:24 +08:00
|
|
|
case Decl::Union:
|
|
|
|
case Decl::Struct:
|
|
|
|
case Decl::Class:
|
2007-01-23 12:08:05 +08:00
|
|
|
New = new RecordDecl(Kind, Loc, Name);
|
2007-01-25 08:44:24 +08:00
|
|
|
break;
|
|
|
|
}
|
2007-01-22 15:39:13 +08:00
|
|
|
|
|
|
|
// If this has an identifier, add it to the scope stack.
|
|
|
|
if (Name) {
|
|
|
|
New->setNext(Name->getFETokenInfo<Decl>());
|
|
|
|
Name->setFETokenInfo(New);
|
|
|
|
S->AddDecl(New);
|
|
|
|
}
|
|
|
|
|
|
|
|
return New;
|
|
|
|
}
|
2007-01-24 07:42:53 +08:00
|
|
|
|
|
|
|
/// ParseField - Each field of a struct/union/class is passed into this in order
|
|
|
|
/// to create a FieldDecl object for it.
|
|
|
|
Sema::DeclTy *Sema::ParseField(Scope *S, DeclTy *TagDecl,
|
|
|
|
SourceLocation DeclStart,
|
|
|
|
Declarator &D, ExprTy *BitfieldWidth) {
|
|
|
|
IdentifierInfo *II = D.getIdentifier();
|
|
|
|
Expr *BitWidth = (Expr*)BitfieldWidth;
|
|
|
|
|
|
|
|
SourceLocation Loc = DeclStart;
|
|
|
|
if (II) Loc = D.getIdentifierLoc();
|
|
|
|
|
|
|
|
if (BitWidth) {
|
|
|
|
// TODO: Validate.
|
2007-01-26 07:09:03 +08:00
|
|
|
printf("WARNING: BITFIELDS IGNORED!\n");
|
2007-01-24 07:42:53 +08:00
|
|
|
|
|
|
|
// 6.7.2.1p3
|
|
|
|
// 6.7.2.1p4
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Not a bitfield.
|
|
|
|
|
|
|
|
// validate II.
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-01-26 07:09:03 +08:00
|
|
|
TypeRef T = GetTypeForDeclarator(D, S);
|
|
|
|
if (T.isNull()) return 0;
|
|
|
|
|
|
|
|
return new FieldDecl(Loc, II, T);
|
2007-01-24 07:42:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::ParseRecordBody(SourceLocation RecLoc, DeclTy *RecDecl,
|
|
|
|
DeclTy **Fields, unsigned NumFields) {
|
2007-01-25 15:29:02 +08:00
|
|
|
RecordDecl *Record = cast<RecordDecl>(static_cast<Decl*>(RecDecl));
|
2007-01-24 07:42:53 +08:00
|
|
|
if (Record->isDefinition()) {
|
|
|
|
// Diagnose code like:
|
2007-01-24 10:11:17 +08:00
|
|
|
// struct S { struct S {} X; };
|
2007-01-24 07:42:53 +08:00
|
|
|
// We discover this when we complete the outer S. Reject and ignore the
|
|
|
|
// outer S.
|
|
|
|
Diag(Record->getLocation(), diag::err_nested_redefinition,
|
|
|
|
Record->getKindName());
|
|
|
|
Diag(RecLoc, diag::err_previous_definition);
|
|
|
|
return;
|
|
|
|
}
|
2007-01-24 10:11:17 +08:00
|
|
|
|
|
|
|
// Verify that all the fields are okay.
|
2007-01-24 10:26:21 +08:00
|
|
|
unsigned NumNamedMembers = 0;
|
2007-01-25 12:52:46 +08:00
|
|
|
SmallVector<Decl*, 32> RecFields;
|
2007-01-26 06:48:42 +08:00
|
|
|
SmallSet<const IdentifierInfo*, 32> FieldIDs;
|
|
|
|
|
2007-01-24 10:11:17 +08:00
|
|
|
for (unsigned i = 0; i != NumFields; ++i) {
|
|
|
|
FieldDecl *FD = cast_or_null<FieldDecl>(static_cast<Decl*>(Fields[i]));
|
|
|
|
if (!FD) continue; // Already issued a diagnostic.
|
2007-01-25 08:44:24 +08:00
|
|
|
|
|
|
|
// Get the type for the field.
|
|
|
|
Type *FDTy = FD->getType()->getCanonicalType();
|
|
|
|
|
2007-01-24 10:11:17 +08:00
|
|
|
// C99 6.7.2.1p2 - A field may not be a function type.
|
2007-01-25 08:44:24 +08:00
|
|
|
if (isa<FunctionType>(FDTy)) {
|
2007-01-24 10:11:17 +08:00
|
|
|
Diag(FD->getLocation(), diag::err_field_declared_as_function,
|
|
|
|
FD->getName());
|
2007-01-24 10:26:21 +08:00
|
|
|
delete FD;
|
2007-01-24 10:11:17 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-01-24 10:26:21 +08:00
|
|
|
// C99 6.7.2.1p2 - A field may not be an incomplete type except...
|
2007-01-25 08:44:24 +08:00
|
|
|
if (FDTy->isIncompleteType()) {
|
2007-01-24 10:26:21 +08:00
|
|
|
if (i != NumFields-1 || // ... that the last member ...
|
|
|
|
Record->getKind() != Decl::Struct || // ... of a structure ...
|
2007-01-25 08:44:24 +08:00
|
|
|
!isa<ArrayType>(FDTy)) { //... may have incomplete array type.
|
2007-01-24 10:26:21 +08:00
|
|
|
Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName());
|
|
|
|
delete FD;
|
|
|
|
continue;
|
|
|
|
}
|
2007-01-25 08:44:24 +08:00
|
|
|
if (NumNamedMembers < 1) { //... must have more than named member ...
|
2007-01-24 10:26:21 +08:00
|
|
|
Diag(FD->getLocation(), diag::err_flexible_array_empty_struct,
|
|
|
|
FD->getName());
|
|
|
|
delete FD;
|
|
|
|
continue;
|
|
|
|
}
|
2007-01-24 10:11:17 +08:00
|
|
|
|
2007-01-25 08:44:24 +08:00
|
|
|
// Okay, we have a legal flexible array member at the end of the struct.
|
2007-01-25 12:52:46 +08:00
|
|
|
Record->setHasFlexibleArrayMember(true);
|
2007-01-25 08:44:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// C99 6.7.2.1p2 - a struct ending in a flexible array member cannot be the
|
|
|
|
/// field of another structure or the element of an array.
|
|
|
|
if (RecordType *FDTTy = dyn_cast<RecordType>(FDTy)) {
|
|
|
|
if (FDTTy->getDecl()->hasFlexibleArrayMember()) {
|
|
|
|
// If this is a member of a union, then entire union becomes "flexible".
|
|
|
|
if (Record->getKind() == Decl::Union) {
|
2007-01-25 12:52:46 +08:00
|
|
|
Record->setHasFlexibleArrayMember(true);
|
2007-01-25 08:44:24 +08:00
|
|
|
} else {
|
|
|
|
// If this is a struct/class and this is not the last element, reject
|
|
|
|
// it. Note that GCC supports variable sized arrays in the middle of
|
|
|
|
// structures.
|
|
|
|
if (i != NumFields-1) {
|
|
|
|
Diag(FD->getLocation(), diag::err_variable_sized_type_in_struct,
|
|
|
|
FD->getName());
|
|
|
|
delete FD;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We support flexible arrays at the end of structs in other structs
|
|
|
|
// as an extension.
|
|
|
|
Diag(FD->getLocation(), diag::ext_flexible_array_in_struct,
|
|
|
|
FD->getName());
|
2007-01-25 12:52:46 +08:00
|
|
|
Record->setHasFlexibleArrayMember(true);
|
2007-01-25 08:44:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-24 10:26:21 +08:00
|
|
|
// Keep track of the number of named members.
|
2007-01-26 06:48:42 +08:00
|
|
|
if (IdentifierInfo *II = FD->getIdentifier()) {
|
|
|
|
// Detect duplicate member names.
|
2007-01-27 10:14:08 +08:00
|
|
|
if (!FieldIDs.insert(II)) {
|
2007-01-26 06:48:42 +08:00
|
|
|
Diag(FD->getLocation(), diag::err_duplicate_member, II->getName());
|
|
|
|
// Find the previous decl.
|
|
|
|
SourceLocation PrevLoc;
|
|
|
|
for (unsigned i = 0, e = RecFields.size(); ; ++i) {
|
|
|
|
assert(i != e && "Didn't find previous def!");
|
|
|
|
if (RecFields[i]->getIdentifier() == II) {
|
|
|
|
PrevLoc = RecFields[i]->getLocation();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Diag(PrevLoc, diag::err_previous_definition);
|
|
|
|
delete FD;
|
|
|
|
continue;
|
|
|
|
}
|
2007-01-24 10:26:21 +08:00
|
|
|
++NumNamedMembers;
|
2007-01-26 06:48:42 +08:00
|
|
|
}
|
2007-01-25 12:52:46 +08:00
|
|
|
|
|
|
|
// Remember good fields.
|
|
|
|
RecFields.push_back(FD);
|
2007-01-24 10:11:17 +08:00
|
|
|
}
|
2007-01-24 10:26:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
// Okay, we successfully defined 'Record'.
|
2007-01-25 12:52:46 +08:00
|
|
|
Record->defineBody(&RecFields[0], RecFields.size());
|
2007-01-24 07:42:53 +08:00
|
|
|
}
|
|
|
|
|
2007-01-25 15:29:02 +08:00
|
|
|
Sema::DeclTy *Sema::ParseEnumConstant(Scope *S, DeclTy *EnumDeclX,
|
|
|
|
SourceLocation IdLoc, IdentifierInfo *Id,
|
|
|
|
SourceLocation EqualLoc, ExprTy *Val) {
|
|
|
|
EnumDecl *TheEnumDecl = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX));
|
2007-01-26 06:38:29 +08:00
|
|
|
|
|
|
|
// Verify that there isn't already something declared with this name in this
|
|
|
|
// scope.
|
|
|
|
if (Decl *PrevDecl = LookupScopedDecl(Id, Decl::IDNS_Ordinary)) {
|
|
|
|
if (S->isDeclScope(PrevDecl)) {
|
|
|
|
if (isa<EnumConstantDecl>(PrevDecl))
|
|
|
|
Diag(IdLoc, diag::err_redefinition_of_enumerator, Id->getName());
|
|
|
|
else
|
|
|
|
Diag(IdLoc, diag::err_redefinition, Id->getName());
|
|
|
|
Diag(PrevDecl->getLocation(), diag::err_previous_definition);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-25 15:29:02 +08:00
|
|
|
TypeRef Ty = Context.getTagDeclType(TheEnumDecl);
|
2007-01-26 06:38:29 +08:00
|
|
|
EnumConstantDecl *New = new EnumConstantDecl(IdLoc, Id, Ty);
|
|
|
|
|
|
|
|
// Register this decl in the current scope stack.
|
|
|
|
New->setNext(Id->getFETokenInfo<Decl>());
|
|
|
|
Id->setFETokenInfo(New);
|
|
|
|
S->AddDecl(New);
|
|
|
|
return New;
|
2007-01-25 15:29:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::ParseEnumBody(SourceLocation EnumLoc, DeclTy *EnumDeclX,
|
|
|
|
DeclTy **Elements, unsigned NumElements) {
|
|
|
|
EnumDecl *Enum = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX));
|
|
|
|
assert(!Enum->isDefinition() && "Enum redefinitions can't reach here");
|
|
|
|
|
|
|
|
// Verify that all the values are okay.
|
|
|
|
SmallVector<EnumConstantDecl*, 32> Values;
|
|
|
|
for (unsigned i = 0; i != NumElements; ++i) {
|
|
|
|
EnumConstantDecl *ECD =
|
|
|
|
cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i]));
|
|
|
|
if (!ECD) continue; // Already issued a diagnostic.
|
|
|
|
|
|
|
|
Values.push_back(ECD);
|
|
|
|
}
|
|
|
|
|
|
|
|
Enum->defineElements(&Values[0], Values.size());
|
|
|
|
}
|
2007-01-24 07:42:53 +08:00
|
|
|
|