blob: 034e460eeb27dc334299876760bbb63288d36b4b [file] [log] [blame]
Anton Korobeynikov55bcea12010-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 Korobeynikov55bcea12010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
John McCall28a0cf72010-08-25 07:42:41 +000016#include "clang/AST/DeclCXX.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/Basic/TargetInfo.h"
18#include "clang/Sema/SemaInternal.h"
Anton Korobeynikov55bcea12010-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 Northovera484bc02013-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 {
Aaron Ballmand9c5c5c2013-12-04 22:02:33 +000064 if (Attr.getKind() == AttributeList::AT_ARMInterrupt) {
Tim Northovera484bc02013-10-01 14:34:25 +000065 HandleARMInterruptAttr(D, Attr, S);
66 return true;
67 }
68 return false;
69 }
70 };
71}
72
Anton Korobeynikov55bcea12010-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 Ballmanb7243382013-07-23 19:30:11 +000077 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
78 << Attr.getName() << 1;
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000079 return;
80 }
81
82 // FIXME: Check for decl - it should be void ()(void).
83
Aaron Ballman00e99962013-08-31 01:11:41 +000084 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000085 llvm::APSInt NumParams(32);
86 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Aaron Ballman9d695092013-07-30 14:10:17 +000087 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
88 << Attr.getName() << AANT_ArgumentIntegerConstant
89 << NumParamsExpr->getSourceRange();
Anton Korobeynikov55bcea12010-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
Alexis Huntdcfba7b2010-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 Korobeynikov55bcea12010-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 {
Aaron Ballmanb4c11232013-12-04 22:23:43 +0000111 // 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 Korobeynikov55bcea12010-01-10 12:58:08 +0000113 if (Attr.getName()->getName() == "interrupt") {
114 HandleMSP430InterruptAttr(D, Attr, S);
115 return true;
116 }
117 return false;
118 }
119 };
120}
121
Charles Davisbbc0aa52010-02-10 23:06:52 +0000122static void HandleX86ForceAlignArgPointerAttr(Decl *D,
123 const AttributeList& Attr,
124 Sema &S) {
125 // Check the attribute arguments.
126 if (Attr.getNumArgs() != 0) {
Aaron Ballmanb7243382013-07-23 19:30:11 +0000127 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
128 << Attr.getName() << 0;
Charles Davisbbc0aa52010-02-10 23:06:52 +0000129 return;
130 }
131
Charles Davis61170a12010-02-17 00:44:47 +0000132 // 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 Daviscb9572e2010-02-18 04:39:19 +0000135 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Charles Davis61170a12010-02-17 00:44:47 +0000136 if (VD && VD->getType()->isFunctionPointerType())
Charles Davisbbc0aa52010-02-10 23:06:52 +0000137 return;
Charles Daviscb9572e2010-02-18 04:39:19 +0000138 // Also don't warn on function pointer typedefs.
Richard Smithdda56e42011-04-15 14:24:37 +0000139 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Charles Davis9fcead72010-02-18 04:56:59 +0000140 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
141 TD->getUnderlyingType()->isFunctionType()))
Charles Daviscb9572e2010-02-18 04:39:19 +0000142 return;
Charles Davisbbc0aa52010-02-10 23:06:52 +0000143 // Attribute can only be applied to function types.
Charles Davis42b38422010-02-10 23:26:12 +0000144 if (!isa<FunctionDecl>(D)) {
Charles Davisbbc0aa52010-02-10 23:06:52 +0000145 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
146 << Attr.getName() << /* function */0;
147 return;
148 }
149
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000150 D->addAttr(::new (S.Context) X86ForceAlignArgPointerAttr(Attr.getRange(),
151 S.Context));
Charles Davisbbc0aa52010-02-10 23:06:52 +0000152}
153
Michael Han99315932013-01-24 16:46:58 +0000154DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
155 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +0000156 if (D->hasAttr<DLLExportAttr>()) {
157 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "dllimport";
Rafael Espindolae200f1c2012-05-13 03:25:18 +0000158 return NULL;
Rafael Espindolac67f2232012-05-10 02:50:16 +0000159 }
160
161 if (D->hasAttr<DLLImportAttr>())
Rafael Espindolae200f1c2012-05-13 03:25:18 +0000162 return NULL;
Rafael Espindolac67f2232012-05-10 02:50:16 +0000163
Reid Kleckner52d598e2013-05-20 21:53:29 +0000164 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 Han99315932013-01-24 16:46:58 +0000173 return ::new (Context) DLLImportAttr(Range, Context,
174 AttrSpellingListIndex);
Rafael Espindolac67f2232012-05-10 02:50:16 +0000175}
176
Charles Davis163855f2010-02-16 18:27:26 +0000177static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
178 // check the attribute arguments.
179 if (Attr.getNumArgs() != 0) {
Aaron Ballmanb7243382013-07-23 19:30:11 +0000180 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
181 << Attr.getName() << 0;
Charles Davis163855f2010-02-16 18:27:26 +0000182 return;
183 }
184
185 // Attribute can be applied only to functions or variables.
Charles Davis163855f2010-02-16 18:27:26 +0000186 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Rafael Espindolac67f2232012-05-10 02:50:16 +0000187 if (!FD && !isa<VarDecl>(D)) {
Ted Kremenek19790072010-02-21 05:12:56 +0000188 // 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 Blaikiebbafb8a2012-03-11 07:00:24 +0000191 if (!S.getLangOpts().MicrosoftExt)
Ted Kremenek19790072010-02-21 05:12:56 +0000192 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
193 << Attr.getName() << 2 /*variable and function*/;
Charles Davis163855f2010-02-16 18:27:26 +0000194 return;
195 }
196
197 // Currently, the dllimport attribute is ignored for inlined functions.
198 // Warning is emitted.
Rafael Espindolac67f2232012-05-10 02:50:16 +0000199 if (FD && FD->isInlineSpecified()) {
Aaron Ballmand9c5c5c2013-12-04 22:02:33 +0000200 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Charles Davis163855f2010-02-16 18:27:26 +0000201 return;
202 }
203
Michael Han99315932013-01-24 16:46:58 +0000204 unsigned Index = Attr.getAttributeSpellingListIndex();
205 DLLImportAttr *NewAttr = S.mergeDLLImportAttr(D, Attr.getRange(), Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +0000206 if (NewAttr)
207 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +0000208}
209
Michael Han99315932013-01-24 16:46:58 +0000210DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
211 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +0000212 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
213 Diag(Import->getLocation(), diag::warn_attribute_ignored) << "dllimport";
214 D->dropAttr<DLLImportAttr>();
Charles Davis163855f2010-02-16 18:27:26 +0000215 }
216
Rafael Espindolac67f2232012-05-10 02:50:16 +0000217 if (D->hasAttr<DLLExportAttr>())
Rafael Espindolae200f1c2012-05-13 03:25:18 +0000218 return NULL;
Rafael Espindolac67f2232012-05-10 02:50:16 +0000219
Michael Han99315932013-01-24 16:46:58 +0000220 return ::new (Context) DLLExportAttr(Range, Context,
221 AttrSpellingListIndex);
Charles Davis163855f2010-02-16 18:27:26 +0000222}
223
224static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
225 // check the attribute arguments.
226 if (Attr.getNumArgs() != 0) {
Aaron Ballmanb7243382013-07-23 19:30:11 +0000227 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
228 << Attr.getName() << 0;
Charles Davis163855f2010-02-16 18:27:26 +0000229 return;
230 }
231
232 // Attribute can be applied only to functions or variables.
Charles Davis163855f2010-02-16 18:27:26 +0000233 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Rafael Espindolac67f2232012-05-10 02:50:16 +0000234 if (!FD && !isa<VarDecl>(D)) {
Charles Davis163855f2010-02-16 18:27:26 +0000235 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 Espindolac67f2232012-05-10 02:50:16 +0000242 if (FD && FD->isInlineSpecified()) {
Charles Davis163855f2010-02-16 18:27:26 +0000243 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
Aaron Ballmand9c5c5c2013-12-04 22:02:33 +0000244 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Charles Davis163855f2010-02-16 18:27:26 +0000245 return;
246 }
247
Michael Han99315932013-01-24 16:46:58 +0000248 unsigned Index = Attr.getAttributeSpellingListIndex();
249 DLLExportAttr *NewAttr = S.mergeDLLExportAttr(D, Attr.getRange(), Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +0000250 if (NewAttr)
251 D->addAttr(NewAttr);
Charles Davis163855f2010-02-16 18:27:26 +0000252}
253
Charles Davisbbc0aa52010-02-10 23:06:52 +0000254namespace {
255 class X86AttributesSema : public TargetAttributesSema {
256 public:
257 X86AttributesSema() { }
258 bool ProcessDeclAttribute(Scope *scope, Decl *D,
259 const AttributeList &Attr, Sema &S) const {
Douglas Gregore8bbc122011-09-02 00:18:52 +0000260 const llvm::Triple &Triple(S.Context.getTargetInfo().getTriple());
Charles Davis163855f2010-02-16 18:27:26 +0000261 if (Triple.getOS() == llvm::Triple::Win32 ||
NAKAMURA Takumi31ea2f12011-02-17 08:51:38 +0000262 Triple.getOS() == llvm::Triple::MinGW32) {
Charles Davis163855f2010-02-16 18:27:26 +0000263 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +0000264 case AttributeList::AT_DLLImport: HandleDLLImportAttr(D, Attr, S);
Charles Davis163855f2010-02-16 18:27:26 +0000265 return true;
Alexis Hunt3bc72c12012-06-19 23:57:03 +0000266 case AttributeList::AT_DLLExport: HandleDLLExportAttr(D, Attr, S);
Charles Davis163855f2010-02-16 18:27:26 +0000267 return true;
268 default: break;
269 }
270 }
Eli Friedman271002f2011-09-30 18:53:25 +0000271 if (Triple.getArch() != llvm::Triple::x86_64 &&
Aaron Ballman30013392013-12-04 21:43:30 +0000272 Attr.getKind() == AttributeList::AT_X86ForceAlignArgPointer) {
Charles Davisbbc0aa52010-02-10 23:06:52 +0000273 HandleX86ForceAlignArgPointerAttr(D, Attr, S);
274 return true;
275 }
276 return false;
277 }
278 };
279}
280
Reed Kotler373feca2013-01-16 17:10:28 +0000281static void HandleMips16Attr(Decl *D, const AttributeList &Attr, Sema &S) {
282 // check the attribute arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +0000283 if (Attr.getNumArgs()) {
Aaron Ballmanb7243382013-07-23 19:30:11 +0000284 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
285 << Attr.getName() << 0;
Reed Kotler373feca2013-01-16 17:10:28 +0000286 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 Han99315932013-01-24 16:46:58 +0000294 D->addAttr(::new (S.Context) Mips16Attr(Attr.getRange(), S.Context,
295 Attr.getAttributeSpellingListIndex()));
Reed Kotler373feca2013-01-16 17:10:28 +0000296}
297
298static void HandleNoMips16Attr(Decl *D, const AttributeList &Attr, Sema &S) {
299 // check the attribute arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +0000300 if (Attr.getNumArgs()) {
Aaron Ballmanb7243382013-07-23 19:30:11 +0000301 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
302 << Attr.getName() << 0;
Reed Kotler373feca2013-01-16 17:10:28 +0000303 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 Han99315932013-01-24 16:46:58 +0000311 D->addAttr(::new (S.Context)
312 NoMips16Attr(Attr.getRange(), S.Context,
313 Attr.getAttributeSpellingListIndex()));
Reed Kotler373feca2013-01-16 17:10:28 +0000314}
315
316namespace {
317 class MipsAttributesSema : public TargetAttributesSema {
318 public:
319 MipsAttributesSema() { }
320 bool ProcessDeclAttribute(Scope *scope, Decl *D, const AttributeList &Attr,
321 Sema &S) const {
Aaron Ballmand9c5c5c2013-12-04 22:02:33 +0000322 if (Attr.getKind() == AttributeList::AT_Mips16) {
Reed Kotler373feca2013-01-16 17:10:28 +0000323 HandleMips16Attr(D, Attr, S);
324 return true;
Aaron Ballmand9c5c5c2013-12-04 22:02:33 +0000325 } else if (Attr.getKind() == AttributeList::AT_NoMips16) {
Reed Kotler373feca2013-01-16 17:10:28 +0000326 HandleNoMips16Attr(D, Attr, S);
327 return true;
328 }
329 return false;
330 }
331 };
332}
333
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000334const TargetAttributesSema &Sema::getTargetAttributesSema() const {
335 if (TheTargetAttributesSema)
336 return *TheTargetAttributesSema;
337
Douglas Gregore8bbc122011-09-02 00:18:52 +0000338 const llvm::Triple &Triple(Context.getTargetInfo().getTriple());
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000339 switch (Triple.getArch()) {
Tim Northovera484bc02013-10-01 14:34:25 +0000340 case llvm::Triple::arm:
341 case llvm::Triple::thumb:
342 return *(TheTargetAttributesSema = new ARMAttributesSema);
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000343 case llvm::Triple::msp430:
344 return *(TheTargetAttributesSema = new MSP430AttributesSema);
Charles Davisbbc0aa52010-02-10 23:06:52 +0000345 case llvm::Triple::x86:
Eli Friedman271002f2011-09-30 18:53:25 +0000346 case llvm::Triple::x86_64:
Charles Davisbbc0aa52010-02-10 23:06:52 +0000347 return *(TheTargetAttributesSema = new X86AttributesSema);
Reed Kotler373feca2013-01-16 17:10:28 +0000348 case llvm::Triple::mips:
349 case llvm::Triple::mipsel:
350 return *(TheTargetAttributesSema = new MipsAttributesSema);
Eli Friedman271002f2011-09-30 18:53:25 +0000351 default:
352 return *(TheTargetAttributesSema = new TargetAttributesSema);
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000353 }
354}