Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 1 | //===--- SemaAttr.cpp - Semantic Analysis for Attributes ------------------===// |
| 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 | // |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 10 | // This file implements semantic analysis for non-trivial attributes and |
| 11 | // pragmas. |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 15 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Lookup.h" |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 17 | #include "clang/AST/Attr.h" |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 18 | #include "clang/AST/Expr.h" |
Daniel Dunbar | 613fd67 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 19 | #include "clang/Basic/TargetInfo.h" |
| 20 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 23 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | ea75a82 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 24 | // Pragma 'pack' and 'options align' |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===// |
| 26 | |
| 27 | namespace { |
Daniel Dunbar | ae2232b | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 28 | struct PackStackEntry { |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 29 | // We just use a sentinel to represent when the stack is set to mac68k |
| 30 | // alignment. |
| 31 | static const unsigned kMac68kAlignmentSentinel = ~0U; |
| 32 | |
Daniel Dunbar | ae2232b | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 33 | unsigned Alignment; |
| 34 | IdentifierInfo *Name; |
| 35 | }; |
| 36 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 37 | /// PragmaPackStack - Simple class to wrap the stack used by #pragma |
| 38 | /// pack. |
| 39 | class PragmaPackStack { |
Daniel Dunbar | ae2232b | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 40 | typedef std::vector<PackStackEntry> stack_ty; |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 41 | |
| 42 | /// Alignment - The current user specified alignment. |
| 43 | unsigned Alignment; |
| 44 | |
| 45 | /// Stack - Entries in the #pragma pack stack, consisting of saved |
| 46 | /// alignments and optional names. |
| 47 | stack_ty Stack; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 48 | |
| 49 | public: |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 50 | PragmaPackStack() : Alignment(0) {} |
| 51 | |
| 52 | void setAlignment(unsigned A) { Alignment = A; } |
| 53 | unsigned getAlignment() { return Alignment; } |
| 54 | |
| 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) { |
Daniel Dunbar | ae2232b | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 58 | PackStackEntry PSE = { Alignment, Name }; |
| 59 | Stack.push_back(PSE); |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 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. |
Daniel Dunbar | ddc6ff6 | 2010-07-16 04:54:16 +0000 | [diff] [blame] | 66 | bool pop(IdentifierInfo *Name, bool IsReset); |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 67 | }; |
| 68 | } // end anonymous namespace. |
| 69 | |
Daniel Dunbar | ddc6ff6 | 2010-07-16 04:54:16 +0000 | [diff] [blame] | 70 | bool PragmaPackStack::pop(IdentifierInfo *Name, bool IsReset) { |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 71 | // If name is empty just pop top. |
| 72 | if (!Name) { |
Daniel Dunbar | ddc6ff6 | 2010-07-16 04:54:16 +0000 | [diff] [blame] | 73 | // An empty stack is a special case... |
| 74 | if (Stack.empty()) { |
| 75 | // If this isn't a reset, it is always an error. |
| 76 | if (!IsReset) |
| 77 | return false; |
| 78 | |
| 79 | // Otherwise, it is an error only if some alignment has been set. |
| 80 | if (!Alignment) |
| 81 | return false; |
| 82 | |
| 83 | // Otherwise, reset to the default alignment. |
| 84 | Alignment = 0; |
| 85 | } else { |
| 86 | Alignment = Stack.back().Alignment; |
| 87 | Stack.pop_back(); |
| 88 | } |
| 89 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 90 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 93 | // Otherwise, find the named record. |
| 94 | for (unsigned i = Stack.size(); i != 0; ) { |
| 95 | --i; |
Daniel Dunbar | ae2232b | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 96 | if (Stack[i].Name == Name) { |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 97 | // Found it, pop up to and including this record. |
Daniel Dunbar | ae2232b | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 98 | Alignment = Stack[i].Alignment; |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 99 | Stack.erase(Stack.begin() + i, Stack.end()); |
| 100 | return true; |
| 101 | } |
| 102 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 104 | return false; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | /// FreePackedContext - Deallocate and null out PackContext. |
| 109 | void Sema::FreePackedContext() { |
| 110 | delete static_cast<PragmaPackStack*>(PackContext); |
| 111 | PackContext = 0; |
| 112 | } |
| 113 | |
Daniel Dunbar | 9f21f89 | 2010-05-27 01:53:40 +0000 | [diff] [blame] | 114 | void Sema::AddAlignmentAttributesForRecord(RecordDecl *RD) { |
| 115 | // If there is no pack context, we don't need any attributes. |
| 116 | if (!PackContext) |
| 117 | return; |
| 118 | |
| 119 | PragmaPackStack *Stack = static_cast<PragmaPackStack*>(PackContext); |
| 120 | |
| 121 | // Otherwise, check to see if we need a max field alignment attribute. |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 122 | if (unsigned Alignment = Stack->getAlignment()) { |
| 123 | if (Alignment == PackStackEntry::kMac68kAlignmentSentinel) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 124 | RD->addAttr(::new (Context) AlignMac68kAttr(SourceLocation(), Context)); |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 125 | else |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 126 | RD->addAttr(::new (Context) MaxFieldAlignmentAttr(SourceLocation(), |
| 127 | Context, |
| 128 | Alignment * 8)); |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 129 | } |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Daniel Dunbar | ea75a82 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 132 | void Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind, |
| 133 | SourceLocation PragmaLoc, |
| 134 | SourceLocation KindLoc) { |
| 135 | if (PackContext == 0) |
| 136 | PackContext = new PragmaPackStack(); |
| 137 | |
| 138 | PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext); |
| 139 | |
Daniel Dunbar | ddc6ff6 | 2010-07-16 04:54:16 +0000 | [diff] [blame] | 140 | // Reset just pops the top of the stack, or resets the current alignment to |
| 141 | // default. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 142 | if (Kind == Sema::POAK_Reset) { |
Daniel Dunbar | ddc6ff6 | 2010-07-16 04:54:16 +0000 | [diff] [blame] | 143 | if (!Context->pop(0, /*IsReset=*/true)) { |
Daniel Dunbar | ea75a82 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 144 | Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed) |
| 145 | << "stack empty"; |
| 146 | } |
| 147 | return; |
| 148 | } |
| 149 | |
Daniel Dunbar | ea75a82 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 150 | switch (Kind) { |
Daniel Dunbar | 638e7cf | 2010-05-27 18:42:09 +0000 | [diff] [blame] | 151 | // For all targets we support native and natural are the same. |
| 152 | // |
| 153 | // FIXME: This is not true on Darwin/PPC. |
| 154 | case POAK_Native: |
Daniel Dunbar | 450f793 | 2010-05-28 19:43:33 +0000 | [diff] [blame] | 155 | case POAK_Power: |
Daniel Dunbar | d6b305d | 2010-05-28 20:08:00 +0000 | [diff] [blame] | 156 | case POAK_Natural: |
Daniel Dunbar | 450f793 | 2010-05-28 19:43:33 +0000 | [diff] [blame] | 157 | Context->push(0); |
| 158 | Context->setAlignment(0); |
| 159 | break; |
| 160 | |
Daniel Dunbar | 6f73914 | 2010-05-27 18:42:17 +0000 | [diff] [blame] | 161 | // Note that '#pragma options align=packed' is not equivalent to attribute |
| 162 | // packed, it has a different precedence relative to attribute aligned. |
| 163 | case POAK_Packed: |
| 164 | Context->push(0); |
| 165 | Context->setAlignment(1); |
| 166 | break; |
| 167 | |
Daniel Dunbar | 613fd67 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 168 | case POAK_Mac68k: |
| 169 | // Check if the target supports this. |
| 170 | if (!PP.getTargetInfo().hasAlignMac68kSupport()) { |
| 171 | Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported); |
| 172 | return; |
Daniel Dunbar | 613fd67 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 173 | } |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 174 | Context->push(0); |
| 175 | Context->setAlignment(PackStackEntry::kMac68kAlignmentSentinel); |
Daniel Dunbar | 613fd67 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 176 | break; |
| 177 | |
Daniel Dunbar | ea75a82 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 178 | default: |
| 179 | Diag(PragmaLoc, diag::warn_pragma_options_align_unsupported_option) |
| 180 | << KindLoc; |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 185 | void Sema::ActOnPragmaPack(PragmaPackKind Kind, IdentifierInfo *Name, |
| 186 | ExprTy *alignment, SourceLocation PragmaLoc, |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 187 | SourceLocation LParenLoc, SourceLocation RParenLoc) { |
| 188 | Expr *Alignment = static_cast<Expr *>(alignment); |
| 189 | |
| 190 | // If specified then alignment must be a "small" power of two. |
| 191 | unsigned AlignmentVal = 0; |
| 192 | if (Alignment) { |
| 193 | llvm::APSInt Val; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 194 | |
Daniel Dunbar | 79cd116 | 2009-03-06 20:45:54 +0000 | [diff] [blame] | 195 | // pack(0) is like pack(), which just works out since that is what |
| 196 | // we use 0 for in PackAttr. |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 197 | if (Alignment->isTypeDependent() || |
| 198 | Alignment->isValueDependent() || |
| 199 | !Alignment->isIntegerConstantExpr(Val, Context) || |
Daniel Dunbar | 79cd116 | 2009-03-06 20:45:54 +0000 | [diff] [blame] | 200 | !(Val == 0 || Val.isPowerOf2()) || |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 201 | Val.getZExtValue() > 16) { |
| 202 | Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 203 | return; // Ignore |
| 204 | } |
| 205 | |
| 206 | AlignmentVal = (unsigned) Val.getZExtValue(); |
| 207 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 208 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 209 | if (PackContext == 0) |
| 210 | PackContext = new PragmaPackStack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 211 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 212 | PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 213 | |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 214 | switch (Kind) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 215 | case Sema::PPK_Default: // pack([n]) |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 216 | Context->setAlignment(AlignmentVal); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 217 | break; |
| 218 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 219 | case Sema::PPK_Show: // pack(show) |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 220 | // Show the current alignment, making sure to show the right value |
| 221 | // for the default. |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 222 | AlignmentVal = Context->getAlignment(); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 223 | // FIXME: This should come from the target. |
| 224 | if (AlignmentVal == 0) |
| 225 | AlignmentVal = 8; |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 226 | if (AlignmentVal == PackStackEntry::kMac68kAlignmentSentinel) |
| 227 | Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k"; |
| 228 | else |
| 229 | Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal; |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 230 | break; |
| 231 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 232 | case Sema::PPK_Push: // pack(push [, id] [, [n]) |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 233 | Context->push(Name); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 234 | // Set the new alignment if specified. |
| 235 | if (Alignment) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 236 | Context->setAlignment(AlignmentVal); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 237 | break; |
| 238 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 239 | case Sema::PPK_Pop: // pack(pop [, id] [, n]) |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 240 | // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack: |
| 241 | // "#pragma pack(pop, identifier, n) is undefined" |
| 242 | if (Alignment && Name) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 243 | Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifer_and_alignment); |
| 244 | |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 245 | // Do the pop. |
Daniel Dunbar | ddc6ff6 | 2010-07-16 04:54:16 +0000 | [diff] [blame] | 246 | if (!Context->pop(Name, /*IsReset=*/false)) { |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 247 | // If a name was specified then failure indicates the name |
| 248 | // wasn't found. Otherwise failure indicates the stack was |
| 249 | // empty. |
| 250 | Diag(PragmaLoc, diag::warn_pragma_pack_pop_failed) |
| 251 | << (Name ? "no record matching name" : "stack empty"); |
| 252 | |
| 253 | // FIXME: Warn about popping named records as MSVC does. |
| 254 | } else { |
| 255 | // Pop succeeded, set the new alignment if specified. |
| 256 | if (Alignment) |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 257 | Context->setAlignment(AlignmentVal); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 258 | } |
| 259 | break; |
| 260 | |
| 261 | default: |
| 262 | assert(0 && "Invalid #pragma pack kind."); |
| 263 | } |
| 264 | } |
| 265 | |
Argyrios Kyrtzidis | b918d0f | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 266 | void Sema::ActOnPragmaUnused(const Token &IdTok, Scope *curScope, |
| 267 | SourceLocation PragmaLoc) { |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 268 | |
Argyrios Kyrtzidis | b918d0f | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 269 | IdentifierInfo *Name = IdTok.getIdentifierInfo(); |
| 270 | LookupResult Lookup(*this, Name, IdTok.getLocation(), LookupOrdinaryName); |
| 271 | LookupParsedName(Lookup, curScope, NULL, true); |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 272 | |
Argyrios Kyrtzidis | b918d0f | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 273 | if (Lookup.empty()) { |
| 274 | Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var) |
| 275 | << Name << SourceRange(IdTok.getLocation()); |
| 276 | return; |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 277 | } |
Argyrios Kyrtzidis | b918d0f | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 278 | |
| 279 | VarDecl *VD = Lookup.getAsSingle<VarDecl>(); |
Argyrios Kyrtzidis | 2a5c45b | 2011-01-27 18:16:48 +0000 | [diff] [blame] | 280 | if (!VD) { |
| 281 | Diag(PragmaLoc, diag::warn_pragma_unused_expected_var_arg) |
Argyrios Kyrtzidis | b918d0f | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 282 | << Name << SourceRange(IdTok.getLocation()); |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | // Warn if this was used before being marked unused. |
| 287 | if (VD->isUsed()) |
| 288 | Diag(PragmaLoc, diag::warn_used_but_marked_unused) << Name; |
| 289 | |
| 290 | VD->addAttr(::new (Context) UnusedAttr(IdTok.getLocation(), Context)); |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 291 | } |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 292 | |
John McCall | 90f1450 | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 293 | typedef std::vector<std::pair<unsigned, SourceLocation> > VisStack; |
| 294 | enum { NoVisibility = (unsigned) -1 }; |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 295 | |
| 296 | void Sema::AddPushedVisibilityAttribute(Decl *D) { |
| 297 | if (!VisContext) |
| 298 | return; |
| 299 | |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 300 | if (isa<NamedDecl>(D) && cast<NamedDecl>(D)->getExplicitVisibility()) |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 301 | return; |
| 302 | |
| 303 | VisStack *Stack = static_cast<VisStack*>(VisContext); |
John McCall | 90f1450 | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 304 | unsigned rawType = Stack->back().first; |
| 305 | if (rawType == NoVisibility) return; |
| 306 | |
| 307 | VisibilityAttr::VisibilityType type |
| 308 | = (VisibilityAttr::VisibilityType) rawType; |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 309 | SourceLocation loc = Stack->back().second; |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 310 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 311 | D->addAttr(::new (Context) VisibilityAttr(loc, Context, type)); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /// FreeVisContext - Deallocate and null out VisContext. |
| 315 | void Sema::FreeVisContext() { |
| 316 | delete static_cast<VisStack*>(VisContext); |
| 317 | VisContext = 0; |
| 318 | } |
| 319 | |
John McCall | 90f1450 | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 320 | static void PushPragmaVisibility(Sema &S, unsigned type, SourceLocation loc) { |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 321 | // Put visibility on stack. |
| 322 | if (!S.VisContext) |
| 323 | S.VisContext = new VisStack; |
| 324 | |
| 325 | VisStack *Stack = static_cast<VisStack*>(S.VisContext); |
| 326 | Stack->push_back(std::make_pair(type, loc)); |
| 327 | } |
| 328 | |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 329 | void Sema::ActOnPragmaVisibility(bool IsPush, const IdentifierInfo* VisType, |
| 330 | SourceLocation PragmaLoc) { |
| 331 | if (IsPush) { |
| 332 | // Compute visibility to use. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 333 | VisibilityAttr::VisibilityType type; |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 334 | if (VisType->isStr("default")) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 335 | type = VisibilityAttr::Default; |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 336 | else if (VisType->isStr("hidden")) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 337 | type = VisibilityAttr::Hidden; |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 338 | else if (VisType->isStr("internal")) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 339 | type = VisibilityAttr::Hidden; // FIXME |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 340 | else if (VisType->isStr("protected")) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 341 | type = VisibilityAttr::Protected; |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 342 | else { |
| 343 | Diag(PragmaLoc, diag::warn_attribute_unknown_visibility) << |
| 344 | VisType->getName(); |
| 345 | return; |
| 346 | } |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 347 | PushPragmaVisibility(*this, type, PragmaLoc); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 348 | } else { |
| 349 | PopPragmaVisibility(); |
| 350 | } |
| 351 | } |
| 352 | |
Peter Collingbourne | 321b817 | 2011-02-14 01:42:35 +0000 | [diff] [blame] | 353 | void Sema::ActOnPragmaFPContract(tok::OnOffSwitch OOS) { |
| 354 | switch (OOS) { |
| 355 | case tok::OOS_ON: |
| 356 | FPFeatures.fp_contract = 1; |
| 357 | break; |
| 358 | case tok::OOS_OFF: |
| 359 | FPFeatures.fp_contract = 0; |
| 360 | break; |
| 361 | case tok::OOS_DEFAULT: |
| 362 | FPFeatures.fp_contract = getLangOptions().DefaultFPContract; |
| 363 | break; |
| 364 | } |
| 365 | } |
| 366 | |
John McCall | 90f1450 | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 367 | void Sema::PushNamespaceVisibilityAttr(const VisibilityAttr *Attr) { |
| 368 | // Visibility calculations will consider the namespace's visibility. |
| 369 | // Here we just want to note that we're in a visibility context |
| 370 | // which overrides any enclosing #pragma context, but doesn't itself |
| 371 | // contribute visibility. |
| 372 | PushPragmaVisibility(*this, NoVisibility, SourceLocation()); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | void Sema::PopPragmaVisibility() { |
| 376 | // Pop visibility from stack, if there is one on the stack. |
| 377 | if (VisContext) { |
| 378 | VisStack *Stack = static_cast<VisStack*>(VisContext); |
| 379 | |
| 380 | Stack->pop_back(); |
| 381 | // To simplify the implementation, never keep around an empty stack. |
| 382 | if (Stack->empty()) |
| 383 | FreeVisContext(); |
| 384 | } |
| 385 | // FIXME: Add diag for pop without push. |
| 386 | } |