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" |
| 15 | #include "clang/Basic/LangOptions.h" |
| 16 | #include "clang/Basic/SourceLocation.h" |
| 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; |
| 23 | if (StorageClassSpec != SCS_unspecified || |
| 24 | SCS_thread_specified) |
| 25 | Res |= PQ_StorageClassSpecifier; |
Mike Stump | d420433 | 2008-06-19 19:52:46 +0000 | [diff] [blame] | 26 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 27 | if (TypeQualifiers != TQ_unspecified) |
| 28 | Res |= PQ_TypeQualifier; |
| 29 | |
| 30 | if (hasTypeSpecifier()) |
| 31 | Res |= PQ_TypeSpecifier; |
| 32 | |
| 33 | if (FS_inline_specified) |
| 34 | Res |= PQ_FunctionSpecifier; |
| 35 | return Res; |
| 36 | } |
| 37 | |
| 38 | const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) { |
| 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 | 9532414 | 2008-02-12 04:08:59 +0000 | [diff] [blame] | 50 | bool DeclSpec::BadSpecifier(SCS S, const char *&PrevSpec) { |
Steve Naroff | 9532414 | 2008-02-12 04:08:59 +0000 | [diff] [blame] | 51 | PrevSpec = getSpecifierName(S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 52 | return true; |
| 53 | } |
| 54 | |
Steve Naroff | 9532414 | 2008-02-12 04:08:59 +0000 | [diff] [blame] | 55 | bool DeclSpec::BadSpecifier(TSW W, const char *&PrevSpec) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 56 | switch (W) { |
Steve Naroff | 9532414 | 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; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 61 | } |
| 62 | return true; |
| 63 | } |
| 64 | |
Steve Naroff | 9532414 | 2008-02-12 04:08:59 +0000 | [diff] [blame] | 65 | bool DeclSpec::BadSpecifier(TSC C, const char *&PrevSpec) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 66 | switch (C) { |
Steve Naroff | 9532414 | 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; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 70 | } |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | |
Steve Naroff | 9532414 | 2008-02-12 04:08:59 +0000 | [diff] [blame] | 75 | bool DeclSpec::BadSpecifier(TSS S, const char *&PrevSpec) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 76 | switch (S) { |
Steve Naroff | 9532414 | 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; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 80 | } |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | const char *DeclSpec::getSpecifierName(DeclSpec::TST T) { |
| 85 | switch (T) { |
| 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"; |
| 90 | case DeclSpec::TST_int: return "int"; |
| 91 | case DeclSpec::TST_float: return "float"; |
| 92 | case DeclSpec::TST_double: return "double"; |
| 93 | case DeclSpec::TST_bool: return "_Bool"; |
| 94 | case DeclSpec::TST_decimal32: return "_Decimal32"; |
| 95 | case DeclSpec::TST_decimal64: return "_Decimal64"; |
| 96 | case DeclSpec::TST_decimal128: return "_Decimal128"; |
| 97 | case DeclSpec::TST_enum: return "enum"; |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 98 | case DeclSpec::TST_class: return "class"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 99 | case DeclSpec::TST_union: return "union"; |
| 100 | case DeclSpec::TST_struct: return "struct"; |
| 101 | case DeclSpec::TST_typedef: return "typedef"; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 102 | case DeclSpec::TST_typeofType: |
| 103 | case DeclSpec::TST_typeofExpr: return "typeof"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
Steve Naroff | 9532414 | 2008-02-12 04:08:59 +0000 | [diff] [blame] | 107 | bool DeclSpec::BadSpecifier(TST T, const char *&PrevSpec) { |
Steve Naroff | 9532414 | 2008-02-12 04:08:59 +0000 | [diff] [blame] | 108 | PrevSpec = getSpecifierName(T); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 109 | return true; |
| 110 | } |
| 111 | |
Steve Naroff | 9532414 | 2008-02-12 04:08:59 +0000 | [diff] [blame] | 112 | bool DeclSpec::BadSpecifier(TQ T, const char *&PrevSpec) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 113 | switch (T) { |
| 114 | case DeclSpec::TQ_unspecified: PrevSpec = "unspecified"; break; |
| 115 | case DeclSpec::TQ_const: PrevSpec = "const"; break; |
| 116 | case DeclSpec::TQ_restrict: PrevSpec = "restrict"; break; |
| 117 | case DeclSpec::TQ_volatile: PrevSpec = "volatile"; break; |
| 118 | } |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc, |
| 123 | const char *&PrevSpec) { |
| 124 | if (StorageClassSpec != SCS_unspecified) |
Chris Lattner | 2efcd2f | 2007-12-02 00:47:03 +0000 | [diff] [blame] | 125 | return BadSpecifier( (SCS)StorageClassSpec, PrevSpec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 126 | StorageClassSpec = S; |
| 127 | StorageClassSpecLoc = Loc; |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc, |
| 132 | const char *&PrevSpec) { |
| 133 | if (SCS_thread_specified) { |
| 134 | PrevSpec = "__thread"; |
| 135 | return true; |
| 136 | } |
| 137 | SCS_thread_specified = true; |
| 138 | SCS_threadLoc = Loc; |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | |
| 143 | /// These methods set the specified attribute of the DeclSpec, but return true |
| 144 | /// and ignore the request if invalid (e.g. "extern" then "auto" is |
| 145 | /// specified). |
| 146 | bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc, |
| 147 | const char *&PrevSpec) { |
| 148 | if (TypeSpecWidth != TSW_unspecified && |
| 149 | // Allow turning long -> long long. |
| 150 | (W != TSW_longlong || TypeSpecWidth != TSW_long)) |
Chris Lattner | 2efcd2f | 2007-12-02 00:47:03 +0000 | [diff] [blame] | 151 | return BadSpecifier( (TSW)TypeSpecWidth, PrevSpec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 152 | TypeSpecWidth = W; |
| 153 | TSWLoc = Loc; |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc, |
| 158 | const char *&PrevSpec) { |
| 159 | if (TypeSpecComplex != TSC_unspecified) |
Chris Lattner | 2efcd2f | 2007-12-02 00:47:03 +0000 | [diff] [blame] | 160 | return BadSpecifier( (TSC)TypeSpecComplex, PrevSpec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 161 | TypeSpecComplex = C; |
| 162 | TSCLoc = Loc; |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc, |
| 167 | const char *&PrevSpec) { |
| 168 | if (TypeSpecSign != TSS_unspecified) |
Chris Lattner | 2efcd2f | 2007-12-02 00:47:03 +0000 | [diff] [blame] | 169 | return BadSpecifier( (TSS)TypeSpecSign, PrevSpec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 170 | TypeSpecSign = S; |
| 171 | TSSLoc = Loc; |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc, |
| 176 | const char *&PrevSpec, void *Rep) { |
| 177 | if (TypeSpecType != TST_unspecified) |
Chris Lattner | 2efcd2f | 2007-12-02 00:47:03 +0000 | [diff] [blame] | 178 | return BadSpecifier( (TST)TypeSpecType, PrevSpec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 179 | TypeSpecType = T; |
| 180 | TypeRep = Rep; |
| 181 | TSTLoc = Loc; |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec, |
| 186 | const LangOptions &Lang) { |
| 187 | // Duplicates turn into warnings pre-C99. |
| 188 | if ((TypeQualifiers & T) && !Lang.C99) |
| 189 | return BadSpecifier(T, PrevSpec); |
| 190 | TypeQualifiers |= T; |
| 191 | |
| 192 | switch (T) { |
| 193 | default: assert(0 && "Unknown type qualifier!"); |
| 194 | case TQ_const: TQ_constLoc = Loc; break; |
| 195 | case TQ_restrict: TQ_restrictLoc = Loc; break; |
| 196 | case TQ_volatile: TQ_volatileLoc = Loc; break; |
| 197 | } |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec){ |
| 202 | // 'inline inline' is ok. |
| 203 | FS_inline_specified = true; |
| 204 | FS_inlineLoc = Loc; |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | |
| 209 | /// Finish - This does final analysis of the declspec, rejecting things like |
| 210 | /// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or |
| 211 | /// diag::NUM_DIAGNOSTICS if there is no error. After calling this method, |
| 212 | /// DeclSpec is guaranteed self-consistent, even if an error occurred. |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 213 | void DeclSpec::Finish(Diagnostic &D, SourceManager& SrcMgr, |
| 214 | const LangOptions &Lang) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 215 | // Check the type specifier components first. |
| 216 | |
| 217 | // signed/unsigned are only valid with int/char. |
| 218 | if (TypeSpecSign != TSS_unspecified) { |
| 219 | if (TypeSpecType == TST_unspecified) |
| 220 | TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int. |
| 221 | else if (TypeSpecType != TST_int && TypeSpecType != TST_char) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 222 | Diag(D, TSSLoc, SrcMgr, diag::err_invalid_sign_spec, |
Chris Lattner | 2efcd2f | 2007-12-02 00:47:03 +0000 | [diff] [blame] | 223 | getSpecifierName( (TST)TypeSpecType)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 224 | // signed double -> double. |
| 225 | TypeSpecSign = TSS_unspecified; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | // Validate the width of the type. |
| 230 | switch (TypeSpecWidth) { |
| 231 | case TSW_unspecified: break; |
| 232 | case TSW_short: // short int |
| 233 | case TSW_longlong: // long long int |
| 234 | if (TypeSpecType == TST_unspecified) |
| 235 | TypeSpecType = TST_int; // short -> short int, long long -> long long int. |
| 236 | else if (TypeSpecType != TST_int) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 237 | Diag(D, TSWLoc, SrcMgr, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 238 | TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec |
| 239 | : diag::err_invalid_longlong_spec, |
Chris Lattner | 2efcd2f | 2007-12-02 00:47:03 +0000 | [diff] [blame] | 240 | getSpecifierName( (TST)TypeSpecType)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 241 | TypeSpecType = TST_int; |
| 242 | } |
| 243 | break; |
| 244 | case TSW_long: // long double, long int |
| 245 | if (TypeSpecType == TST_unspecified) |
| 246 | TypeSpecType = TST_int; // long -> long int. |
| 247 | else if (TypeSpecType != TST_int && TypeSpecType != TST_double) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 248 | Diag(D, TSWLoc, SrcMgr, diag::err_invalid_long_spec, |
Chris Lattner | 2efcd2f | 2007-12-02 00:47:03 +0000 | [diff] [blame] | 249 | getSpecifierName( (TST)TypeSpecType)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 250 | TypeSpecType = TST_int; |
| 251 | } |
| 252 | break; |
| 253 | } |
| 254 | |
| 255 | // TODO: if the implementation does not implement _Complex or _Imaginary, |
| 256 | // disallow their use. Need information about the backend. |
| 257 | if (TypeSpecComplex != TSC_unspecified) { |
| 258 | if (TypeSpecType == TST_unspecified) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 259 | Diag(D, TSCLoc, SrcMgr, diag::ext_plain_complex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 260 | TypeSpecType = TST_double; // _Complex -> _Complex double. |
| 261 | } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) { |
| 262 | // Note that this intentionally doesn't include _Complex _Bool. |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 263 | Diag(D, TSTLoc, SrcMgr, diag::ext_integer_complex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 264 | } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 265 | Diag(D, TSCLoc, SrcMgr, diag::err_invalid_complex_spec, |
Chris Lattner | 2efcd2f | 2007-12-02 00:47:03 +0000 | [diff] [blame] | 266 | getSpecifierName( (TST)TypeSpecType)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 267 | TypeSpecComplex = TSC_unspecified; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | // Verify __thread. |
| 272 | if (SCS_thread_specified) { |
| 273 | if (StorageClassSpec == SCS_unspecified) { |
| 274 | StorageClassSpec = SCS_extern; // '__thread int' -> 'extern __thread int' |
| 275 | } else if (StorageClassSpec != SCS_extern && |
| 276 | StorageClassSpec != SCS_static) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 277 | Diag(D, getStorageClassSpecLoc(), SrcMgr, diag::err_invalid_thread_spec, |
Chris Lattner | 2efcd2f | 2007-12-02 00:47:03 +0000 | [diff] [blame] | 278 | getSpecifierName( (SCS)StorageClassSpec)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 279 | SCS_thread_specified = false; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | // Okay, now we can infer the real type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 284 | |
| 285 | // TODO: return "auto function" and other bad things based on the real type. |
| 286 | |
| 287 | // 'data definition has no type or storage class'? |
| 288 | } |