Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SemaDeclSpec.cpp - Declaration Specifier Semantic Analysis -------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for declaration specifiers. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 15 | #include "clang/Parse/ParseDiagnostic.h" |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 16 | #include "clang/Lex/Preprocessor.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | #include "clang/Basic/LangOptions.h" |
Chris Lattner | 5af2f35 | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 19 | #include <cstring> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
Chris Lattner | 254be6a | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 22 | |
| 23 | static DiagnosticBuilder Diag(Diagnostic &D, SourceLocation Loc, |
| 24 | SourceManager &SrcMgr, unsigned DiagID) { |
| 25 | return D.Report(FullSourceLoc(Loc, SrcMgr), DiagID); |
| 26 | } |
| 27 | |
Chris Lattner | 5af2f35 | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 28 | /// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function. |
| 29 | /// "TheDeclarator" is the declarator that this will be added to. |
| 30 | DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto, bool isVariadic, |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 31 | SourceLocation EllipsisLoc, |
Chris Lattner | 5af2f35 | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 32 | ParamInfo *ArgInfo, |
| 33 | unsigned NumArgs, |
| 34 | unsigned TypeQuals, |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 35 | bool hasExceptionSpec, |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 36 | SourceLocation ThrowLoc, |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 37 | bool hasAnyExceptionSpec, |
| 38 | ActionBase::TypeTy **Exceptions, |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 39 | SourceRange *ExceptionRanges, |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 40 | unsigned NumExceptions, |
Chris Lattner | 5af2f35 | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 41 | SourceLocation Loc, |
| 42 | Declarator &TheDeclarator) { |
| 43 | DeclaratorChunk I; |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 44 | I.Kind = Function; |
| 45 | I.Loc = Loc; |
| 46 | I.Fun.hasPrototype = hasProto; |
| 47 | I.Fun.isVariadic = isVariadic; |
| 48 | I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding(); |
| 49 | I.Fun.DeleteArgInfo = false; |
| 50 | I.Fun.TypeQuals = TypeQuals; |
| 51 | I.Fun.NumArgs = NumArgs; |
| 52 | I.Fun.ArgInfo = 0; |
| 53 | I.Fun.hasExceptionSpec = hasExceptionSpec; |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 54 | I.Fun.ThrowLoc = ThrowLoc.getRawEncoding(); |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 55 | I.Fun.hasAnyExceptionSpec = hasAnyExceptionSpec; |
| 56 | I.Fun.NumExceptions = NumExceptions; |
| 57 | I.Fun.Exceptions = 0; |
| 58 | |
Chris Lattner | 5af2f35 | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 59 | // new[] an argument array if needed. |
| 60 | if (NumArgs) { |
| 61 | // If the 'InlineParams' in Declarator is unused and big enough, put our |
| 62 | // parameter list there (in an effort to avoid new/delete traffic). If it |
| 63 | // is already used (consider a function returning a function pointer) or too |
| 64 | // small (function taking too many arguments), go to the heap. |
| 65 | if (!TheDeclarator.InlineParamsUsed && |
| 66 | NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) { |
| 67 | I.Fun.ArgInfo = TheDeclarator.InlineParams; |
| 68 | I.Fun.DeleteArgInfo = false; |
| 69 | TheDeclarator.InlineParamsUsed = true; |
| 70 | } else { |
| 71 | I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs]; |
| 72 | I.Fun.DeleteArgInfo = true; |
| 73 | } |
| 74 | memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs); |
| 75 | } |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 76 | // new[] an exception array if needed |
| 77 | if (NumExceptions) { |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 78 | I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions]; |
| 79 | for (unsigned i = 0; i != NumExceptions; ++i) { |
| 80 | I.Fun.Exceptions[i].Ty = Exceptions[i]; |
| 81 | I.Fun.Exceptions[i].Range = ExceptionRanges[i]; |
| 82 | } |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 83 | } |
Chris Lattner | 5af2f35 | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 84 | return I; |
| 85 | } |
Chris Lattner | 254be6a | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 86 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 87 | /// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this |
Chris Lattner | 2a327d1 | 2009-02-27 18:35:46 +0000 | [diff] [blame] | 88 | /// declaration specifier includes. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 89 | /// |
| 90 | unsigned DeclSpec::getParsedSpecifiers() const { |
| 91 | unsigned Res = 0; |
| 92 | if (StorageClassSpec != SCS_unspecified || |
| 93 | SCS_thread_specified) |
| 94 | Res |= PQ_StorageClassSpecifier; |
Mike Stump | d420433 | 2008-06-19 19:52:46 +0000 | [diff] [blame] | 95 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 96 | if (TypeQualifiers != TQ_unspecified) |
| 97 | Res |= PQ_TypeQualifier; |
| 98 | |
| 99 | if (hasTypeSpecifier()) |
| 100 | Res |= PQ_TypeSpecifier; |
| 101 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 102 | if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 103 | Res |= PQ_FunctionSpecifier; |
| 104 | return Res; |
| 105 | } |
| 106 | |
| 107 | const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) { |
| 108 | switch (S) { |
| 109 | default: assert(0 && "Unknown typespec!"); |
| 110 | case DeclSpec::SCS_unspecified: return "unspecified"; |
| 111 | case DeclSpec::SCS_typedef: return "typedef"; |
| 112 | case DeclSpec::SCS_extern: return "extern"; |
| 113 | case DeclSpec::SCS_static: return "static"; |
| 114 | case DeclSpec::SCS_auto: return "auto"; |
| 115 | case DeclSpec::SCS_register: return "register"; |
Eli Friedman | 63054b3 | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 116 | case DeclSpec::SCS_private_extern: return "__private_extern__"; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 117 | case DeclSpec::SCS_mutable: return "mutable"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
Steve Naroff | 9532414 | 2008-02-12 04:08:59 +0000 | [diff] [blame] | 121 | bool DeclSpec::BadSpecifier(SCS S, const char *&PrevSpec) { |
Steve Naroff | 9532414 | 2008-02-12 04:08:59 +0000 | [diff] [blame] | 122 | PrevSpec = getSpecifierName(S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 123 | return true; |
| 124 | } |
| 125 | |
Steve Naroff | 9532414 | 2008-02-12 04:08:59 +0000 | [diff] [blame] | 126 | bool DeclSpec::BadSpecifier(TSW W, const char *&PrevSpec) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 127 | switch (W) { |
Steve Naroff | 9532414 | 2008-02-12 04:08:59 +0000 | [diff] [blame] | 128 | case TSW_unspecified: PrevSpec = "unspecified"; break; |
| 129 | case TSW_short: PrevSpec = "short"; break; |
| 130 | case TSW_long: PrevSpec = "long"; break; |
| 131 | case TSW_longlong: PrevSpec = "long long"; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 132 | } |
| 133 | return true; |
| 134 | } |
| 135 | |
Steve Naroff | 9532414 | 2008-02-12 04:08:59 +0000 | [diff] [blame] | 136 | bool DeclSpec::BadSpecifier(TSC C, const char *&PrevSpec) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 137 | switch (C) { |
Steve Naroff | 9532414 | 2008-02-12 04:08:59 +0000 | [diff] [blame] | 138 | case TSC_unspecified: PrevSpec = "unspecified"; break; |
| 139 | case TSC_imaginary: PrevSpec = "imaginary"; break; |
| 140 | case TSC_complex: PrevSpec = "complex"; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 141 | } |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | |
Steve Naroff | 9532414 | 2008-02-12 04:08:59 +0000 | [diff] [blame] | 146 | bool DeclSpec::BadSpecifier(TSS S, const char *&PrevSpec) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 147 | switch (S) { |
Steve Naroff | 9532414 | 2008-02-12 04:08:59 +0000 | [diff] [blame] | 148 | case TSS_unspecified: PrevSpec = "unspecified"; break; |
| 149 | case TSS_signed: PrevSpec = "signed"; break; |
| 150 | case TSS_unsigned: PrevSpec = "unsigned"; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 151 | } |
| 152 | return true; |
| 153 | } |
| 154 | |
| 155 | const char *DeclSpec::getSpecifierName(DeclSpec::TST T) { |
| 156 | switch (T) { |
| 157 | default: assert(0 && "Unknown typespec!"); |
| 158 | case DeclSpec::TST_unspecified: return "unspecified"; |
| 159 | case DeclSpec::TST_void: return "void"; |
| 160 | case DeclSpec::TST_char: return "char"; |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 161 | case DeclSpec::TST_wchar: return "wchar_t"; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame^] | 162 | case DeclSpec::TST_char16: return "char16_t"; |
| 163 | case DeclSpec::TST_char32: return "char32_t"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 164 | case DeclSpec::TST_int: return "int"; |
| 165 | case DeclSpec::TST_float: return "float"; |
| 166 | case DeclSpec::TST_double: return "double"; |
| 167 | case DeclSpec::TST_bool: return "_Bool"; |
| 168 | case DeclSpec::TST_decimal32: return "_Decimal32"; |
| 169 | case DeclSpec::TST_decimal64: return "_Decimal64"; |
| 170 | case DeclSpec::TST_decimal128: return "_Decimal128"; |
| 171 | case DeclSpec::TST_enum: return "enum"; |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 172 | case DeclSpec::TST_class: return "class"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 173 | case DeclSpec::TST_union: return "union"; |
| 174 | case DeclSpec::TST_struct: return "struct"; |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 175 | case DeclSpec::TST_typename: return "type-name"; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 176 | case DeclSpec::TST_typeofType: |
| 177 | case DeclSpec::TST_typeofExpr: return "typeof"; |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 178 | case DeclSpec::TST_auto: return "auto"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
Steve Naroff | 9532414 | 2008-02-12 04:08:59 +0000 | [diff] [blame] | 182 | bool DeclSpec::BadSpecifier(TST T, const char *&PrevSpec) { |
Steve Naroff | 9532414 | 2008-02-12 04:08:59 +0000 | [diff] [blame] | 183 | PrevSpec = getSpecifierName(T); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 184 | return true; |
| 185 | } |
| 186 | |
Steve Naroff | 9532414 | 2008-02-12 04:08:59 +0000 | [diff] [blame] | 187 | bool DeclSpec::BadSpecifier(TQ T, const char *&PrevSpec) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 188 | switch (T) { |
| 189 | case DeclSpec::TQ_unspecified: PrevSpec = "unspecified"; break; |
| 190 | case DeclSpec::TQ_const: PrevSpec = "const"; break; |
| 191 | case DeclSpec::TQ_restrict: PrevSpec = "restrict"; break; |
| 192 | case DeclSpec::TQ_volatile: PrevSpec = "volatile"; break; |
| 193 | } |
| 194 | return true; |
| 195 | } |
| 196 | |
| 197 | bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc, |
| 198 | const char *&PrevSpec) { |
| 199 | if (StorageClassSpec != SCS_unspecified) |
Chris Lattner | 254be6a | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 200 | return BadSpecifier((SCS)StorageClassSpec, PrevSpec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 201 | StorageClassSpec = S; |
| 202 | StorageClassSpecLoc = Loc; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 203 | assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 204 | return false; |
| 205 | } |
| 206 | |
| 207 | bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc, |
| 208 | const char *&PrevSpec) { |
| 209 | if (SCS_thread_specified) { |
| 210 | PrevSpec = "__thread"; |
| 211 | return true; |
| 212 | } |
| 213 | SCS_thread_specified = true; |
| 214 | SCS_threadLoc = Loc; |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | |
| 219 | /// These methods set the specified attribute of the DeclSpec, but return true |
| 220 | /// and ignore the request if invalid (e.g. "extern" then "auto" is |
| 221 | /// specified). |
| 222 | bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc, |
| 223 | const char *&PrevSpec) { |
| 224 | if (TypeSpecWidth != TSW_unspecified && |
| 225 | // Allow turning long -> long long. |
| 226 | (W != TSW_longlong || TypeSpecWidth != TSW_long)) |
Chris Lattner | 254be6a | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 227 | return BadSpecifier((TSW)TypeSpecWidth, PrevSpec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 228 | TypeSpecWidth = W; |
| 229 | TSWLoc = Loc; |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc, |
| 234 | const char *&PrevSpec) { |
| 235 | if (TypeSpecComplex != TSC_unspecified) |
Chris Lattner | 254be6a | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 236 | return BadSpecifier((TSC)TypeSpecComplex, PrevSpec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 237 | TypeSpecComplex = C; |
| 238 | TSCLoc = Loc; |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc, |
| 243 | const char *&PrevSpec) { |
| 244 | if (TypeSpecSign != TSS_unspecified) |
Chris Lattner | 254be6a | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 245 | return BadSpecifier((TSS)TypeSpecSign, PrevSpec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 246 | TypeSpecSign = S; |
| 247 | TSSLoc = Loc; |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc, |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 252 | const char *&PrevSpec, void *Rep, |
| 253 | bool Owned) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 254 | if (TypeSpecType != TST_unspecified) |
Chris Lattner | 254be6a | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 255 | return BadSpecifier((TST)TypeSpecType, PrevSpec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 256 | TypeSpecType = T; |
| 257 | TypeRep = Rep; |
| 258 | TSTLoc = Loc; |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 259 | TypeSpecOwned = Owned; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 260 | return false; |
| 261 | } |
| 262 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 263 | bool DeclSpec::SetTypeSpecError() { |
| 264 | TypeSpecType = TST_error; |
| 265 | TypeRep = 0; |
| 266 | TSTLoc = SourceLocation(); |
| 267 | return false; |
| 268 | } |
| 269 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 270 | bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec, |
| 271 | const LangOptions &Lang) { |
| 272 | // Duplicates turn into warnings pre-C99. |
| 273 | if ((TypeQualifiers & T) && !Lang.C99) |
| 274 | return BadSpecifier(T, PrevSpec); |
| 275 | TypeQualifiers |= T; |
| 276 | |
| 277 | switch (T) { |
| 278 | default: assert(0 && "Unknown type qualifier!"); |
| 279 | case TQ_const: TQ_constLoc = Loc; break; |
| 280 | case TQ_restrict: TQ_restrictLoc = Loc; break; |
| 281 | case TQ_volatile: TQ_volatileLoc = Loc; break; |
| 282 | } |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec){ |
| 287 | // 'inline inline' is ok. |
| 288 | FS_inline_specified = true; |
| 289 | FS_inlineLoc = Loc; |
| 290 | return false; |
| 291 | } |
| 292 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 293 | bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec){ |
| 294 | // 'virtual virtual' is ok. |
| 295 | FS_virtual_specified = true; |
| 296 | FS_virtualLoc = Loc; |
| 297 | return false; |
| 298 | } |
| 299 | |
| 300 | bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec){ |
| 301 | // 'explicit explicit' is ok. |
| 302 | FS_explicit_specified = true; |
| 303 | FS_explicitLoc = Loc; |
| 304 | return false; |
| 305 | } |
| 306 | |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 307 | bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec) { |
| 308 | if (Friend_specified) { |
| 309 | PrevSpec = "friend"; |
| 310 | return true; |
| 311 | } |
| 312 | |
| 313 | Friend_specified = true; |
| 314 | FriendLoc = Loc; |
| 315 | return false; |
| 316 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 317 | |
| 318 | /// Finish - This does final analysis of the declspec, rejecting things like |
| 319 | /// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or |
| 320 | /// diag::NUM_DIAGNOSTICS if there is no error. After calling this method, |
| 321 | /// DeclSpec is guaranteed self-consistent, even if an error occurred. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 322 | void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 323 | // Check the type specifier components first. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 324 | SourceManager &SrcMgr = PP.getSourceManager(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 325 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 326 | // signed/unsigned are only valid with int/char/wchar_t. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 327 | if (TypeSpecSign != TSS_unspecified) { |
| 328 | if (TypeSpecType == TST_unspecified) |
| 329 | TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int. |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 330 | else if (TypeSpecType != TST_int && |
| 331 | TypeSpecType != TST_char && TypeSpecType != TST_wchar) { |
Chris Lattner | 254be6a | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 332 | Diag(D, TSSLoc, SrcMgr, diag::err_invalid_sign_spec) |
| 333 | << getSpecifierName((TST)TypeSpecType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 334 | // signed double -> double. |
| 335 | TypeSpecSign = TSS_unspecified; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | // Validate the width of the type. |
| 340 | switch (TypeSpecWidth) { |
| 341 | case TSW_unspecified: break; |
| 342 | case TSW_short: // short int |
| 343 | case TSW_longlong: // long long int |
| 344 | if (TypeSpecType == TST_unspecified) |
| 345 | TypeSpecType = TST_int; // short -> short int, long long -> long long int. |
| 346 | else if (TypeSpecType != TST_int) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 347 | Diag(D, TSWLoc, SrcMgr, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 348 | TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec |
Chris Lattner | 254be6a | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 349 | : diag::err_invalid_longlong_spec) |
| 350 | << getSpecifierName((TST)TypeSpecType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 351 | TypeSpecType = TST_int; |
| 352 | } |
| 353 | break; |
| 354 | case TSW_long: // long double, long int |
| 355 | if (TypeSpecType == TST_unspecified) |
| 356 | TypeSpecType = TST_int; // long -> long int. |
| 357 | else if (TypeSpecType != TST_int && TypeSpecType != TST_double) { |
Chris Lattner | 254be6a | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 358 | Diag(D, TSWLoc, SrcMgr, diag::err_invalid_long_spec) |
| 359 | << getSpecifierName((TST)TypeSpecType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 360 | TypeSpecType = TST_int; |
| 361 | } |
| 362 | break; |
| 363 | } |
| 364 | |
| 365 | // TODO: if the implementation does not implement _Complex or _Imaginary, |
| 366 | // disallow their use. Need information about the backend. |
| 367 | if (TypeSpecComplex != TSC_unspecified) { |
| 368 | if (TypeSpecType == TST_unspecified) { |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 369 | Diag(D, TSCLoc, SrcMgr, diag::ext_plain_complex) |
| 370 | << CodeModificationHint::CreateInsertion( |
| 371 | PP.getLocForEndOfToken(getTypeSpecComplexLoc()), |
| 372 | " double"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 373 | TypeSpecType = TST_double; // _Complex -> _Complex double. |
| 374 | } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) { |
| 375 | // Note that this intentionally doesn't include _Complex _Bool. |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 376 | Diag(D, TSTLoc, SrcMgr, diag::ext_integer_complex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 377 | } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) { |
Chris Lattner | 254be6a | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 378 | Diag(D, TSCLoc, SrcMgr, diag::err_invalid_complex_spec) |
| 379 | << getSpecifierName((TST)TypeSpecType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 380 | TypeSpecComplex = TSC_unspecified; |
| 381 | } |
| 382 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 383 | |
| 384 | // Okay, now we can infer the real type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 385 | |
| 386 | // TODO: return "auto function" and other bad things based on the real type. |
| 387 | |
| 388 | // 'data definition has no type or storage class'? |
| 389 | } |
Daniel Dunbar | e4858a6 | 2008-08-11 03:45:03 +0000 | [diff] [blame] | 390 | |
Sebastian Redl | a4ed0d8 | 2008-12-28 15:28:59 +0000 | [diff] [blame] | 391 | bool DeclSpec::isMissingDeclaratorOk() { |
| 392 | TST tst = getTypeSpecType(); |
| 393 | return (tst == TST_union |
| 394 | || tst == TST_struct |
| 395 | || tst == TST_class |
| 396 | || tst == TST_enum |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 397 | ) && getTypeRep() != 0 && StorageClassSpec != DeclSpec::SCS_typedef; |
Sebastian Redl | a4ed0d8 | 2008-12-28 15:28:59 +0000 | [diff] [blame] | 398 | } |