blob: 45067dee9b8dad117432aa961897f3350cd54c79 [file] [log] [blame]
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001//===-- 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 Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
John McCall384aff82010-08-25 07:42:41 +000016#include "clang/AST/DeclCXX.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/Basic/TargetInfo.h"
18#include "clang/Sema/SemaInternal.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000019#include "llvm/ADT/Triple.h"
20
21using namespace clang;
22
23TargetAttributesSema::~TargetAttributesSema() {}
24bool TargetAttributesSema::ProcessDeclAttribute(Scope *scope, Decl *D,
25 const AttributeList &Attr, Sema &S) const {
26 return false;
27}
28
Tim Northover64eac852013-10-01 14:34:25 +000029static 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
58namespace {
59 class ARMAttributesSema : public TargetAttributesSema {
60 public:
61 ARMAttributesSema() { }
62 bool ProcessDeclAttribute(Scope *scope, Decl *D,
63 const AttributeList &Attr, Sema &S) const {
64 if (Attr.getName()->getName() == "interrupt") {
65 HandleARMInterruptAttr(D, Attr, S);
66 return true;
67 }
68 return false;
69 }
70 };
71}
72
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000073static void HandleMSP430InterruptAttr(Decl *d,
74 const AttributeList &Attr, Sema &S) {
75 // Check the attribute arguments.
76 if (Attr.getNumArgs() != 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +000077 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
78 << Attr.getName() << 1;
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000079 return;
80 }
81
82 // FIXME: Check for decl - it should be void ()(void).
83
Aaron Ballman624421f2013-08-31 01:11:41 +000084 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000085 llvm::APSInt NumParams(32);
86 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +000087 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
88 << Attr.getName() << AANT_ArgumentIntegerConstant
89 << NumParamsExpr->getSourceRange();
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000090 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
Sean Huntcf807c42010-08-18 23:23:40 +0000101 d->addAttr(::new (S.Context) MSP430InterruptAttr(Attr.getLoc(), S.Context, Num));
102 d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000103 }
104
105namespace {
106 class MSP430AttributesSema : public TargetAttributesSema {
107 public:
108 MSP430AttributesSema() { }
109 bool ProcessDeclAttribute(Scope *scope, Decl *D,
110 const AttributeList &Attr, Sema &S) const {
111 if (Attr.getName()->getName() == "interrupt") {
112 HandleMSP430InterruptAttr(D, Attr, S);
113 return true;
114 }
115 return false;
116 }
117 };
118}
119
Charles Davis5a0164d2010-02-10 23:06:52 +0000120static void HandleX86ForceAlignArgPointerAttr(Decl *D,
121 const AttributeList& Attr,
122 Sema &S) {
123 // Check the attribute arguments.
124 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +0000125 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
126 << Attr.getName() << 0;
Charles Davis5a0164d2010-02-10 23:06:52 +0000127 return;
128 }
129
Charles Davisab442162010-02-17 00:44:47 +0000130 // If we try to apply it to a function pointer, don't warn, but don't
131 // do anything, either. It doesn't matter anyway, because there's nothing
132 // special about calling a force_align_arg_pointer function.
Charles Davisbeaf5ed2010-02-18 04:39:19 +0000133 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Charles Davisab442162010-02-17 00:44:47 +0000134 if (VD && VD->getType()->isFunctionPointerType())
Charles Davis5a0164d2010-02-10 23:06:52 +0000135 return;
Charles Davisbeaf5ed2010-02-18 04:39:19 +0000136 // Also don't warn on function pointer typedefs.
Richard Smith162e1c12011-04-15 14:24:37 +0000137 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Charles Davise01c0632010-02-18 04:56:59 +0000138 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
139 TD->getUnderlyingType()->isFunctionType()))
Charles Davisbeaf5ed2010-02-18 04:39:19 +0000140 return;
Charles Davis5a0164d2010-02-10 23:06:52 +0000141 // Attribute can only be applied to function types.
Charles Davis9c00be52010-02-10 23:26:12 +0000142 if (!isa<FunctionDecl>(D)) {
Charles Davis5a0164d2010-02-10 23:06:52 +0000143 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
144 << Attr.getName() << /* function */0;
145 return;
146 }
147
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000148 D->addAttr(::new (S.Context) X86ForceAlignArgPointerAttr(Attr.getRange(),
149 S.Context));
Charles Davis5a0164d2010-02-10 23:06:52 +0000150}
151
Michael Han51d8c522013-01-24 16:46:58 +0000152DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
153 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +0000154 if (D->hasAttr<DLLExportAttr>()) {
155 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "dllimport";
Rafael Espindola599f1b72012-05-13 03:25:18 +0000156 return NULL;
Rafael Espindola98ae8342012-05-10 02:50:16 +0000157 }
158
159 if (D->hasAttr<DLLImportAttr>())
Rafael Espindola599f1b72012-05-13 03:25:18 +0000160 return NULL;
Rafael Espindola98ae8342012-05-10 02:50:16 +0000161
Reid Klecknerbeba3e82013-05-20 21:53:29 +0000162 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
163 if (VD->hasDefinition()) {
164 // dllimport cannot be applied to definitions.
165 Diag(D->getLocation(), diag::warn_attribute_invalid_on_definition)
166 << "dllimport";
167 return NULL;
168 }
169 }
170
Michael Han51d8c522013-01-24 16:46:58 +0000171 return ::new (Context) DLLImportAttr(Range, Context,
172 AttrSpellingListIndex);
Rafael Espindola98ae8342012-05-10 02:50:16 +0000173}
174
Charles Davisf0122fe2010-02-16 18:27:26 +0000175static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
176 // check the attribute arguments.
177 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +0000178 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
179 << Attr.getName() << 0;
Charles Davisf0122fe2010-02-16 18:27:26 +0000180 return;
181 }
182
183 // Attribute can be applied only to functions or variables.
Charles Davisf0122fe2010-02-16 18:27:26 +0000184 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Rafael Espindola98ae8342012-05-10 02:50:16 +0000185 if (!FD && !isa<VarDecl>(D)) {
Ted Kremenek240670c2010-02-21 05:12:56 +0000186 // Apparently Visual C++ thinks it is okay to not emit a warning
187 // in this case, so only emit a warning when -fms-extensions is not
188 // specified.
David Blaikie4e4d0842012-03-11 07:00:24 +0000189 if (!S.getLangOpts().MicrosoftExt)
Ted Kremenek240670c2010-02-21 05:12:56 +0000190 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
191 << Attr.getName() << 2 /*variable and function*/;
Charles Davisf0122fe2010-02-16 18:27:26 +0000192 return;
193 }
194
195 // Currently, the dllimport attribute is ignored for inlined functions.
196 // Warning is emitted.
Rafael Espindola98ae8342012-05-10 02:50:16 +0000197 if (FD && FD->isInlineSpecified()) {
Charles Davisf0122fe2010-02-16 18:27:26 +0000198 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
199 return;
200 }
201
Michael Han51d8c522013-01-24 16:46:58 +0000202 unsigned Index = Attr.getAttributeSpellingListIndex();
203 DLLImportAttr *NewAttr = S.mergeDLLImportAttr(D, Attr.getRange(), Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +0000204 if (NewAttr)
205 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +0000206}
207
Michael Han51d8c522013-01-24 16:46:58 +0000208DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
209 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +0000210 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
211 Diag(Import->getLocation(), diag::warn_attribute_ignored) << "dllimport";
212 D->dropAttr<DLLImportAttr>();
Charles Davisf0122fe2010-02-16 18:27:26 +0000213 }
214
Rafael Espindola98ae8342012-05-10 02:50:16 +0000215 if (D->hasAttr<DLLExportAttr>())
Rafael Espindola599f1b72012-05-13 03:25:18 +0000216 return NULL;
Rafael Espindola98ae8342012-05-10 02:50:16 +0000217
Michael Han51d8c522013-01-24 16:46:58 +0000218 return ::new (Context) DLLExportAttr(Range, Context,
219 AttrSpellingListIndex);
Charles Davisf0122fe2010-02-16 18:27:26 +0000220}
221
222static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
223 // check the attribute arguments.
224 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +0000225 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
226 << Attr.getName() << 0;
Charles Davisf0122fe2010-02-16 18:27:26 +0000227 return;
228 }
229
230 // Attribute can be applied only to functions or variables.
Charles Davisf0122fe2010-02-16 18:27:26 +0000231 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Rafael Espindola98ae8342012-05-10 02:50:16 +0000232 if (!FD && !isa<VarDecl>(D)) {
Charles Davisf0122fe2010-02-16 18:27:26 +0000233 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
234 << Attr.getName() << 2 /*variable and function*/;
235 return;
236 }
237
238 // Currently, the dllexport attribute is ignored for inlined functions, unless
239 // the -fkeep-inline-functions flag has been used. Warning is emitted;
Rafael Espindola98ae8342012-05-10 02:50:16 +0000240 if (FD && FD->isInlineSpecified()) {
Charles Davisf0122fe2010-02-16 18:27:26 +0000241 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
242 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
243 return;
244 }
245
Michael Han51d8c522013-01-24 16:46:58 +0000246 unsigned Index = Attr.getAttributeSpellingListIndex();
247 DLLExportAttr *NewAttr = S.mergeDLLExportAttr(D, Attr.getRange(), Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +0000248 if (NewAttr)
249 D->addAttr(NewAttr);
Charles Davisf0122fe2010-02-16 18:27:26 +0000250}
251
Charles Davis5a0164d2010-02-10 23:06:52 +0000252namespace {
253 class X86AttributesSema : public TargetAttributesSema {
254 public:
255 X86AttributesSema() { }
256 bool ProcessDeclAttribute(Scope *scope, Decl *D,
257 const AttributeList &Attr, Sema &S) const {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000258 const llvm::Triple &Triple(S.Context.getTargetInfo().getTriple());
Charles Davisf0122fe2010-02-16 18:27:26 +0000259 if (Triple.getOS() == llvm::Triple::Win32 ||
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +0000260 Triple.getOS() == llvm::Triple::MinGW32) {
Charles Davisf0122fe2010-02-16 18:27:26 +0000261 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +0000262 case AttributeList::AT_DLLImport: HandleDLLImportAttr(D, Attr, S);
Charles Davisf0122fe2010-02-16 18:27:26 +0000263 return true;
Sean Hunt8e083e72012-06-19 23:57:03 +0000264 case AttributeList::AT_DLLExport: HandleDLLExportAttr(D, Attr, S);
Charles Davisf0122fe2010-02-16 18:27:26 +0000265 return true;
266 default: break;
267 }
268 }
Eli Friedman9afbfbe2011-09-30 18:53:25 +0000269 if (Triple.getArch() != llvm::Triple::x86_64 &&
Benjamin Kramerf2cee5c2011-09-30 20:32:22 +0000270 (Attr.getName()->getName() == "force_align_arg_pointer" ||
271 Attr.getName()->getName() == "__force_align_arg_pointer__")) {
Charles Davis5a0164d2010-02-10 23:06:52 +0000272 HandleX86ForceAlignArgPointerAttr(D, Attr, S);
273 return true;
274 }
275 return false;
276 }
277 };
278}
279
Reed Kotler7dfd1822013-01-16 17:10:28 +0000280static void HandleMips16Attr(Decl *D, const AttributeList &Attr, Sema &S) {
281 // check the attribute arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +0000282 if (Attr.getNumArgs()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +0000283 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
284 << Attr.getName() << 0;
Reed Kotler7dfd1822013-01-16 17:10:28 +0000285 return;
286 }
287 // Attribute can only be applied to function types.
288 if (!isa<FunctionDecl>(D)) {
289 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
290 << Attr.getName() << /* function */0;
291 return;
292 }
Michael Han51d8c522013-01-24 16:46:58 +0000293 D->addAttr(::new (S.Context) Mips16Attr(Attr.getRange(), S.Context,
294 Attr.getAttributeSpellingListIndex()));
Reed Kotler7dfd1822013-01-16 17:10:28 +0000295}
296
297static void HandleNoMips16Attr(Decl *D, const AttributeList &Attr, Sema &S) {
298 // check the attribute arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +0000299 if (Attr.getNumArgs()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +0000300 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
301 << Attr.getName() << 0;
Reed Kotler7dfd1822013-01-16 17:10:28 +0000302 return;
303 }
304 // Attribute can only be applied to function types.
305 if (!isa<FunctionDecl>(D)) {
306 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
307 << Attr.getName() << /* function */0;
308 return;
309 }
Michael Han51d8c522013-01-24 16:46:58 +0000310 D->addAttr(::new (S.Context)
311 NoMips16Attr(Attr.getRange(), S.Context,
312 Attr.getAttributeSpellingListIndex()));
Reed Kotler7dfd1822013-01-16 17:10:28 +0000313}
314
315namespace {
316 class MipsAttributesSema : public TargetAttributesSema {
317 public:
318 MipsAttributesSema() { }
319 bool ProcessDeclAttribute(Scope *scope, Decl *D, const AttributeList &Attr,
320 Sema &S) const {
321 if (Attr.getName()->getName() == "mips16") {
322 HandleMips16Attr(D, Attr, S);
323 return true;
324 } else if (Attr.getName()->getName() == "nomips16") {
325 HandleNoMips16Attr(D, Attr, S);
326 return true;
327 }
328 return false;
329 }
330 };
331}
332
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000333const TargetAttributesSema &Sema::getTargetAttributesSema() const {
334 if (TheTargetAttributesSema)
335 return *TheTargetAttributesSema;
336
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000337 const llvm::Triple &Triple(Context.getTargetInfo().getTriple());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000338 switch (Triple.getArch()) {
Tim Northover64eac852013-10-01 14:34:25 +0000339 case llvm::Triple::arm:
340 case llvm::Triple::thumb:
341 return *(TheTargetAttributesSema = new ARMAttributesSema);
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000342 case llvm::Triple::msp430:
343 return *(TheTargetAttributesSema = new MSP430AttributesSema);
Charles Davis5a0164d2010-02-10 23:06:52 +0000344 case llvm::Triple::x86:
Eli Friedman9afbfbe2011-09-30 18:53:25 +0000345 case llvm::Triple::x86_64:
Charles Davis5a0164d2010-02-10 23:06:52 +0000346 return *(TheTargetAttributesSema = new X86AttributesSema);
Reed Kotler7dfd1822013-01-16 17:10:28 +0000347 case llvm::Triple::mips:
348 case llvm::Triple::mipsel:
349 return *(TheTargetAttributesSema = new MipsAttributesSema);
Eli Friedman9afbfbe2011-09-30 18:53:25 +0000350 default:
351 return *(TheTargetAttributesSema = new TargetAttributesSema);
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000352 }
353}