Chris Lattner | 289ab7b | 2006-11-08 06:54:53 +0000 | [diff] [blame] | 1 | //===--- SemaDeclSpec.cpp - Declaration Specifier Semantic Analysis -------===// |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 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. |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | 289ab7b | 2006-11-08 06:54:53 +0000 | [diff] [blame] | 10 | // This file implements semantic analysis for declaration specifiers. |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 288e86ff1 | 2006-11-11 23:03:42 +0000 | [diff] [blame] | 14 | #include "clang/Parse/DeclSpec.h" |
Daniel Dunbar | c74b5cc | 2008-08-11 03:45:03 +0000 | [diff] [blame] | 15 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 16 | #include "clang/Basic/LangOptions.h" |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 17 | using namespace clang; |
| 18 | |
| 19 | /// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this |
| 20 | /// |
| 21 | unsigned DeclSpec::getParsedSpecifiers() const { |
| 22 | unsigned Res = 0; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 23 | if (StorageClassSpec != SCS_unspecified || |
| 24 | SCS_thread_specified) |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 25 | Res |= PQ_StorageClassSpecifier; |
Mike Stump | 65643c6 | 2008-06-19 19:52:46 +0000 | [diff] [blame] | 26 | |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 27 | if (TypeQualifiers != TQ_unspecified) |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 28 | Res |= PQ_TypeQualifier; |
| 29 | |
Chris Lattner | f055d43 | 2006-11-28 04:28:12 +0000 | [diff] [blame] | 30 | if (hasTypeSpecifier()) |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 31 | Res |= PQ_TypeSpecifier; |
| 32 | |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame^] | 33 | if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified) |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 34 | Res |= PQ_FunctionSpecifier; |
| 35 | return Res; |
| 36 | } |
| 37 | |
Chris Lattner | 7bd11fe | 2007-01-23 04:35:33 +0000 | [diff] [blame] | 38 | const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) { |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 39 | switch (S) { |
| 40 | default: assert(0 && "Unknown typespec!"); |
| 41 | case DeclSpec::SCS_unspecified: return "unspecified"; |
| 42 | case DeclSpec::SCS_typedef: return "typedef"; |
| 43 | case DeclSpec::SCS_extern: return "extern"; |
| 44 | case DeclSpec::SCS_static: return "static"; |
| 45 | case DeclSpec::SCS_auto: return "auto"; |
| 46 | case DeclSpec::SCS_register: return "register"; |
| 47 | } |
| 48 | } |
| 49 | |
Steve Naroff | ab468cb | 2008-02-12 04:08:59 +0000 | [diff] [blame] | 50 | bool DeclSpec::BadSpecifier(SCS S, const char *&PrevSpec) { |
Steve Naroff | ab468cb | 2008-02-12 04:08:59 +0000 | [diff] [blame] | 51 | PrevSpec = getSpecifierName(S); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 52 | return true; |
| 53 | } |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 54 | |
Steve Naroff | ab468cb | 2008-02-12 04:08:59 +0000 | [diff] [blame] | 55 | bool DeclSpec::BadSpecifier(TSW W, const char *&PrevSpec) { |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 56 | switch (W) { |
Steve Naroff | ab468cb | 2008-02-12 04:08:59 +0000 | [diff] [blame] | 57 | case TSW_unspecified: PrevSpec = "unspecified"; break; |
| 58 | case TSW_short: PrevSpec = "short"; break; |
| 59 | case TSW_long: PrevSpec = "long"; break; |
| 60 | case TSW_longlong: PrevSpec = "long long"; break; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 61 | } |
| 62 | return true; |
| 63 | } |
| 64 | |
Steve Naroff | ab468cb | 2008-02-12 04:08:59 +0000 | [diff] [blame] | 65 | bool DeclSpec::BadSpecifier(TSC C, const char *&PrevSpec) { |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 66 | switch (C) { |
Steve Naroff | ab468cb | 2008-02-12 04:08:59 +0000 | [diff] [blame] | 67 | case TSC_unspecified: PrevSpec = "unspecified"; break; |
| 68 | case TSC_imaginary: PrevSpec = "imaginary"; break; |
| 69 | case TSC_complex: PrevSpec = "complex"; break; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 70 | } |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | |
Steve Naroff | ab468cb | 2008-02-12 04:08:59 +0000 | [diff] [blame] | 75 | bool DeclSpec::BadSpecifier(TSS S, const char *&PrevSpec) { |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 76 | switch (S) { |
Steve Naroff | ab468cb | 2008-02-12 04:08:59 +0000 | [diff] [blame] | 77 | case TSS_unspecified: PrevSpec = "unspecified"; break; |
| 78 | case TSS_signed: PrevSpec = "signed"; break; |
| 79 | case TSS_unsigned: PrevSpec = "unsigned"; break; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 80 | } |
| 81 | return true; |
| 82 | } |
| 83 | |
Chris Lattner | 69680ea | 2007-01-23 04:34:43 +0000 | [diff] [blame] | 84 | const char *DeclSpec::getSpecifierName(DeclSpec::TST T) { |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 85 | switch (T) { |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 86 | default: assert(0 && "Unknown typespec!"); |
| 87 | case DeclSpec::TST_unspecified: return "unspecified"; |
| 88 | case DeclSpec::TST_void: return "void"; |
| 89 | case DeclSpec::TST_char: return "char"; |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 90 | case DeclSpec::TST_wchar: return "wchar_t"; |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 91 | case DeclSpec::TST_int: return "int"; |
| 92 | case DeclSpec::TST_float: return "float"; |
| 93 | case DeclSpec::TST_double: return "double"; |
| 94 | case DeclSpec::TST_bool: return "_Bool"; |
| 95 | case DeclSpec::TST_decimal32: return "_Decimal32"; |
| 96 | case DeclSpec::TST_decimal64: return "_Decimal64"; |
| 97 | case DeclSpec::TST_decimal128: return "_Decimal128"; |
Chris Lattner | da72c82 | 2006-08-13 22:16:42 +0000 | [diff] [blame] | 98 | case DeclSpec::TST_enum: return "enum"; |
Chris Lattner | 861a226 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 99 | case DeclSpec::TST_class: return "class"; |
Chris Lattner | da72c82 | 2006-08-13 22:16:42 +0000 | [diff] [blame] | 100 | case DeclSpec::TST_union: return "union"; |
| 101 | case DeclSpec::TST_struct: return "struct"; |
Chris Lattner | 3b4fdda3 | 2006-08-14 00:45:39 +0000 | [diff] [blame] | 102 | case DeclSpec::TST_typedef: return "typedef"; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 103 | case DeclSpec::TST_typeofType: |
| 104 | case DeclSpec::TST_typeofExpr: return "typeof"; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 105 | } |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Steve Naroff | ab468cb | 2008-02-12 04:08:59 +0000 | [diff] [blame] | 108 | bool DeclSpec::BadSpecifier(TST T, const char *&PrevSpec) { |
Steve Naroff | ab468cb | 2008-02-12 04:08:59 +0000 | [diff] [blame] | 109 | PrevSpec = getSpecifierName(T); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 110 | return true; |
| 111 | } |
| 112 | |
Steve Naroff | ab468cb | 2008-02-12 04:08:59 +0000 | [diff] [blame] | 113 | bool DeclSpec::BadSpecifier(TQ T, const char *&PrevSpec) { |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 114 | switch (T) { |
| 115 | case DeclSpec::TQ_unspecified: PrevSpec = "unspecified"; break; |
| 116 | case DeclSpec::TQ_const: PrevSpec = "const"; break; |
| 117 | case DeclSpec::TQ_restrict: PrevSpec = "restrict"; break; |
| 118 | case DeclSpec::TQ_volatile: PrevSpec = "volatile"; break; |
| 119 | } |
| 120 | return true; |
| 121 | } |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 122 | |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 123 | bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc, |
| 124 | const char *&PrevSpec) { |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 125 | if (StorageClassSpec != SCS_unspecified) |
Chris Lattner | 9724ce1 | 2007-12-02 00:47:03 +0000 | [diff] [blame] | 126 | return BadSpecifier( (SCS)StorageClassSpec, PrevSpec); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 127 | StorageClassSpec = S; |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 128 | StorageClassSpecLoc = Loc; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 129 | return false; |
| 130 | } |
| 131 | |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 132 | bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc, |
| 133 | const char *&PrevSpec) { |
Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 134 | if (SCS_thread_specified) { |
| 135 | PrevSpec = "__thread"; |
| 136 | return true; |
| 137 | } |
| 138 | SCS_thread_specified = true; |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 139 | SCS_threadLoc = Loc; |
Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 140 | return false; |
| 141 | } |
| 142 | |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 143 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 144 | /// These methods set the specified attribute of the DeclSpec, but return true |
| 145 | /// and ignore the request if invalid (e.g. "extern" then "auto" is |
| 146 | /// specified). |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 147 | bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc, |
| 148 | const char *&PrevSpec) { |
Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 149 | if (TypeSpecWidth != TSW_unspecified && |
| 150 | // Allow turning long -> long long. |
| 151 | (W != TSW_longlong || TypeSpecWidth != TSW_long)) |
Chris Lattner | 9724ce1 | 2007-12-02 00:47:03 +0000 | [diff] [blame] | 152 | return BadSpecifier( (TSW)TypeSpecWidth, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 153 | TypeSpecWidth = W; |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 154 | TSWLoc = Loc; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 155 | return false; |
| 156 | } |
| 157 | |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 158 | bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc, |
| 159 | const char *&PrevSpec) { |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 160 | if (TypeSpecComplex != TSC_unspecified) |
Chris Lattner | 9724ce1 | 2007-12-02 00:47:03 +0000 | [diff] [blame] | 161 | return BadSpecifier( (TSC)TypeSpecComplex, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 162 | TypeSpecComplex = C; |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 163 | TSCLoc = Loc; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 164 | return false; |
| 165 | } |
| 166 | |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 167 | bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc, |
| 168 | const char *&PrevSpec) { |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 169 | if (TypeSpecSign != TSS_unspecified) |
Chris Lattner | 9724ce1 | 2007-12-02 00:47:03 +0000 | [diff] [blame] | 170 | return BadSpecifier( (TSS)TypeSpecSign, PrevSpec); |
Chris Lattner | deb42f5 | 2006-08-04 05:26:52 +0000 | [diff] [blame] | 171 | TypeSpecSign = S; |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 172 | TSSLoc = Loc; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 173 | return false; |
| 174 | } |
| 175 | |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 176 | bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc, |
Argyrios Kyrtzidis | 25d05e8 | 2008-08-01 10:35:27 +0000 | [diff] [blame] | 177 | const char *&PrevSpec, Action::TypeTy *Rep) { |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 178 | if (TypeSpecType != TST_unspecified) |
Chris Lattner | 9724ce1 | 2007-12-02 00:47:03 +0000 | [diff] [blame] | 179 | return BadSpecifier( (TST)TypeSpecType, PrevSpec); |
Chris Lattner | deb42f5 | 2006-08-04 05:26:52 +0000 | [diff] [blame] | 180 | TypeSpecType = T; |
Chris Lattner | b9d572a | 2007-01-23 04:58:34 +0000 | [diff] [blame] | 181 | TypeRep = Rep; |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 182 | TSTLoc = Loc; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 183 | return false; |
| 184 | } |
| 185 | |
Chris Lattner | 60809f5 | 2006-11-28 05:18:46 +0000 | [diff] [blame] | 186 | bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec, |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 187 | const LangOptions &Lang) { |
| 188 | // Duplicates turn into warnings pre-C99. |
| 189 | if ((TypeQualifiers & T) && !Lang.C99) |
| 190 | return BadSpecifier(T, PrevSpec); |
| 191 | TypeQualifiers |= T; |
Chris Lattner | 60809f5 | 2006-11-28 05:18:46 +0000 | [diff] [blame] | 192 | |
| 193 | switch (T) { |
| 194 | default: assert(0 && "Unknown type qualifier!"); |
| 195 | case TQ_const: TQ_constLoc = Loc; break; |
| 196 | case TQ_restrict: TQ_restrictLoc = Loc; break; |
| 197 | case TQ_volatile: TQ_volatileLoc = Loc; break; |
| 198 | } |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 199 | return false; |
| 200 | } |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 201 | |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 202 | bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec){ |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 203 | // 'inline inline' is ok. |
| 204 | FS_inline_specified = true; |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 205 | FS_inlineLoc = Loc; |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 206 | return false; |
| 207 | } |
| 208 | |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame^] | 209 | bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec){ |
| 210 | // 'virtual virtual' is ok. |
| 211 | FS_virtual_specified = true; |
| 212 | FS_virtualLoc = Loc; |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec){ |
| 217 | // 'explicit explicit' is ok. |
| 218 | FS_explicit_specified = true; |
| 219 | FS_explicitLoc = Loc; |
| 220 | return false; |
| 221 | } |
| 222 | |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 223 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 224 | /// Finish - This does final analysis of the declspec, rejecting things like |
| 225 | /// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or |
| 226 | /// diag::NUM_DIAGNOSTICS if there is no error. After calling this method, |
| 227 | /// DeclSpec is guaranteed self-consistent, even if an error occurred. |
Ted Kremenek | d4e5fba | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 228 | void DeclSpec::Finish(Diagnostic &D, SourceManager& SrcMgr, |
| 229 | const LangOptions &Lang) { |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 230 | // Check the type specifier components first. |
| 231 | |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 232 | // signed/unsigned are only valid with int/char/wchar_t. |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 233 | if (TypeSpecSign != TSS_unspecified) { |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 234 | if (TypeSpecType == TST_unspecified) |
| 235 | TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int. |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 236 | else if (TypeSpecType != TST_int && |
| 237 | TypeSpecType != TST_char && TypeSpecType != TST_wchar) { |
Ted Kremenek | d4e5fba | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 238 | Diag(D, TSSLoc, SrcMgr, diag::err_invalid_sign_spec, |
Chris Lattner | 9724ce1 | 2007-12-02 00:47:03 +0000 | [diff] [blame] | 239 | getSpecifierName( (TST)TypeSpecType)); |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 240 | // signed double -> double. |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 241 | TypeSpecSign = TSS_unspecified; |
| 242 | } |
| 243 | } |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 244 | |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 245 | // Validate the width of the type. |
| 246 | switch (TypeSpecWidth) { |
| 247 | case TSW_unspecified: break; |
| 248 | case TSW_short: // short int |
| 249 | case TSW_longlong: // long long int |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 250 | if (TypeSpecType == TST_unspecified) |
| 251 | TypeSpecType = TST_int; // short -> short int, long long -> long long int. |
| 252 | else if (TypeSpecType != TST_int) { |
Ted Kremenek | d4e5fba | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 253 | Diag(D, TSWLoc, SrcMgr, |
Chris Lattner | 36982e4 | 2007-05-16 17:49:37 +0000 | [diff] [blame] | 254 | TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec |
| 255 | : diag::err_invalid_longlong_spec, |
Chris Lattner | 9724ce1 | 2007-12-02 00:47:03 +0000 | [diff] [blame] | 256 | getSpecifierName( (TST)TypeSpecType)); |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 257 | TypeSpecType = TST_int; |
| 258 | } |
| 259 | break; |
| 260 | case TSW_long: // long double, long int |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 261 | if (TypeSpecType == TST_unspecified) |
| 262 | TypeSpecType = TST_int; // long -> long int. |
| 263 | else if (TypeSpecType != TST_int && TypeSpecType != TST_double) { |
Ted Kremenek | d4e5fba | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 264 | Diag(D, TSWLoc, SrcMgr, diag::err_invalid_long_spec, |
Chris Lattner | 9724ce1 | 2007-12-02 00:47:03 +0000 | [diff] [blame] | 265 | getSpecifierName( (TST)TypeSpecType)); |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 266 | TypeSpecType = TST_int; |
| 267 | } |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 268 | break; |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 271 | // TODO: if the implementation does not implement _Complex or _Imaginary, |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 272 | // disallow their use. Need information about the backend. |
| 273 | if (TypeSpecComplex != TSC_unspecified) { |
| 274 | if (TypeSpecType == TST_unspecified) { |
Ted Kremenek | d4e5fba | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 275 | Diag(D, TSCLoc, SrcMgr, diag::ext_plain_complex); |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 276 | TypeSpecType = TST_double; // _Complex -> _Complex double. |
| 277 | } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) { |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 278 | // Note that this intentionally doesn't include _Complex _Bool. |
Ted Kremenek | d4e5fba | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 279 | Diag(D, TSTLoc, SrcMgr, diag::ext_integer_complex); |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 280 | } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) { |
Ted Kremenek | d4e5fba | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 281 | Diag(D, TSCLoc, SrcMgr, diag::err_invalid_complex_spec, |
Chris Lattner | 9724ce1 | 2007-12-02 00:47:03 +0000 | [diff] [blame] | 282 | getSpecifierName( (TST)TypeSpecType)); |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 283 | TypeSpecComplex = TSC_unspecified; |
| 284 | } |
| 285 | } |
Chris Lattner | 1ae2329 | 2006-08-04 06:42:31 +0000 | [diff] [blame] | 286 | |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 287 | // Verify __thread. |
| 288 | if (SCS_thread_specified) { |
| 289 | if (StorageClassSpec == SCS_unspecified) { |
| 290 | StorageClassSpec = SCS_extern; // '__thread int' -> 'extern __thread int' |
| 291 | } else if (StorageClassSpec != SCS_extern && |
| 292 | StorageClassSpec != SCS_static) { |
Ted Kremenek | d4e5fba | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 293 | Diag(D, getStorageClassSpecLoc(), SrcMgr, diag::err_invalid_thread_spec, |
Chris Lattner | 9724ce1 | 2007-12-02 00:47:03 +0000 | [diff] [blame] | 294 | getSpecifierName( (SCS)StorageClassSpec)); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 295 | SCS_thread_specified = false; |
| 296 | } |
| 297 | } |
Chris Lattner | 8e90ef6 | 2006-08-05 03:30:45 +0000 | [diff] [blame] | 298 | |
| 299 | // Okay, now we can infer the real type. |
Chris Lattner | 8e90ef6 | 2006-08-05 03:30:45 +0000 | [diff] [blame] | 300 | |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 301 | // TODO: return "auto function" and other bad things based on the real type. |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 302 | |
Chris Lattner | 1ae2329 | 2006-08-04 06:42:31 +0000 | [diff] [blame] | 303 | // 'data definition has no type or storage class'? |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 304 | } |
Daniel Dunbar | c74b5cc | 2008-08-11 03:45:03 +0000 | [diff] [blame] | 305 | |
| 306 | void DeclSpec::Diag(Diagnostic &D, SourceLocation Loc, SourceManager& SrcMgr, |
| 307 | unsigned DiagID) { |
| 308 | D.Report(FullSourceLoc(Loc,SrcMgr), DiagID); |
| 309 | } |
| 310 | |
| 311 | void DeclSpec::Diag(Diagnostic &D, SourceLocation Loc, SourceManager& SrcMgr, |
| 312 | unsigned DiagID, const std::string &info) { |
| 313 | D.Report(FullSourceLoc(Loc,SrcMgr), DiagID, &info, 1); |
| 314 | } |