1 //===--- SemaAttr.cpp - Semantic Analysis for Attributes ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements semantic analysis for non-trivial attributes and
13 //===----------------------------------------------------------------------===//
15 #include "clang/Sema/SemaInternal.h"
16 #include "clang/Sema/Lookup.h"
17 #include "clang/AST/Attr.h"
18 #include "clang/AST/Expr.h"
19 #include "clang/Basic/TargetInfo.h"
20 #include "clang/Lex/Preprocessor.h"
21 using namespace clang;
23 //===----------------------------------------------------------------------===//
24 // Pragma 'pack' and 'options align'
25 //===----------------------------------------------------------------------===//
28 struct PackStackEntry {
29 // We just use a sentinel to represent when the stack is set to mac68k
31 static const unsigned kMac68kAlignmentSentinel = ~0U;
37 /// PragmaPackStack - Simple class to wrap the stack used by #pragma
39 class PragmaPackStack {
40 typedef std::vector<PackStackEntry> stack_ty;
42 /// Alignment - The current user specified alignment.
45 /// Stack - Entries in the #pragma pack stack, consisting of saved
46 /// alignments and optional names.
50 PragmaPackStack() : Alignment(0) {}
52 void setAlignment(unsigned A) { Alignment = A; }
53 unsigned getAlignment() { return Alignment; }
55 /// push - Push the current alignment onto the stack, optionally
56 /// using the given \arg Name for the record, if non-zero.
57 void push(IdentifierInfo *Name) {
58 PackStackEntry PSE = { Alignment, Name };
62 /// pop - Pop a record from the stack and restore the current
63 /// alignment to the previous value. If \arg Name is non-zero then
64 /// the first such named record is popped, otherwise the top record
65 /// is popped. Returns true if the pop succeeded.
66 bool pop(IdentifierInfo *Name, bool IsReset);
68 } // end anonymous namespace.
70 bool PragmaPackStack::pop(IdentifierInfo *Name, bool IsReset) {
71 // If name is empty just pop top.
73 // An empty stack is a special case...
75 // If this isn't a reset, it is always an error.
79 // Otherwise, it is an error only if some alignment has been set.
83 // Otherwise, reset to the default alignment.
86 Alignment = Stack.back().Alignment;
93 // Otherwise, find the named record.
94 for (unsigned i = Stack.size(); i != 0; ) {
96 if (Stack[i].Name == Name) {
97 // Found it, pop up to and including this record.
98 Alignment = Stack[i].Alignment;
99 Stack.erase(Stack.begin() + i, Stack.end());
108 /// FreePackedContext - Deallocate and null out PackContext.
109 void Sema::FreePackedContext() {
110 delete static_cast<PragmaPackStack*>(PackContext);
114 void Sema::AddAlignmentAttributesForRecord(RecordDecl *RD) {
115 // If there is no pack context, we don't need any attributes.
119 PragmaPackStack *Stack = static_cast<PragmaPackStack*>(PackContext);
121 // Otherwise, check to see if we need a max field alignment attribute.
122 if (unsigned Alignment = Stack->getAlignment()) {
123 if (Alignment == PackStackEntry::kMac68kAlignmentSentinel)
124 RD->addAttr(::new (Context) AlignMac68kAttr(SourceLocation(), Context));
126 RD->addAttr(::new (Context) MaxFieldAlignmentAttr(SourceLocation(),
132 void Sema::AddMsStructLayoutForRecord(RecordDecl *RD) {
133 if (!MSStructPragmaOn)
135 RD->addAttr(::new (Context) MsStructAttr(SourceLocation(), Context));
138 void Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind,
139 SourceLocation PragmaLoc,
140 SourceLocation KindLoc) {
141 if (PackContext == 0)
142 PackContext = new PragmaPackStack();
144 PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext);
146 // Reset just pops the top of the stack, or resets the current alignment to
148 if (Kind == Sema::POAK_Reset) {
149 if (!Context->pop(0, /*IsReset=*/true)) {
150 Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed)
157 // For all targets we support native and natural are the same.
159 // FIXME: This is not true on Darwin/PPC.
164 Context->setAlignment(0);
167 // Note that '#pragma options align=packed' is not equivalent to attribute
168 // packed, it has a different precedence relative to attribute aligned.
171 Context->setAlignment(1);
175 // Check if the target supports this.
176 if (!PP.getTargetInfo().hasAlignMac68kSupport()) {
177 Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported);
181 Context->setAlignment(PackStackEntry::kMac68kAlignmentSentinel);
185 Diag(PragmaLoc, diag::warn_pragma_options_align_unsupported_option)
191 void Sema::ActOnPragmaPack(PragmaPackKind Kind, IdentifierInfo *Name,
192 ExprTy *alignment, SourceLocation PragmaLoc,
193 SourceLocation LParenLoc, SourceLocation RParenLoc) {
194 Expr *Alignment = static_cast<Expr *>(alignment);
196 // If specified then alignment must be a "small" power of two.
197 unsigned AlignmentVal = 0;
201 // pack(0) is like pack(), which just works out since that is what
202 // we use 0 for in PackAttr.
203 if (Alignment->isTypeDependent() ||
204 Alignment->isValueDependent() ||
205 !Alignment->isIntegerConstantExpr(Val, Context) ||
206 !(Val == 0 || Val.isPowerOf2()) ||
207 Val.getZExtValue() > 16) {
208 Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment);
212 AlignmentVal = (unsigned) Val.getZExtValue();
215 if (PackContext == 0)
216 PackContext = new PragmaPackStack();
218 PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext);
221 case Sema::PPK_Default: // pack([n])
222 Context->setAlignment(AlignmentVal);
225 case Sema::PPK_Show: // pack(show)
226 // Show the current alignment, making sure to show the right value
228 AlignmentVal = Context->getAlignment();
229 // FIXME: This should come from the target.
230 if (AlignmentVal == 0)
232 if (AlignmentVal == PackStackEntry::kMac68kAlignmentSentinel)
233 Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k";
235 Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal;
238 case Sema::PPK_Push: // pack(push [, id] [, [n])
240 // Set the new alignment if specified.
242 Context->setAlignment(AlignmentVal);
245 case Sema::PPK_Pop: // pack(pop [, id] [, n])
246 // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack:
247 // "#pragma pack(pop, identifier, n) is undefined"
248 if (Alignment && Name)
249 Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifer_and_alignment);
252 if (!Context->pop(Name, /*IsReset=*/false)) {
253 // If a name was specified then failure indicates the name
254 // wasn't found. Otherwise failure indicates the stack was
256 Diag(PragmaLoc, diag::warn_pragma_pack_pop_failed)
257 << (Name ? "no record matching name" : "stack empty");
259 // FIXME: Warn about popping named records as MSVC does.
261 // Pop succeeded, set the new alignment if specified.
263 Context->setAlignment(AlignmentVal);
268 assert(0 && "Invalid #pragma pack kind.");
272 void Sema::ActOnPragmaMSStruct(PragmaMSStructKind Kind) {
273 MSStructPragmaOn = (Kind == PMSST_ON);
276 void Sema::ActOnPragmaUnused(const Token &IdTok, Scope *curScope,
277 SourceLocation PragmaLoc) {
279 IdentifierInfo *Name = IdTok.getIdentifierInfo();
280 LookupResult Lookup(*this, Name, IdTok.getLocation(), LookupOrdinaryName);
281 LookupParsedName(Lookup, curScope, NULL, true);
283 if (Lookup.empty()) {
284 Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var)
285 << Name << SourceRange(IdTok.getLocation());
289 VarDecl *VD = Lookup.getAsSingle<VarDecl>();
291 Diag(PragmaLoc, diag::warn_pragma_unused_expected_var_arg)
292 << Name << SourceRange(IdTok.getLocation());
296 // Warn if this was used before being marked unused.
298 Diag(PragmaLoc, diag::warn_used_but_marked_unused) << Name;
300 VD->addAttr(::new (Context) UnusedAttr(IdTok.getLocation(), Context));
303 typedef std::vector<std::pair<unsigned, SourceLocation> > VisStack;
304 enum { NoVisibility = (unsigned) -1 };
306 void Sema::AddPushedVisibilityAttribute(Decl *D) {
310 if (isa<NamedDecl>(D) && cast<NamedDecl>(D)->getExplicitVisibility())
313 VisStack *Stack = static_cast<VisStack*>(VisContext);
314 unsigned rawType = Stack->back().first;
315 if (rawType == NoVisibility) return;
317 VisibilityAttr::VisibilityType type
318 = (VisibilityAttr::VisibilityType) rawType;
319 SourceLocation loc = Stack->back().second;
321 D->addAttr(::new (Context) VisibilityAttr(loc, Context, type));
324 /// FreeVisContext - Deallocate and null out VisContext.
325 void Sema::FreeVisContext() {
326 delete static_cast<VisStack*>(VisContext);
330 static void PushPragmaVisibility(Sema &S, unsigned type, SourceLocation loc) {
331 // Put visibility on stack.
333 S.VisContext = new VisStack;
335 VisStack *Stack = static_cast<VisStack*>(S.VisContext);
336 Stack->push_back(std::make_pair(type, loc));
339 void Sema::ActOnPragmaVisibility(bool IsPush, const IdentifierInfo* VisType,
340 SourceLocation PragmaLoc) {
342 // Compute visibility to use.
343 VisibilityAttr::VisibilityType type;
344 if (VisType->isStr("default"))
345 type = VisibilityAttr::Default;
346 else if (VisType->isStr("hidden"))
347 type = VisibilityAttr::Hidden;
348 else if (VisType->isStr("internal"))
349 type = VisibilityAttr::Hidden; // FIXME
350 else if (VisType->isStr("protected"))
351 type = VisibilityAttr::Protected;
353 Diag(PragmaLoc, diag::warn_attribute_unknown_visibility) <<
357 PushPragmaVisibility(*this, type, PragmaLoc);
359 PopPragmaVisibility();
363 void Sema::ActOnPragmaFPContract(tok::OnOffSwitch OOS) {
366 FPFeatures.fp_contract = 1;
369 FPFeatures.fp_contract = 0;
371 case tok::OOS_DEFAULT:
372 FPFeatures.fp_contract = getLangOptions().DefaultFPContract;
377 void Sema::PushNamespaceVisibilityAttr(const VisibilityAttr *Attr) {
378 // Visibility calculations will consider the namespace's visibility.
379 // Here we just want to note that we're in a visibility context
380 // which overrides any enclosing #pragma context, but doesn't itself
381 // contribute visibility.
382 PushPragmaVisibility(*this, NoVisibility, SourceLocation());
385 void Sema::PopPragmaVisibility() {
386 // Pop visibility from stack, if there is one on the stack.
388 VisStack *Stack = static_cast<VisStack*>(VisContext);
391 // To simplify the implementation, never keep around an empty stack.
395 // FIXME: Add diag for pop without push.