blob: 9ace9ad65aca4f34331cb17fbf59f6e6d3b7e77c [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 McCall83024632010-08-25 22:03:47 +000016#include "clang/Sema/SemaInternal.h"
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000017#include "clang/Basic/TargetInfo.h"
John McCall28a0cf72010-08-25 07:42:41 +000018#include "clang/AST/DeclCXX.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
29static 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
Alexis Huntdcfba7b2010-08-18 23:23:40 +000055 d->addAttr(::new (S.Context) MSP430InterruptAttr(Attr.getLoc(), S.Context, Num));
56 d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000057 }
58
59namespace {
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
Wesley Peck36a1f682010-12-19 19:57:51 +000074static void HandleMBlazeInterruptHandlerAttr(Decl *d, 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) << 1;
79 return;
80 }
81
82 // FIXME: Check for decl - it should be void ()(void).
83
84 d->addAttr(::new (S.Context) MBlazeInterruptHandlerAttr(Attr.getLoc(),
85 S.Context));
86 d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
87}
88
89static void HandleMBlazeSaveVolatilesAttr(Decl *d, const AttributeList &Attr,
90 Sema &S) {
91 // Check the attribute arguments.
92 if (Attr.getNumArgs() != 0) {
93 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
94 return;
95 }
96
97 // FIXME: Check for decl - it should be void ()(void).
98
99 d->addAttr(::new (S.Context) MBlazeSaveVolatilesAttr(Attr.getLoc(),
100 S.Context));
101 d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
102}
103
104
105namespace {
106 class MBlazeAttributesSema : public TargetAttributesSema {
107 public:
108 MBlazeAttributesSema() { }
109 bool ProcessDeclAttribute(Scope *scope, Decl *D, const AttributeList &Attr,
110 Sema &S) const {
111 if (Attr.getName()->getName() == "interrupt_handler") {
112 HandleMBlazeInterruptHandlerAttr(D, Attr, S);
113 return true;
114 } else if (Attr.getName()->getName() == "save_volatiles") {
115 HandleMBlazeSaveVolatilesAttr(D, Attr, S);
116 return true;
117 }
118 return false;
119 }
120 };
121}
122
Charles Davisbbc0aa52010-02-10 23:06:52 +0000123static void HandleX86ForceAlignArgPointerAttr(Decl *D,
124 const AttributeList& Attr,
125 Sema &S) {
126 // Check the attribute arguments.
127 if (Attr.getNumArgs() != 0) {
128 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
129 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
Rafael Espindolac67f2232012-05-10 02:50:16 +0000154bool Sema::mergeDLLImportAttr(Decl *D, SourceRange Range, bool Inherited) {
155 if (D->hasAttr<DLLExportAttr>()) {
156 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "dllimport";
157 return false;
158 }
159
160 if (D->hasAttr<DLLImportAttr>())
161 return false;
162
163 DLLImportAttr *Attr =
164 ::new (Context) DLLImportAttr(Range, Context);
165 if (Inherited)
166 Attr->setInherited(true);
167 D->addAttr(Attr);
168
169 return true;
170}
171
Charles Davis163855f2010-02-16 18:27:26 +0000172static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
173 // check the attribute arguments.
174 if (Attr.getNumArgs() != 0) {
175 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
176 return;
177 }
178
179 // Attribute can be applied only to functions or variables.
Charles Davis163855f2010-02-16 18:27:26 +0000180 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Rafael Espindolac67f2232012-05-10 02:50:16 +0000181 if (!FD && !isa<VarDecl>(D)) {
Ted Kremenek19790072010-02-21 05:12:56 +0000182 // Apparently Visual C++ thinks it is okay to not emit a warning
183 // in this case, so only emit a warning when -fms-extensions is not
184 // specified.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000185 if (!S.getLangOpts().MicrosoftExt)
Ted Kremenek19790072010-02-21 05:12:56 +0000186 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
187 << Attr.getName() << 2 /*variable and function*/;
Charles Davis163855f2010-02-16 18:27:26 +0000188 return;
189 }
190
191 // Currently, the dllimport attribute is ignored for inlined functions.
192 // Warning is emitted.
Rafael Espindolac67f2232012-05-10 02:50:16 +0000193 if (FD && FD->isInlineSpecified()) {
Charles Davis163855f2010-02-16 18:27:26 +0000194 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
195 return;
196 }
197
Rafael Espindolac67f2232012-05-10 02:50:16 +0000198 S.mergeDLLImportAttr(D, Attr.getRange(), false);
199}
200
201bool Sema::mergeDLLExportAttr(Decl *D, SourceRange Range, bool Inherited) {
202 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
203 Diag(Import->getLocation(), diag::warn_attribute_ignored) << "dllimport";
204 D->dropAttr<DLLImportAttr>();
Charles Davis163855f2010-02-16 18:27:26 +0000205 }
206
Rafael Espindolac67f2232012-05-10 02:50:16 +0000207 if (D->hasAttr<DLLExportAttr>())
208 return false;
209
210 DLLExportAttr *Attr =
211 ::new (Context) DLLExportAttr(Range, Context);
212 if (Inherited)
213 Attr->setInherited(true);
214 D->addAttr(Attr);
215
216 return true;
Charles Davis163855f2010-02-16 18:27:26 +0000217}
218
219static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
220 // check the attribute arguments.
221 if (Attr.getNumArgs() != 0) {
222 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
223 return;
224 }
225
226 // Attribute can be applied only to functions or variables.
Charles Davis163855f2010-02-16 18:27:26 +0000227 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Rafael Espindolac67f2232012-05-10 02:50:16 +0000228 if (!FD && !isa<VarDecl>(D)) {
Charles Davis163855f2010-02-16 18:27:26 +0000229 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
230 << Attr.getName() << 2 /*variable and function*/;
231 return;
232 }
233
234 // Currently, the dllexport attribute is ignored for inlined functions, unless
235 // the -fkeep-inline-functions flag has been used. Warning is emitted;
Rafael Espindolac67f2232012-05-10 02:50:16 +0000236 if (FD && FD->isInlineSpecified()) {
Charles Davis163855f2010-02-16 18:27:26 +0000237 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
238 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
239 return;
240 }
241
Rafael Espindolac67f2232012-05-10 02:50:16 +0000242 S.mergeDLLExportAttr(D, Attr.getRange(), false);
Charles Davis163855f2010-02-16 18:27:26 +0000243}
244
Charles Davisbbc0aa52010-02-10 23:06:52 +0000245namespace {
246 class X86AttributesSema : public TargetAttributesSema {
247 public:
248 X86AttributesSema() { }
249 bool ProcessDeclAttribute(Scope *scope, Decl *D,
250 const AttributeList &Attr, Sema &S) const {
Douglas Gregore8bbc122011-09-02 00:18:52 +0000251 const llvm::Triple &Triple(S.Context.getTargetInfo().getTriple());
Charles Davis163855f2010-02-16 18:27:26 +0000252 if (Triple.getOS() == llvm::Triple::Win32 ||
NAKAMURA Takumi31ea2f12011-02-17 08:51:38 +0000253 Triple.getOS() == llvm::Triple::MinGW32) {
Charles Davis163855f2010-02-16 18:27:26 +0000254 switch (Attr.getKind()) {
255 case AttributeList::AT_dllimport: HandleDLLImportAttr(D, Attr, S);
256 return true;
257 case AttributeList::AT_dllexport: HandleDLLExportAttr(D, Attr, S);
258 return true;
259 default: break;
260 }
261 }
Eli Friedman271002f2011-09-30 18:53:25 +0000262 if (Triple.getArch() != llvm::Triple::x86_64 &&
Benjamin Kramer7827da02011-09-30 20:32:22 +0000263 (Attr.getName()->getName() == "force_align_arg_pointer" ||
264 Attr.getName()->getName() == "__force_align_arg_pointer__")) {
Charles Davisbbc0aa52010-02-10 23:06:52 +0000265 HandleX86ForceAlignArgPointerAttr(D, Attr, S);
266 return true;
267 }
268 return false;
269 }
270 };
271}
272
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000273const TargetAttributesSema &Sema::getTargetAttributesSema() const {
274 if (TheTargetAttributesSema)
275 return *TheTargetAttributesSema;
276
Douglas Gregore8bbc122011-09-02 00:18:52 +0000277 const llvm::Triple &Triple(Context.getTargetInfo().getTriple());
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000278 switch (Triple.getArch()) {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000279 case llvm::Triple::msp430:
280 return *(TheTargetAttributesSema = new MSP430AttributesSema);
Wesley Peck36a1f682010-12-19 19:57:51 +0000281 case llvm::Triple::mblaze:
282 return *(TheTargetAttributesSema = new MBlazeAttributesSema);
Charles Davisbbc0aa52010-02-10 23:06:52 +0000283 case llvm::Triple::x86:
Eli Friedman271002f2011-09-30 18:53:25 +0000284 case llvm::Triple::x86_64:
Charles Davisbbc0aa52010-02-10 23:06:52 +0000285 return *(TheTargetAttributesSema = new X86AttributesSema);
Eli Friedman271002f2011-09-30 18:53:25 +0000286 default:
287 return *(TheTargetAttributesSema = new TargetAttributesSema);
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000288 }
289}