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