Anton Korobeynikov | 55bcea1 | 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 | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 15 | #include "TargetAttributesSema.h" |
John McCall | 28a0cf7 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclCXX.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
| 18 | #include "clang/Sema/SemaInternal.h" |
Anton Korobeynikov | 55bcea1 | 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 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 29 | static void HandleARMInterruptAttr(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_too_many_arguments) |
| 34 | << 1; |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | StringRef Str; |
| 39 | SourceLocation ArgLoc; |
| 40 | |
| 41 | if (Attr.getNumArgs() == 0) |
| 42 | Str = ""; |
| 43 | else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc)) |
| 44 | return; |
| 45 | |
| 46 | ARMInterruptAttr::InterruptType Kind; |
| 47 | if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
| 48 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 49 | << Attr.getName() << Str << ArgLoc; |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 54 | d->addAttr(::new (S.Context) |
| 55 | ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index)); |
| 56 | } |
| 57 | |
| 58 | namespace { |
| 59 | class ARMAttributesSema : public TargetAttributesSema { |
| 60 | public: |
| 61 | ARMAttributesSema() { } |
| 62 | bool ProcessDeclAttribute(Scope *scope, Decl *D, |
| 63 | const AttributeList &Attr, Sema &S) const { |
Aaron Ballman | d9c5c5c | 2013-12-04 22:02:33 +0000 | [diff] [blame] | 64 | if (Attr.getKind() == AttributeList::AT_ARMInterrupt) { |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 65 | HandleARMInterruptAttr(D, Attr, S); |
| 66 | return true; |
| 67 | } |
| 68 | return false; |
| 69 | } |
| 70 | }; |
| 71 | } |
| 72 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 73 | static void HandleMSP430InterruptAttr(Decl *d, |
| 74 | const AttributeList &Attr, Sema &S) { |
| 75 | // Check the attribute arguments. |
| 76 | if (Attr.getNumArgs() != 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 77 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 78 | << Attr.getName() << 1; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 79 | return; |
| 80 | } |
| 81 | |
| 82 | // FIXME: Check for decl - it should be void ()(void). |
| 83 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 84 | Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0)); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 85 | llvm::APSInt NumParams(32); |
| 86 | if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) { |
Aaron Ballman | 9d69509 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 87 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 88 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 89 | << NumParamsExpr->getSourceRange(); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 90 | return; |
| 91 | } |
| 92 | |
| 93 | unsigned Num = NumParams.getLimitedValue(255); |
| 94 | if ((Num & 1) || Num > 30) { |
| 95 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 96 | << "interrupt" << (int)NumParams.getSExtValue() |
| 97 | << NumParamsExpr->getSourceRange(); |
| 98 | return; |
| 99 | } |
| 100 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 101 | d->addAttr(::new (S.Context) MSP430InterruptAttr(Attr.getLoc(), S.Context, Num)); |
| 102 | d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context)); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | namespace { |
| 106 | class MSP430AttributesSema : public TargetAttributesSema { |
| 107 | public: |
| 108 | MSP430AttributesSema() { } |
| 109 | bool ProcessDeclAttribute(Scope *scope, Decl *D, |
| 110 | const AttributeList &Attr, Sema &S) const { |
Aaron Ballman | b4c1123 | 2013-12-04 22:23:43 +0000 | [diff] [blame] | 111 | // Because this attribute has no spelling (see the FIXME in Attr.td as to |
| 112 | // why), we must check for the name instead of the attribute kind. |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 113 | if (Attr.getName()->getName() == "interrupt") { |
| 114 | HandleMSP430InterruptAttr(D, Attr, S); |
| 115 | return true; |
| 116 | } |
| 117 | return false; |
| 118 | } |
| 119 | }; |
| 120 | } |
| 121 | |
Charles Davis | bbc0aa5 | 2010-02-10 23:06:52 +0000 | [diff] [blame] | 122 | static void HandleX86ForceAlignArgPointerAttr(Decl *D, |
| 123 | const AttributeList& Attr, |
| 124 | Sema &S) { |
| 125 | // Check the attribute arguments. |
| 126 | if (Attr.getNumArgs() != 0) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 127 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 128 | << Attr.getName() << 0; |
Charles Davis | bbc0aa5 | 2010-02-10 23:06:52 +0000 | [diff] [blame] | 129 | return; |
| 130 | } |
| 131 | |
Charles Davis | 61170a1 | 2010-02-17 00:44:47 +0000 | [diff] [blame] | 132 | // If we try to apply it to a function pointer, don't warn, but don't |
| 133 | // do anything, either. It doesn't matter anyway, because there's nothing |
| 134 | // special about calling a force_align_arg_pointer function. |
Charles Davis | cb9572e | 2010-02-18 04:39:19 +0000 | [diff] [blame] | 135 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Charles Davis | 61170a1 | 2010-02-17 00:44:47 +0000 | [diff] [blame] | 136 | if (VD && VD->getType()->isFunctionPointerType()) |
Charles Davis | bbc0aa5 | 2010-02-10 23:06:52 +0000 | [diff] [blame] | 137 | return; |
Charles Davis | cb9572e | 2010-02-18 04:39:19 +0000 | [diff] [blame] | 138 | // Also don't warn on function pointer typedefs. |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 139 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Charles Davis | 9fcead7 | 2010-02-18 04:56:59 +0000 | [diff] [blame] | 140 | if (TD && (TD->getUnderlyingType()->isFunctionPointerType() || |
| 141 | TD->getUnderlyingType()->isFunctionType())) |
Charles Davis | cb9572e | 2010-02-18 04:39:19 +0000 | [diff] [blame] | 142 | return; |
Charles Davis | bbc0aa5 | 2010-02-10 23:06:52 +0000 | [diff] [blame] | 143 | // Attribute can only be applied to function types. |
Charles Davis | 42b3842 | 2010-02-10 23:26:12 +0000 | [diff] [blame] | 144 | if (!isa<FunctionDecl>(D)) { |
Charles Davis | bbc0aa5 | 2010-02-10 23:06:52 +0000 | [diff] [blame] | 145 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 146 | << Attr.getName() << /* function */0; |
| 147 | return; |
| 148 | } |
| 149 | |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 150 | D->addAttr(::new (S.Context) X86ForceAlignArgPointerAttr(Attr.getRange(), |
| 151 | S.Context)); |
Charles Davis | bbc0aa5 | 2010-02-10 23:06:52 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 154 | DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range, |
| 155 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 156 | if (D->hasAttr<DLLExportAttr>()) { |
| 157 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "dllimport"; |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 158 | return NULL; |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | if (D->hasAttr<DLLImportAttr>()) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 162 | return NULL; |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 163 | |
Reid Kleckner | 52d598e | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 164 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 165 | if (VD->hasDefinition()) { |
| 166 | // dllimport cannot be applied to definitions. |
| 167 | Diag(D->getLocation(), diag::warn_attribute_invalid_on_definition) |
| 168 | << "dllimport"; |
| 169 | return NULL; |
| 170 | } |
| 171 | } |
| 172 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 173 | return ::new (Context) DLLImportAttr(Range, Context, |
| 174 | AttrSpellingListIndex); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 177 | static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
| 178 | // check the attribute arguments. |
| 179 | if (Attr.getNumArgs() != 0) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 180 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 181 | << Attr.getName() << 0; |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 182 | return; |
| 183 | } |
| 184 | |
| 185 | // Attribute can be applied only to functions or variables. |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 186 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 187 | if (!FD && !isa<VarDecl>(D)) { |
Ted Kremenek | 1979007 | 2010-02-21 05:12:56 +0000 | [diff] [blame] | 188 | // Apparently Visual C++ thinks it is okay to not emit a warning |
| 189 | // in this case, so only emit a warning when -fms-extensions is not |
| 190 | // specified. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 191 | if (!S.getLangOpts().MicrosoftExt) |
Ted Kremenek | 1979007 | 2010-02-21 05:12:56 +0000 | [diff] [blame] | 192 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 193 | << Attr.getName() << 2 /*variable and function*/; |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 194 | return; |
| 195 | } |
| 196 | |
| 197 | // Currently, the dllimport attribute is ignored for inlined functions. |
| 198 | // Warning is emitted. |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 199 | if (FD && FD->isInlineSpecified()) { |
Aaron Ballman | d9c5c5c | 2013-12-04 22:02:33 +0000 | [diff] [blame] | 200 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 201 | return; |
| 202 | } |
| 203 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 204 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 205 | DLLImportAttr *NewAttr = S.mergeDLLImportAttr(D, Attr.getRange(), Index); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 206 | if (NewAttr) |
| 207 | D->addAttr(NewAttr); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 210 | DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range, |
| 211 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 212 | if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) { |
| 213 | Diag(Import->getLocation(), diag::warn_attribute_ignored) << "dllimport"; |
| 214 | D->dropAttr<DLLImportAttr>(); |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 217 | if (D->hasAttr<DLLExportAttr>()) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 218 | return NULL; |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 219 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 220 | return ::new (Context) DLLExportAttr(Range, Context, |
| 221 | AttrSpellingListIndex); |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
| 225 | // check the attribute arguments. |
| 226 | if (Attr.getNumArgs() != 0) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 227 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 228 | << Attr.getName() << 0; |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 229 | return; |
| 230 | } |
| 231 | |
| 232 | // Attribute can be applied only to functions or variables. |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 233 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 234 | if (!FD && !isa<VarDecl>(D)) { |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 235 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 236 | << Attr.getName() << 2 /*variable and function*/; |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | // Currently, the dllexport attribute is ignored for inlined functions, unless |
| 241 | // the -fkeep-inline-functions flag has been used. Warning is emitted; |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 242 | if (FD && FD->isInlineSpecified()) { |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 243 | // FIXME: ... unless the -fkeep-inline-functions flag has been used. |
Aaron Ballman | d9c5c5c | 2013-12-04 22:02:33 +0000 | [diff] [blame] | 244 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 245 | return; |
| 246 | } |
| 247 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 248 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 249 | DLLExportAttr *NewAttr = S.mergeDLLExportAttr(D, Attr.getRange(), Index); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 250 | if (NewAttr) |
| 251 | D->addAttr(NewAttr); |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Charles Davis | bbc0aa5 | 2010-02-10 23:06:52 +0000 | [diff] [blame] | 254 | namespace { |
| 255 | class X86AttributesSema : public TargetAttributesSema { |
| 256 | public: |
| 257 | X86AttributesSema() { } |
| 258 | bool ProcessDeclAttribute(Scope *scope, Decl *D, |
| 259 | const AttributeList &Attr, Sema &S) const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 260 | const llvm::Triple &Triple(S.Context.getTargetInfo().getTriple()); |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 261 | if (Triple.getOS() == llvm::Triple::Win32 || |
NAKAMURA Takumi | 31ea2f1 | 2011-02-17 08:51:38 +0000 | [diff] [blame] | 262 | Triple.getOS() == llvm::Triple::MinGW32) { |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 263 | switch (Attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 264 | case AttributeList::AT_DLLImport: HandleDLLImportAttr(D, Attr, S); |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 265 | return true; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 266 | case AttributeList::AT_DLLExport: HandleDLLExportAttr(D, Attr, S); |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 267 | return true; |
| 268 | default: break; |
| 269 | } |
| 270 | } |
Eli Friedman | 271002f | 2011-09-30 18:53:25 +0000 | [diff] [blame] | 271 | if (Triple.getArch() != llvm::Triple::x86_64 && |
Aaron Ballman | 3001339 | 2013-12-04 21:43:30 +0000 | [diff] [blame] | 272 | Attr.getKind() == AttributeList::AT_X86ForceAlignArgPointer) { |
Charles Davis | bbc0aa5 | 2010-02-10 23:06:52 +0000 | [diff] [blame] | 273 | HandleX86ForceAlignArgPointerAttr(D, Attr, S); |
| 274 | return true; |
| 275 | } |
| 276 | return false; |
| 277 | } |
| 278 | }; |
| 279 | } |
| 280 | |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 281 | static void HandleMips16Attr(Decl *D, const AttributeList &Attr, Sema &S) { |
| 282 | // check the attribute arguments. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 283 | if (Attr.getNumArgs()) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 284 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 285 | << Attr.getName() << 0; |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 286 | return; |
| 287 | } |
| 288 | // Attribute can only be applied to function types. |
| 289 | if (!isa<FunctionDecl>(D)) { |
| 290 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 291 | << Attr.getName() << /* function */0; |
| 292 | return; |
| 293 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 294 | D->addAttr(::new (S.Context) Mips16Attr(Attr.getRange(), S.Context, |
| 295 | Attr.getAttributeSpellingListIndex())); |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | static void HandleNoMips16Attr(Decl *D, const AttributeList &Attr, Sema &S) { |
| 299 | // check the attribute arguments. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 300 | if (Attr.getNumArgs()) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 301 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 302 | << Attr.getName() << 0; |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 303 | return; |
| 304 | } |
| 305 | // Attribute can only be applied to function types. |
| 306 | if (!isa<FunctionDecl>(D)) { |
| 307 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 308 | << Attr.getName() << /* function */0; |
| 309 | return; |
| 310 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 311 | D->addAttr(::new (S.Context) |
| 312 | NoMips16Attr(Attr.getRange(), S.Context, |
| 313 | Attr.getAttributeSpellingListIndex())); |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | namespace { |
| 317 | class MipsAttributesSema : public TargetAttributesSema { |
| 318 | public: |
| 319 | MipsAttributesSema() { } |
| 320 | bool ProcessDeclAttribute(Scope *scope, Decl *D, const AttributeList &Attr, |
| 321 | Sema &S) const { |
Aaron Ballman | d9c5c5c | 2013-12-04 22:02:33 +0000 | [diff] [blame] | 322 | if (Attr.getKind() == AttributeList::AT_Mips16) { |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 323 | HandleMips16Attr(D, Attr, S); |
| 324 | return true; |
Aaron Ballman | d9c5c5c | 2013-12-04 22:02:33 +0000 | [diff] [blame] | 325 | } else if (Attr.getKind() == AttributeList::AT_NoMips16) { |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 326 | HandleNoMips16Attr(D, Attr, S); |
| 327 | return true; |
| 328 | } |
| 329 | return false; |
| 330 | } |
| 331 | }; |
| 332 | } |
| 333 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 334 | const TargetAttributesSema &Sema::getTargetAttributesSema() const { |
| 335 | if (TheTargetAttributesSema) |
| 336 | return *TheTargetAttributesSema; |
| 337 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 338 | const llvm::Triple &Triple(Context.getTargetInfo().getTriple()); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 339 | switch (Triple.getArch()) { |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 340 | case llvm::Triple::arm: |
| 341 | case llvm::Triple::thumb: |
| 342 | return *(TheTargetAttributesSema = new ARMAttributesSema); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 343 | case llvm::Triple::msp430: |
| 344 | return *(TheTargetAttributesSema = new MSP430AttributesSema); |
Charles Davis | bbc0aa5 | 2010-02-10 23:06:52 +0000 | [diff] [blame] | 345 | case llvm::Triple::x86: |
Eli Friedman | 271002f | 2011-09-30 18:53:25 +0000 | [diff] [blame] | 346 | case llvm::Triple::x86_64: |
Charles Davis | bbc0aa5 | 2010-02-10 23:06:52 +0000 | [diff] [blame] | 347 | return *(TheTargetAttributesSema = new X86AttributesSema); |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 348 | case llvm::Triple::mips: |
| 349 | case llvm::Triple::mipsel: |
| 350 | return *(TheTargetAttributesSema = new MipsAttributesSema); |
Eli Friedman | 271002f | 2011-09-30 18:53:25 +0000 | [diff] [blame] | 351 | default: |
| 352 | return *(TheTargetAttributesSema = new TargetAttributesSema); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 353 | } |
| 354 | } |