2010-01-10 20:58:08 +08:00
|
|
|
//===-- TargetAttributesSema.cpp - Encapsulate target attributes-*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains semantic analysis implementation for target-specific
|
|
|
|
// attributes.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "TargetAttributesSema.h"
|
2010-08-25 15:42:41 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
|
|
|
#include "clang/Sema/SemaInternal.h"
|
2010-01-10 20:58:08 +08:00
|
|
|
#include "llvm/ADT/Triple.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
TargetAttributesSema::~TargetAttributesSema() {}
|
|
|
|
bool TargetAttributesSema::ProcessDeclAttribute(Scope *scope, Decl *D,
|
|
|
|
const AttributeList &Attr, Sema &S) const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void HandleMSP430InterruptAttr(Decl *d,
|
|
|
|
const AttributeList &Attr, Sema &S) {
|
|
|
|
// Check the attribute arguments.
|
|
|
|
if (Attr.getNumArgs() != 1) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Check for decl - it should be void ()(void).
|
|
|
|
|
|
|
|
Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
|
|
|
|
llvm::APSInt NumParams(32);
|
|
|
|
if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
|
|
|
|
<< "interrupt" << NumParamsExpr->getSourceRange();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Num = NumParams.getLimitedValue(255);
|
|
|
|
if ((Num & 1) || Num > 30) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
|
|
|
|
<< "interrupt" << (int)NumParams.getSExtValue()
|
|
|
|
<< NumParamsExpr->getSourceRange();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-19 07:23:40 +08:00
|
|
|
d->addAttr(::new (S.Context) MSP430InterruptAttr(Attr.getLoc(), S.Context, Num));
|
|
|
|
d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
|
2010-01-10 20:58:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class MSP430AttributesSema : public TargetAttributesSema {
|
|
|
|
public:
|
|
|
|
MSP430AttributesSema() { }
|
|
|
|
bool ProcessDeclAttribute(Scope *scope, Decl *D,
|
|
|
|
const AttributeList &Attr, Sema &S) const {
|
|
|
|
if (Attr.getName()->getName() == "interrupt") {
|
|
|
|
HandleMSP430InterruptAttr(D, Attr, S);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2010-12-20 03:57:51 +08:00
|
|
|
static void HandleMBlazeInterruptHandlerAttr(Decl *d, const AttributeList &Attr,
|
|
|
|
Sema &S) {
|
|
|
|
// Check the attribute arguments.
|
|
|
|
if (Attr.getNumArgs() != 0) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Check for decl - it should be void ()(void).
|
|
|
|
|
|
|
|
d->addAttr(::new (S.Context) MBlazeInterruptHandlerAttr(Attr.getLoc(),
|
|
|
|
S.Context));
|
|
|
|
d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void HandleMBlazeSaveVolatilesAttr(Decl *d, const AttributeList &Attr,
|
|
|
|
Sema &S) {
|
|
|
|
// Check the attribute arguments.
|
|
|
|
if (Attr.getNumArgs() != 0) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Check for decl - it should be void ()(void).
|
|
|
|
|
|
|
|
d->addAttr(::new (S.Context) MBlazeSaveVolatilesAttr(Attr.getLoc(),
|
|
|
|
S.Context));
|
|
|
|
d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class MBlazeAttributesSema : public TargetAttributesSema {
|
|
|
|
public:
|
|
|
|
MBlazeAttributesSema() { }
|
|
|
|
bool ProcessDeclAttribute(Scope *scope, Decl *D, const AttributeList &Attr,
|
|
|
|
Sema &S) const {
|
|
|
|
if (Attr.getName()->getName() == "interrupt_handler") {
|
|
|
|
HandleMBlazeInterruptHandlerAttr(D, Attr, S);
|
|
|
|
return true;
|
|
|
|
} else if (Attr.getName()->getName() == "save_volatiles") {
|
|
|
|
HandleMBlazeSaveVolatilesAttr(D, Attr, S);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2010-02-11 07:06:52 +08:00
|
|
|
static void HandleX86ForceAlignArgPointerAttr(Decl *D,
|
|
|
|
const AttributeList& Attr,
|
|
|
|
Sema &S) {
|
|
|
|
// Check the attribute arguments.
|
|
|
|
if (Attr.getNumArgs() != 0) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-02-17 08:44:47 +08:00
|
|
|
// If we try to apply it to a function pointer, don't warn, but don't
|
|
|
|
// do anything, either. It doesn't matter anyway, because there's nothing
|
|
|
|
// special about calling a force_align_arg_pointer function.
|
2010-02-18 12:39:19 +08:00
|
|
|
ValueDecl *VD = dyn_cast<ValueDecl>(D);
|
2010-02-17 08:44:47 +08:00
|
|
|
if (VD && VD->getType()->isFunctionPointerType())
|
2010-02-11 07:06:52 +08:00
|
|
|
return;
|
2010-02-18 12:39:19 +08:00
|
|
|
// Also don't warn on function pointer typedefs.
|
2011-04-15 22:24:37 +08:00
|
|
|
TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
|
2010-02-18 12:56:59 +08:00
|
|
|
if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
|
|
|
|
TD->getUnderlyingType()->isFunctionType()))
|
2010-02-18 12:39:19 +08:00
|
|
|
return;
|
2010-02-11 07:06:52 +08:00
|
|
|
// Attribute can only be applied to function types.
|
2010-02-11 07:26:12 +08:00
|
|
|
if (!isa<FunctionDecl>(D)) {
|
2010-02-11 07:06:52 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
|
|
|
<< Attr.getName() << /* function */0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-14 00:05:58 +08:00
|
|
|
D->addAttr(::new (S.Context) X86ForceAlignArgPointerAttr(Attr.getRange(),
|
|
|
|
S.Context));
|
2010-02-11 07:06:52 +08:00
|
|
|
}
|
|
|
|
|
2013-01-25 00:46:58 +08:00
|
|
|
DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
|
|
|
|
unsigned AttrSpellingListIndex) {
|
2012-05-10 10:50:16 +08:00
|
|
|
if (D->hasAttr<DLLExportAttr>()) {
|
|
|
|
Diag(Range.getBegin(), diag::warn_attribute_ignored) << "dllimport";
|
2012-05-13 11:25:18 +08:00
|
|
|
return NULL;
|
2012-05-10 10:50:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (D->hasAttr<DLLImportAttr>())
|
2012-05-13 11:25:18 +08:00
|
|
|
return NULL;
|
2012-05-10 10:50:16 +08:00
|
|
|
|
2013-05-21 05:53:29 +08:00
|
|
|
if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
|
|
|
|
if (VD->hasDefinition()) {
|
|
|
|
// dllimport cannot be applied to definitions.
|
|
|
|
Diag(D->getLocation(), diag::warn_attribute_invalid_on_definition)
|
|
|
|
<< "dllimport";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-25 00:46:58 +08:00
|
|
|
return ::new (Context) DLLImportAttr(Range, Context,
|
|
|
|
AttrSpellingListIndex);
|
2012-05-10 10:50:16 +08:00
|
|
|
}
|
|
|
|
|
2010-02-17 02:27:26 +08:00
|
|
|
static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
|
|
|
|
// check the attribute arguments.
|
|
|
|
if (Attr.getNumArgs() != 0) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attribute can be applied only to functions or variables.
|
|
|
|
FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
|
2012-05-10 10:50:16 +08:00
|
|
|
if (!FD && !isa<VarDecl>(D)) {
|
2010-02-21 13:12:56 +08:00
|
|
|
// Apparently Visual C++ thinks it is okay to not emit a warning
|
|
|
|
// in this case, so only emit a warning when -fms-extensions is not
|
|
|
|
// specified.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (!S.getLangOpts().MicrosoftExt)
|
2010-02-21 13:12:56 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
|
|
|
<< Attr.getName() << 2 /*variable and function*/;
|
2010-02-17 02:27:26 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Currently, the dllimport attribute is ignored for inlined functions.
|
|
|
|
// Warning is emitted.
|
2012-05-10 10:50:16 +08:00
|
|
|
if (FD && FD->isInlineSpecified()) {
|
2010-02-17 02:27:26 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-25 00:46:58 +08:00
|
|
|
unsigned Index = Attr.getAttributeSpellingListIndex();
|
|
|
|
DLLImportAttr *NewAttr = S.mergeDLLImportAttr(D, Attr.getRange(), Index);
|
2012-05-13 11:25:18 +08:00
|
|
|
if (NewAttr)
|
|
|
|
D->addAttr(NewAttr);
|
2012-05-10 10:50:16 +08:00
|
|
|
}
|
|
|
|
|
2013-01-25 00:46:58 +08:00
|
|
|
DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
|
|
|
|
unsigned AttrSpellingListIndex) {
|
2012-05-10 10:50:16 +08:00
|
|
|
if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
|
|
|
|
Diag(Import->getLocation(), diag::warn_attribute_ignored) << "dllimport";
|
|
|
|
D->dropAttr<DLLImportAttr>();
|
2010-02-17 02:27:26 +08:00
|
|
|
}
|
|
|
|
|
2012-05-10 10:50:16 +08:00
|
|
|
if (D->hasAttr<DLLExportAttr>())
|
2012-05-13 11:25:18 +08:00
|
|
|
return NULL;
|
2012-05-10 10:50:16 +08:00
|
|
|
|
2013-01-25 00:46:58 +08:00
|
|
|
return ::new (Context) DLLExportAttr(Range, Context,
|
|
|
|
AttrSpellingListIndex);
|
2010-02-17 02:27:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
|
|
|
|
// check the attribute arguments.
|
|
|
|
if (Attr.getNumArgs() != 0) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attribute can be applied only to functions or variables.
|
|
|
|
FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
|
2012-05-10 10:50:16 +08:00
|
|
|
if (!FD && !isa<VarDecl>(D)) {
|
2010-02-17 02:27:26 +08:00
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
|
|
|
|
<< Attr.getName() << 2 /*variable and function*/;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Currently, the dllexport attribute is ignored for inlined functions, unless
|
|
|
|
// the -fkeep-inline-functions flag has been used. Warning is emitted;
|
2012-05-10 10:50:16 +08:00
|
|
|
if (FD && FD->isInlineSpecified()) {
|
2010-02-17 02:27:26 +08:00
|
|
|
// FIXME: ... unless the -fkeep-inline-functions flag has been used.
|
|
|
|
S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-25 00:46:58 +08:00
|
|
|
unsigned Index = Attr.getAttributeSpellingListIndex();
|
|
|
|
DLLExportAttr *NewAttr = S.mergeDLLExportAttr(D, Attr.getRange(), Index);
|
2012-05-13 11:25:18 +08:00
|
|
|
if (NewAttr)
|
|
|
|
D->addAttr(NewAttr);
|
2010-02-17 02:27:26 +08:00
|
|
|
}
|
|
|
|
|
2010-02-11 07:06:52 +08:00
|
|
|
namespace {
|
|
|
|
class X86AttributesSema : public TargetAttributesSema {
|
|
|
|
public:
|
|
|
|
X86AttributesSema() { }
|
|
|
|
bool ProcessDeclAttribute(Scope *scope, Decl *D,
|
|
|
|
const AttributeList &Attr, Sema &S) const {
|
2011-09-02 08:18:52 +08:00
|
|
|
const llvm::Triple &Triple(S.Context.getTargetInfo().getTriple());
|
2010-02-17 02:27:26 +08:00
|
|
|
if (Triple.getOS() == llvm::Triple::Win32 ||
|
2011-02-17 16:51:38 +08:00
|
|
|
Triple.getOS() == llvm::Triple::MinGW32) {
|
2010-02-17 02:27:26 +08:00
|
|
|
switch (Attr.getKind()) {
|
2012-06-20 07:57:03 +08:00
|
|
|
case AttributeList::AT_DLLImport: HandleDLLImportAttr(D, Attr, S);
|
2010-02-17 02:27:26 +08:00
|
|
|
return true;
|
2012-06-20 07:57:03 +08:00
|
|
|
case AttributeList::AT_DLLExport: HandleDLLExportAttr(D, Attr, S);
|
2010-02-17 02:27:26 +08:00
|
|
|
return true;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
2011-10-01 02:53:25 +08:00
|
|
|
if (Triple.getArch() != llvm::Triple::x86_64 &&
|
2011-10-01 04:32:22 +08:00
|
|
|
(Attr.getName()->getName() == "force_align_arg_pointer" ||
|
|
|
|
Attr.getName()->getName() == "__force_align_arg_pointer__")) {
|
2010-02-11 07:06:52 +08:00
|
|
|
HandleX86ForceAlignArgPointerAttr(D, Attr, S);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-01-17 01:10:28 +08:00
|
|
|
static void HandleMips16Attr(Decl *D, const AttributeList &Attr, Sema &S) {
|
|
|
|
// check the attribute arguments.
|
|
|
|
if (Attr.hasParameterOrArguments()) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Attribute can only be applied to function types.
|
|
|
|
if (!isa<FunctionDecl>(D)) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
|
|
|
|
<< Attr.getName() << /* function */0;
|
|
|
|
return;
|
|
|
|
}
|
2013-01-25 00:46:58 +08:00
|
|
|
D->addAttr(::new (S.Context) Mips16Attr(Attr.getRange(), S.Context,
|
|
|
|
Attr.getAttributeSpellingListIndex()));
|
2013-01-17 01:10:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void HandleNoMips16Attr(Decl *D, const AttributeList &Attr, Sema &S) {
|
|
|
|
// check the attribute arguments.
|
|
|
|
if (Attr.hasParameterOrArguments()) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Attribute can only be applied to function types.
|
|
|
|
if (!isa<FunctionDecl>(D)) {
|
|
|
|
S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
|
|
|
|
<< Attr.getName() << /* function */0;
|
|
|
|
return;
|
|
|
|
}
|
2013-01-25 00:46:58 +08:00
|
|
|
D->addAttr(::new (S.Context)
|
|
|
|
NoMips16Attr(Attr.getRange(), S.Context,
|
|
|
|
Attr.getAttributeSpellingListIndex()));
|
2013-01-17 01:10:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class MipsAttributesSema : public TargetAttributesSema {
|
|
|
|
public:
|
|
|
|
MipsAttributesSema() { }
|
|
|
|
bool ProcessDeclAttribute(Scope *scope, Decl *D, const AttributeList &Attr,
|
|
|
|
Sema &S) const {
|
|
|
|
if (Attr.getName()->getName() == "mips16") {
|
|
|
|
HandleMips16Attr(D, Attr, S);
|
|
|
|
return true;
|
|
|
|
} else if (Attr.getName()->getName() == "nomips16") {
|
|
|
|
HandleNoMips16Attr(D, Attr, S);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2010-01-10 20:58:08 +08:00
|
|
|
const TargetAttributesSema &Sema::getTargetAttributesSema() const {
|
|
|
|
if (TheTargetAttributesSema)
|
|
|
|
return *TheTargetAttributesSema;
|
|
|
|
|
2011-09-02 08:18:52 +08:00
|
|
|
const llvm::Triple &Triple(Context.getTargetInfo().getTriple());
|
2010-01-10 20:58:08 +08:00
|
|
|
switch (Triple.getArch()) {
|
|
|
|
case llvm::Triple::msp430:
|
|
|
|
return *(TheTargetAttributesSema = new MSP430AttributesSema);
|
2010-12-20 03:57:51 +08:00
|
|
|
case llvm::Triple::mblaze:
|
|
|
|
return *(TheTargetAttributesSema = new MBlazeAttributesSema);
|
2010-02-11 07:06:52 +08:00
|
|
|
case llvm::Triple::x86:
|
2011-10-01 02:53:25 +08:00
|
|
|
case llvm::Triple::x86_64:
|
2010-02-11 07:06:52 +08:00
|
|
|
return *(TheTargetAttributesSema = new X86AttributesSema);
|
2013-01-17 01:10:28 +08:00
|
|
|
case llvm::Triple::mips:
|
|
|
|
case llvm::Triple::mipsel:
|
|
|
|
return *(TheTargetAttributesSema = new MipsAttributesSema);
|
2011-10-01 02:53:25 +08:00
|
|
|
default:
|
|
|
|
return *(TheTargetAttributesSema = new TargetAttributesSema);
|
2010-01-10 20:58:08 +08:00
|
|
|
}
|
|
|
|
}
|