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