blob: 76cff1f31518aab11e748e55767da4f77ea0e6aa [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
15#include "Sema.h"
16#include "TargetAttributesSema.h"
17#include "clang/Basic/TargetInfo.h"
18#include "llvm/ADT/Triple.h"
19
20using namespace clang;
21
22TargetAttributesSema::~TargetAttributesSema() {}
23bool TargetAttributesSema::ProcessDeclAttribute(Scope *scope, Decl *D,
24 const AttributeList &Attr, Sema &S) const {
25 return false;
26}
27
28static void HandleMSP430InterruptAttr(Decl *d,
29 const AttributeList &Attr, Sema &S) {
30 // Check the attribute arguments.
31 if (Attr.getNumArgs() != 1) {
32 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
33 return;
34 }
35
36 // FIXME: Check for decl - it should be void ()(void).
37
38 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
39 llvm::APSInt NumParams(32);
40 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
41 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
42 << "interrupt" << NumParamsExpr->getSourceRange();
43 return;
44 }
45
46 unsigned Num = NumParams.getLimitedValue(255);
47 if ((Num & 1) || Num > 30) {
48 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
49 << "interrupt" << (int)NumParams.getSExtValue()
50 << NumParamsExpr->getSourceRange();
51 return;
52 }
53
54 d->addAttr(::new (S.Context) MSP430InterruptAttr(Num));
55 d->addAttr(::new (S.Context) UsedAttr());
56 }
57
58namespace {
59 class MSP430AttributesSema : public TargetAttributesSema {
60 public:
61 MSP430AttributesSema() { }
62 bool ProcessDeclAttribute(Scope *scope, Decl *D,
63 const AttributeList &Attr, Sema &S) const {
64 if (Attr.getName()->getName() == "interrupt") {
65 HandleMSP430InterruptAttr(D, Attr, S);
66 return true;
67 }
68 return false;
69 }
70 };
71}
72
Charles Davisbbc0aa52010-02-10 23:06:52 +000073static void HandleX86ForceAlignArgPointerAttr(Decl *D,
74 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) << 0;
79 return;
80 }
81
Charles Davis61170a12010-02-17 00:44:47 +000082 // If we try to apply it to a function pointer, don't warn, but don't
83 // do anything, either. It doesn't matter anyway, because there's nothing
84 // special about calling a force_align_arg_pointer function.
Charles Davisbbc0aa52010-02-10 23:06:52 +000085 ValueDecl* VD = dyn_cast<ValueDecl>(D);
Charles Davis61170a12010-02-17 00:44:47 +000086 if (VD && VD->getType()->isFunctionPointerType())
Charles Davisbbc0aa52010-02-10 23:06:52 +000087 return;
88 // Attribute can only be applied to function types.
Charles Davis42b38422010-02-10 23:26:12 +000089 if (!isa<FunctionDecl>(D)) {
Charles Davisbbc0aa52010-02-10 23:06:52 +000090 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
91 << Attr.getName() << /* function */0;
92 return;
93 }
94
95 D->addAttr(::new (S.Context) X86ForceAlignArgPointerAttr());
96}
97
Charles Davis163855f2010-02-16 18:27:26 +000098static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
99 // check the attribute arguments.
100 if (Attr.getNumArgs() != 0) {
101 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
102 return;
103 }
104
105 // Attribute can be applied only to functions or variables.
106 if (isa<VarDecl>(D)) {
107 D->addAttr(::new (S.Context) DLLImportAttr());
108 return;
109 }
110
111 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
112 if (!FD) {
113 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
114 << Attr.getName() << 2 /*variable and function*/;
115 return;
116 }
117
118 // Currently, the dllimport attribute is ignored for inlined functions.
119 // Warning is emitted.
120 if (FD->isInlineSpecified()) {
121 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
122 return;
123 }
124
125 // The attribute is also overridden by a subsequent declaration as dllexport.
126 // Warning is emitted.
127 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
128 nextAttr = nextAttr->getNext()) {
129 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
130 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
131 return;
132 }
133 }
134
135 if (D->getAttr<DLLExportAttr>()) {
136 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
137 return;
138 }
139
140 D->addAttr(::new (S.Context) DLLImportAttr());
141}
142
143static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
144 // check the attribute arguments.
145 if (Attr.getNumArgs() != 0) {
146 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
147 return;
148 }
149
150 // Attribute can be applied only to functions or variables.
151 if (isa<VarDecl>(D)) {
152 D->addAttr(::new (S.Context) DLLExportAttr());
153 return;
154 }
155
156 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
157 if (!FD) {
158 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
159 << Attr.getName() << 2 /*variable and function*/;
160 return;
161 }
162
163 // Currently, the dllexport attribute is ignored for inlined functions, unless
164 // the -fkeep-inline-functions flag has been used. Warning is emitted;
165 if (FD->isInlineSpecified()) {
166 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
167 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
168 return;
169 }
170
171 D->addAttr(::new (S.Context) DLLExportAttr());
172}
173
Charles Davisbbc0aa52010-02-10 23:06:52 +0000174namespace {
175 class X86AttributesSema : public TargetAttributesSema {
176 public:
177 X86AttributesSema() { }
178 bool ProcessDeclAttribute(Scope *scope, Decl *D,
179 const AttributeList &Attr, Sema &S) const {
Charles Davis163855f2010-02-16 18:27:26 +0000180 const llvm::Triple &Triple(S.Context.Target.getTriple());
181 if (Triple.getOS() == llvm::Triple::Win32 ||
182 Triple.getOS() == llvm::Triple::MinGW32 ||
183 Triple.getOS() == llvm::Triple::MinGW64) {
184 switch (Attr.getKind()) {
185 case AttributeList::AT_dllimport: HandleDLLImportAttr(D, Attr, S);
186 return true;
187 case AttributeList::AT_dllexport: HandleDLLExportAttr(D, Attr, S);
188 return true;
189 default: break;
190 }
191 }
Charles Davisbbc0aa52010-02-10 23:06:52 +0000192 if (Attr.getName()->getName() == "force_align_arg_pointer") {
193 HandleX86ForceAlignArgPointerAttr(D, Attr, S);
194 return true;
195 }
196 return false;
197 }
198 };
199}
200
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000201const TargetAttributesSema &Sema::getTargetAttributesSema() const {
202 if (TheTargetAttributesSema)
203 return *TheTargetAttributesSema;
204
205 const llvm::Triple &Triple(Context.Target.getTriple());
206 switch (Triple.getArch()) {
207 default:
208 return *(TheTargetAttributesSema = new TargetAttributesSema);
209
210 case llvm::Triple::msp430:
211 return *(TheTargetAttributesSema = new MSP430AttributesSema);
Charles Davisbbc0aa52010-02-10 23:06:52 +0000212 case llvm::Triple::x86:
213 return *(TheTargetAttributesSema = new X86AttributesSema);
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000214 }
215}
216