Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1 | //===-- TargetAttributesSema.cpp - Encapsulate target attributes-*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains semantic analysis implementation for target-specific |
| 11 | // attributes. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "Sema.h" |
| 16 | #include "TargetAttributesSema.h" |
| 17 | #include "clang/Basic/TargetInfo.h" |
| 18 | #include "llvm/ADT/Triple.h" |
| 19 | |
| 20 | using namespace clang; |
| 21 | |
| 22 | TargetAttributesSema::~TargetAttributesSema() {} |
| 23 | bool TargetAttributesSema::ProcessDeclAttribute(Scope *scope, Decl *D, |
| 24 | const AttributeList &Attr, Sema &S) const { |
| 25 | return false; |
| 26 | } |
| 27 | |
| 28 | static void HandleMSP430InterruptAttr(Decl *d, |
| 29 | const AttributeList &Attr, Sema &S) { |
| 30 | // Check the attribute arguments. |
| 31 | if (Attr.getNumArgs() != 1) { |
| 32 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | // FIXME: Check for decl - it should be void ()(void). |
| 37 | |
| 38 | Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 39 | llvm::APSInt NumParams(32); |
| 40 | if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) { |
| 41 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 42 | << "interrupt" << NumParamsExpr->getSourceRange(); |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | unsigned Num = NumParams.getLimitedValue(255); |
| 47 | if ((Num & 1) || Num > 30) { |
| 48 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 49 | << "interrupt" << (int)NumParams.getSExtValue() |
| 50 | << NumParamsExpr->getSourceRange(); |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | d->addAttr(::new (S.Context) MSP430InterruptAttr(Num)); |
| 55 | d->addAttr(::new (S.Context) UsedAttr()); |
| 56 | } |
| 57 | |
| 58 | namespace { |
| 59 | class MSP430AttributesSema : public TargetAttributesSema { |
| 60 | public: |
| 61 | MSP430AttributesSema() { } |
| 62 | bool ProcessDeclAttribute(Scope *scope, Decl *D, |
| 63 | const AttributeList &Attr, Sema &S) const { |
| 64 | if (Attr.getName()->getName() == "interrupt") { |
| 65 | HandleMSP430InterruptAttr(D, Attr, S); |
| 66 | return true; |
| 67 | } |
| 68 | return false; |
| 69 | } |
| 70 | }; |
| 71 | } |
| 72 | |
Charles Davis | 5a0164d | 2010-02-10 23:06:52 +0000 | [diff] [blame] | 73 | static void HandleX86ForceAlignArgPointerAttr(Decl *D, |
| 74 | const AttributeList& Attr, |
| 75 | Sema &S) { |
| 76 | // Check the attribute arguments. |
| 77 | if (Attr.getNumArgs() != 0) { |
| 78 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 79 | return; |
| 80 | } |
| 81 | |
Charles Davis | ab44216 | 2010-02-17 00:44:47 +0000 | [diff] [blame] | 82 | // If we try to apply it to a function pointer, don't warn, but don't |
| 83 | // do anything, either. It doesn't matter anyway, because there's nothing |
| 84 | // special about calling a force_align_arg_pointer function. |
Charles Davis | beaf5ed | 2010-02-18 04:39:19 +0000 | [diff] [blame^] | 85 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Charles Davis | ab44216 | 2010-02-17 00:44:47 +0000 | [diff] [blame] | 86 | if (VD && VD->getType()->isFunctionPointerType()) |
Charles Davis | 5a0164d | 2010-02-10 23:06:52 +0000 | [diff] [blame] | 87 | return; |
Charles Davis | beaf5ed | 2010-02-18 04:39:19 +0000 | [diff] [blame^] | 88 | // Also don't warn on function pointer typedefs. |
| 89 | TypedefDecl *TD = dyn_cast<TypedefDecl>(D); |
| 90 | if (TD && TD->getUnderlyingType()->isFunctionPointerType()) |
| 91 | return; |
Charles Davis | 5a0164d | 2010-02-10 23:06:52 +0000 | [diff] [blame] | 92 | // Attribute can only be applied to function types. |
Charles Davis | 9c00be5 | 2010-02-10 23:26:12 +0000 | [diff] [blame] | 93 | if (!isa<FunctionDecl>(D)) { |
Charles Davis | 5a0164d | 2010-02-10 23:06:52 +0000 | [diff] [blame] | 94 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 95 | << Attr.getName() << /* function */0; |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | D->addAttr(::new (S.Context) X86ForceAlignArgPointerAttr()); |
| 100 | } |
| 101 | |
Charles Davis | f0122fe | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 102 | static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
| 103 | // check the attribute arguments. |
| 104 | if (Attr.getNumArgs() != 0) { |
| 105 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | // Attribute can be applied only to functions or variables. |
| 110 | if (isa<VarDecl>(D)) { |
| 111 | D->addAttr(::new (S.Context) DLLImportAttr()); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 116 | if (!FD) { |
| 117 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 118 | << Attr.getName() << 2 /*variable and function*/; |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | // Currently, the dllimport attribute is ignored for inlined functions. |
| 123 | // Warning is emitted. |
| 124 | if (FD->isInlineSpecified()) { |
| 125 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport"; |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | // The attribute is also overridden by a subsequent declaration as dllexport. |
| 130 | // Warning is emitted. |
| 131 | for (AttributeList *nextAttr = Attr.getNext(); nextAttr; |
| 132 | nextAttr = nextAttr->getNext()) { |
| 133 | if (nextAttr->getKind() == AttributeList::AT_dllexport) { |
| 134 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport"; |
| 135 | return; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | if (D->getAttr<DLLExportAttr>()) { |
| 140 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport"; |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | D->addAttr(::new (S.Context) DLLImportAttr()); |
| 145 | } |
| 146 | |
| 147 | static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
| 148 | // check the attribute arguments. |
| 149 | if (Attr.getNumArgs() != 0) { |
| 150 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | // Attribute can be applied only to functions or variables. |
| 155 | if (isa<VarDecl>(D)) { |
| 156 | D->addAttr(::new (S.Context) DLLExportAttr()); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 161 | if (!FD) { |
| 162 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 163 | << Attr.getName() << 2 /*variable and function*/; |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | // Currently, the dllexport attribute is ignored for inlined functions, unless |
| 168 | // the -fkeep-inline-functions flag has been used. Warning is emitted; |
| 169 | if (FD->isInlineSpecified()) { |
| 170 | // FIXME: ... unless the -fkeep-inline-functions flag has been used. |
| 171 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport"; |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | D->addAttr(::new (S.Context) DLLExportAttr()); |
| 176 | } |
| 177 | |
Charles Davis | 5a0164d | 2010-02-10 23:06:52 +0000 | [diff] [blame] | 178 | namespace { |
| 179 | class X86AttributesSema : public TargetAttributesSema { |
| 180 | public: |
| 181 | X86AttributesSema() { } |
| 182 | bool ProcessDeclAttribute(Scope *scope, Decl *D, |
| 183 | const AttributeList &Attr, Sema &S) const { |
Charles Davis | f0122fe | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 184 | const llvm::Triple &Triple(S.Context.Target.getTriple()); |
| 185 | if (Triple.getOS() == llvm::Triple::Win32 || |
| 186 | Triple.getOS() == llvm::Triple::MinGW32 || |
| 187 | Triple.getOS() == llvm::Triple::MinGW64) { |
| 188 | switch (Attr.getKind()) { |
| 189 | case AttributeList::AT_dllimport: HandleDLLImportAttr(D, Attr, S); |
| 190 | return true; |
| 191 | case AttributeList::AT_dllexport: HandleDLLExportAttr(D, Attr, S); |
| 192 | return true; |
| 193 | default: break; |
| 194 | } |
| 195 | } |
Charles Davis | beaf5ed | 2010-02-18 04:39:19 +0000 | [diff] [blame^] | 196 | if (Attr.getName()->getName() == "force_align_arg_pointer" || |
| 197 | Attr.getName()->getName() == "__force_align_arg_pointer__") { |
Charles Davis | 5a0164d | 2010-02-10 23:06:52 +0000 | [diff] [blame] | 198 | HandleX86ForceAlignArgPointerAttr(D, Attr, S); |
| 199 | return true; |
| 200 | } |
| 201 | return false; |
| 202 | } |
| 203 | }; |
| 204 | } |
| 205 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 206 | const TargetAttributesSema &Sema::getTargetAttributesSema() const { |
| 207 | if (TheTargetAttributesSema) |
| 208 | return *TheTargetAttributesSema; |
| 209 | |
| 210 | const llvm::Triple &Triple(Context.Target.getTriple()); |
| 211 | switch (Triple.getArch()) { |
| 212 | default: |
| 213 | return *(TheTargetAttributesSema = new TargetAttributesSema); |
| 214 | |
| 215 | case llvm::Triple::msp430: |
| 216 | return *(TheTargetAttributesSema = new MSP430AttributesSema); |
Charles Davis | 5a0164d | 2010-02-10 23:06:52 +0000 | [diff] [blame] | 217 | case llvm::Triple::x86: |
| 218 | return *(TheTargetAttributesSema = new X86AttributesSema); |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |