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