2007-07-11 21:01:13 +04:00
|
|
|
//===--- SemaDecl.cpp - Semantic Analysis for Declarations ----------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 22:59:25 +03:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-07-11 21:01:13 +04:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements semantic analysis for declarations.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Sema.h"
|
2008-02-06 03:51:33 +03:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2007-07-11 21:01:13 +04:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2008-02-15 10:04:12 +03:00
|
|
|
#include "clang/AST/Attr.h"
|
2007-07-11 21:01:13 +04:00
|
|
|
#include "clang/AST/Builtins.h"
|
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "clang/AST/Expr.h"
|
|
|
|
#include "clang/AST/Type.h"
|
|
|
|
#include "clang/Parse/DeclSpec.h"
|
|
|
|
#include "clang/Parse/Scope.h"
|
|
|
|
#include "clang/Basic/LangOptions.h"
|
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2008-01-31 02:46:05 +03:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
// FIXME: layering (ideally, Sema shouldn't be dependent on Lex API's)
|
2008-02-06 03:51:33 +03:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2008-01-31 02:46:05 +03:00
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
2007-09-19 03:55:05 +04:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2007-07-11 21:01:13 +04:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2007-10-05 22:00:57 +04:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2007-07-11 21:01:13 +04:00
|
|
|
using namespace clang;
|
|
|
|
|
2008-04-02 18:35:35 +04:00
|
|
|
Sema::DeclTy *Sema::isTypeName(const IdentifierInfo &II, Scope *S) {
|
|
|
|
Decl *IIDecl = LookupDecl(&II, Decl::IDNS_Ordinary, S, false);
|
|
|
|
|
2008-04-14 01:07:44 +04:00
|
|
|
if (IIDecl && (isa<TypedefDecl>(IIDecl) ||
|
|
|
|
isa<ObjCInterfaceDecl>(IIDecl) ||
|
|
|
|
isa<TagDecl>(IIDecl)))
|
2007-10-12 20:34:10 +04:00
|
|
|
return IIDecl;
|
2007-09-07 01:24:23 +04:00
|
|
|
return 0;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2008-04-22 22:39:57 +04:00
|
|
|
void Sema::PushDeclContext(DeclContext *DC) {
|
2008-04-27 17:50:30 +04:00
|
|
|
assert( ( (isa<ObjCMethodDecl>(DC) && isa<TranslationUnitDecl>(CurContext))
|
2008-04-22 22:39:57 +04:00
|
|
|
|| DC->getParent() == CurContext ) &&
|
2008-04-06 08:47:34 +04:00
|
|
|
"The next DeclContext should be directly contained in the current one.");
|
2008-04-22 22:39:57 +04:00
|
|
|
CurContext = DC;
|
2008-04-04 10:12:32 +04:00
|
|
|
}
|
|
|
|
|
2008-04-06 08:47:34 +04:00
|
|
|
void Sema::PopDeclContext() {
|
|
|
|
assert(CurContext && "DeclContext imbalance!");
|
2008-04-17 18:40:12 +04:00
|
|
|
// If CurContext is a ObjC method, getParent() will return NULL.
|
2008-04-27 17:50:30 +04:00
|
|
|
CurContext = isa<ObjCMethodDecl>(CurContext)
|
2008-04-17 18:40:12 +04:00
|
|
|
? Context.getTranslationUnitDecl()
|
|
|
|
: CurContext->getParent();
|
2008-04-04 10:12:32 +04:00
|
|
|
}
|
|
|
|
|
2008-04-12 04:47:19 +04:00
|
|
|
/// Add this decl to the scope shadowed decl chains.
|
|
|
|
void Sema::PushOnScopeChains(NamedDecl *D, Scope *S) {
|
|
|
|
S->AddDecl(D);
|
2008-05-10 03:39:43 +04:00
|
|
|
|
|
|
|
// C++ [basic.scope]p4:
|
|
|
|
// -- exactly one declaration shall declare a class name or
|
|
|
|
// enumeration name that is not a typedef name and the other
|
|
|
|
// declarations shall all refer to the same object or
|
|
|
|
// enumerator, or all refer to functions and function templates;
|
|
|
|
// in this case the class name or enumeration name is hidden.
|
|
|
|
if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
|
|
|
|
// We are pushing the name of a tag (enum or class).
|
|
|
|
IdentifierResolver::ctx_iterator
|
|
|
|
CIT = IdResolver.ctx_begin(TD->getIdentifier(), TD->getDeclContext());
|
|
|
|
if (CIT != IdResolver.ctx_end(TD->getIdentifier()) &&
|
|
|
|
IdResolver.isDeclInScope(*CIT, TD->getDeclContext(), S)) {
|
|
|
|
// There is already a declaration with the same name in the same
|
|
|
|
// scope. It must be found before we find the new declaration,
|
|
|
|
// so swap the order on the shadowed declaration chain.
|
|
|
|
|
|
|
|
IdResolver.AddShadowedDecl(TD, *CIT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IdResolver.AddDecl(D);
|
2008-04-12 04:47:19 +04:00
|
|
|
}
|
|
|
|
|
2007-10-10 02:01:59 +04:00
|
|
|
void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) {
|
2007-08-26 10:24:45 +04:00
|
|
|
if (S->decl_empty()) return;
|
|
|
|
assert((S->getFlags() & Scope::DeclScope) &&"Scope shouldn't contain decls!");
|
2008-05-10 03:39:43 +04:00
|
|
|
|
|
|
|
// We only want to remove the decls from the identifier decl chains for local
|
|
|
|
// scopes, when inside a function/method.
|
|
|
|
if (S->getFnParent() == 0)
|
|
|
|
return;
|
2007-08-26 10:24:45 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
|
|
|
|
I != E; ++I) {
|
2007-09-13 22:10:37 +04:00
|
|
|
Decl *TmpD = static_cast<Decl*>(*I);
|
|
|
|
assert(TmpD && "This decl didn't get pushed??");
|
|
|
|
ScopedDecl *D = dyn_cast<ScopedDecl>(TmpD);
|
|
|
|
assert(D && "This decl isn't a ScopedDecl?");
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
IdentifierInfo *II = D->getIdentifier();
|
|
|
|
if (!II) continue;
|
|
|
|
|
2008-04-11 11:00:53 +04:00
|
|
|
// Unlink this decl from the identifier.
|
|
|
|
IdResolver.RemoveDecl(D);
|
|
|
|
|
2007-07-11 21:01:13 +04: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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-02 03:04:06 +04:00
|
|
|
/// getObjCInterfaceDecl - Look up a for a class declaration in the scope.
|
|
|
|
/// return 0 if one not found.
|
|
|
|
ObjCInterfaceDecl *Sema::getObjCInterfaceDecl(IdentifierInfo *Id) {
|
2008-04-02 22:30:49 +04:00
|
|
|
// The third "scope" argument is 0 since we aren't enabling lazy built-in
|
|
|
|
// creation from this context.
|
|
|
|
Decl *IDecl = LookupDecl(Id, Decl::IDNS_Ordinary, 0, false);
|
2007-10-12 23:38:20 +04:00
|
|
|
|
2008-04-02 18:35:35 +04:00
|
|
|
return dyn_cast_or_null<ObjCInterfaceDecl>(IDecl);
|
2007-10-12 23:38:20 +04:00
|
|
|
}
|
|
|
|
|
2008-04-02 03:04:06 +04:00
|
|
|
/// LookupDecl - Look up the inner-most declaration in the specified
|
2007-07-11 21:01:13 +04:00
|
|
|
/// namespace.
|
2008-04-02 18:35:35 +04:00
|
|
|
Decl *Sema::LookupDecl(const IdentifierInfo *II, unsigned NSI,
|
|
|
|
Scope *S, bool enableLazyBuiltinCreation) {
|
2007-07-11 21:01:13 +04:00
|
|
|
if (II == 0) return 0;
|
2008-04-14 01:07:44 +04:00
|
|
|
unsigned NS = NSI;
|
|
|
|
if (getLangOptions().CPlusPlus && (NS & Decl::IDNS_Ordinary))
|
|
|
|
NS |= Decl::IDNS_Tag;
|
2008-04-11 11:00:53 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// 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.
|
2008-05-10 03:39:43 +04:00
|
|
|
for (IdentifierResolver::iterator
|
|
|
|
I = IdResolver.begin(II, CurContext), E = IdResolver.end(II); I != E; ++I)
|
|
|
|
if ((*I)->getIdentifierNamespace() & NS)
|
|
|
|
return *I;
|
2008-04-11 11:00:53 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// If we didn't find a use of this identifier, and if the identifier
|
|
|
|
// corresponds to a compiler builtin, create the decl object for the builtin
|
|
|
|
// now, injecting it into translation unit scope, and return it.
|
2008-04-14 01:07:44 +04:00
|
|
|
if (NS & Decl::IDNS_Ordinary) {
|
2008-04-02 18:35:35 +04:00
|
|
|
if (enableLazyBuiltinCreation) {
|
|
|
|
// If this is a builtin on this (or all) targets, create the decl.
|
|
|
|
if (unsigned BuiltinID = II->getBuiltinID())
|
|
|
|
return LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, S);
|
|
|
|
}
|
2008-04-02 03:04:06 +04:00
|
|
|
if (getLangOptions().ObjC1) {
|
|
|
|
// @interface and @compatibility_alias introduce typedef-like names.
|
|
|
|
// Unlike typedef's, they can only be introduced at file-scope (and are
|
2008-04-02 04:39:51 +04:00
|
|
|
// therefore not scoped decls). They can, however, be shadowed by
|
2008-04-02 03:04:06 +04:00
|
|
|
// other names in IDNS_Ordinary.
|
2008-04-02 22:30:49 +04:00
|
|
|
ObjCInterfaceDeclsTy::iterator IDI = ObjCInterfaceDecls.find(II);
|
|
|
|
if (IDI != ObjCInterfaceDecls.end())
|
|
|
|
return IDI->second;
|
2008-04-02 03:04:06 +04:00
|
|
|
ObjCAliasTy::iterator I = ObjCAliasDecls.find(II);
|
|
|
|
if (I != ObjCAliasDecls.end())
|
|
|
|
return I->second->getClassInterface();
|
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-05-06 02:18:14 +04:00
|
|
|
void Sema::InitBuiltinVaListType() {
|
2007-10-16 00:28:48 +04:00
|
|
|
if (!Context.getBuiltinVaListType().isNull())
|
|
|
|
return;
|
|
|
|
|
|
|
|
IdentifierInfo *VaIdent = &Context.Idents.get("__builtin_va_list");
|
2008-04-02 18:35:35 +04:00
|
|
|
Decl *VaDecl = LookupDecl(VaIdent, Decl::IDNS_Ordinary, TUScope);
|
2007-10-19 02:17:45 +04:00
|
|
|
TypedefDecl *VaTypedef = cast<TypedefDecl>(VaDecl);
|
2007-10-16 00:28:48 +04:00
|
|
|
Context.setBuiltinVaListType(Context.getTypedefType(VaTypedef));
|
|
|
|
}
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
/// LazilyCreateBuiltin - The specified Builtin-ID was first used at file scope.
|
|
|
|
/// lazily create a decl for it.
|
2007-10-11 03:42:28 +04:00
|
|
|
ScopedDecl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid,
|
|
|
|
Scope *S) {
|
2007-07-11 21:01:13 +04:00
|
|
|
Builtin::ID BID = (Builtin::ID)bid;
|
|
|
|
|
2007-10-16 00:28:48 +04:00
|
|
|
if (BID == Builtin::BI__builtin_va_start ||
|
2008-05-06 02:18:14 +04:00
|
|
|
BID == Builtin::BI__builtin_va_copy ||
|
|
|
|
BID == Builtin::BI__builtin_va_end)
|
2007-10-16 00:28:48 +04:00
|
|
|
InitBuiltinVaListType();
|
|
|
|
|
2007-10-11 05:00:40 +04:00
|
|
|
QualType R = Context.BuiltinInfo.GetBuiltinType(BID, Context);
|
2008-04-17 18:47:13 +04:00
|
|
|
FunctionDecl *New = FunctionDecl::Create(Context,
|
|
|
|
Context.getTranslationUnitDecl(),
|
2008-04-04 10:12:32 +04:00
|
|
|
SourceLocation(), II, R,
|
2008-03-16 00:24:04 +03:00
|
|
|
FunctionDecl::Extern, false, 0);
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2008-05-06 02:18:14 +04:00
|
|
|
// Create Decl objects for each parameter, adding them to the
|
|
|
|
// FunctionDecl.
|
|
|
|
if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(R)) {
|
|
|
|
llvm::SmallVector<ParmVarDecl*, 16> Params;
|
|
|
|
for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i)
|
|
|
|
Params.push_back(ParmVarDecl::Create(Context, New, SourceLocation(), 0,
|
|
|
|
FT->getArgType(i), VarDecl::None, 0,
|
|
|
|
0));
|
|
|
|
New->setParams(&Params[0], Params.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-04-11 11:00:53 +04:00
|
|
|
// TUScope is the translation-unit scope to insert this function into.
|
2008-05-10 03:39:43 +04:00
|
|
|
PushOnScopeChains(New, TUScope);
|
2007-07-11 21:01:13 +04:00
|
|
|
return New;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// MergeTypeDefDecl - We just parsed a typedef 'New' which has the same name
|
|
|
|
/// and scope as a previous declaration 'Old'. Figure out how to resolve this
|
|
|
|
/// situation, merging decls or emitting diagnostics as appropriate.
|
|
|
|
///
|
2008-04-02 03:04:06 +04:00
|
|
|
TypedefDecl *Sema::MergeTypeDefDecl(TypedefDecl *New, Decl *OldD) {
|
2007-07-11 21:01:13 +04:00
|
|
|
// Verify the old decl was also a typedef.
|
|
|
|
TypedefDecl *Old = dyn_cast<TypedefDecl>(OldD);
|
|
|
|
if (!Old) {
|
|
|
|
Diag(New->getLocation(), diag::err_redefinition_different_kind,
|
|
|
|
New->getName());
|
|
|
|
Diag(OldD->getLocation(), diag::err_previous_definition);
|
|
|
|
return New;
|
|
|
|
}
|
|
|
|
|
2007-10-31 21:42:27 +03:00
|
|
|
// Allow multiple definitions for ObjC built-in typedefs.
|
|
|
|
// FIXME: Verify the underlying types are equivalent!
|
2008-01-07 22:49:32 +03:00
|
|
|
if (getLangOptions().ObjC1 && isBuiltinObjCType(New))
|
2007-10-31 21:42:27 +03:00
|
|
|
return Old;
|
2008-01-31 02:46:05 +03:00
|
|
|
|
|
|
|
// Redeclaration of a type is a constraint violation (6.7.2.3p1).
|
|
|
|
// Apparently GCC, Intel, and Sun all silently ignore the redeclaration if
|
|
|
|
// *either* declaration is in a system header. The code below implements
|
|
|
|
// this adhoc compatibility rule. FIXME: The following code will not
|
|
|
|
// work properly when compiling ".i" files (containing preprocessed output).
|
|
|
|
SourceManager &SrcMgr = Context.getSourceManager();
|
|
|
|
const FileEntry *OldDeclFile = SrcMgr.getFileEntryForLoc(Old->getLocation());
|
|
|
|
const FileEntry *NewDeclFile = SrcMgr.getFileEntryForLoc(New->getLocation());
|
|
|
|
HeaderSearch &HdrInfo = PP.getHeaderSearchInfo();
|
|
|
|
DirectoryLookup::DirType OldDirType = HdrInfo.getFileDirFlavor(OldDeclFile);
|
|
|
|
DirectoryLookup::DirType NewDirType = HdrInfo.getFileDirFlavor(NewDeclFile);
|
|
|
|
|
2008-03-27 00:27:00 +03:00
|
|
|
// Allow reclarations in both SystemHeaderDir and ExternCSystemHeaderDir.
|
|
|
|
if ((OldDirType != DirectoryLookup::NormalHeaderDir ||
|
|
|
|
NewDirType != DirectoryLookup::NormalHeaderDir) ||
|
2008-02-07 06:50:06 +03:00
|
|
|
getLangOptions().Microsoft)
|
2008-01-31 02:46:05 +03:00
|
|
|
return New;
|
2008-03-27 00:27:00 +03:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
|
|
|
|
// TODO: This is totally simplistic. It should handle merging functions
|
|
|
|
// together etc, merging extern int X; int X; ...
|
|
|
|
Diag(New->getLocation(), diag::err_redefinition, New->getName());
|
|
|
|
Diag(Old->getLocation(), diag::err_previous_definition);
|
|
|
|
return New;
|
|
|
|
}
|
|
|
|
|
2008-03-03 06:28:21 +03:00
|
|
|
/// DeclhasAttr - returns true if decl Declaration already has the target attribute.
|
|
|
|
static bool DeclHasAttr(const Decl *decl, const Attr *target) {
|
|
|
|
for (const Attr *attr = decl->getAttrs(); attr; attr = attr->getNext())
|
|
|
|
if (attr->getKind() == target->getKind())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// MergeAttributes - append attributes from the Old decl to the New one.
|
|
|
|
static void MergeAttributes(Decl *New, Decl *Old) {
|
|
|
|
Attr *attr = const_cast<Attr*>(Old->getAttrs()), *tmp;
|
|
|
|
|
|
|
|
// FIXME: fix this code to cleanup the Old attrs correctly
|
|
|
|
while (attr) {
|
|
|
|
tmp = attr;
|
|
|
|
attr = attr->getNext();
|
|
|
|
|
|
|
|
if (!DeclHasAttr(New, tmp)) {
|
|
|
|
New->addAttr(tmp);
|
|
|
|
} else {
|
|
|
|
tmp->setNext(0);
|
|
|
|
delete(tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-08 08:40:51 +04:00
|
|
|
/// MergeFunctionDecl - We just parsed a function 'New' from
|
|
|
|
/// declarator D which has the same name and scope as a previous
|
|
|
|
/// declaration 'Old'. Figure out how to resolve this situation,
|
|
|
|
/// merging decls or emitting diagnostics as appropriate.
|
2008-04-21 06:02:58 +04:00
|
|
|
/// Redeclaration will be set true if thisNew is a redeclaration OldD.
|
|
|
|
FunctionDecl *
|
|
|
|
Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD, bool &Redeclaration) {
|
|
|
|
Redeclaration = false;
|
2007-07-11 21:01:13 +04:00
|
|
|
// Verify the old decl was also a function.
|
|
|
|
FunctionDecl *Old = dyn_cast<FunctionDecl>(OldD);
|
|
|
|
if (!Old) {
|
|
|
|
Diag(New->getLocation(), diag::err_redefinition_different_kind,
|
|
|
|
New->getName());
|
|
|
|
Diag(OldD->getLocation(), diag::err_previous_definition);
|
|
|
|
return New;
|
|
|
|
}
|
2008-04-08 08:40:51 +04:00
|
|
|
|
2008-04-07 03:10:54 +04:00
|
|
|
QualType OldQType = Context.getCanonicalType(Old->getType());
|
|
|
|
QualType NewQType = Context.getCanonicalType(New->getType());
|
2007-11-20 22:04:50 +03:00
|
|
|
|
2008-04-08 08:40:51 +04:00
|
|
|
// C++ [dcl.fct]p3:
|
|
|
|
// All declarations for a function shall agree exactly in both the
|
|
|
|
// return type and the parameter-type-list.
|
2008-04-21 06:02:58 +04:00
|
|
|
if (getLangOptions().CPlusPlus && OldQType == NewQType) {
|
|
|
|
MergeAttributes(New, Old);
|
|
|
|
Redeclaration = true;
|
2008-04-08 08:40:51 +04:00
|
|
|
return MergeCXXFunctionDecl(New, Old);
|
2008-04-21 06:02:58 +04:00
|
|
|
}
|
2008-04-08 08:40:51 +04:00
|
|
|
|
|
|
|
// C: Function types need to be compatible, not identical. This handles
|
2008-01-14 23:51:29 +03:00
|
|
|
// duplicate function decls like "void f(int); void f(enum X);" properly.
|
2008-04-08 08:40:51 +04:00
|
|
|
if (!getLangOptions().CPlusPlus &&
|
|
|
|
Context.functionTypesAreCompatible(OldQType, NewQType)) {
|
2008-04-21 06:02:58 +04:00
|
|
|
MergeAttributes(New, Old);
|
|
|
|
Redeclaration = true;
|
2008-01-14 23:51:29 +03:00
|
|
|
return New;
|
2008-04-08 08:40:51 +04:00
|
|
|
}
|
2007-11-06 09:07:26 +03:00
|
|
|
|
2008-01-16 18:01:34 +03:00
|
|
|
// A function that has already been declared has been redeclared or defined
|
|
|
|
// with a different type- show appropriate diagnostic
|
2008-04-04 18:32:09 +04:00
|
|
|
diag::kind PrevDiag;
|
2008-04-21 06:02:58 +04:00
|
|
|
if (Old->isThisDeclarationADefinition())
|
2008-04-04 18:32:09 +04:00
|
|
|
PrevDiag = diag::err_previous_definition;
|
|
|
|
else if (Old->isImplicit())
|
|
|
|
PrevDiag = diag::err_previous_implicit_declaration;
|
2008-04-08 08:40:51 +04:00
|
|
|
else
|
2008-04-04 18:32:09 +04:00
|
|
|
PrevDiag = diag::err_previous_declaration;
|
2008-01-16 18:01:34 +03:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
|
|
|
|
// TODO: This is totally simplistic. It should handle merging functions
|
|
|
|
// together etc, merging extern int X; int X; ...
|
2008-01-16 18:01:34 +03:00
|
|
|
Diag(New->getLocation(), diag::err_conflicting_types, New->getName());
|
|
|
|
Diag(Old->getLocation(), PrevDiag);
|
2007-07-11 21:01:13 +04:00
|
|
|
return New;
|
|
|
|
}
|
|
|
|
|
2007-11-06 07:28:31 +03:00
|
|
|
/// equivalentArrayTypes - Used to determine whether two array types are
|
|
|
|
/// equivalent.
|
|
|
|
/// We need to check this explicitly as an incomplete array definition is
|
|
|
|
/// considered a VariableArrayType, so will not match a complete array
|
|
|
|
/// definition that would be otherwise equivalent.
|
|
|
|
static bool areEquivalentArrayTypes(QualType NewQType, QualType OldQType) {
|
|
|
|
const ArrayType *NewAT = NewQType->getAsArrayType();
|
|
|
|
const ArrayType *OldAT = OldQType->getAsArrayType();
|
|
|
|
|
|
|
|
if (!NewAT || !OldAT)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If either (or both) array types in incomplete we need to strip off the
|
|
|
|
// outer VariableArrayType. Once the outer VAT is removed the remaining
|
|
|
|
// types must be identical if the array types are to be considered
|
|
|
|
// equivalent.
|
|
|
|
// eg. int[][1] and int[1][1] become
|
|
|
|
// VAT(null, CAT(1, int)) and CAT(1, CAT(1, int))
|
|
|
|
// removing the outermost VAT gives
|
|
|
|
// CAT(1, int) and CAT(1, int)
|
|
|
|
// which are equal, therefore the array types are equivalent.
|
2008-02-15 15:53:51 +03:00
|
|
|
if (NewAT->isIncompleteArrayType() || OldAT->isIncompleteArrayType()) {
|
2007-11-06 07:28:31 +03:00
|
|
|
if (NewAT->getIndexTypeQualifier() != OldAT->getIndexTypeQualifier())
|
|
|
|
return false;
|
2008-01-29 10:51:12 +03:00
|
|
|
NewQType = NewAT->getElementType().getCanonicalType();
|
|
|
|
OldQType = OldAT->getElementType().getCanonicalType();
|
2007-11-06 07:28:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NewQType == OldQType;
|
|
|
|
}
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
/// MergeVarDecl - We just parsed a variable 'New' which has the same name
|
|
|
|
/// and scope as a previous declaration 'Old'. Figure out how to resolve this
|
|
|
|
/// situation, merging decls or emitting diagnostics as appropriate.
|
|
|
|
///
|
|
|
|
/// FIXME: Need to carefully consider tentative definition rules (C99 6.9.2p2).
|
|
|
|
/// For example, we incorrectly complain about i1, i4 from C99 6.9.2p4.
|
|
|
|
///
|
2008-04-02 03:04:06 +04:00
|
|
|
VarDecl *Sema::MergeVarDecl(VarDecl *New, Decl *OldD) {
|
2007-07-11 21:01:13 +04:00
|
|
|
// Verify the old decl was also a variable.
|
|
|
|
VarDecl *Old = dyn_cast<VarDecl>(OldD);
|
|
|
|
if (!Old) {
|
|
|
|
Diag(New->getLocation(), diag::err_redefinition_different_kind,
|
|
|
|
New->getName());
|
|
|
|
Diag(OldD->getLocation(), diag::err_previous_definition);
|
|
|
|
return New;
|
|
|
|
}
|
2008-03-03 06:28:21 +03:00
|
|
|
|
|
|
|
MergeAttributes(New, Old);
|
|
|
|
|
2008-01-30 03:44:01 +03:00
|
|
|
// Verify the types match.
|
2008-04-07 03:10:54 +04:00
|
|
|
QualType OldCType = Context.getCanonicalType(Old->getType());
|
|
|
|
QualType NewCType = Context.getCanonicalType(New->getType());
|
|
|
|
if (OldCType != NewCType && !areEquivalentArrayTypes(NewCType, OldCType)) {
|
2008-01-30 03:44:01 +03:00
|
|
|
Diag(New->getLocation(), diag::err_redefinition, New->getName());
|
|
|
|
Diag(Old->getLocation(), diag::err_previous_definition);
|
|
|
|
return New;
|
|
|
|
}
|
|
|
|
// C99 6.2.2p4: Check if we have a static decl followed by a non-static.
|
|
|
|
if (New->getStorageClass() == VarDecl::Static &&
|
|
|
|
(Old->getStorageClass() == VarDecl::None ||
|
|
|
|
Old->getStorageClass() == VarDecl::Extern)) {
|
|
|
|
Diag(New->getLocation(), diag::err_static_non_static, New->getName());
|
|
|
|
Diag(Old->getLocation(), diag::err_previous_definition);
|
|
|
|
return New;
|
|
|
|
}
|
|
|
|
// C99 6.2.2p4: Check if we have a non-static decl followed by a static.
|
|
|
|
if (New->getStorageClass() != VarDecl::Static &&
|
|
|
|
Old->getStorageClass() == VarDecl::Static) {
|
|
|
|
Diag(New->getLocation(), diag::err_non_static_static, New->getName());
|
|
|
|
Diag(Old->getLocation(), diag::err_previous_definition);
|
|
|
|
return New;
|
|
|
|
}
|
|
|
|
// We've verified the types match, now handle "tentative" definitions.
|
2008-04-16 02:42:06 +04:00
|
|
|
if (Old->isFileVarDecl() && New->isFileVarDecl()) {
|
2008-01-30 03:44:01 +03:00
|
|
|
// Handle C "tentative" external object definitions (C99 6.9.2).
|
|
|
|
bool OldIsTentative = false;
|
|
|
|
bool NewIsTentative = false;
|
|
|
|
|
2008-04-16 02:42:06 +04:00
|
|
|
if (!Old->getInit() &&
|
|
|
|
(Old->getStorageClass() == VarDecl::None ||
|
|
|
|
Old->getStorageClass() == VarDecl::Static))
|
2007-08-30 05:06:46 +04:00
|
|
|
OldIsTentative = true;
|
2008-01-30 03:44:01 +03:00
|
|
|
|
|
|
|
// FIXME: this check doesn't work (since the initializer hasn't been
|
|
|
|
// attached yet). This check should be moved to FinalizeDeclaratorGroup.
|
|
|
|
// Unfortunately, by the time we get to FinializeDeclaratorGroup, we've
|
|
|
|
// thrown out the old decl.
|
2008-04-16 02:42:06 +04:00
|
|
|
if (!New->getInit() &&
|
|
|
|
(New->getStorageClass() == VarDecl::None ||
|
|
|
|
New->getStorageClass() == VarDecl::Static))
|
2008-01-30 03:44:01 +03:00
|
|
|
; // change to NewIsTentative = true; once the code is moved.
|
|
|
|
|
|
|
|
if (NewIsTentative || OldIsTentative)
|
|
|
|
return New;
|
2007-08-30 05:06:46 +04:00
|
|
|
}
|
2008-01-30 03:44:01 +03:00
|
|
|
if (Old->getStorageClass() != VarDecl::Extern &&
|
|
|
|
New->getStorageClass() != VarDecl::Extern) {
|
2007-07-11 21:01:13 +04:00
|
|
|
Diag(New->getLocation(), diag::err_redefinition, New->getName());
|
|
|
|
Diag(Old->getLocation(), diag::err_previous_definition);
|
|
|
|
}
|
|
|
|
return New;
|
|
|
|
}
|
|
|
|
|
2008-04-08 08:40:51 +04:00
|
|
|
/// CheckParmsForFunctionDef - Check that the parameters of the given
|
|
|
|
/// function are appropriate for the definition of a function. This
|
|
|
|
/// takes care of any checks that cannot be performed on the
|
|
|
|
/// declaration itself, e.g., that the types of each of the function
|
|
|
|
/// parameters are complete.
|
|
|
|
bool Sema::CheckParmsForFunctionDef(FunctionDecl *FD) {
|
|
|
|
bool HasInvalidParm = false;
|
|
|
|
for (unsigned p = 0, NumParams = FD->getNumParams(); p < NumParams; ++p) {
|
|
|
|
ParmVarDecl *Param = FD->getParamDecl(p);
|
|
|
|
|
|
|
|
// C99 6.7.5.3p4: the parameters in a parameter type list in a
|
|
|
|
// function declarator that is part of a function definition of
|
|
|
|
// that function shall not have incomplete type.
|
|
|
|
if (Param->getType()->isIncompleteType() &&
|
|
|
|
!Param->isInvalidDecl()) {
|
|
|
|
Diag(Param->getLocation(), diag::err_typecheck_decl_incomplete_type,
|
|
|
|
Param->getType().getAsString());
|
|
|
|
Param->setInvalidDecl();
|
|
|
|
HasInvalidParm = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return HasInvalidParm;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CreateImplicitParameter - Creates an implicit function parameter
|
|
|
|
/// in the scope S and with the given type. This routine is used, for
|
|
|
|
/// example, to create the implicit "self" parameter in an Objective-C
|
|
|
|
/// method.
|
|
|
|
ParmVarDecl *
|
|
|
|
Sema::CreateImplicitParameter(Scope *S, IdentifierInfo *Id,
|
|
|
|
SourceLocation IdLoc, QualType Type) {
|
|
|
|
ParmVarDecl *New = ParmVarDecl::Create(Context, CurContext, IdLoc, Id, Type,
|
|
|
|
VarDecl::None, 0, 0);
|
2008-04-12 04:47:19 +04:00
|
|
|
if (Id)
|
|
|
|
PushOnScopeChains(New, S);
|
2008-04-08 08:40:51 +04:00
|
|
|
|
|
|
|
return New;
|
|
|
|
}
|
|
|
|
|
2007-07-11 21:01:13 +04: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(...);
|
|
|
|
|
2007-11-18 00:37:36 +03:00
|
|
|
return dyn_cast_or_null<TagDecl>(static_cast<Decl *>(DS.getTypeRep()));
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2008-01-11 01:15:12 +03:00
|
|
|
bool Sema::CheckSingleInitializer(Expr *&Init, QualType DeclType) {
|
2007-09-02 06:04:30 +04:00
|
|
|
// Get the type before calling CheckSingleAssignmentConstraints(), since
|
|
|
|
// it can promote the expression.
|
2008-01-04 21:04:52 +03:00
|
|
|
QualType InitType = Init->getType();
|
2007-09-02 06:04:30 +04:00
|
|
|
|
2008-01-04 21:04:52 +03:00
|
|
|
AssignConvertType ConvTy = CheckSingleAssignmentConstraints(DeclType, Init);
|
|
|
|
return DiagnoseAssignmentResult(ConvTy, Init->getLocStart(), DeclType,
|
|
|
|
InitType, Init, "initializing");
|
2007-09-02 06:04:30 +04:00
|
|
|
}
|
|
|
|
|
2007-09-04 18:36:54 +04:00
|
|
|
bool Sema::CheckInitExpr(Expr *expr, InitListExpr *IList, unsigned slot,
|
2008-01-11 01:15:12 +03:00
|
|
|
QualType ElementType) {
|
2007-12-12 02:15:04 +03:00
|
|
|
Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
|
2008-01-11 01:15:12 +03:00
|
|
|
if (CheckSingleInitializer(expr, ElementType))
|
2007-12-12 02:15:04 +03:00
|
|
|
return true; // types weren't compatible.
|
|
|
|
|
2007-09-04 18:36:54 +04:00
|
|
|
if (savExpr != expr) // The type was promoted, update initializer list.
|
|
|
|
IList->setInit(slot, expr);
|
2007-09-04 06:20:04 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-01-22 03:55:40 +03:00
|
|
|
bool Sema::CheckStringLiteralInit(StringLiteral *strLiteral, QualType &DeclT) {
|
2008-02-15 21:16:39 +03:00
|
|
|
if (const IncompleteArrayType *IAT = DeclT->getAsIncompleteArrayType()) {
|
2008-01-22 03:55:40 +03:00
|
|
|
// C99 6.7.8p14. We have an array of character type with unknown size
|
|
|
|
// being initialized to a string literal.
|
|
|
|
llvm::APSInt ConstVal(32);
|
|
|
|
ConstVal = strLiteral->getByteLength() + 1;
|
|
|
|
// Return a new array type (C99 6.7.8p22).
|
2008-02-15 21:16:39 +03:00
|
|
|
DeclT = Context.getConstantArrayType(IAT->getElementType(), ConstVal,
|
2008-01-22 03:55:40 +03:00
|
|
|
ArrayType::Normal, 0);
|
|
|
|
} else if (const ConstantArrayType *CAT = DeclT->getAsConstantArrayType()) {
|
|
|
|
// C99 6.7.8p14. We have an array of character type with known size.
|
|
|
|
if (strLiteral->getByteLength() > (unsigned)CAT->getMaximumElements())
|
|
|
|
Diag(strLiteral->getSourceRange().getBegin(),
|
|
|
|
diag::warn_initializer_string_for_char_array_too_long,
|
|
|
|
strLiteral->getSourceRange());
|
|
|
|
} else {
|
|
|
|
assert(0 && "HandleStringLiteralInit(): Invalid array type");
|
|
|
|
}
|
|
|
|
// Set type from "char *" to "constant array of char".
|
|
|
|
strLiteral->setType(DeclT);
|
|
|
|
// For now, we always return false (meaning success).
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringLiteral *Sema::IsStringLiteralInit(Expr *Init, QualType DeclType) {
|
|
|
|
const ArrayType *AT = DeclType->getAsArrayType();
|
2008-01-25 03:51:06 +03:00
|
|
|
if (AT && AT->getElementType()->isCharType()) {
|
|
|
|
return dyn_cast<StringLiteral>(Init);
|
|
|
|
}
|
2008-01-22 03:55:40 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-25 03:51:06 +03:00
|
|
|
// CheckInitializerListTypes - Checks the types of elements of an initializer
|
|
|
|
// list. This function is recursive: it calls itself to initialize subelements
|
|
|
|
// of aggregate types. Note that the topLevel parameter essentially refers to
|
|
|
|
// whether this expression "owns" the initializer list passed in, or if this
|
|
|
|
// initialization is taking elements out of a parent initializer. Each
|
|
|
|
// call to this function adds zero or more to startIndex, reports any errors,
|
|
|
|
// and returns true if it found any inconsistent types.
|
|
|
|
bool Sema::CheckInitializerListTypes(InitListExpr*& IList, QualType &DeclType,
|
|
|
|
bool topLevel, unsigned& startIndex) {
|
2007-12-11 01:44:33 +03:00
|
|
|
bool hadError = false;
|
2008-01-25 03:51:06 +03:00
|
|
|
|
|
|
|
if (DeclType->isScalarType()) {
|
|
|
|
// The simplest case: initializing a single scalar
|
|
|
|
if (topLevel) {
|
|
|
|
Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init,
|
|
|
|
IList->getSourceRange());
|
|
|
|
}
|
|
|
|
if (startIndex < IList->getNumInits()) {
|
|
|
|
Expr* expr = IList->getInit(startIndex);
|
|
|
|
if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
|
|
|
|
// FIXME: Should an error be reported here instead?
|
|
|
|
unsigned newIndex = 0;
|
|
|
|
CheckInitializerListTypes(SubInitList, DeclType, true, newIndex);
|
|
|
|
} else {
|
|
|
|
hadError |= CheckInitExpr(expr, IList, startIndex, DeclType);
|
|
|
|
}
|
|
|
|
++startIndex;
|
|
|
|
}
|
|
|
|
// FIXME: Should an error be reported for empty initializer list + scalar?
|
|
|
|
} else if (DeclType->isVectorType()) {
|
|
|
|
if (startIndex < IList->getNumInits()) {
|
|
|
|
const VectorType *VT = DeclType->getAsVectorType();
|
|
|
|
int maxElements = VT->getNumElements();
|
|
|
|
QualType elementType = VT->getElementType();
|
|
|
|
|
|
|
|
for (int i = 0; i < maxElements; ++i) {
|
|
|
|
// Don't attempt to go past the end of the init list
|
|
|
|
if (startIndex >= IList->getNumInits())
|
|
|
|
break;
|
|
|
|
Expr* expr = IList->getInit(startIndex);
|
|
|
|
if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
|
|
|
|
unsigned newIndex = 0;
|
|
|
|
hadError |= CheckInitializerListTypes(SubInitList, elementType,
|
|
|
|
true, newIndex);
|
|
|
|
++startIndex;
|
|
|
|
} else {
|
|
|
|
hadError |= CheckInitializerListTypes(IList, elementType,
|
|
|
|
false, startIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (DeclType->isAggregateType() || DeclType->isUnionType()) {
|
|
|
|
if (DeclType->isStructureType() || DeclType->isUnionType()) {
|
2008-01-28 05:00:41 +03:00
|
|
|
if (startIndex < IList->getNumInits() && !topLevel &&
|
|
|
|
Context.typesAreCompatible(IList->getInit(startIndex)->getType(),
|
|
|
|
DeclType)) {
|
2008-01-25 03:51:06 +03:00
|
|
|
// We found a compatible struct; per the standard, this initializes the
|
|
|
|
// struct. (The C standard technically says that this only applies for
|
|
|
|
// initializers for declarations with automatic scope; however, this
|
|
|
|
// construct is unambiguous anyway because a struct cannot contain
|
|
|
|
// a type compatible with itself. We'll output an error when we check
|
|
|
|
// if the initializer is constant.)
|
|
|
|
// FIXME: Is a call to CheckSingleInitializer required here?
|
|
|
|
++startIndex;
|
|
|
|
} else {
|
|
|
|
RecordDecl* structDecl = DeclType->getAsRecordType()->getDecl();
|
2008-02-11 03:06:17 +03:00
|
|
|
|
2008-02-12 00:52:37 +03:00
|
|
|
// If the record is invalid, some of it's members are invalid. To avoid
|
|
|
|
// confusion, we forgo checking the intializer for the entire record.
|
2008-02-11 03:06:17 +03:00
|
|
|
if (structDecl->isInvalidDecl())
|
|
|
|
return true;
|
|
|
|
|
2008-01-25 03:51:06 +03:00
|
|
|
// If structDecl is a forward declaration, this loop won't do anything;
|
|
|
|
// That's okay, because an error should get printed out elsewhere. It
|
|
|
|
// might be worthwhile to skip over the rest of the initializer, though.
|
|
|
|
int numMembers = structDecl->getNumMembers() -
|
|
|
|
structDecl->hasFlexibleArrayMember();
|
|
|
|
for (int i = 0; i < numMembers; i++) {
|
|
|
|
// Don't attempt to go past the end of the init list
|
|
|
|
if (startIndex >= IList->getNumInits())
|
|
|
|
break;
|
|
|
|
FieldDecl * curField = structDecl->getMember(i);
|
|
|
|
if (!curField->getIdentifier()) {
|
|
|
|
// Don't initialize unnamed fields, e.g. "int : 20;"
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
QualType fieldType = curField->getType();
|
|
|
|
Expr* expr = IList->getInit(startIndex);
|
|
|
|
if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
|
|
|
|
unsigned newStart = 0;
|
|
|
|
hadError |= CheckInitializerListTypes(SubInitList, fieldType,
|
|
|
|
true, newStart);
|
|
|
|
++startIndex;
|
|
|
|
} else {
|
|
|
|
hadError |= CheckInitializerListTypes(IList, fieldType,
|
|
|
|
false, startIndex);
|
|
|
|
}
|
|
|
|
if (DeclType->isUnionType())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// FIXME: Implement flexible array initialization GCC extension (it's a
|
|
|
|
// really messy extension to implement, unfortunately...the necessary
|
|
|
|
// information isn't actually even here!)
|
|
|
|
}
|
|
|
|
} else if (DeclType->isArrayType()) {
|
|
|
|
// Check for the special-case of initializing an array with a string.
|
|
|
|
if (startIndex < IList->getNumInits()) {
|
|
|
|
if (StringLiteral *lit = IsStringLiteralInit(IList->getInit(startIndex),
|
|
|
|
DeclType)) {
|
|
|
|
CheckStringLiteralInit(lit, DeclType);
|
|
|
|
++startIndex;
|
|
|
|
if (topLevel && startIndex < IList->getNumInits()) {
|
|
|
|
// We have leftover initializers; warn
|
|
|
|
Diag(IList->getInit(startIndex)->getLocStart(),
|
|
|
|
diag::err_excess_initializers_in_char_array_initializer,
|
|
|
|
IList->getInit(startIndex)->getSourceRange());
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int maxElements;
|
2008-02-15 21:16:39 +03:00
|
|
|
if (DeclType->isIncompleteArrayType()) {
|
2008-01-25 03:51:06 +03:00
|
|
|
// FIXME: use a proper constant
|
|
|
|
maxElements = 0x7FFFFFFF;
|
2008-02-21 02:17:35 +03:00
|
|
|
} else if (const VariableArrayType *VAT =
|
|
|
|
DeclType->getAsVariableArrayType()) {
|
2008-01-25 03:51:06 +03:00
|
|
|
// Check for VLAs; in standard C it would be possible to check this
|
|
|
|
// earlier, but I don't know where clang accepts VLAs (gcc accepts
|
|
|
|
// them in all sorts of strange places).
|
2008-02-15 21:16:39 +03:00
|
|
|
Diag(VAT->getSizeExpr()->getLocStart(),
|
|
|
|
diag::err_variable_object_no_init,
|
|
|
|
VAT->getSizeExpr()->getSourceRange());
|
|
|
|
hadError = true;
|
|
|
|
maxElements = 0x7FFFFFFF;
|
2008-01-25 03:51:06 +03:00
|
|
|
} else {
|
|
|
|
const ConstantArrayType *CAT = DeclType->getAsConstantArrayType();
|
|
|
|
maxElements = static_cast<int>(CAT->getSize().getZExtValue());
|
|
|
|
}
|
|
|
|
QualType elementType = DeclType->getAsArrayType()->getElementType();
|
|
|
|
int numElements = 0;
|
|
|
|
for (int i = 0; i < maxElements; ++i, ++numElements) {
|
|
|
|
// Don't attempt to go past the end of the init list
|
|
|
|
if (startIndex >= IList->getNumInits())
|
|
|
|
break;
|
|
|
|
Expr* expr = IList->getInit(startIndex);
|
|
|
|
if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
|
|
|
|
unsigned newIndex = 0;
|
|
|
|
hadError |= CheckInitializerListTypes(SubInitList, elementType,
|
|
|
|
true, newIndex);
|
|
|
|
++startIndex;
|
|
|
|
} else {
|
|
|
|
hadError |= CheckInitializerListTypes(IList, elementType,
|
|
|
|
false, startIndex);
|
|
|
|
}
|
|
|
|
}
|
2008-02-15 15:53:51 +03:00
|
|
|
if (DeclType->isIncompleteArrayType()) {
|
2008-01-25 03:51:06 +03:00
|
|
|
// If this is an incomplete array type, the actual type needs to
|
|
|
|
// be calculated here
|
|
|
|
if (numElements == 0) {
|
|
|
|
// Sizing an array implicitly to zero is not allowed
|
|
|
|
// (It could in theory be allowed, but it doesn't really matter.)
|
|
|
|
Diag(IList->getLocStart(),
|
|
|
|
diag::err_at_least_one_initializer_needed_to_size_array);
|
|
|
|
hadError = true;
|
|
|
|
} else {
|
|
|
|
llvm::APSInt ConstVal(32);
|
|
|
|
ConstVal = numElements;
|
|
|
|
DeclType = Context.getConstantArrayType(elementType, ConstVal,
|
|
|
|
ArrayType::Normal, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert(0 && "Aggregate that isn't a function or array?!");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// In C, all types are either scalars or aggregates, but
|
|
|
|
// additional handling is needed here for C++ (and possibly others?).
|
|
|
|
assert(0 && "Unsupported initializer type");
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this init list is a base list, we set the type; an initializer doesn't
|
|
|
|
// fundamentally have a type, but this makes the ASTs a bit easier to read
|
|
|
|
if (topLevel)
|
|
|
|
IList->setType(DeclType);
|
|
|
|
|
|
|
|
if (topLevel && startIndex < IList->getNumInits()) {
|
|
|
|
// We have leftover initializers; warn
|
|
|
|
Diag(IList->getInit(startIndex)->getLocStart(),
|
|
|
|
diag::warn_excess_initializers,
|
|
|
|
IList->getInit(startIndex)->getSourceRange());
|
|
|
|
}
|
|
|
|
return hadError;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Sema::CheckInitializerTypes(Expr *&Init, QualType &DeclType) {
|
2008-01-22 02:53:58 +03:00
|
|
|
// C99 6.7.8p3: The type of the entity to be initialized shall be an array
|
|
|
|
// of unknown size ("[]") or an object type that is not a variable array type.
|
2008-02-15 21:16:39 +03:00
|
|
|
if (const VariableArrayType *VAT = DeclType->getAsVariableArrayType())
|
2008-01-22 02:53:58 +03:00
|
|
|
return Diag(VAT->getSizeExpr()->getLocStart(),
|
|
|
|
diag::err_variable_object_no_init,
|
|
|
|
VAT->getSizeExpr()->getSourceRange());
|
|
|
|
|
2007-09-02 06:04:30 +04:00
|
|
|
InitListExpr *InitList = dyn_cast<InitListExpr>(Init);
|
2007-12-11 01:44:33 +03:00
|
|
|
if (!InitList) {
|
2008-01-22 03:55:40 +03:00
|
|
|
// FIXME: Handle wide strings
|
|
|
|
if (StringLiteral *strLiteral = IsStringLiteralInit(Init, DeclType))
|
|
|
|
return CheckStringLiteralInit(strLiteral, DeclType);
|
2008-02-08 03:48:24 +03:00
|
|
|
|
|
|
|
if (DeclType->isArrayType())
|
|
|
|
return Diag(Init->getLocStart(),
|
|
|
|
diag::err_array_init_list_required,
|
|
|
|
Init->getSourceRange());
|
|
|
|
|
2008-01-11 01:15:12 +03:00
|
|
|
return CheckSingleInitializer(Init, DeclType);
|
2007-12-11 01:44:33 +03:00
|
|
|
}
|
2008-05-02 02:18:59 +04:00
|
|
|
#if 1
|
2008-01-25 03:51:06 +03:00
|
|
|
unsigned newIndex = 0;
|
|
|
|
return CheckInitializerListTypes(InitList, DeclType, true, newIndex);
|
2008-05-02 02:18:59 +04:00
|
|
|
#else
|
|
|
|
InitListChecker CheckInitList(this, InitList, DeclType);
|
|
|
|
return CheckInitList.HadError();
|
|
|
|
#endif
|
2007-09-02 06:04:30 +04:00
|
|
|
}
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
Sema::DeclTy *
|
2007-09-15 22:49:24 +04:00
|
|
|
Sema::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *lastDecl) {
|
2007-09-14 03:52:58 +04:00
|
|
|
ScopedDecl *LastDeclarator = dyn_cast_or_null<ScopedDecl>((Decl *)lastDecl);
|
2007-07-11 21:01:13 +04:00
|
|
|
IdentifierInfo *II = D.getIdentifier();
|
|
|
|
|
2007-07-25 04:24:17 +04:00
|
|
|
// All of these full declarators require an identifier. If it doesn't have
|
|
|
|
// one, the ParsedFreeStandingDeclSpec action should be used.
|
|
|
|
if (II == 0) {
|
2007-10-17 02:36:42 +04:00
|
|
|
Diag(D.getDeclSpec().getSourceRange().getBegin(),
|
2007-08-28 10:17:15 +04:00
|
|
|
diag::err_declarator_need_ident,
|
2007-07-25 04:24:17 +04:00
|
|
|
D.getDeclSpec().getSourceRange(), D.getSourceRange());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-08-26 10:24:45 +04:00
|
|
|
// The scope passed in may not be a decl scope. Zip up the scope tree until
|
|
|
|
// we find one that is.
|
|
|
|
while ((S->getFlags() & Scope::DeclScope) == 0)
|
|
|
|
S = S->getParent();
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// See if this is a redefinition of a variable in the same scope.
|
2008-04-02 18:35:35 +04:00
|
|
|
Decl *PrevDecl = LookupDecl(II, Decl::IDNS_Ordinary, S);
|
2007-09-13 22:10:37 +04:00
|
|
|
ScopedDecl *New;
|
2007-08-29 00:14:24 +04:00
|
|
|
bool InvalidDecl = false;
|
2008-04-14 01:07:44 +04:00
|
|
|
|
|
|
|
// In C++, the previous declaration we find might be a tag type
|
|
|
|
// (class or enum). In this case, the new declaration will hide the
|
|
|
|
// tag type.
|
|
|
|
if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
|
|
|
|
PrevDecl = 0;
|
|
|
|
|
2007-11-14 09:34:38 +03:00
|
|
|
QualType R = GetTypeForDeclarator(D, S);
|
|
|
|
assert(!R.isNull() && "GetTypeForDeclarator() returned null type");
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
|
2008-05-07 08:49:29 +04:00
|
|
|
// Check that there are no default arguments (C++ only).
|
|
|
|
if (getLangOptions().CPlusPlus)
|
|
|
|
CheckExtraCXXDefaultArguments(D);
|
|
|
|
|
2007-11-14 09:34:38 +03:00
|
|
|
TypedefDecl *NewTD = ParseTypedefDecl(S, D, R, LastDeclarator);
|
2007-07-11 21:01:13 +04:00
|
|
|
if (!NewTD) return 0;
|
|
|
|
|
|
|
|
// Handle attributes prior to checking for duplicates in MergeVarDecl
|
|
|
|
HandleDeclAttributes(NewTD, D.getDeclSpec().getAttributes(),
|
|
|
|
D.getAttributes());
|
2008-01-10 02:34:55 +03:00
|
|
|
// Merge the decl with the existing one if appropriate. If the decl is
|
|
|
|
// in an outer scope, it isn't the same thing.
|
2008-05-10 03:39:43 +04:00
|
|
|
if (PrevDecl && IdResolver.isDeclInScope(PrevDecl, CurContext, S)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
NewTD = MergeTypeDefDecl(NewTD, PrevDecl);
|
|
|
|
if (NewTD == 0) return 0;
|
|
|
|
}
|
|
|
|
New = NewTD;
|
2008-05-10 03:39:43 +04:00
|
|
|
if (S->getFnParent() == 0) {
|
2007-07-11 21:01:13 +04:00
|
|
|
// C99 6.7.7p2: If a typedef name specifies a variably modified type
|
|
|
|
// then it shall have block scope.
|
2008-02-15 15:53:51 +03:00
|
|
|
if (NewTD->getUnderlyingType()->isVariablyModifiedType()) {
|
|
|
|
// FIXME: Diagnostic needs to be fixed.
|
|
|
|
Diag(D.getIdentifierLoc(), diag::err_typecheck_illegal_vla);
|
2007-08-31 21:20:07 +04:00
|
|
|
InvalidDecl = true;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
}
|
2007-11-14 09:34:38 +03:00
|
|
|
} else if (R.getTypePtr()->isFunctionType()) {
|
2007-09-27 19:15:46 +04:00
|
|
|
FunctionDecl::StorageClass SC = FunctionDecl::None;
|
2007-07-11 21:01:13 +04:00
|
|
|
switch (D.getDeclSpec().getStorageClassSpec()) {
|
|
|
|
default: assert(0 && "Unknown storage class!");
|
|
|
|
case DeclSpec::SCS_auto:
|
|
|
|
case DeclSpec::SCS_register:
|
|
|
|
Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_func,
|
|
|
|
R.getAsString());
|
2007-08-29 00:14:24 +04:00
|
|
|
InvalidDecl = true;
|
|
|
|
break;
|
2007-07-11 21:01:13 +04:00
|
|
|
case DeclSpec::SCS_unspecified: SC = FunctionDecl::None; break;
|
|
|
|
case DeclSpec::SCS_extern: SC = FunctionDecl::Extern; break;
|
|
|
|
case DeclSpec::SCS_static: SC = FunctionDecl::Static; break;
|
2008-01-29 00:57:15 +03:00
|
|
|
case DeclSpec::SCS_private_extern: SC = FunctionDecl::PrivateExtern;break;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2008-03-16 00:24:04 +03:00
|
|
|
bool isInline = D.getDeclSpec().isInlineSpecified();
|
2008-04-04 10:12:32 +04:00
|
|
|
FunctionDecl *NewFD = FunctionDecl::Create(Context, CurContext,
|
|
|
|
D.getIdentifierLoc(),
|
2008-03-16 00:24:04 +03:00
|
|
|
II, R, SC, isInline,
|
|
|
|
LastDeclarator);
|
2008-02-28 01:18:07 +03:00
|
|
|
// Handle attributes.
|
|
|
|
HandleDeclAttributes(NewFD, D.getDeclSpec().getAttributes(),
|
|
|
|
D.getAttributes());
|
2008-04-08 08:40:51 +04:00
|
|
|
|
|
|
|
// Copy the parameter declarations from the declarator D to
|
|
|
|
// the function declaration NewFD, if they are available.
|
|
|
|
if (D.getNumTypeObjects() > 0 &&
|
|
|
|
D.getTypeObject(0).Fun.hasPrototype) {
|
|
|
|
DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
|
|
|
|
|
|
|
|
// Create Decl objects for each parameter, adding them to the
|
|
|
|
// FunctionDecl.
|
|
|
|
llvm::SmallVector<ParmVarDecl*, 16> Params;
|
|
|
|
|
|
|
|
// Check for C99 6.7.5.3p10 - foo(void) is a non-varargs
|
|
|
|
// function that takes no arguments, not a function that takes a
|
2008-04-10 06:22:51 +04:00
|
|
|
// single void argument.
|
2008-04-08 08:40:51 +04:00
|
|
|
if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 &&
|
|
|
|
FTI.ArgInfo[0].Param &&
|
|
|
|
!((ParmVarDecl*)FTI.ArgInfo[0].Param)->getType().getCVRQualifiers() &&
|
|
|
|
((ParmVarDecl*)FTI.ArgInfo[0].Param)->getType()->isVoidType()) {
|
|
|
|
// empty arg list, don't push any params.
|
2008-04-10 06:22:51 +04:00
|
|
|
ParmVarDecl *Param = (ParmVarDecl*)FTI.ArgInfo[0].Param;
|
|
|
|
|
2008-04-10 06:26:16 +04:00
|
|
|
// In C++, the empty parameter-type-list must be spelled "void"; a
|
|
|
|
// typedef of void is not permitted.
|
|
|
|
if (getLangOptions().CPlusPlus &&
|
2008-04-10 06:22:51 +04:00
|
|
|
Param->getType() != Context.VoidTy) {
|
|
|
|
Diag(Param->getLocation(), diag::ext_param_typedef_of_void);
|
|
|
|
}
|
|
|
|
|
2008-04-08 08:40:51 +04:00
|
|
|
} else {
|
|
|
|
for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i)
|
|
|
|
Params.push_back((ParmVarDecl *)FTI.ArgInfo[i].Param);
|
|
|
|
}
|
|
|
|
|
|
|
|
NewFD->setParams(&Params[0], Params.size());
|
|
|
|
}
|
|
|
|
|
2008-01-10 02:34:55 +03:00
|
|
|
// Merge the decl with the existing one if appropriate. Since C functions
|
|
|
|
// are in a flat namespace, make sure we consider decls in outer scopes.
|
2008-05-10 03:39:43 +04:00
|
|
|
if (PrevDecl &&
|
|
|
|
(!getLangOptions().CPlusPlus ||
|
|
|
|
IdResolver.isDeclInScope(PrevDecl, CurContext, S)) ) {
|
2008-04-21 06:02:58 +04:00
|
|
|
bool Redeclaration = false;
|
|
|
|
NewFD = MergeFunctionDecl(NewFD, PrevDecl, Redeclaration);
|
2007-07-11 21:01:13 +04:00
|
|
|
if (NewFD == 0) return 0;
|
2008-04-21 06:02:58 +04:00
|
|
|
if (Redeclaration) {
|
|
|
|
// Note that the new declaration is a redeclaration of the
|
|
|
|
// older declaration. Then return the older declaration: the
|
|
|
|
// new one is only kept within the set of previous
|
|
|
|
// declarations for this function.
|
|
|
|
FunctionDecl *OldFD = (FunctionDecl *)PrevDecl;
|
|
|
|
OldFD->AddRedeclaration(NewFD);
|
|
|
|
return OldFD;
|
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
New = NewFD;
|
2008-04-08 08:40:51 +04:00
|
|
|
|
|
|
|
// In C++, check default arguments now that we have merged decls.
|
|
|
|
if (getLangOptions().CPlusPlus)
|
|
|
|
CheckCXXDefaultArguments(NewFD);
|
2007-07-11 21:01:13 +04:00
|
|
|
} else {
|
2008-05-07 08:49:29 +04:00
|
|
|
// Check that there are no default arguments (C++ only).
|
|
|
|
if (getLangOptions().CPlusPlus)
|
|
|
|
CheckExtraCXXDefaultArguments(D);
|
|
|
|
|
2008-01-07 22:49:32 +03:00
|
|
|
if (R.getTypePtr()->isObjCInterfaceType()) {
|
2007-10-13 02:10:42 +04:00
|
|
|
Diag(D.getIdentifierLoc(), diag::err_statically_allocated_object,
|
|
|
|
D.getIdentifier()->getName());
|
|
|
|
InvalidDecl = true;
|
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
VarDecl *NewVD;
|
|
|
|
VarDecl::StorageClass SC;
|
|
|
|
switch (D.getDeclSpec().getStorageClassSpec()) {
|
2008-03-16 00:10:16 +03:00
|
|
|
default: assert(0 && "Unknown storage class!");
|
|
|
|
case DeclSpec::SCS_unspecified: SC = VarDecl::None; break;
|
|
|
|
case DeclSpec::SCS_extern: SC = VarDecl::Extern; break;
|
|
|
|
case DeclSpec::SCS_static: SC = VarDecl::Static; break;
|
|
|
|
case DeclSpec::SCS_auto: SC = VarDecl::Auto; break;
|
|
|
|
case DeclSpec::SCS_register: SC = VarDecl::Register; break;
|
|
|
|
case DeclSpec::SCS_private_extern: SC = VarDecl::PrivateExtern; break;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2008-05-10 03:39:43 +04:00
|
|
|
if (S->getFnParent() == 0) {
|
2007-07-11 21:01:13 +04:00
|
|
|
// C99 6.9p2: The storage-class specifiers auto and register shall not
|
|
|
|
// appear in the declaration specifiers in an external declaration.
|
|
|
|
if (SC == VarDecl::Auto || SC == VarDecl::Register) {
|
|
|
|
Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope,
|
|
|
|
R.getAsString());
|
2007-08-28 22:45:29 +04:00
|
|
|
InvalidDecl = true;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2008-04-16 02:42:06 +04:00
|
|
|
NewVD = VarDecl::Create(Context, CurContext, D.getIdentifierLoc(),
|
|
|
|
II, R, SC, LastDeclarator);
|
2007-09-02 06:04:30 +04:00
|
|
|
} else {
|
2008-04-16 02:42:06 +04:00
|
|
|
NewVD = VarDecl::Create(Context, CurContext, D.getIdentifierLoc(),
|
|
|
|
II, R, SC, LastDeclarator);
|
2007-08-28 22:45:29 +04:00
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
// Handle attributes prior to checking for duplicates in MergeVarDecl
|
|
|
|
HandleDeclAttributes(NewVD, D.getDeclSpec().getAttributes(),
|
|
|
|
D.getAttributes());
|
2008-03-14 21:07:10 +03:00
|
|
|
|
|
|
|
// Emit an error if an address space was applied to decl with local storage.
|
|
|
|
// This includes arrays of objects with address space qualifiers, but not
|
|
|
|
// automatic variables that point to other address spaces.
|
|
|
|
// ISO/IEC TR 18037 S5.1.2
|
2008-03-25 21:36:32 +03:00
|
|
|
if (NewVD->hasLocalStorage() && (NewVD->getType().getAddressSpace() != 0)) {
|
|
|
|
Diag(D.getIdentifierLoc(), diag::err_as_qualified_auto_decl);
|
|
|
|
InvalidDecl = true;
|
2008-03-14 03:22:18 +03:00
|
|
|
}
|
2008-01-10 02:34:55 +03:00
|
|
|
// Merge the decl with the existing one if appropriate. If the decl is
|
|
|
|
// in an outer scope, it isn't the same thing.
|
2008-05-10 03:39:43 +04:00
|
|
|
if (PrevDecl && IdResolver.isDeclInScope(PrevDecl, CurContext, S)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
NewVD = MergeVarDecl(NewVD, PrevDecl);
|
|
|
|
if (NewVD == 0) return 0;
|
|
|
|
}
|
|
|
|
New = NewVD;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this has an identifier, add it to the scope stack.
|
2008-04-12 04:47:19 +04:00
|
|
|
if (II)
|
|
|
|
PushOnScopeChains(New, S);
|
2007-08-29 00:14:24 +04:00
|
|
|
// If any semantic error occurred, mark the decl as invalid.
|
|
|
|
if (D.getInvalidType() || InvalidDecl)
|
|
|
|
New->setInvalidDecl();
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
return New;
|
|
|
|
}
|
|
|
|
|
2008-01-11 01:15:12 +03:00
|
|
|
bool Sema::CheckForConstantInitializer(Expr *Init, QualType DclT) {
|
|
|
|
SourceLocation loc;
|
|
|
|
// FIXME: Remove the isReference check and handle assignment to a reference.
|
|
|
|
if (!DclT->isReferenceType() && !Init->isConstantExpr(Context, &loc)) {
|
|
|
|
assert(loc.isValid() && "isConstantExpr didn't return a loc!");
|
|
|
|
Diag(loc, diag::err_init_element_not_constant, Init->getSourceRange());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-09-12 18:07:44 +04:00
|
|
|
void Sema::AddInitializerToDecl(DeclTy *dcl, ExprTy *init) {
|
2007-09-13 00:13:48 +04:00
|
|
|
Decl *RealDecl = static_cast<Decl *>(dcl);
|
2007-09-12 18:07:44 +04:00
|
|
|
Expr *Init = static_cast<Expr *>(init);
|
2007-10-20 00:10:30 +04:00
|
|
|
assert(Init && "missing initializer");
|
2007-09-12 18:07:44 +04:00
|
|
|
|
2007-10-20 00:10:30 +04:00
|
|
|
// If there is no declaration, there was an error parsing it. Just ignore
|
|
|
|
// the initializer.
|
|
|
|
if (RealDecl == 0) {
|
|
|
|
delete Init;
|
|
|
|
return;
|
|
|
|
}
|
2007-09-12 18:07:44 +04:00
|
|
|
|
2007-09-13 00:13:48 +04:00
|
|
|
VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl);
|
|
|
|
if (!VDecl) {
|
2007-09-14 01:41:19 +04:00
|
|
|
Diag(dyn_cast<ScopedDecl>(RealDecl)->getLocation(),
|
|
|
|
diag::err_illegal_initializer);
|
2007-09-13 00:13:48 +04:00
|
|
|
RealDecl->setInvalidDecl();
|
|
|
|
return;
|
|
|
|
}
|
2007-09-12 18:07:44 +04:00
|
|
|
// Get the decls type and save a reference for later, since
|
2008-01-11 01:15:12 +03:00
|
|
|
// CheckInitializerTypes may change it.
|
2007-09-13 00:13:48 +04:00
|
|
|
QualType DclT = VDecl->getType(), SavT = DclT;
|
2008-04-16 02:42:06 +04:00
|
|
|
if (VDecl->isBlockVarDecl()) {
|
|
|
|
VarDecl::StorageClass SC = VDecl->getStorageClass();
|
2007-09-12 18:07:44 +04:00
|
|
|
if (SC == VarDecl::Extern) { // C99 6.7.8p5
|
2007-09-13 00:13:48 +04:00
|
|
|
Diag(VDecl->getLocation(), diag::err_block_extern_cant_init);
|
2008-04-16 02:42:06 +04:00
|
|
|
VDecl->setInvalidDecl();
|
|
|
|
} else if (!VDecl->isInvalidDecl()) {
|
2008-01-25 03:51:06 +03:00
|
|
|
if (CheckInitializerTypes(Init, DclT))
|
2008-04-16 02:42:06 +04:00
|
|
|
VDecl->setInvalidDecl();
|
2008-01-11 01:15:12 +03:00
|
|
|
if (SC == VarDecl::Static) // C99 6.7.8p4.
|
|
|
|
CheckForConstantInitializer(Init, DclT);
|
2007-09-12 18:07:44 +04:00
|
|
|
}
|
2008-04-16 02:42:06 +04:00
|
|
|
} else if (VDecl->isFileVarDecl()) {
|
|
|
|
if (VDecl->getStorageClass() == VarDecl::Extern)
|
2007-09-13 00:13:48 +04:00
|
|
|
Diag(VDecl->getLocation(), diag::warn_extern_init);
|
2008-04-16 02:42:06 +04:00
|
|
|
if (!VDecl->isInvalidDecl())
|
2008-01-25 03:51:06 +03:00
|
|
|
if (CheckInitializerTypes(Init, DclT))
|
2008-04-16 02:42:06 +04:00
|
|
|
VDecl->setInvalidDecl();
|
2008-01-11 01:15:12 +03:00
|
|
|
|
|
|
|
// C99 6.7.8p4. All file scoped initializers need to be constant.
|
|
|
|
CheckForConstantInitializer(Init, DclT);
|
2007-09-12 18:07:44 +04:00
|
|
|
}
|
|
|
|
// If the type changed, it means we had an incomplete type that was
|
|
|
|
// completed by the initializer. For example:
|
|
|
|
// int ary[] = { 1, 3, 5 };
|
|
|
|
// "ary" transitions from a VariableArrayType to a ConstantArrayType.
|
2007-11-29 22:09:19 +03:00
|
|
|
if (!VDecl->isInvalidDecl() && (DclT != SavT)) {
|
2007-09-13 00:13:48 +04:00
|
|
|
VDecl->setType(DclT);
|
2007-11-29 22:09:19 +03:00
|
|
|
Init->setType(DclT);
|
|
|
|
}
|
2007-09-12 18:07:44 +04:00
|
|
|
|
|
|
|
// Attach the initializer to the decl.
|
2007-09-13 00:13:48 +04:00
|
|
|
VDecl->setInit(Init);
|
2007-09-12 18:07:44 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
/// The declarators are chained together backwards, reverse the list.
|
|
|
|
Sema::DeclTy *Sema::FinalizeDeclaratorGroup(Scope *S, DeclTy *group) {
|
|
|
|
// Often we have single declarators, handle them quickly.
|
2007-09-14 03:52:58 +04:00
|
|
|
Decl *GroupDecl = static_cast<Decl*>(group);
|
|
|
|
if (GroupDecl == 0)
|
2007-09-12 18:07:44 +04:00
|
|
|
return 0;
|
2007-09-14 03:52:58 +04:00
|
|
|
|
|
|
|
ScopedDecl *Group = dyn_cast<ScopedDecl>(GroupDecl);
|
|
|
|
ScopedDecl *NewGroup = 0;
|
2007-09-12 18:07:44 +04:00
|
|
|
if (Group->getNextDeclarator() == 0)
|
2007-07-11 21:01:13 +04:00
|
|
|
NewGroup = Group;
|
2007-09-12 18:07:44 +04:00
|
|
|
else { // reverse the list.
|
|
|
|
while (Group) {
|
2007-09-14 03:52:58 +04:00
|
|
|
ScopedDecl *Next = Group->getNextDeclarator();
|
2007-09-12 18:07:44 +04:00
|
|
|
Group->setNextDeclarator(NewGroup);
|
|
|
|
NewGroup = Group;
|
|
|
|
Group = Next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Perform semantic analysis that depends on having fully processed both
|
|
|
|
// the declarator and initializer.
|
2007-09-14 03:52:58 +04:00
|
|
|
for (ScopedDecl *ID = NewGroup; ID; ID = ID->getNextDeclarator()) {
|
2007-09-12 18:07:44 +04:00
|
|
|
VarDecl *IDecl = dyn_cast<VarDecl>(ID);
|
|
|
|
if (!IDecl)
|
|
|
|
continue;
|
|
|
|
QualType T = IDecl->getType();
|
|
|
|
|
|
|
|
// C99 6.7.5.2p2: If an identifier is declared to be an object with
|
|
|
|
// static storage duration, it shall not have a variable length array.
|
2008-04-16 02:42:06 +04:00
|
|
|
if ((IDecl->isFileVarDecl() || IDecl->isBlockVarDecl()) &&
|
|
|
|
IDecl->getStorageClass() == VarDecl::Static) {
|
2008-02-15 22:53:52 +03:00
|
|
|
if (T->getAsVariableArrayType()) {
|
2008-02-15 21:16:39 +03:00
|
|
|
Diag(IDecl->getLocation(), diag::err_typecheck_illegal_vla);
|
|
|
|
IDecl->setInvalidDecl();
|
2007-09-12 18:07:44 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Block scope. C99 6.7p7: If an identifier for an object is declared with
|
|
|
|
// no linkage (C99 6.2.2p6), the type for the object shall be complete...
|
2008-04-16 02:42:06 +04:00
|
|
|
if (IDecl->isBlockVarDecl() &&
|
|
|
|
IDecl->getStorageClass() != VarDecl::Extern) {
|
2008-04-02 05:05:10 +04:00
|
|
|
if (T->isIncompleteType() && !IDecl->isInvalidDecl()) {
|
2007-12-02 10:50:03 +03:00
|
|
|
Diag(IDecl->getLocation(), diag::err_typecheck_decl_incomplete_type,
|
|
|
|
T.getAsString());
|
2007-09-12 18:07:44 +04:00
|
|
|
IDecl->setInvalidDecl();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// File scope. C99 6.9.2p2: A declaration of an identifier for and
|
|
|
|
// object that has file scope without an initializer, and without a
|
|
|
|
// storage-class specifier or with the storage-class specifier "static",
|
|
|
|
// constitutes a tentative definition. Note: A tentative definition with
|
|
|
|
// external linkage is valid (C99 6.2.2p5).
|
2008-04-16 02:42:06 +04:00
|
|
|
if (IDecl && !IDecl->getInit() &&
|
|
|
|
(IDecl->getStorageClass() == VarDecl::Static ||
|
|
|
|
IDecl->getStorageClass() == VarDecl::None)) {
|
2008-02-15 15:53:51 +03:00
|
|
|
if (T->isIncompleteArrayType()) {
|
2008-01-18 23:40:52 +03:00
|
|
|
// C99 6.9.2 (p2, p5): Implicit initialization causes an incomplete
|
|
|
|
// array to be completed. Don't issue a diagnostic.
|
2008-04-02 05:05:10 +04:00
|
|
|
} else if (T->isIncompleteType() && !IDecl->isInvalidDecl()) {
|
2008-01-18 23:40:52 +03:00
|
|
|
// C99 6.9.2p3: If the declaration of an identifier for an object is
|
|
|
|
// a tentative definition and has internal linkage (C99 6.2.2p3), the
|
|
|
|
// declared type shall not be an incomplete type.
|
2007-12-02 10:50:03 +03:00
|
|
|
Diag(IDecl->getLocation(), diag::err_typecheck_decl_incomplete_type,
|
|
|
|
T.getAsString());
|
2007-09-12 18:07:44 +04:00
|
|
|
IDecl->setInvalidDecl();
|
|
|
|
}
|
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
return NewGroup;
|
|
|
|
}
|
2007-08-28 07:03:08 +04:00
|
|
|
|
2008-04-08 08:40:51 +04:00
|
|
|
/// ActOnParamDeclarator - Called from Parser::ParseFunctionDeclarator()
|
|
|
|
/// to introduce parameters into function prototype scope.
|
|
|
|
Sema::DeclTy *
|
|
|
|
Sema::ActOnParamDeclarator(Scope *S, Declarator &D) {
|
|
|
|
DeclSpec &DS = D.getDeclSpec();
|
|
|
|
|
|
|
|
// Verify C99 6.7.5.3p2: The only SCS allowed is 'register'.
|
|
|
|
if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
|
|
|
|
DS.getStorageClassSpec() != DeclSpec::SCS_register) {
|
|
|
|
Diag(DS.getStorageClassSpecLoc(),
|
|
|
|
diag::err_invalid_storage_class_in_func_decl);
|
|
|
|
DS.ClearStorageClassSpecs();
|
|
|
|
}
|
|
|
|
if (DS.isThreadSpecified()) {
|
|
|
|
Diag(DS.getThreadSpecLoc(),
|
|
|
|
diag::err_invalid_storage_class_in_func_decl);
|
|
|
|
DS.ClearStorageClassSpecs();
|
|
|
|
}
|
|
|
|
|
2008-05-07 08:49:29 +04:00
|
|
|
// Check that there are no default arguments inside the type of this
|
|
|
|
// parameter (C++ only).
|
|
|
|
if (getLangOptions().CPlusPlus)
|
|
|
|
CheckExtraCXXDefaultArguments(D);
|
|
|
|
|
2008-04-08 08:40:51 +04:00
|
|
|
// In this context, we *do not* check D.getInvalidType(). If the declarator
|
|
|
|
// type was invalid, GetTypeForDeclarator() still returns a "valid" type,
|
|
|
|
// though it will not reflect the user specified type.
|
|
|
|
QualType parmDeclType = GetTypeForDeclarator(D, S);
|
|
|
|
|
|
|
|
assert(!parmDeclType.isNull() && "GetTypeForDeclarator() returned null type");
|
|
|
|
|
2007-07-11 21:01:13 +04: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.
|
2008-04-08 08:40:51 +04:00
|
|
|
IdentifierInfo *II = D.getIdentifier();
|
|
|
|
if (Decl *PrevDecl = LookupDecl(II, Decl::IDNS_Ordinary, S)) {
|
|
|
|
if (S->isDeclScope(PrevDecl)) {
|
|
|
|
Diag(D.getIdentifierLoc(), diag::err_param_redefinition,
|
|
|
|
dyn_cast<NamedDecl>(PrevDecl)->getName());
|
|
|
|
|
|
|
|
// Recover by removing the name
|
|
|
|
II = 0;
|
|
|
|
D.SetIdentifier(0, D.getIdentifierLoc());
|
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2007-08-08 02:44:21 +04:00
|
|
|
|
|
|
|
// Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
|
|
|
|
// Doing the promotion here has a win and a loss. The win is the type for
|
|
|
|
// both Decl's and DeclRefExpr's will match (a convenient invariant for the
|
|
|
|
// code generator). The loss is the orginal type isn't preserved. For example:
|
|
|
|
//
|
|
|
|
// void func(int parmvardecl[5]) { // convert "int [5]" to "int *"
|
|
|
|
// int blockvardecl[5];
|
|
|
|
// sizeof(parmvardecl); // size == 4
|
|
|
|
// sizeof(blockvardecl); // size == 20
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// For expressions, all implicit conversions are captured using the
|
|
|
|
// ImplicitCastExpr AST node (we have no such mechanism for Decl's).
|
|
|
|
//
|
|
|
|
// FIXME: If a source translation tool needs to see the original type, then
|
|
|
|
// we need to consider storing both types (in ParmVarDecl)...
|
|
|
|
//
|
2008-04-02 09:18:44 +04:00
|
|
|
if (parmDeclType->isArrayType()) {
|
2008-01-03 01:50:48 +03:00
|
|
|
// int x[restrict 4] -> int *restrict
|
2008-04-02 09:18:44 +04:00
|
|
|
parmDeclType = Context.getArrayDecayedType(parmDeclType);
|
2008-01-03 01:50:48 +03:00
|
|
|
} else if (parmDeclType->isFunctionType())
|
2007-08-08 02:44:21 +04:00
|
|
|
parmDeclType = Context.getPointerType(parmDeclType);
|
|
|
|
|
2008-04-08 08:40:51 +04:00
|
|
|
ParmVarDecl *New = ParmVarDecl::Create(Context, CurContext,
|
|
|
|
D.getIdentifierLoc(), II,
|
|
|
|
parmDeclType, VarDecl::None,
|
|
|
|
0, 0);
|
2008-02-15 10:04:12 +03:00
|
|
|
|
2008-04-08 08:40:51 +04:00
|
|
|
if (D.getInvalidType())
|
2007-08-28 22:45:29 +04:00
|
|
|
New->setInvalidDecl();
|
|
|
|
|
2008-04-12 04:47:19 +04:00
|
|
|
if (II)
|
|
|
|
PushOnScopeChains(New, S);
|
2008-02-18 00:20:31 +03:00
|
|
|
|
2008-05-09 20:56:01 +04:00
|
|
|
HandleDeclAttributes(New, D.getDeclSpec().getAttributes(),
|
|
|
|
D.getAttributes());
|
2007-07-11 21:01:13 +04:00
|
|
|
return New;
|
2008-04-08 08:40:51 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2007-11-09 02:49:49 +03:00
|
|
|
|
2007-10-09 21:14:05 +04:00
|
|
|
Sema::DeclTy *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) {
|
2007-07-11 21:01:13 +04:00
|
|
|
assert(CurFunctionDecl == 0 && "Function parsing confused");
|
|
|
|
assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
|
|
|
|
"Not a function declarator!");
|
|
|
|
DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
|
2008-04-08 08:40:51 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// 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) {
|
2008-04-08 08:40:51 +04:00
|
|
|
if (FTI.ArgInfo[i].Param == 0) {
|
2007-07-11 21:01:13 +04:00
|
|
|
Diag(FTI.ArgInfo[i].IdentLoc, diag::ext_param_not_declared,
|
|
|
|
FTI.ArgInfo[i].Ident->getName());
|
|
|
|
// Implicitly declare the argument as type 'int' for lack of a better
|
|
|
|
// type.
|
2008-04-08 08:40:51 +04:00
|
|
|
DeclSpec DS;
|
|
|
|
const char* PrevSpec; // unused
|
|
|
|
DS.SetTypeSpecType(DeclSpec::TST_int, FTI.ArgInfo[i].IdentLoc,
|
|
|
|
PrevSpec);
|
|
|
|
Declarator ParamD(DS, Declarator::KNRTypeListContext);
|
|
|
|
ParamD.SetIdentifier(FTI.ArgInfo[i].Ident, FTI.ArgInfo[i].IdentLoc);
|
|
|
|
FTI.ArgInfo[i].Param = ActOnParamDeclarator(FnBodyScope, ParamD);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
}
|
2008-02-17 22:31:09 +03:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// Since this is a function definition, act as though we have information
|
|
|
|
// about the arguments.
|
2008-02-17 22:31:09 +03:00
|
|
|
if (FTI.NumArgs)
|
|
|
|
FTI.hasPrototype = true;
|
2007-07-11 21:01:13 +04:00
|
|
|
} else {
|
2008-04-08 08:40:51 +04:00
|
|
|
// FIXME: Diagnose arguments without names in C.
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Scope *GlobalScope = FnBodyScope->getParent();
|
2008-01-14 23:51:29 +03:00
|
|
|
|
|
|
|
// See if this is a redefinition.
|
2008-04-02 03:04:06 +04:00
|
|
|
Decl *PrevDcl = LookupDecl(D.getIdentifier(), Decl::IDNS_Ordinary,
|
2008-04-02 18:35:35 +04:00
|
|
|
GlobalScope);
|
2008-05-10 03:39:43 +04:00
|
|
|
if (PrevDcl && IdResolver.isDeclInScope(PrevDcl, CurContext)) {
|
|
|
|
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PrevDcl)) {
|
|
|
|
const FunctionDecl *Definition;
|
|
|
|
if (FD->getBody(Definition)) {
|
|
|
|
Diag(D.getIdentifierLoc(), diag::err_redefinition,
|
|
|
|
D.getIdentifier()->getName());
|
|
|
|
Diag(Definition->getLocation(), diag::err_previous_definition);
|
|
|
|
}
|
2008-01-14 23:51:29 +03:00
|
|
|
}
|
|
|
|
}
|
2008-02-12 04:09:36 +03:00
|
|
|
Decl *decl = static_cast<Decl*>(ActOnDeclarator(GlobalScope, D, 0));
|
2008-02-16 04:20:36 +03:00
|
|
|
FunctionDecl *FD = cast<FunctionDecl>(decl);
|
2007-07-11 21:01:13 +04:00
|
|
|
CurFunctionDecl = FD;
|
2008-04-06 08:47:34 +04:00
|
|
|
PushDeclContext(FD);
|
2008-04-08 08:40:51 +04:00
|
|
|
|
|
|
|
// Check the validity of our function parameters
|
|
|
|
CheckParmsForFunctionDef(FD);
|
|
|
|
|
|
|
|
// Introduce our parameters into the function scope
|
|
|
|
for (unsigned p = 0, NumParams = FD->getNumParams(); p < NumParams; ++p) {
|
|
|
|
ParmVarDecl *Param = FD->getParamDecl(p);
|
|
|
|
// If this has an identifier, add it to the scope stack.
|
2008-04-12 04:47:19 +04:00
|
|
|
if (Param->getIdentifier())
|
|
|
|
PushOnScopeChains(Param, FnBodyScope);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2008-04-08 08:40:51 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
return FD;
|
|
|
|
}
|
|
|
|
|
2007-11-12 02:20:51 +03:00
|
|
|
Sema::DeclTy *Sema::ActOnFinishFunctionBody(DeclTy *D, StmtTy *Body) {
|
|
|
|
Decl *dcl = static_cast<Decl *>(D);
|
|
|
|
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(dcl)) {
|
|
|
|
FD->setBody((Stmt*)Body);
|
|
|
|
assert(FD == CurFunctionDecl && "Function parsing confused");
|
2007-12-13 21:18:56 +03:00
|
|
|
CurFunctionDecl = 0;
|
2008-01-07 22:49:32 +03:00
|
|
|
} else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(dcl)) {
|
2007-11-12 02:20:51 +03:00
|
|
|
MD->setBody((Stmt*)Body);
|
2007-11-12 16:56:41 +03:00
|
|
|
CurMethodDecl = 0;
|
2007-12-13 21:18:56 +03:00
|
|
|
}
|
2008-04-06 08:47:34 +04:00
|
|
|
PopDeclContext();
|
2007-11-10 19:31:34 +03:00
|
|
|
// Verify and clean out per-function state.
|
|
|
|
|
|
|
|
// Check goto/label use.
|
|
|
|
for (llvm::DenseMap<IdentifierInfo*, LabelStmt*>::iterator
|
|
|
|
I = LabelMap.begin(), E = LabelMap.end(); I != E; ++I) {
|
|
|
|
// Verify that we have no forward references left. If so, there was a goto
|
|
|
|
// or address of a label taken, but no definition of it. Label fwd
|
|
|
|
// definitions are indicated with a null substmt.
|
|
|
|
if (I->second->getSubStmt() == 0) {
|
|
|
|
LabelStmt *L = I->second;
|
|
|
|
// Emit error.
|
|
|
|
Diag(L->getIdentLoc(), diag::err_undeclared_label_use, L->getName());
|
|
|
|
|
|
|
|
// At this point, we have gotos that use the bogus label. Stitch it into
|
|
|
|
// the function body so that they aren't leaked and that the AST is well
|
|
|
|
// formed.
|
2008-01-25 03:01:10 +03:00
|
|
|
if (Body) {
|
|
|
|
L->setSubStmt(new NullStmt(L->getIdentLoc()));
|
|
|
|
cast<CompoundStmt>((Stmt*)Body)->push_back(L);
|
|
|
|
} else {
|
|
|
|
// The whole function wasn't parsed correctly, just delete this.
|
|
|
|
delete L;
|
|
|
|
}
|
2007-11-10 19:31:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
LabelMap.clear();
|
2007-11-12 02:20:51 +03:00
|
|
|
|
|
|
|
return D;
|
2007-11-10 19:31:34 +03:00
|
|
|
}
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
/// ImplicitlyDefineFunction - An undeclared identifier was used in a function
|
|
|
|
/// call, forming a call to an implicitly defined function (per C99 6.5.1p2).
|
2007-09-16 20:16:00 +04:00
|
|
|
ScopedDecl *Sema::ImplicitlyDefineFunction(SourceLocation Loc,
|
|
|
|
IdentifierInfo &II, Scope *S) {
|
2008-05-06 01:18:06 +04:00
|
|
|
// Extension in C99. Legal in C90, but warn about it.
|
|
|
|
if (getLangOptions().C99)
|
2007-07-11 21:01:13 +04:00
|
|
|
Diag(Loc, diag::ext_implicit_function_decl, II.getName());
|
2008-05-06 01:18:06 +04:00
|
|
|
else
|
2007-07-11 21:01:13 +04:00
|
|
|
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();
|
|
|
|
const char *Dummy;
|
|
|
|
DeclSpec DS;
|
|
|
|
bool Error = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, Dummy);
|
|
|
|
Error = Error; // Silence warning.
|
|
|
|
assert(!Error && "Error setting up implicit decl!");
|
|
|
|
Declarator D(DS, Declarator::BlockContext);
|
|
|
|
D.AddTypeInfo(DeclaratorChunk::getFunction(false, false, 0, 0, Loc));
|
|
|
|
D.SetIdentifier(&II, Loc);
|
|
|
|
|
2008-05-02 01:04:16 +04:00
|
|
|
// Insert this function into translation-unit scope.
|
|
|
|
|
|
|
|
DeclContext *PrevDC = CurContext;
|
|
|
|
CurContext = Context.getTranslationUnitDecl();
|
|
|
|
|
2008-04-04 18:32:09 +04:00
|
|
|
FunctionDecl *FD =
|
2008-05-02 01:04:16 +04:00
|
|
|
dyn_cast<FunctionDecl>(static_cast<Decl*>(ActOnDeclarator(TUScope, D, 0)));
|
2008-04-04 18:32:09 +04:00
|
|
|
FD->setImplicit();
|
2008-05-02 01:04:16 +04:00
|
|
|
|
|
|
|
CurContext = PrevDC;
|
|
|
|
|
2008-04-04 18:32:09 +04:00
|
|
|
return FD;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-14 09:34:38 +03:00
|
|
|
TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, QualType T,
|
2007-09-14 03:52:58 +04:00
|
|
|
ScopedDecl *LastDeclarator) {
|
2007-07-11 21:01:13 +04:00
|
|
|
assert(D.getIdentifier() && "Wrong callback for declspec without declarator");
|
2007-08-29 00:14:24 +04:00
|
|
|
assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
// Scope manipulation handled by caller.
|
2008-04-04 10:12:32 +04:00
|
|
|
TypedefDecl *NewTD = TypedefDecl::Create(Context, CurContext,
|
|
|
|
D.getIdentifierLoc(),
|
2008-03-15 09:12:44 +03:00
|
|
|
D.getIdentifier(),
|
2008-03-16 00:32:50 +03:00
|
|
|
T, LastDeclarator);
|
2007-08-29 00:14:24 +04:00
|
|
|
if (D.getInvalidType())
|
|
|
|
NewTD->setInvalidDecl();
|
|
|
|
return NewTD;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2007-09-15 22:49:24 +04:00
|
|
|
/// ActOnTag - This is invoked when we see 'struct foo' or 'struct {'. In the
|
2007-07-11 21:01:13 +04:00
|
|
|
/// 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-09-15 22:49:24 +04:00
|
|
|
Sema::DeclTy *Sema::ActOnTag(Scope *S, unsigned TagType, TagKind TK,
|
2007-07-11 21:01:13 +04:00
|
|
|
SourceLocation KWLoc, IdentifierInfo *Name,
|
|
|
|
SourceLocation NameLoc, AttributeList *Attr) {
|
|
|
|
// If this is a use of an existing tag, it must have a name.
|
|
|
|
assert((Name != 0 || TK == TK_Definition) &&
|
|
|
|
"Nameless record must be a definition!");
|
|
|
|
|
|
|
|
Decl::Kind Kind;
|
|
|
|
switch (TagType) {
|
|
|
|
default: assert(0 && "Unknown tag type!");
|
|
|
|
case DeclSpec::TST_struct: Kind = Decl::Struct; break;
|
|
|
|
case DeclSpec::TST_union: Kind = Decl::Union; break;
|
2008-04-13 22:59:07 +04:00
|
|
|
case DeclSpec::TST_class: Kind = Decl::Class; break;
|
2007-07-11 21:01:13 +04:00
|
|
|
case DeclSpec::TST_enum: Kind = Decl::Enum; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a named struct, check to see if there was a previous forward
|
|
|
|
// declaration or definition.
|
2008-04-27 17:50:30 +04:00
|
|
|
// Use ScopedDecl instead of TagDecl, because a NamespaceDecl may come up.
|
|
|
|
if (ScopedDecl *PrevDecl =
|
|
|
|
dyn_cast_or_null<ScopedDecl>(LookupDecl(Name, Decl::IDNS_Tag, S))) {
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2008-04-27 17:50:30 +04:00
|
|
|
assert((isa<TagDecl>(PrevDecl) || isa<NamespaceDecl>(PrevDecl)) &&
|
|
|
|
"unexpected Decl type");
|
|
|
|
if (TagDecl *PrevTagDecl = dyn_cast<TagDecl>(PrevDecl)) {
|
|
|
|
// 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.
|
2008-05-10 03:39:43 +04:00
|
|
|
if (TK == TK_Reference ||
|
|
|
|
IdResolver.isDeclInScope(PrevDecl, CurContext, S)) {
|
2008-04-27 17:50:30 +04:00
|
|
|
// Make sure that this wasn't declared as an enum and now used as a struct
|
|
|
|
// or something similar.
|
|
|
|
if (PrevDecl->getKind() != Kind) {
|
|
|
|
Diag(KWLoc, diag::err_use_with_wrong_tag, Name->getName());
|
|
|
|
Diag(PrevDecl->getLocation(), diag::err_previous_use);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a use or a forward declaration, we're good.
|
|
|
|
if (TK != TK_Definition)
|
|
|
|
return PrevDecl;
|
|
|
|
|
|
|
|
// Diagnose attempts to redefine a tag.
|
|
|
|
if (PrevTagDecl->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-07-11 21:01:13 +04:00
|
|
|
}
|
2008-04-27 17:50:30 +04: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.
|
|
|
|
} else {
|
|
|
|
// The tag name clashes with a namespace name, issue an error and recover
|
|
|
|
// by making this tag be anonymous.
|
|
|
|
Diag(NameLoc, diag::err_redefinition_different_kind, Name->getName());
|
|
|
|
Diag(PrevDecl->getLocation(), diag::err_previous_definition);
|
|
|
|
Name = 0;
|
2007-07-11 21:01:13 +04: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;
|
|
|
|
|
|
|
|
// Otherwise, if this is the first time we've seen this tag, create the decl.
|
|
|
|
TagDecl *New;
|
|
|
|
switch (Kind) {
|
|
|
|
default: assert(0 && "Unknown tag kind!");
|
|
|
|
case Decl::Enum:
|
|
|
|
// FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.:
|
|
|
|
// enum X { A, B, C } D; D should chain to X.
|
2008-04-04 10:12:32 +04:00
|
|
|
New = EnumDecl::Create(Context, CurContext, Loc, Name, 0);
|
2007-07-11 21:01:13 +04:00
|
|
|
// If this is an undefined enum, warn.
|
|
|
|
if (TK != TK_Definition) Diag(Loc, diag::ext_forward_ref_enum);
|
|
|
|
break;
|
|
|
|
case Decl::Union:
|
|
|
|
case Decl::Struct:
|
|
|
|
case Decl::Class:
|
|
|
|
// FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.:
|
|
|
|
// struct X { int A; } D; D should chain to X.
|
2008-04-04 10:12:32 +04:00
|
|
|
New = RecordDecl::Create(Context, Kind, CurContext, Loc, Name, 0);
|
2007-07-11 21:01:13 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this has an identifier, add it to the scope stack.
|
|
|
|
if (Name) {
|
2007-08-26 10:24:45 +04:00
|
|
|
// The scope passed in may not be a decl scope. Zip up the scope tree until
|
|
|
|
// we find one that is.
|
|
|
|
while ((S->getFlags() & Scope::DeclScope) == 0)
|
|
|
|
S = S->getParent();
|
|
|
|
|
|
|
|
// Add it to the decl chain.
|
2008-04-12 04:47:19 +04:00
|
|
|
PushOnScopeChains(New, S);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2008-02-06 03:51:33 +03:00
|
|
|
|
2008-02-16 03:29:18 +03:00
|
|
|
HandleDeclAttributes(New, Attr, 0);
|
2007-07-11 21:01:13 +04:00
|
|
|
return New;
|
|
|
|
}
|
|
|
|
|
2007-09-15 22:49:24 +04:00
|
|
|
/// ActOnField - Each field of a struct/union/class is passed into this in order
|
2007-07-11 21:01:13 +04:00
|
|
|
/// to create a FieldDecl object for it.
|
2008-04-11 03:32:45 +04:00
|
|
|
Sema::DeclTy *Sema::ActOnField(Scope *S,
|
2007-07-11 21:01:13 +04:00
|
|
|
SourceLocation DeclStart,
|
|
|
|
Declarator &D, ExprTy *BitfieldWidth) {
|
|
|
|
IdentifierInfo *II = D.getIdentifier();
|
|
|
|
Expr *BitWidth = (Expr*)BitfieldWidth;
|
|
|
|
SourceLocation Loc = DeclStart;
|
|
|
|
if (II) Loc = D.getIdentifierLoc();
|
|
|
|
|
|
|
|
// FIXME: Unnamed fields can be handled in various different ways, for
|
|
|
|
// example, unnamed unions inject all members into the struct namespace!
|
|
|
|
|
|
|
|
|
|
|
|
if (BitWidth) {
|
|
|
|
// TODO: Validate.
|
|
|
|
//printf("WARNING: BITFIELDS IGNORED!\n");
|
|
|
|
|
|
|
|
// 6.7.2.1p3
|
|
|
|
// 6.7.2.1p4
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Not a bitfield.
|
|
|
|
|
|
|
|
// validate II.
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QualType T = GetTypeForDeclarator(D, S);
|
2007-08-29 00:14:24 +04:00
|
|
|
assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
|
|
|
|
bool InvalidDecl = false;
|
2007-08-31 21:20:07 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// C99 6.7.2.1p8: A member of a structure or union may have any type other
|
|
|
|
// than a variably modified type.
|
2008-02-15 15:53:51 +03:00
|
|
|
if (T->isVariablyModifiedType()) {
|
|
|
|
// FIXME: This diagnostic needs work
|
|
|
|
Diag(Loc, diag::err_typecheck_illegal_vla, Loc);
|
2007-08-31 21:20:07 +04:00
|
|
|
InvalidDecl = true;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
// FIXME: Chain fielddecls together.
|
2008-04-11 20:55:42 +04:00
|
|
|
FieldDecl *NewFD = FieldDecl::Create(Context, Loc, II, T, BitWidth);
|
2007-09-12 01:17:26 +04:00
|
|
|
|
2008-02-16 03:29:18 +03:00
|
|
|
HandleDeclAttributes(NewFD, D.getDeclSpec().getAttributes(),
|
|
|
|
D.getAttributes());
|
|
|
|
|
2007-08-29 00:14:24 +04:00
|
|
|
if (D.getInvalidType() || InvalidDecl)
|
|
|
|
NewFD->setInvalidDecl();
|
|
|
|
return NewFD;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2007-10-01 20:53:59 +04:00
|
|
|
/// TranslateIvarVisibility - Translate visibility from a token ID to an
|
|
|
|
/// AST enum value.
|
2008-01-07 22:49:32 +03:00
|
|
|
static ObjCIvarDecl::AccessControl
|
2007-10-01 20:53:59 +04:00
|
|
|
TranslateIvarVisibility(tok::ObjCKeywordKind ivarVisibility) {
|
2007-09-15 03:09:53 +04:00
|
|
|
switch (ivarVisibility) {
|
2008-01-07 22:49:32 +03:00
|
|
|
case tok::objc_private: return ObjCIvarDecl::Private;
|
|
|
|
case tok::objc_public: return ObjCIvarDecl::Public;
|
|
|
|
case tok::objc_protected: return ObjCIvarDecl::Protected;
|
|
|
|
case tok::objc_package: return ObjCIvarDecl::Package;
|
2007-10-01 20:53:59 +04:00
|
|
|
default: assert(false && "Unknown visitibility kind");
|
2007-09-15 03:09:53 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-11 20:55:42 +04:00
|
|
|
/// ActOnIvar - Each ivar field of an objective-c class is passed into this
|
|
|
|
/// in order to create an IvarDecl object for it.
|
2008-04-11 03:32:45 +04:00
|
|
|
Sema::DeclTy *Sema::ActOnIvar(Scope *S,
|
2008-04-11 20:55:42 +04:00
|
|
|
SourceLocation DeclStart,
|
|
|
|
Declarator &D, ExprTy *BitfieldWidth,
|
|
|
|
tok::ObjCKeywordKind Visibility) {
|
2008-04-11 03:32:45 +04:00
|
|
|
IdentifierInfo *II = D.getIdentifier();
|
|
|
|
Expr *BitWidth = (Expr*)BitfieldWidth;
|
|
|
|
SourceLocation Loc = DeclStart;
|
|
|
|
if (II) Loc = D.getIdentifierLoc();
|
|
|
|
|
|
|
|
// FIXME: Unnamed fields can be handled in various different ways, for
|
|
|
|
// example, unnamed unions inject all members into the struct namespace!
|
|
|
|
|
|
|
|
|
|
|
|
if (BitWidth) {
|
|
|
|
// TODO: Validate.
|
|
|
|
//printf("WARNING: BITFIELDS IGNORED!\n");
|
|
|
|
|
|
|
|
// 6.7.2.1p3
|
|
|
|
// 6.7.2.1p4
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Not a bitfield.
|
|
|
|
|
|
|
|
// validate II.
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QualType T = GetTypeForDeclarator(D, S);
|
|
|
|
assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
|
|
|
|
bool InvalidDecl = false;
|
|
|
|
|
|
|
|
// C99 6.7.2.1p8: A member of a structure or union may have any type other
|
|
|
|
// than a variably modified type.
|
|
|
|
if (T->isVariablyModifiedType()) {
|
|
|
|
// FIXME: This diagnostic needs work
|
|
|
|
Diag(Loc, diag::err_typecheck_illegal_vla, Loc);
|
|
|
|
InvalidDecl = true;
|
|
|
|
}
|
|
|
|
|
2008-04-11 20:55:42 +04:00
|
|
|
ObjCIvarDecl *NewID = ObjCIvarDecl::Create(Context, Loc, II, T);
|
2008-04-11 03:32:45 +04:00
|
|
|
|
|
|
|
HandleDeclAttributes(NewID, D.getDeclSpec().getAttributes(),
|
|
|
|
D.getAttributes());
|
|
|
|
|
|
|
|
if (D.getInvalidType() || InvalidDecl)
|
|
|
|
NewID->setInvalidDecl();
|
|
|
|
// If we have visibility info, make sure the AST is set accordingly.
|
2008-04-11 20:55:42 +04:00
|
|
|
if (Visibility != tok::objc_not_keyword)
|
|
|
|
NewID->setAccessControl(TranslateIvarVisibility(Visibility));
|
2008-04-11 03:32:45 +04:00
|
|
|
return NewID;
|
|
|
|
}
|
|
|
|
|
2007-09-29 04:54:24 +04:00
|
|
|
void Sema::ActOnFields(Scope* S,
|
2007-10-04 04:45:27 +04:00
|
|
|
SourceLocation RecLoc, DeclTy *RecDecl,
|
2007-09-15 22:49:24 +04:00
|
|
|
DeclTy **Fields, unsigned NumFields,
|
2008-04-11 03:32:45 +04:00
|
|
|
SourceLocation LBrac, SourceLocation RBrac) {
|
2007-09-15 02:20:54 +04:00
|
|
|
Decl *EnclosingDecl = static_cast<Decl*>(RecDecl);
|
|
|
|
assert(EnclosingDecl && "missing record or interface decl");
|
|
|
|
RecordDecl *Record = dyn_cast<RecordDecl>(EnclosingDecl);
|
|
|
|
|
2007-09-14 20:27:55 +04:00
|
|
|
if (Record && Record->isDefinition()) {
|
2007-07-11 21:01:13 +04:00
|
|
|
// Diagnose code like:
|
|
|
|
// struct S { struct S {} X; };
|
|
|
|
// 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);
|
2007-09-15 02:20:54 +04:00
|
|
|
Record->setInvalidDecl();
|
2007-07-11 21:01:13 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Verify that all the fields are okay.
|
|
|
|
unsigned NumNamedMembers = 0;
|
|
|
|
llvm::SmallVector<FieldDecl*, 32> RecFields;
|
|
|
|
llvm::SmallSet<const IdentifierInfo*, 32> FieldIDs;
|
2007-09-15 02:20:54 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
for (unsigned i = 0; i != NumFields; ++i) {
|
2007-09-14 20:27:55 +04:00
|
|
|
|
2007-09-15 02:20:54 +04:00
|
|
|
FieldDecl *FD = cast_or_null<FieldDecl>(static_cast<Decl*>(Fields[i]));
|
|
|
|
assert(FD && "missing field decl");
|
|
|
|
|
|
|
|
// Remember all fields.
|
|
|
|
RecFields.push_back(FD);
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
// Get the type for the field.
|
2007-08-01 01:33:24 +04:00
|
|
|
Type *FDTy = FD->getType().getTypePtr();
|
2007-09-15 03:09:53 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// C99 6.7.2.1p2 - A field may not be a function type.
|
2007-08-01 01:33:24 +04:00
|
|
|
if (FDTy->isFunctionType()) {
|
2007-09-15 02:20:54 +04:00
|
|
|
Diag(FD->getLocation(), diag::err_field_declared_as_function,
|
2007-07-11 21:01:13 +04:00
|
|
|
FD->getName());
|
2007-09-15 02:20:54 +04:00
|
|
|
FD->setInvalidDecl();
|
|
|
|
EnclosingDecl->setInvalidDecl();
|
2007-07-11 21:01:13 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// C99 6.7.2.1p2 - A field may not be an incomplete type except...
|
|
|
|
if (FDTy->isIncompleteType()) {
|
2007-09-14 20:27:55 +04:00
|
|
|
if (!Record) { // Incomplete ivar type is always an error.
|
2007-10-04 04:45:27 +04:00
|
|
|
Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName());
|
2007-09-15 02:20:54 +04:00
|
|
|
FD->setInvalidDecl();
|
|
|
|
EnclosingDecl->setInvalidDecl();
|
2007-10-04 04:45:27 +04:00
|
|
|
continue;
|
2007-09-14 20:27:55 +04:00
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
if (i != NumFields-1 || // ... that the last member ...
|
|
|
|
Record->getKind() != Decl::Struct || // ... of a structure ...
|
2007-08-01 01:33:24 +04:00
|
|
|
!FDTy->isArrayType()) { //... may have incomplete array type.
|
2007-07-11 21:01:13 +04:00
|
|
|
Diag(FD->getLocation(), diag::err_field_incomplete, FD->getName());
|
2007-09-15 02:20:54 +04:00
|
|
|
FD->setInvalidDecl();
|
|
|
|
EnclosingDecl->setInvalidDecl();
|
2007-07-11 21:01:13 +04:00
|
|
|
continue;
|
|
|
|
}
|
2007-09-14 20:27:55 +04:00
|
|
|
if (NumNamedMembers < 1) { //... must have more than named member ...
|
2007-07-11 21:01:13 +04:00
|
|
|
Diag(FD->getLocation(), diag::err_flexible_array_empty_struct,
|
|
|
|
FD->getName());
|
2007-09-15 02:20:54 +04:00
|
|
|
FD->setInvalidDecl();
|
|
|
|
EnclosingDecl->setInvalidDecl();
|
2007-07-11 21:01:13 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Okay, we have a legal flexible array member at the end of the struct.
|
2007-09-14 20:27:55 +04:00
|
|
|
if (Record)
|
|
|
|
Record->setHasFlexibleArrayMember(true);
|
2007-07-11 21:01:13 +04: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.
|
2007-08-01 01:33:24 +04:00
|
|
|
if (const RecordType *FDTTy = FDTy->getAsRecordType()) {
|
2007-07-11 21:01:13 +04:00
|
|
|
if (FDTTy->getDecl()->hasFlexibleArrayMember()) {
|
|
|
|
// If this is a member of a union, then entire union becomes "flexible".
|
2007-09-14 20:27:55 +04:00
|
|
|
if (Record && Record->getKind() == Decl::Union) {
|
2007-07-11 21:01:13 +04:00
|
|
|
Record->setHasFlexibleArrayMember(true);
|
|
|
|
} 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());
|
2007-09-15 02:20:54 +04:00
|
|
|
FD->setInvalidDecl();
|
|
|
|
EnclosingDecl->setInvalidDecl();
|
2007-07-11 21:01:13 +04:00
|
|
|
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-10-04 04:45:27 +04:00
|
|
|
if (Record)
|
2007-09-14 20:27:55 +04:00
|
|
|
Record->setHasFlexibleArrayMember(true);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-10-13 02:10:42 +04:00
|
|
|
/// A field cannot be an Objective-c object
|
2008-01-07 22:49:32 +03:00
|
|
|
if (FDTy->isObjCInterfaceType()) {
|
2007-10-13 02:10:42 +04:00
|
|
|
Diag(FD->getLocation(), diag::err_statically_allocated_object,
|
|
|
|
FD->getName());
|
|
|
|
FD->setInvalidDecl();
|
|
|
|
EnclosingDecl->setInvalidDecl();
|
|
|
|
continue;
|
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
// Keep track of the number of named members.
|
|
|
|
if (IdentifierInfo *II = FD->getIdentifier()) {
|
|
|
|
// Detect duplicate member names.
|
|
|
|
if (!FieldIDs.insert(II)) {
|
|
|
|
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);
|
2007-09-15 02:20:54 +04:00
|
|
|
FD->setInvalidDecl();
|
|
|
|
EnclosingDecl->setInvalidDecl();
|
2007-07-11 21:01:13 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
++NumNamedMembers;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Okay, we successfully defined 'Record'.
|
2008-02-06 03:51:33 +03:00
|
|
|
if (Record) {
|
2007-09-14 20:27:55 +04:00
|
|
|
Record->defineBody(&RecFields[0], RecFields.size());
|
2008-02-06 03:51:33 +03:00
|
|
|
Consumer.HandleTagDeclDefinition(Record);
|
|
|
|
} else {
|
2008-02-06 01:40:55 +03:00
|
|
|
ObjCIvarDecl **ClsFields = reinterpret_cast<ObjCIvarDecl**>(&RecFields[0]);
|
|
|
|
if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(EnclosingDecl))
|
|
|
|
ID->addInstanceVariablesToClass(ClsFields, RecFields.size(), RBrac);
|
|
|
|
else if (ObjCImplementationDecl *IMPDecl =
|
|
|
|
dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) {
|
2008-01-07 22:49:32 +03:00
|
|
|
assert(IMPDecl && "ActOnFields - missing ObjCImplementationDecl");
|
|
|
|
IMPDecl->ObjCAddInstanceVariablesToClassImpl(ClsFields, RecFields.size());
|
2007-10-31 21:48:14 +03:00
|
|
|
CheckImplementationIvars(IMPDecl, ClsFields, RecFields.size(), RBrac);
|
2007-09-26 22:27:25 +04:00
|
|
|
}
|
2007-09-15 01:08:27 +04:00
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2007-09-15 22:49:24 +04:00
|
|
|
Sema::DeclTy *Sema::ActOnEnumConstant(Scope *S, DeclTy *theEnumDecl,
|
2007-07-11 21:01:13 +04:00
|
|
|
DeclTy *lastEnumConst,
|
|
|
|
SourceLocation IdLoc, IdentifierInfo *Id,
|
|
|
|
SourceLocation EqualLoc, ExprTy *val) {
|
2008-04-04 10:12:32 +04:00
|
|
|
EnumDecl *TheEnumDecl = cast<EnumDecl>(static_cast<Decl*>(theEnumDecl));
|
2007-07-11 21:01:13 +04:00
|
|
|
EnumConstantDecl *LastEnumConst =
|
|
|
|
cast_or_null<EnumConstantDecl>(static_cast<Decl*>(lastEnumConst));
|
|
|
|
Expr *Val = static_cast<Expr*>(val);
|
|
|
|
|
2007-08-26 10:24:45 +04:00
|
|
|
// The scope passed in may not be a decl scope. Zip up the scope tree until
|
|
|
|
// we find one that is.
|
|
|
|
while ((S->getFlags() & Scope::DeclScope) == 0)
|
|
|
|
S = S->getParent();
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// Verify that there isn't already something declared with this name in this
|
|
|
|
// scope.
|
2008-04-02 18:35:35 +04:00
|
|
|
if (Decl *PrevDecl = LookupDecl(Id, Decl::IDNS_Ordinary, S)) {
|
2008-05-10 03:39:43 +04:00
|
|
|
if (IdResolver.isDeclInScope(PrevDecl, CurContext, S)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
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);
|
2008-02-26 03:33:57 +03:00
|
|
|
delete Val;
|
2007-07-11 21:01:13 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::APSInt EnumVal(32);
|
|
|
|
QualType EltTy;
|
|
|
|
if (Val) {
|
2007-08-28 01:16:18 +04:00
|
|
|
// Make sure to promote the operand type to int.
|
|
|
|
UsualUnaryConversions(Val);
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// C99 6.7.2.2p2: Make sure we have an integer constant expression.
|
|
|
|
SourceLocation ExpLoc;
|
2007-07-16 03:26:56 +04:00
|
|
|
if (!Val->isIntegerConstantExpr(EnumVal, Context, &ExpLoc)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
Diag(ExpLoc, diag::err_enum_value_not_integer_constant_expr,
|
|
|
|
Id->getName());
|
2008-02-26 03:33:57 +03:00
|
|
|
delete Val;
|
2007-08-27 21:37:24 +04:00
|
|
|
Val = 0; // Just forget about it.
|
2007-08-29 20:03:41 +04:00
|
|
|
} else {
|
|
|
|
EltTy = Val->getType();
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2007-08-27 21:37:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!Val) {
|
|
|
|
if (LastEnumConst) {
|
|
|
|
// Assign the last value + 1.
|
|
|
|
EnumVal = LastEnumConst->getInitVal();
|
|
|
|
++EnumVal;
|
2007-08-28 01:16:18 +04:00
|
|
|
|
|
|
|
// Check for overflow on increment.
|
|
|
|
if (EnumVal < LastEnumConst->getInitVal())
|
|
|
|
Diag(IdLoc, diag::warn_enum_value_overflow);
|
|
|
|
|
2007-08-27 21:37:24 +04:00
|
|
|
EltTy = LastEnumConst->getType();
|
|
|
|
} else {
|
|
|
|
// First value, set to zero.
|
|
|
|
EltTy = Context.IntTy;
|
2008-03-05 21:54:05 +03:00
|
|
|
EnumVal.zextOrTrunc(static_cast<uint32_t>(Context.getTypeSize(EltTy)));
|
2007-08-27 21:37:24 +04:00
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2008-03-15 09:12:44 +03:00
|
|
|
EnumConstantDecl *New =
|
2008-04-04 10:12:32 +04:00
|
|
|
EnumConstantDecl::Create(Context, TheEnumDecl, IdLoc, Id, EltTy,
|
|
|
|
Val, EnumVal,
|
2008-03-16 00:32:50 +03:00
|
|
|
LastEnumConst);
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
// Register this decl in the current scope stack.
|
2008-04-12 04:47:19 +04:00
|
|
|
PushOnScopeChains(New, S);
|
2007-07-11 21:01:13 +04:00
|
|
|
return New;
|
|
|
|
}
|
|
|
|
|
2007-09-15 22:49:24 +04:00
|
|
|
void Sema::ActOnEnumBody(SourceLocation EnumLoc, DeclTy *EnumDeclX,
|
2007-07-11 21:01:13 +04:00
|
|
|
DeclTy **Elements, unsigned NumElements) {
|
|
|
|
EnumDecl *Enum = cast<EnumDecl>(static_cast<Decl*>(EnumDeclX));
|
|
|
|
assert(!Enum->isDefinition() && "Enum redefinitions can't reach here");
|
|
|
|
|
2007-08-28 09:10:31 +04:00
|
|
|
// TODO: If the result value doesn't fit in an int, it must be a long or long
|
|
|
|
// long value. ISO C does not support this, but GCC does as an extension,
|
|
|
|
// emit a warning.
|
2008-03-05 21:54:05 +03:00
|
|
|
unsigned IntWidth = Context.Target.getIntWidth();
|
2007-08-28 09:10:31 +04:00
|
|
|
|
2007-08-28 10:15:15 +04:00
|
|
|
// Verify that all the values are okay, compute the size of the values, and
|
|
|
|
// reverse the list.
|
|
|
|
unsigned NumNegativeBits = 0;
|
|
|
|
unsigned NumPositiveBits = 0;
|
|
|
|
|
|
|
|
// Keep track of whether all elements have type int.
|
|
|
|
bool AllElementsInt = true;
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
EnumConstantDecl *EltList = 0;
|
|
|
|
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.
|
2007-08-28 09:27:00 +04:00
|
|
|
|
|
|
|
// If the enum value doesn't fit in an int, emit an extension warning.
|
|
|
|
const llvm::APSInt &InitVal = ECD->getInitVal();
|
2008-02-26 03:33:57 +03:00
|
|
|
assert(InitVal.getBitWidth() >= IntWidth &&
|
|
|
|
"Should have promoted value to int");
|
2007-08-28 09:27:00 +04:00
|
|
|
if (InitVal.getBitWidth() > IntWidth) {
|
|
|
|
llvm::APSInt V(InitVal);
|
|
|
|
V.trunc(IntWidth);
|
|
|
|
V.extend(InitVal.getBitWidth());
|
|
|
|
if (V != InitVal)
|
|
|
|
Diag(ECD->getLocation(), diag::ext_enum_value_not_int,
|
|
|
|
InitVal.toString());
|
|
|
|
}
|
2007-08-28 10:15:15 +04:00
|
|
|
|
|
|
|
// Keep track of the size of positive and negative values.
|
2008-02-26 03:33:57 +03:00
|
|
|
if (InitVal.isUnsigned() || InitVal.isNonNegative())
|
2008-01-15 00:47:29 +03:00
|
|
|
NumPositiveBits = std::max(NumPositiveBits,
|
|
|
|
(unsigned)InitVal.getActiveBits());
|
2007-08-28 10:15:15 +04:00
|
|
|
else
|
2008-01-15 00:47:29 +03:00
|
|
|
NumNegativeBits = std::max(NumNegativeBits,
|
|
|
|
(unsigned)InitVal.getMinSignedBits());
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2007-08-28 10:15:15 +04:00
|
|
|
// Keep track of whether every enum element has type int (very commmon).
|
|
|
|
if (AllElementsInt)
|
|
|
|
AllElementsInt = ECD->getType() == Context.IntTy;
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
ECD->setNextDeclarator(EltList);
|
|
|
|
EltList = ECD;
|
|
|
|
}
|
|
|
|
|
2007-08-28 10:15:15 +04:00
|
|
|
// Figure out the type that should be used for this enum.
|
|
|
|
// FIXME: Support attribute(packed) on enums and -fshort-enums.
|
|
|
|
QualType BestType;
|
2007-08-29 21:31:48 +04:00
|
|
|
unsigned BestWidth;
|
2007-08-28 10:15:15 +04:00
|
|
|
|
|
|
|
if (NumNegativeBits) {
|
|
|
|
// If there is a negative value, figure out the smallest integer type (of
|
|
|
|
// int/long/longlong) that fits.
|
2007-08-29 21:31:48 +04:00
|
|
|
if (NumNegativeBits <= IntWidth && NumPositiveBits < IntWidth) {
|
2007-08-28 10:15:15 +04:00
|
|
|
BestType = Context.IntTy;
|
2007-08-29 21:31:48 +04:00
|
|
|
BestWidth = IntWidth;
|
|
|
|
} else {
|
2008-03-05 21:54:05 +03:00
|
|
|
BestWidth = Context.Target.getLongWidth();
|
2007-12-13 01:39:36 +03:00
|
|
|
|
2007-08-29 21:31:48 +04:00
|
|
|
if (NumNegativeBits <= BestWidth && NumPositiveBits < BestWidth)
|
2007-08-28 10:15:15 +04:00
|
|
|
BestType = Context.LongTy;
|
|
|
|
else {
|
2008-03-05 21:54:05 +03:00
|
|
|
BestWidth = Context.Target.getLongLongWidth();
|
2007-12-13 01:39:36 +03:00
|
|
|
|
2007-08-29 21:31:48 +04:00
|
|
|
if (NumNegativeBits > BestWidth || NumPositiveBits >= BestWidth)
|
2007-08-28 10:15:15 +04:00
|
|
|
Diag(Enum->getLocation(), diag::warn_enum_too_large);
|
|
|
|
BestType = Context.LongLongTy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If there is no negative value, figure out which of uint, ulong, ulonglong
|
|
|
|
// fits.
|
2007-08-29 21:31:48 +04:00
|
|
|
if (NumPositiveBits <= IntWidth) {
|
2007-08-28 10:15:15 +04:00
|
|
|
BestType = Context.UnsignedIntTy;
|
2007-08-29 21:31:48 +04:00
|
|
|
BestWidth = IntWidth;
|
|
|
|
} else if (NumPositiveBits <=
|
2008-03-05 21:54:05 +03:00
|
|
|
(BestWidth = Context.Target.getLongWidth())) {
|
2007-08-28 10:15:15 +04:00
|
|
|
BestType = Context.UnsignedLongTy;
|
2008-03-05 21:54:05 +03:00
|
|
|
} else {
|
|
|
|
BestWidth = Context.Target.getLongLongWidth();
|
2007-08-29 21:31:48 +04:00
|
|
|
assert(NumPositiveBits <= BestWidth &&
|
2007-08-28 10:15:15 +04:00
|
|
|
"How could an initializer get larger than ULL?");
|
|
|
|
BestType = Context.UnsignedLongLongTy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-29 21:31:48 +04:00
|
|
|
// Loop over all of the enumerator constants, changing their types to match
|
|
|
|
// the type of the enum if needed.
|
|
|
|
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.
|
|
|
|
|
|
|
|
// Standard C says the enumerators have int type, but we allow, as an
|
|
|
|
// extension, the enumerators to be larger than int size. If each
|
|
|
|
// enumerator value fits in an int, type it as an int, otherwise type it the
|
|
|
|
// same as the enumerator decl itself. This means that in "enum { X = 1U }"
|
|
|
|
// that X has type 'int', not 'unsigned'.
|
2008-02-26 03:33:57 +03:00
|
|
|
if (ECD->getType() == Context.IntTy) {
|
|
|
|
// Make sure the init value is signed.
|
|
|
|
llvm::APSInt IV = ECD->getInitVal();
|
|
|
|
IV.setIsSigned(true);
|
|
|
|
ECD->setInitVal(IV);
|
2007-08-29 21:31:48 +04:00
|
|
|
continue; // Already int type.
|
2008-02-26 03:33:57 +03:00
|
|
|
}
|
2007-08-29 21:31:48 +04:00
|
|
|
|
|
|
|
// Determine whether the value fits into an int.
|
|
|
|
llvm::APSInt InitVal = ECD->getInitVal();
|
|
|
|
bool FitsInInt;
|
|
|
|
if (InitVal.isUnsigned() || !InitVal.isNegative())
|
|
|
|
FitsInInt = InitVal.getActiveBits() < IntWidth;
|
|
|
|
else
|
|
|
|
FitsInInt = InitVal.getMinSignedBits() <= IntWidth;
|
|
|
|
|
|
|
|
// If it fits into an integer type, force it. Otherwise force it to match
|
|
|
|
// the enum decl type.
|
|
|
|
QualType NewTy;
|
|
|
|
unsigned NewWidth;
|
|
|
|
bool NewSign;
|
|
|
|
if (FitsInInt) {
|
|
|
|
NewTy = Context.IntTy;
|
|
|
|
NewWidth = IntWidth;
|
|
|
|
NewSign = true;
|
|
|
|
} else if (ECD->getType() == BestType) {
|
|
|
|
// Already the right type!
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
NewTy = BestType;
|
|
|
|
NewWidth = BestWidth;
|
|
|
|
NewSign = BestType->isSignedIntegerType();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust the APSInt value.
|
|
|
|
InitVal.extOrTrunc(NewWidth);
|
|
|
|
InitVal.setIsSigned(NewSign);
|
|
|
|
ECD->setInitVal(InitVal);
|
|
|
|
|
|
|
|
// Adjust the Expr initializer and type.
|
|
|
|
ECD->setInitExpr(new ImplicitCastExpr(NewTy, ECD->getInitExpr()));
|
|
|
|
ECD->setType(NewTy);
|
|
|
|
}
|
2007-08-28 10:15:15 +04:00
|
|
|
|
2007-08-28 22:24:31 +04:00
|
|
|
Enum->defineElements(EltList, BestType);
|
2008-02-06 03:51:33 +03:00
|
|
|
Consumer.HandleTagDeclDefinition(Enum);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2008-02-08 03:33:21 +03:00
|
|
|
Sema::DeclTy *Sema::ActOnFileScopeAsmDecl(SourceLocation Loc,
|
|
|
|
ExprTy *expr) {
|
|
|
|
StringLiteral *AsmString = cast<StringLiteral>((Expr*)expr);
|
|
|
|
|
2008-03-16 03:16:02 +03:00
|
|
|
return FileScopeAsmDecl::Create(Context, Loc, AsmString);
|
2008-02-08 03:33:21 +03:00
|
|
|
}
|
|
|
|
|
2008-01-12 10:05:38 +03:00
|
|
|
Sema::DeclTy* Sema::ActOnLinkageSpec(SourceLocation Loc,
|
2008-02-26 00:04:36 +03:00
|
|
|
SourceLocation LBrace,
|
|
|
|
SourceLocation RBrace,
|
|
|
|
const char *Lang,
|
|
|
|
unsigned StrSize,
|
|
|
|
DeclTy *D) {
|
2008-01-12 10:05:38 +03:00
|
|
|
LinkageSpecDecl::LanguageIDs Language;
|
|
|
|
Decl *dcl = static_cast<Decl *>(D);
|
|
|
|
if (strncmp(Lang, "\"C\"", StrSize) == 0)
|
|
|
|
Language = LinkageSpecDecl::lang_c;
|
|
|
|
else if (strncmp(Lang, "\"C++\"", StrSize) == 0)
|
|
|
|
Language = LinkageSpecDecl::lang_cxx;
|
|
|
|
else {
|
|
|
|
Diag(Loc, diag::err_bad_language);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Add all the various semantics of linkage specifications
|
2008-03-16 03:16:02 +03:00
|
|
|
return LinkageSpecDecl::Create(Context, Loc, Language, dcl);
|
2008-01-12 10:05:38 +03:00
|
|
|
}
|
|
|
|
|
2008-02-21 03:48:22 +03:00
|
|
|
void Sema::HandleDeclAttribute(Decl *New, AttributeList *Attr) {
|
2007-12-19 20:43:24 +03:00
|
|
|
|
2008-02-21 03:48:22 +03:00
|
|
|
switch (Attr->getKind()) {
|
2008-02-21 02:17:35 +03:00
|
|
|
case AttributeList::AT_vector_size:
|
2007-07-11 21:01:13 +04:00
|
|
|
if (ValueDecl *vDecl = dyn_cast<ValueDecl>(New)) {
|
2008-02-21 03:48:22 +03:00
|
|
|
QualType newType = HandleVectorTypeAttribute(vDecl->getType(), Attr);
|
2007-07-11 21:01:13 +04:00
|
|
|
if (!newType.isNull()) // install the new vector type into the decl
|
|
|
|
vDecl->setType(newType);
|
|
|
|
}
|
|
|
|
if (TypedefDecl *tDecl = dyn_cast<TypedefDecl>(New)) {
|
|
|
|
QualType newType = HandleVectorTypeAttribute(tDecl->getUnderlyingType(),
|
2008-02-21 03:48:22 +03:00
|
|
|
Attr);
|
2007-07-11 21:01:13 +04:00
|
|
|
if (!newType.isNull()) // install the new vector type into the decl
|
|
|
|
tDecl->setUnderlyingType(newType);
|
|
|
|
}
|
2008-02-21 02:17:35 +03:00
|
|
|
break;
|
2008-04-19 03:10:10 +04:00
|
|
|
case AttributeList::AT_ext_vector_type:
|
2007-07-29 20:33:31 +04:00
|
|
|
if (TypedefDecl *tDecl = dyn_cast<TypedefDecl>(New))
|
2008-04-19 03:10:10 +04:00
|
|
|
HandleExtVectorTypeAttribute(tDecl, Attr);
|
2007-07-29 20:33:31 +04:00
|
|
|
else
|
2008-02-21 03:48:22 +03:00
|
|
|
Diag(Attr->getLoc(),
|
2008-04-19 03:10:10 +04:00
|
|
|
diag::err_typecheck_ext_vector_not_typedef);
|
2008-02-21 02:17:35 +03:00
|
|
|
break;
|
|
|
|
case AttributeList::AT_address_space:
|
2008-02-04 05:31:56 +03:00
|
|
|
if (TypedefDecl *tDecl = dyn_cast<TypedefDecl>(New)) {
|
|
|
|
QualType newType = HandleAddressSpaceTypeAttribute(
|
|
|
|
tDecl->getUnderlyingType(),
|
2008-02-21 03:48:22 +03:00
|
|
|
Attr);
|
|
|
|
tDecl->setUnderlyingType(newType);
|
2008-02-04 05:31:56 +03:00
|
|
|
} else if (ValueDecl *vDecl = dyn_cast<ValueDecl>(New)) {
|
|
|
|
QualType newType = HandleAddressSpaceTypeAttribute(vDecl->getType(),
|
2008-02-21 03:48:22 +03:00
|
|
|
Attr);
|
|
|
|
// install the new addr spaced type into the decl
|
|
|
|
vDecl->setType(newType);
|
2008-02-04 05:31:56 +03:00
|
|
|
}
|
2008-02-21 02:17:35 +03:00
|
|
|
break;
|
2008-02-29 19:48:43 +03:00
|
|
|
case AttributeList::AT_deprecated:
|
2008-03-03 06:28:21 +03:00
|
|
|
HandleDeprecatedAttribute(New, Attr);
|
|
|
|
break;
|
|
|
|
case AttributeList::AT_visibility:
|
|
|
|
HandleVisibilityAttribute(New, Attr);
|
|
|
|
break;
|
|
|
|
case AttributeList::AT_weak:
|
|
|
|
HandleWeakAttribute(New, Attr);
|
|
|
|
break;
|
|
|
|
case AttributeList::AT_dllimport:
|
|
|
|
HandleDLLImportAttribute(New, Attr);
|
|
|
|
break;
|
|
|
|
case AttributeList::AT_dllexport:
|
|
|
|
HandleDLLExportAttribute(New, Attr);
|
|
|
|
break;
|
|
|
|
case AttributeList::AT_nothrow:
|
|
|
|
HandleNothrowAttribute(New, Attr);
|
2008-02-29 19:48:43 +03:00
|
|
|
break;
|
2008-03-07 23:04:22 +03:00
|
|
|
case AttributeList::AT_stdcall:
|
|
|
|
HandleStdCallAttribute(New, Attr);
|
|
|
|
break;
|
|
|
|
case AttributeList::AT_fastcall:
|
|
|
|
HandleFastCallAttribute(New, Attr);
|
|
|
|
break;
|
2008-02-21 02:17:35 +03:00
|
|
|
case AttributeList::AT_aligned:
|
2008-02-21 03:48:22 +03:00
|
|
|
HandleAlignedAttribute(New, Attr);
|
2008-02-21 02:17:35 +03:00
|
|
|
break;
|
|
|
|
case AttributeList::AT_packed:
|
2008-02-21 03:48:22 +03:00
|
|
|
HandlePackedAttribute(New, Attr);
|
2008-02-21 02:17:35 +03:00
|
|
|
break;
|
2008-02-21 22:30:49 +03:00
|
|
|
case AttributeList::AT_annotate:
|
|
|
|
HandleAnnotateAttribute(New, Attr);
|
|
|
|
break;
|
2008-02-27 23:43:06 +03:00
|
|
|
case AttributeList::AT_noreturn:
|
|
|
|
HandleNoReturnAttribute(New, Attr);
|
|
|
|
break;
|
2008-03-03 06:28:21 +03:00
|
|
|
case AttributeList::AT_format:
|
|
|
|
HandleFormatAttribute(New, Attr);
|
|
|
|
break;
|
2008-04-25 13:32:00 +04:00
|
|
|
case AttributeList::AT_transparent_union:
|
|
|
|
HandleTransparentUnionAttribute(New, Attr);
|
|
|
|
break;
|
2008-02-21 02:17:35 +03:00
|
|
|
default:
|
2008-02-29 19:48:43 +03:00
|
|
|
#if 0
|
|
|
|
// TODO: when we have the full set of attributes, warn about unknown ones.
|
|
|
|
Diag(Attr->getLoc(), diag::warn_attribute_ignored,
|
|
|
|
Attr->getName()->getName());
|
|
|
|
#endif
|
2008-02-21 02:17:35 +03:00
|
|
|
break;
|
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::HandleDeclAttributes(Decl *New, AttributeList *declspec_prefix,
|
|
|
|
AttributeList *declarator_postfix) {
|
|
|
|
while (declspec_prefix) {
|
|
|
|
HandleDeclAttribute(New, declspec_prefix);
|
|
|
|
declspec_prefix = declspec_prefix->getNext();
|
|
|
|
}
|
|
|
|
while (declarator_postfix) {
|
|
|
|
HandleDeclAttribute(New, declarator_postfix);
|
|
|
|
declarator_postfix = declarator_postfix->getNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-19 03:10:10 +04:00
|
|
|
void Sema::HandleExtVectorTypeAttribute(TypedefDecl *tDecl,
|
2007-07-29 20:33:31 +04:00
|
|
|
AttributeList *rawAttr) {
|
|
|
|
QualType curType = tDecl->getUnderlyingType();
|
2007-12-19 10:19:40 +03:00
|
|
|
// check the attribute arguments.
|
2007-07-18 22:00:27 +04:00
|
|
|
if (rawAttr->getNumArgs() != 1) {
|
2008-02-21 02:25:22 +03:00
|
|
|
Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
|
2007-07-18 22:00:27 +04:00
|
|
|
std::string("1"));
|
2007-07-29 20:33:31 +04:00
|
|
|
return;
|
2007-07-18 22:00:27 +04:00
|
|
|
}
|
|
|
|
Expr *sizeExpr = static_cast<Expr *>(rawAttr->getArg(0));
|
|
|
|
llvm::APSInt vecSize(32);
|
|
|
|
if (!sizeExpr->isIntegerConstantExpr(vecSize, Context)) {
|
2008-02-21 02:25:22 +03:00
|
|
|
Diag(rawAttr->getLoc(), diag::err_attribute_argument_not_int,
|
2008-04-19 03:10:10 +04:00
|
|
|
"ext_vector_type", sizeExpr->getSourceRange());
|
2007-07-29 20:33:31 +04:00
|
|
|
return;
|
2007-07-18 22:00:27 +04:00
|
|
|
}
|
|
|
|
// unlike gcc's vector_size attribute, we do not allow vectors to be defined
|
|
|
|
// in conjunction with complex types (pointers, arrays, functions, etc.).
|
|
|
|
Type *canonType = curType.getCanonicalType().getTypePtr();
|
|
|
|
if (!(canonType->isIntegerType() || canonType->isRealFloatingType())) {
|
2008-02-21 02:25:22 +03:00
|
|
|
Diag(rawAttr->getLoc(), diag::err_attribute_invalid_vector_type,
|
2007-07-18 22:00:27 +04:00
|
|
|
curType.getCanonicalType().getAsString());
|
2007-07-29 20:33:31 +04:00
|
|
|
return;
|
2007-07-18 22:00:27 +04:00
|
|
|
}
|
|
|
|
// unlike gcc's vector_size attribute, the size is specified as the
|
|
|
|
// number of elements, not the number of bytes.
|
2007-09-04 06:45:27 +04:00
|
|
|
unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
|
2007-07-18 22:00:27 +04:00
|
|
|
|
|
|
|
if (vectorSize == 0) {
|
2008-02-21 02:25:22 +03:00
|
|
|
Diag(rawAttr->getLoc(), diag::err_attribute_zero_size,
|
2007-07-18 22:00:27 +04:00
|
|
|
sizeExpr->getSourceRange());
|
2007-07-29 20:33:31 +04:00
|
|
|
return;
|
2007-07-18 22:00:27 +04:00
|
|
|
}
|
2007-07-29 20:33:31 +04:00
|
|
|
// Instantiate/Install the vector type, the number of elements is > 0.
|
2008-04-19 03:10:10 +04:00
|
|
|
tDecl->setUnderlyingType(Context.getExtVectorType(curType, vectorSize));
|
2007-07-29 20:33:31 +04:00
|
|
|
// Remember this typedef decl, we will need it later for diagnostics.
|
2008-04-19 03:10:10 +04:00
|
|
|
ExtVectorDecls.push_back(tDecl);
|
2007-07-18 22:00:27 +04:00
|
|
|
}
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
QualType Sema::HandleVectorTypeAttribute(QualType curType,
|
2007-07-14 02:13:22 +04:00
|
|
|
AttributeList *rawAttr) {
|
2007-07-11 21:01:13 +04:00
|
|
|
// check the attribute arugments.
|
|
|
|
if (rawAttr->getNumArgs() != 1) {
|
2008-02-21 02:25:22 +03:00
|
|
|
Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
|
2007-07-11 21:01:13 +04:00
|
|
|
std::string("1"));
|
|
|
|
return QualType();
|
|
|
|
}
|
|
|
|
Expr *sizeExpr = static_cast<Expr *>(rawAttr->getArg(0));
|
|
|
|
llvm::APSInt vecSize(32);
|
2007-07-16 03:26:56 +04:00
|
|
|
if (!sizeExpr->isIntegerConstantExpr(vecSize, Context)) {
|
2008-02-21 02:25:22 +03:00
|
|
|
Diag(rawAttr->getLoc(), diag::err_attribute_argument_not_int,
|
2008-02-16 22:51:27 +03:00
|
|
|
"vector_size", sizeExpr->getSourceRange());
|
2007-07-11 21:01:13 +04:00
|
|
|
return QualType();
|
|
|
|
}
|
|
|
|
// navigate to the base type - we need to provide for vector pointers,
|
|
|
|
// vector arrays, and functions returning vectors.
|
|
|
|
Type *canonType = curType.getCanonicalType().getTypePtr();
|
|
|
|
|
2007-07-18 22:00:27 +04:00
|
|
|
if (canonType->isPointerType() || canonType->isArrayType() ||
|
|
|
|
canonType->isFunctionType()) {
|
2007-12-19 08:38:06 +03:00
|
|
|
assert(0 && "HandleVector(): Complex type construction unimplemented");
|
2007-07-18 22:00:27 +04:00
|
|
|
/* FIXME: rebuild the type from the inside out, vectorizing the inner type.
|
|
|
|
do {
|
|
|
|
if (PointerType *PT = dyn_cast<PointerType>(canonType))
|
|
|
|
canonType = PT->getPointeeType().getTypePtr();
|
|
|
|
else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
|
|
|
|
canonType = AT->getElementType().getTypePtr();
|
|
|
|
else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
|
|
|
|
canonType = FT->getResultType().getTypePtr();
|
|
|
|
} while (canonType->isPointerType() || canonType->isArrayType() ||
|
|
|
|
canonType->isFunctionType());
|
|
|
|
*/
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
// the base type must be integer or float.
|
|
|
|
if (!(canonType->isIntegerType() || canonType->isRealFloatingType())) {
|
2008-02-21 02:25:22 +03:00
|
|
|
Diag(rawAttr->getLoc(), diag::err_attribute_invalid_vector_type,
|
2007-07-11 21:01:13 +04:00
|
|
|
curType.getCanonicalType().getAsString());
|
|
|
|
return QualType();
|
|
|
|
}
|
2008-03-05 21:54:05 +03:00
|
|
|
unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(curType));
|
2007-07-11 21:01:13 +04:00
|
|
|
// vecSize is specified in bytes - convert to bits.
|
2007-09-04 06:45:27 +04:00
|
|
|
unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
// the vector size needs to be an integral multiple of the type size.
|
|
|
|
if (vectorSize % typeSize) {
|
2008-02-21 02:25:22 +03:00
|
|
|
Diag(rawAttr->getLoc(), diag::err_attribute_invalid_size,
|
2007-07-11 21:01:13 +04:00
|
|
|
sizeExpr->getSourceRange());
|
|
|
|
return QualType();
|
|
|
|
}
|
|
|
|
if (vectorSize == 0) {
|
2008-02-21 02:25:22 +03:00
|
|
|
Diag(rawAttr->getLoc(), diag::err_attribute_zero_size,
|
2007-07-11 21:01:13 +04:00
|
|
|
sizeExpr->getSourceRange());
|
|
|
|
return QualType();
|
|
|
|
}
|
2008-02-21 22:30:49 +03:00
|
|
|
// Instantiate the vector type, the number of elements is > 0, and not
|
|
|
|
// required to be a power of 2, unlike GCC.
|
2007-07-18 22:00:27 +04:00
|
|
|
return Context.getVectorType(curType, vectorSize/typeSize);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2008-02-21 02:25:22 +03:00
|
|
|
void Sema::HandlePackedAttribute(Decl *d, AttributeList *rawAttr) {
|
2008-02-16 03:29:18 +03:00
|
|
|
// check the attribute arguments.
|
|
|
|
if (rawAttr->getNumArgs() > 0) {
|
2008-02-21 02:25:22 +03:00
|
|
|
Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
|
2008-02-16 03:29:18 +03:00
|
|
|
std::string("0"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TagDecl *TD = dyn_cast<TagDecl>(d))
|
|
|
|
TD->addAttr(new PackedAttr);
|
|
|
|
else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
|
|
|
|
// If the alignment is less than or equal to 8 bits, the packed attribute
|
|
|
|
// has no effect.
|
2008-05-09 09:34:49 +04:00
|
|
|
if (!FD->getType()->isIncompleteType() &&
|
|
|
|
Context.getTypeAlign(FD->getType()) <= 8)
|
2008-02-21 02:25:22 +03:00
|
|
|
Diag(rawAttr->getLoc(),
|
2008-02-16 03:29:18 +03:00
|
|
|
diag::warn_attribute_ignored_for_field_of_type,
|
2008-02-21 02:25:22 +03:00
|
|
|
rawAttr->getName()->getName(), FD->getType().getAsString());
|
2008-02-16 03:29:18 +03:00
|
|
|
else
|
2008-02-16 03:39:40 +03:00
|
|
|
FD->addAttr(new PackedAttr);
|
2008-02-16 03:29:18 +03:00
|
|
|
} else
|
2008-02-21 02:25:22 +03:00
|
|
|
Diag(rawAttr->getLoc(), diag::warn_attribute_ignored,
|
|
|
|
rawAttr->getName()->getName());
|
2008-02-16 03:29:18 +03:00
|
|
|
}
|
2008-02-21 22:30:49 +03:00
|
|
|
|
2008-02-27 23:43:06 +03:00
|
|
|
void Sema::HandleNoReturnAttribute(Decl *d, AttributeList *rawAttr) {
|
|
|
|
// check the attribute arguments.
|
|
|
|
if (rawAttr->getNumArgs() != 0) {
|
|
|
|
Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
|
|
|
|
std::string("0"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-03-03 19:52:27 +03:00
|
|
|
FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
|
|
|
|
|
|
|
|
if (!Fn) {
|
|
|
|
Diag(rawAttr->getLoc(), diag::warn_attribute_wrong_decl_type,
|
|
|
|
"noreturn", "function");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-02-27 23:43:06 +03:00
|
|
|
d->addAttr(new NoReturnAttr());
|
|
|
|
}
|
|
|
|
|
2008-03-03 06:28:21 +03:00
|
|
|
void Sema::HandleDeprecatedAttribute(Decl *d, AttributeList *rawAttr) {
|
|
|
|
// check the attribute arguments.
|
|
|
|
if (rawAttr->getNumArgs() != 0) {
|
|
|
|
Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
|
|
|
|
std::string("0"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->addAttr(new DeprecatedAttr());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::HandleVisibilityAttribute(Decl *d, AttributeList *rawAttr) {
|
|
|
|
// check the attribute arguments.
|
2008-03-04 21:08:48 +03:00
|
|
|
if (rawAttr->getNumArgs() != 1) {
|
2008-03-03 06:28:21 +03:00
|
|
|
Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
|
|
|
|
std::string("1"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-03-04 21:08:48 +03:00
|
|
|
Expr *Arg = static_cast<Expr*>(rawAttr->getArg(0));
|
|
|
|
Arg = Arg->IgnoreParenCasts();
|
|
|
|
StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
|
|
|
|
|
|
|
|
if (Str == 0 || Str->isWide()) {
|
2008-03-03 06:28:21 +03:00
|
|
|
Diag(rawAttr->getLoc(), diag::err_attribute_argument_n_not_string,
|
2008-03-04 21:08:48 +03:00
|
|
|
"visibility", std::string("1"));
|
2008-03-03 06:28:21 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-03-04 21:08:48 +03:00
|
|
|
const char *TypeStr = Str->getStrData();
|
|
|
|
unsigned TypeLen = Str->getByteLength();
|
2008-03-03 06:28:21 +03:00
|
|
|
llvm::GlobalValue::VisibilityTypes type;
|
|
|
|
|
2008-03-04 21:08:48 +03:00
|
|
|
if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
|
2008-03-03 06:28:21 +03:00
|
|
|
type = llvm::GlobalValue::DefaultVisibility;
|
2008-03-04 21:08:48 +03:00
|
|
|
else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
|
2008-03-03 06:28:21 +03:00
|
|
|
type = llvm::GlobalValue::HiddenVisibility;
|
2008-03-04 21:08:48 +03:00
|
|
|
else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
|
2008-03-03 06:28:21 +03:00
|
|
|
type = llvm::GlobalValue::HiddenVisibility; // FIXME
|
2008-03-04 21:08:48 +03:00
|
|
|
else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
|
2008-03-03 06:28:21 +03:00
|
|
|
type = llvm::GlobalValue::ProtectedVisibility;
|
|
|
|
else {
|
|
|
|
Diag(rawAttr->getLoc(), diag::warn_attribute_type_not_supported,
|
2008-03-04 21:08:48 +03:00
|
|
|
"visibility", TypeStr);
|
2008-03-03 06:28:21 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->addAttr(new VisibilityAttr(type));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::HandleWeakAttribute(Decl *d, AttributeList *rawAttr) {
|
|
|
|
// check the attribute arguments.
|
|
|
|
if (rawAttr->getNumArgs() != 0) {
|
|
|
|
Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
|
|
|
|
std::string("0"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->addAttr(new WeakAttr());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::HandleDLLImportAttribute(Decl *d, AttributeList *rawAttr) {
|
|
|
|
// check the attribute arguments.
|
|
|
|
if (rawAttr->getNumArgs() != 0) {
|
|
|
|
Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
|
|
|
|
std::string("0"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->addAttr(new DLLImportAttr());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::HandleDLLExportAttribute(Decl *d, AttributeList *rawAttr) {
|
|
|
|
// check the attribute arguments.
|
|
|
|
if (rawAttr->getNumArgs() != 0) {
|
|
|
|
Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
|
|
|
|
std::string("0"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->addAttr(new DLLExportAttr());
|
|
|
|
}
|
|
|
|
|
2008-03-07 23:04:22 +03:00
|
|
|
void Sema::HandleStdCallAttribute(Decl *d, AttributeList *rawAttr) {
|
|
|
|
// check the attribute arguments.
|
|
|
|
if (rawAttr->getNumArgs() != 0) {
|
|
|
|
Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
|
|
|
|
std::string("0"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->addAttr(new StdCallAttr());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::HandleFastCallAttribute(Decl *d, AttributeList *rawAttr) {
|
|
|
|
// check the attribute arguments.
|
|
|
|
if (rawAttr->getNumArgs() != 0) {
|
|
|
|
Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
|
|
|
|
std::string("0"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->addAttr(new FastCallAttr());
|
|
|
|
}
|
|
|
|
|
2008-03-03 06:28:21 +03:00
|
|
|
void Sema::HandleNothrowAttribute(Decl *d, AttributeList *rawAttr) {
|
|
|
|
// check the attribute arguments.
|
|
|
|
if (rawAttr->getNumArgs() != 0) {
|
|
|
|
Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
|
|
|
|
std::string("0"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->addAttr(new NoThrowAttr());
|
|
|
|
}
|
|
|
|
|
2008-03-26 02:01:48 +03:00
|
|
|
static const FunctionTypeProto *getFunctionProto(Decl *d) {
|
2008-04-19 02:43:39 +04:00
|
|
|
QualType Ty;
|
2008-03-26 02:01:48 +03:00
|
|
|
|
2008-04-19 02:43:39 +04:00
|
|
|
if (ValueDecl *decl = dyn_cast<ValueDecl>(d))
|
|
|
|
Ty = decl->getType();
|
|
|
|
else if (FieldDecl *decl = dyn_cast<FieldDecl>(d))
|
|
|
|
Ty = decl->getType();
|
2008-05-09 21:36:24 +04:00
|
|
|
else if (TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
|
|
|
|
Ty = decl->getUnderlyingType();
|
2008-04-19 02:43:39 +04:00
|
|
|
else
|
|
|
|
return 0;
|
2008-03-26 02:01:48 +03:00
|
|
|
|
|
|
|
if (Ty->isFunctionPointerType()) {
|
|
|
|
const PointerType *PtrTy = Ty->getAsPointerType();
|
|
|
|
Ty = PtrTy->getPointeeType();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const FunctionType *FnTy = Ty->getAsFunctionType())
|
|
|
|
return dyn_cast<FunctionTypeProto>(FnTy->getAsFunctionType());
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-05-08 23:43:35 +04:00
|
|
|
static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
|
|
|
|
if (!T->isPointerType())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
T = T->getAsPointerType()->getPointeeType().getCanonicalType();
|
|
|
|
ObjCInterfaceType* ClsT = dyn_cast<ObjCInterfaceType>(T.getTypePtr());
|
|
|
|
|
|
|
|
if (!ClsT)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
|
|
|
|
|
|
|
|
// FIXME: Should we walk the chain of classes?
|
|
|
|
return ClsName == &Ctx.Idents.get("NSString") ||
|
|
|
|
ClsName == &Ctx.Idents.get("NSMutableString");
|
|
|
|
}
|
2008-03-26 02:01:48 +03:00
|
|
|
|
2008-03-07 21:43:49 +03:00
|
|
|
/// Handle __attribute__((format(type,idx,firstarg))) attributes
|
|
|
|
/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
|
2008-03-03 06:28:21 +03:00
|
|
|
void Sema::HandleFormatAttribute(Decl *d, AttributeList *rawAttr) {
|
|
|
|
|
|
|
|
if (!rawAttr->getParameterName()) {
|
|
|
|
Diag(rawAttr->getLoc(), diag::err_attribute_argument_n_not_string,
|
|
|
|
"format", std::string("1"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rawAttr->getNumArgs() != 2) {
|
|
|
|
Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
|
|
|
|
std::string("3"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-03-26 02:01:48 +03:00
|
|
|
// GCC ignores the format attribute on K&R style function
|
|
|
|
// prototypes, so we ignore it as well
|
|
|
|
const FunctionTypeProto *proto = getFunctionProto(d);
|
|
|
|
|
|
|
|
if (!proto) {
|
2008-03-03 06:28:21 +03:00
|
|
|
Diag(rawAttr->getLoc(), diag::warn_attribute_wrong_decl_type,
|
|
|
|
"format", "function");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: in C++ the implicit 'this' function parameter also counts.
|
2008-03-07 21:43:49 +03:00
|
|
|
// this is needed in order to be compatible with GCC
|
2008-03-03 06:28:21 +03:00
|
|
|
// the index must start in 1 and the limit is numargs+1
|
2008-03-26 02:01:48 +03:00
|
|
|
unsigned NumArgs = proto->getNumArgs();
|
2008-03-07 21:43:49 +03:00
|
|
|
unsigned FirstIdx = 1;
|
2008-03-03 06:28:21 +03:00
|
|
|
|
|
|
|
const char *Format = rawAttr->getParameterName()->getName();
|
|
|
|
unsigned FormatLen = rawAttr->getParameterName()->getLength();
|
|
|
|
|
|
|
|
// Normalize the argument, __foo__ becomes foo.
|
|
|
|
if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
|
|
|
|
Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
|
|
|
|
Format += 2;
|
|
|
|
FormatLen -= 4;
|
|
|
|
}
|
|
|
|
|
2008-05-08 23:43:35 +04:00
|
|
|
bool Supported = false;
|
|
|
|
bool is_NSString = false;
|
|
|
|
bool is_strftime = false;
|
|
|
|
|
|
|
|
switch (FormatLen) {
|
|
|
|
default: break;
|
|
|
|
case 5:
|
|
|
|
Supported = !memcmp(Format, "scanf", 5);
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
Supported = !memcmp(Format, "printf", 6);
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
Supported = !memcmp(Format, "strfmon", 7);
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
|
|
|
|
(is_NSString = !memcmp(Format, "NSString", 8));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Supported) {
|
2008-03-03 06:28:21 +03:00
|
|
|
Diag(rawAttr->getLoc(), diag::warn_attribute_type_not_supported,
|
|
|
|
"format", rawAttr->getParameterName()->getName());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-03-07 21:43:49 +03:00
|
|
|
// checks for the 2nd argument
|
2008-03-03 06:28:21 +03:00
|
|
|
Expr *IdxExpr = static_cast<Expr *>(rawAttr->getArg(0));
|
2008-03-07 21:43:49 +03:00
|
|
|
llvm::APSInt Idx(Context.getTypeSize(IdxExpr->getType()));
|
2008-03-03 06:28:21 +03:00
|
|
|
if (!IdxExpr->isIntegerConstantExpr(Idx, Context)) {
|
|
|
|
Diag(rawAttr->getLoc(), diag::err_attribute_argument_n_not_int,
|
|
|
|
"format", std::string("2"), IdxExpr->getSourceRange());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-03-07 21:43:49 +03:00
|
|
|
if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
|
2008-03-03 06:28:21 +03:00
|
|
|
Diag(rawAttr->getLoc(), diag::err_attribute_argument_out_of_bounds,
|
|
|
|
"format", std::string("2"), IdxExpr->getSourceRange());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-05-08 23:43:35 +04:00
|
|
|
// FIXME: Do we need to bounds check?
|
|
|
|
unsigned ArgIdx = Idx.getZExtValue() - 1;
|
|
|
|
|
2008-03-07 21:43:49 +03:00
|
|
|
// make sure the format string is really a string
|
2008-05-08 23:43:35 +04:00
|
|
|
QualType Ty = proto->getArgType(ArgIdx);
|
|
|
|
|
|
|
|
if (is_NSString) {
|
|
|
|
// FIXME: do we need to check if the type is NSString*? What are
|
|
|
|
// the semantics?
|
|
|
|
if (!isNSStringType(Ty, Context)) {
|
|
|
|
// FIXME: Should highlight the actual expression that has the
|
|
|
|
// wrong type.
|
|
|
|
Diag(rawAttr->getLoc(), diag::err_format_attribute_not_NSString,
|
|
|
|
IdxExpr->getSourceRange());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!Ty->isPointerType() ||
|
2008-03-07 21:43:49 +03:00
|
|
|
!Ty->getAsPointerType()->getPointeeType()->isCharType()) {
|
2008-05-08 23:43:35 +04:00
|
|
|
// FIXME: Should highlight the actual expression that has the
|
|
|
|
// wrong type.
|
2008-03-07 21:43:49 +03:00
|
|
|
Diag(rawAttr->getLoc(), diag::err_format_attribute_not_string,
|
|
|
|
IdxExpr->getSourceRange());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the 3rd argument
|
2008-03-03 06:28:21 +03:00
|
|
|
Expr *FirstArgExpr = static_cast<Expr *>(rawAttr->getArg(1));
|
2008-03-07 21:43:49 +03:00
|
|
|
llvm::APSInt FirstArg(Context.getTypeSize(FirstArgExpr->getType()));
|
2008-03-03 06:28:21 +03:00
|
|
|
if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, Context)) {
|
|
|
|
Diag(rawAttr->getLoc(), diag::err_attribute_argument_n_not_int,
|
|
|
|
"format", std::string("3"), FirstArgExpr->getSourceRange());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-03-07 21:43:49 +03:00
|
|
|
// check if the function is variadic if the 3rd argument non-zero
|
|
|
|
if (FirstArg != 0) {
|
|
|
|
if (proto->isVariadic()) {
|
|
|
|
++NumArgs; // +1 for ...
|
|
|
|
} else {
|
|
|
|
Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// strftime requires FirstArg to be 0 because it doesn't read from any variable
|
|
|
|
// the input is just the current time + the format string
|
2008-05-08 23:43:35 +04:00
|
|
|
if (is_strftime) {
|
2008-03-07 21:43:49 +03:00
|
|
|
if (FirstArg != 0) {
|
2008-03-03 06:28:21 +03:00
|
|
|
Diag(rawAttr->getLoc(), diag::err_format_strftime_third_parameter,
|
|
|
|
FirstArgExpr->getSourceRange());
|
|
|
|
return;
|
|
|
|
}
|
2008-03-07 21:43:49 +03:00
|
|
|
// if 0 it disables parameter checking (to use with e.g. va_list)
|
|
|
|
} else if (FirstArg != 0 && FirstArg != NumArgs) {
|
2008-03-03 06:28:21 +03:00
|
|
|
Diag(rawAttr->getLoc(), diag::err_attribute_argument_out_of_bounds,
|
|
|
|
"format", std::string("3"), FirstArgExpr->getSourceRange());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->addAttr(new FormatAttr(std::string(Format, FormatLen),
|
|
|
|
Idx.getZExtValue(), FirstArg.getZExtValue()));
|
|
|
|
}
|
|
|
|
|
2008-04-25 13:32:00 +04:00
|
|
|
void Sema::HandleTransparentUnionAttribute(Decl *d, AttributeList *rawAttr) {
|
|
|
|
// check the attribute arguments.
|
|
|
|
if (rawAttr->getNumArgs() != 0) {
|
|
|
|
Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
|
|
|
|
std::string("0"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TypeDecl *decl = dyn_cast<TypeDecl>(d);
|
|
|
|
|
|
|
|
if (!decl || !Context.getTypeDeclType(decl)->isUnionType()) {
|
|
|
|
Diag(rawAttr->getLoc(), diag::warn_attribute_wrong_decl_type,
|
|
|
|
"transparent_union", "union");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-30 20:04:01 +04:00
|
|
|
//QualType QTy = Context.getTypeDeclType(decl);
|
|
|
|
//const RecordType *Ty = QTy->getAsUnionType();
|
2008-04-25 13:32:00 +04:00
|
|
|
|
|
|
|
// FIXME
|
|
|
|
// Ty->addAttr(new TransparentUnionAttr());
|
|
|
|
}
|
|
|
|
|
2008-02-21 22:30:49 +03:00
|
|
|
void Sema::HandleAnnotateAttribute(Decl *d, AttributeList *rawAttr) {
|
|
|
|
// check the attribute arguments.
|
|
|
|
if (rawAttr->getNumArgs() != 1) {
|
|
|
|
Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
|
|
|
|
std::string("1"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Expr *argExpr = static_cast<Expr *>(rawAttr->getArg(0));
|
|
|
|
StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
|
2008-02-16 03:29:18 +03:00
|
|
|
|
2008-02-21 22:30:49 +03:00
|
|
|
// Make sure that there is a string literal as the annotation's single
|
|
|
|
// argument.
|
|
|
|
if (!SE) {
|
|
|
|
Diag(rawAttr->getLoc(), diag::err_attribute_annotate_no_string);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
d->addAttr(new AnnotateAttr(std::string(SE->getStrData(),
|
|
|
|
SE->getByteLength())));
|
|
|
|
}
|
|
|
|
|
2007-12-19 10:19:40 +03:00
|
|
|
void Sema::HandleAlignedAttribute(Decl *d, AttributeList *rawAttr)
|
|
|
|
{
|
|
|
|
// check the attribute arguments.
|
2008-01-30 20:38:42 +03:00
|
|
|
if (rawAttr->getNumArgs() > 1) {
|
2008-02-21 02:25:22 +03:00
|
|
|
Diag(rawAttr->getLoc(), diag::err_attribute_wrong_number_arguments,
|
2007-12-19 10:19:40 +03:00
|
|
|
std::string("1"));
|
|
|
|
return;
|
|
|
|
}
|
2008-01-30 20:38:42 +03:00
|
|
|
|
2008-02-16 22:51:27 +03:00
|
|
|
unsigned Align = 0;
|
|
|
|
|
|
|
|
if (rawAttr->getNumArgs() == 0) {
|
|
|
|
// FIXME: This should be the target specific maximum alignment.
|
|
|
|
// (For now we just use 128 bits which is the maximum on X86.
|
|
|
|
Align = 128;
|
2008-01-30 20:38:42 +03:00
|
|
|
return;
|
2008-02-16 22:51:27 +03:00
|
|
|
} else {
|
|
|
|
Expr *alignmentExpr = static_cast<Expr *>(rawAttr->getArg(0));
|
|
|
|
llvm::APSInt alignment(32);
|
|
|
|
if (!alignmentExpr->isIntegerConstantExpr(alignment, Context)) {
|
2008-02-21 02:25:22 +03:00
|
|
|
Diag(rawAttr->getLoc(), diag::err_attribute_argument_not_int,
|
2008-02-16 22:51:27 +03:00
|
|
|
"aligned", alignmentExpr->getSourceRange());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Align = alignment.getZExtValue() * 8;
|
|
|
|
}
|
2008-01-30 20:38:42 +03:00
|
|
|
|
2008-02-16 22:51:27 +03:00
|
|
|
d->addAttr(new AlignedAttr(Align));
|
2007-12-19 10:19:40 +03:00
|
|
|
}
|