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 | |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 14 | #include "clang/Parse/ParseDiagnostic.h" // FIXME: remove this back-dependency! |
| 15 | #include "clang/Sema/DeclSpec.h" |
| 16 | #include "clang/Sema/ParsedTemplate.h" |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 17 | #include "clang/Lex/Preprocessor.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "clang/Basic/LangOptions.h" |
Chris Lattner | 5af2f35 | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/STLExtras.h" |
John McCall | 32d335e | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ErrorHandling.h" |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 21 | #include <cstring> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Chris Lattner | 254be6a | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 24 | |
| 25 | static DiagnosticBuilder Diag(Diagnostic &D, SourceLocation Loc, |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame^] | 26 | unsigned DiagID) { |
| 27 | return D.Report(Loc, DiagID); |
Chris Lattner | 254be6a | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 30 | |
| 31 | void UnqualifiedId::setTemplateId(TemplateIdAnnotation *TemplateId) { |
| 32 | assert(TemplateId && "NULL template-id annotation?"); |
| 33 | Kind = IK_TemplateId; |
| 34 | this->TemplateId = TemplateId; |
| 35 | StartLocation = TemplateId->TemplateNameLoc; |
| 36 | EndLocation = TemplateId->RAngleLoc; |
| 37 | } |
| 38 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 39 | void UnqualifiedId::setConstructorTemplateId(TemplateIdAnnotation *TemplateId) { |
| 40 | assert(TemplateId && "NULL template-id annotation?"); |
| 41 | Kind = IK_ConstructorTemplateId; |
| 42 | this->TemplateId = TemplateId; |
| 43 | StartLocation = TemplateId->TemplateNameLoc; |
| 44 | EndLocation = TemplateId->RAngleLoc; |
| 45 | } |
| 46 | |
Chris Lattner | 5af2f35 | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 47 | /// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function. |
| 48 | /// "TheDeclarator" is the declarator that this will be added to. |
| 49 | DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto, bool isVariadic, |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 50 | SourceLocation EllipsisLoc, |
Chris Lattner | 5af2f35 | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 51 | ParamInfo *ArgInfo, |
| 52 | unsigned NumArgs, |
| 53 | unsigned TypeQuals, |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 54 | bool hasExceptionSpec, |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 55 | SourceLocation ThrowLoc, |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 56 | bool hasAnyExceptionSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 57 | ParsedType *Exceptions, |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 58 | SourceRange *ExceptionRanges, |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 59 | unsigned NumExceptions, |
Argyrios Kyrtzidis | 82bf010 | 2009-08-19 23:14:54 +0000 | [diff] [blame] | 60 | SourceLocation LPLoc, |
| 61 | SourceLocation RPLoc, |
Douglas Gregor | dab60ad | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 62 | Declarator &TheDeclarator, |
| 63 | ParsedType TrailingReturnType) { |
Chris Lattner | 5af2f35 | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 64 | DeclaratorChunk I; |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 65 | I.Kind = Function; |
Argyrios Kyrtzidis | 82bf010 | 2009-08-19 23:14:54 +0000 | [diff] [blame] | 66 | I.Loc = LPLoc; |
| 67 | I.EndLoc = RPLoc; |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 68 | I.Fun.hasPrototype = hasProto; |
| 69 | I.Fun.isVariadic = isVariadic; |
| 70 | I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding(); |
| 71 | I.Fun.DeleteArgInfo = false; |
| 72 | I.Fun.TypeQuals = TypeQuals; |
| 73 | I.Fun.NumArgs = NumArgs; |
| 74 | I.Fun.ArgInfo = 0; |
| 75 | I.Fun.hasExceptionSpec = hasExceptionSpec; |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 76 | I.Fun.ThrowLoc = ThrowLoc.getRawEncoding(); |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 77 | I.Fun.hasAnyExceptionSpec = hasAnyExceptionSpec; |
| 78 | I.Fun.NumExceptions = NumExceptions; |
| 79 | I.Fun.Exceptions = 0; |
Douglas Gregor | dab60ad | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 80 | I.Fun.TrailingReturnType = TrailingReturnType.getAsOpaquePtr(); |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 81 | |
Chris Lattner | 5af2f35 | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 82 | // new[] an argument array if needed. |
| 83 | if (NumArgs) { |
| 84 | // If the 'InlineParams' in Declarator is unused and big enough, put our |
| 85 | // parameter list there (in an effort to avoid new/delete traffic). If it |
| 86 | // is already used (consider a function returning a function pointer) or too |
| 87 | // small (function taking too many arguments), go to the heap. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 88 | if (!TheDeclarator.InlineParamsUsed && |
Chris Lattner | 5af2f35 | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 89 | NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) { |
| 90 | I.Fun.ArgInfo = TheDeclarator.InlineParams; |
| 91 | I.Fun.DeleteArgInfo = false; |
| 92 | TheDeclarator.InlineParamsUsed = true; |
| 93 | } else { |
| 94 | I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs]; |
| 95 | I.Fun.DeleteArgInfo = true; |
| 96 | } |
| 97 | memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs); |
| 98 | } |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 99 | // new[] an exception array if needed |
| 100 | if (NumExceptions) { |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 101 | I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions]; |
| 102 | for (unsigned i = 0; i != NumExceptions; ++i) { |
| 103 | I.Fun.Exceptions[i].Ty = Exceptions[i]; |
| 104 | I.Fun.Exceptions[i].Range = ExceptionRanges[i]; |
| 105 | } |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 106 | } |
Chris Lattner | 5af2f35 | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 107 | return I; |
| 108 | } |
Chris Lattner | 254be6a | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 109 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 110 | /// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this |
Chris Lattner | 2a327d1 | 2009-02-27 18:35:46 +0000 | [diff] [blame] | 111 | /// declaration specifier includes. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 112 | /// |
| 113 | unsigned DeclSpec::getParsedSpecifiers() const { |
| 114 | unsigned Res = 0; |
| 115 | if (StorageClassSpec != SCS_unspecified || |
| 116 | SCS_thread_specified) |
| 117 | Res |= PQ_StorageClassSpecifier; |
Mike Stump | d420433 | 2008-06-19 19:52:46 +0000 | [diff] [blame] | 118 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 119 | if (TypeQualifiers != TQ_unspecified) |
| 120 | Res |= PQ_TypeQualifier; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 122 | if (hasTypeSpecifier()) |
| 123 | Res |= PQ_TypeSpecifier; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 124 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 125 | if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 126 | Res |= PQ_FunctionSpecifier; |
| 127 | return Res; |
| 128 | } |
| 129 | |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 130 | template <class T> static bool BadSpecifier(T TNew, T TPrev, |
| 131 | const char *&PrevSpec, |
| 132 | unsigned &DiagID) { |
John McCall | 32d335e | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 133 | PrevSpec = DeclSpec::getSpecifierName(TPrev); |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 134 | DiagID = (TNew == TPrev ? diag::ext_duplicate_declspec |
| 135 | : diag::err_invalid_decl_spec_combination); |
John McCall | 32d335e | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 136 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | } |
John McCall | 32d335e | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 138 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 139 | const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) { |
| 140 | switch (S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 141 | case DeclSpec::SCS_unspecified: return "unspecified"; |
| 142 | case DeclSpec::SCS_typedef: return "typedef"; |
| 143 | case DeclSpec::SCS_extern: return "extern"; |
| 144 | case DeclSpec::SCS_static: return "static"; |
| 145 | case DeclSpec::SCS_auto: return "auto"; |
| 146 | case DeclSpec::SCS_register: return "register"; |
Eli Friedman | 63054b3 | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 147 | case DeclSpec::SCS_private_extern: return "__private_extern__"; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 148 | case DeclSpec::SCS_mutable: return "mutable"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 149 | } |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 150 | llvm_unreachable("Unknown typespec!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 151 | } |
| 152 | |
John McCall | 32d335e | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 153 | const char *DeclSpec::getSpecifierName(TSW W) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 154 | switch (W) { |
John McCall | 32d335e | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 155 | case TSW_unspecified: return "unspecified"; |
| 156 | case TSW_short: return "short"; |
| 157 | case TSW_long: return "long"; |
| 158 | case TSW_longlong: return "long long"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 159 | } |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 160 | llvm_unreachable("Unknown typespec!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 161 | } |
| 162 | |
John McCall | 32d335e | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 163 | const char *DeclSpec::getSpecifierName(TSC C) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 164 | switch (C) { |
John McCall | 32d335e | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 165 | case TSC_unspecified: return "unspecified"; |
| 166 | case TSC_imaginary: return "imaginary"; |
| 167 | case TSC_complex: return "complex"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 168 | } |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 169 | llvm_unreachable("Unknown typespec!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | |
John McCall | 32d335e | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 173 | const char *DeclSpec::getSpecifierName(TSS S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 174 | switch (S) { |
John McCall | 32d335e | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 175 | case TSS_unspecified: return "unspecified"; |
| 176 | case TSS_signed: return "signed"; |
| 177 | case TSS_unsigned: return "unsigned"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 178 | } |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 179 | llvm_unreachable("Unknown typespec!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | const char *DeclSpec::getSpecifierName(DeclSpec::TST T) { |
| 183 | switch (T) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 184 | case DeclSpec::TST_unspecified: return "unspecified"; |
| 185 | case DeclSpec::TST_void: return "void"; |
| 186 | case DeclSpec::TST_char: return "char"; |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 187 | case DeclSpec::TST_wchar: return "wchar_t"; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 188 | case DeclSpec::TST_char16: return "char16_t"; |
| 189 | case DeclSpec::TST_char32: return "char32_t"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 190 | case DeclSpec::TST_int: return "int"; |
| 191 | case DeclSpec::TST_float: return "float"; |
| 192 | case DeclSpec::TST_double: return "double"; |
| 193 | case DeclSpec::TST_bool: return "_Bool"; |
| 194 | case DeclSpec::TST_decimal32: return "_Decimal32"; |
| 195 | case DeclSpec::TST_decimal64: return "_Decimal64"; |
| 196 | case DeclSpec::TST_decimal128: return "_Decimal128"; |
| 197 | case DeclSpec::TST_enum: return "enum"; |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 198 | case DeclSpec::TST_class: return "class"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 199 | case DeclSpec::TST_union: return "union"; |
| 200 | case DeclSpec::TST_struct: return "struct"; |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 201 | case DeclSpec::TST_typename: return "type-name"; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 202 | case DeclSpec::TST_typeofType: |
| 203 | case DeclSpec::TST_typeofExpr: return "typeof"; |
John McCall | 32d335e | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 204 | case DeclSpec::TST_auto: return "auto"; |
| 205 | case DeclSpec::TST_decltype: return "(decltype)"; |
| 206 | case DeclSpec::TST_error: return "(error)"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 207 | } |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 208 | llvm_unreachable("Unknown typespec!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 209 | } |
| 210 | |
John McCall | 32d335e | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 211 | const char *DeclSpec::getSpecifierName(TQ T) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 212 | switch (T) { |
John McCall | 32d335e | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 213 | case DeclSpec::TQ_unspecified: return "unspecified"; |
| 214 | case DeclSpec::TQ_const: return "const"; |
| 215 | case DeclSpec::TQ_restrict: return "restrict"; |
| 216 | case DeclSpec::TQ_volatile: return "volatile"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 217 | } |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 218 | llvm_unreachable("Unknown typespec!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 222 | const char *&PrevSpec, |
| 223 | unsigned &DiagID) { |
Abramo Bagnara | 35f9a19 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 224 | if (StorageClassSpec != SCS_unspecified) { |
| 225 | // Changing storage class is allowed only if the previous one |
| 226 | // was the 'extern' that is part of a linkage specification and |
| 227 | // the new storage class is 'typedef'. |
| 228 | if (!(SCS_extern_in_linkage_spec && |
| 229 | StorageClassSpec == SCS_extern && |
| 230 | S == SCS_typedef)) |
| 231 | return BadSpecifier(S, (SCS)StorageClassSpec, PrevSpec, DiagID); |
| 232 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 233 | StorageClassSpec = S; |
| 234 | StorageClassSpecLoc = Loc; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 235 | assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 236 | return false; |
| 237 | } |
| 238 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 239 | bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 240 | const char *&PrevSpec, |
| 241 | unsigned &DiagID) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 242 | if (SCS_thread_specified) { |
| 243 | PrevSpec = "__thread"; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 244 | DiagID = diag::ext_duplicate_declspec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 245 | return true; |
| 246 | } |
| 247 | SCS_thread_specified = true; |
| 248 | SCS_threadLoc = Loc; |
| 249 | return false; |
| 250 | } |
| 251 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 252 | /// These methods set the specified attribute of the DeclSpec, but return true |
| 253 | /// and ignore the request if invalid (e.g. "extern" then "auto" is |
| 254 | /// specified). |
| 255 | bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 256 | const char *&PrevSpec, |
| 257 | unsigned &DiagID) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 258 | if (TypeSpecWidth != TSW_unspecified && |
| 259 | // Allow turning long -> long long. |
| 260 | (W != TSW_longlong || TypeSpecWidth != TSW_long)) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 261 | return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 262 | TypeSpecWidth = W; |
| 263 | TSWLoc = Loc; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 264 | if (TypeAltiVecVector && !TypeAltiVecBool && |
| 265 | ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) { |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 266 | PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); |
| 267 | DiagID = diag::warn_vector_long_decl_spec_combination; |
| 268 | return true; |
| 269 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 270 | return false; |
| 271 | } |
| 272 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 273 | bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 274 | const char *&PrevSpec, |
| 275 | unsigned &DiagID) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 276 | if (TypeSpecComplex != TSC_unspecified) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 277 | return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 278 | TypeSpecComplex = C; |
| 279 | TSCLoc = Loc; |
| 280 | return false; |
| 281 | } |
| 282 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 284 | const char *&PrevSpec, |
| 285 | unsigned &DiagID) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 286 | if (TypeSpecSign != TSS_unspecified) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 287 | return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 288 | TypeSpecSign = S; |
| 289 | TSSLoc = Loc; |
| 290 | return false; |
| 291 | } |
| 292 | |
| 293 | bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 294 | const char *&PrevSpec, |
| 295 | unsigned &DiagID, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 296 | ParsedType Rep) { |
| 297 | assert(isTypeRep(T) && "T does not store a type"); |
| 298 | assert(Rep && "no type provided!"); |
| 299 | if (TypeSpecType != TST_unspecified) { |
| 300 | PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); |
| 301 | DiagID = diag::err_invalid_decl_spec_combination; |
| 302 | return true; |
| 303 | } |
| 304 | TypeSpecType = T; |
| 305 | TypeRep = Rep; |
| 306 | TSTLoc = Loc; |
| 307 | TypeSpecOwned = false; |
| 308 | return false; |
| 309 | } |
| 310 | |
| 311 | bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc, |
| 312 | const char *&PrevSpec, |
| 313 | unsigned &DiagID, |
| 314 | Expr *Rep) { |
| 315 | assert(isExprRep(T) && "T does not store an expr"); |
| 316 | assert(Rep && "no expression provided!"); |
| 317 | if (TypeSpecType != TST_unspecified) { |
| 318 | PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); |
| 319 | DiagID = diag::err_invalid_decl_spec_combination; |
| 320 | return true; |
| 321 | } |
| 322 | TypeSpecType = T; |
| 323 | ExprRep = Rep; |
| 324 | TSTLoc = Loc; |
| 325 | TypeSpecOwned = false; |
| 326 | return false; |
| 327 | } |
| 328 | |
| 329 | bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc, |
| 330 | const char *&PrevSpec, |
| 331 | unsigned &DiagID, |
| 332 | Decl *Rep, bool Owned) { |
| 333 | assert(isDeclRep(T) && "T does not store a decl"); |
| 334 | // Unlike the other cases, we don't assert that we actually get a decl. |
| 335 | |
| 336 | if (TypeSpecType != TST_unspecified) { |
| 337 | PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); |
| 338 | DiagID = diag::err_invalid_decl_spec_combination; |
| 339 | return true; |
| 340 | } |
| 341 | TypeSpecType = T; |
| 342 | DeclRep = Rep; |
| 343 | TSTLoc = Loc; |
| 344 | TypeSpecOwned = Owned; |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc, |
| 349 | const char *&PrevSpec, |
| 350 | unsigned &DiagID) { |
| 351 | assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) && |
| 352 | "rep required for these type-spec kinds!"); |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 353 | if (TypeSpecType != TST_unspecified) { |
| 354 | PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); |
| 355 | DiagID = diag::err_invalid_decl_spec_combination; |
| 356 | return true; |
| 357 | } |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 358 | if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) { |
| 359 | TypeAltiVecBool = true; |
| 360 | TSTLoc = Loc; |
| 361 | return false; |
| 362 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 363 | TypeSpecType = T; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 364 | TSTLoc = Loc; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 365 | TypeSpecOwned = false; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 366 | if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) { |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 367 | PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 368 | DiagID = diag::err_invalid_vector_decl_spec; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 369 | return true; |
| 370 | } |
| 371 | return false; |
| 372 | } |
| 373 | |
| 374 | bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc, |
| 375 | const char *&PrevSpec, unsigned &DiagID) { |
| 376 | if (TypeSpecType != TST_unspecified) { |
| 377 | PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); |
| 378 | DiagID = diag::err_invalid_vector_decl_spec_combination; |
| 379 | return true; |
| 380 | } |
| 381 | TypeAltiVecVector = isAltiVecVector; |
| 382 | AltiVecLoc = Loc; |
| 383 | return false; |
| 384 | } |
| 385 | |
| 386 | bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc, |
| 387 | const char *&PrevSpec, unsigned &DiagID) { |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 388 | if (!TypeAltiVecVector || TypeAltiVecPixel || |
| 389 | (TypeSpecType != TST_unspecified)) { |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 390 | PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); |
| 391 | DiagID = diag::err_invalid_pixel_decl_spec_combination; |
| 392 | return true; |
| 393 | } |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 394 | TypeAltiVecPixel = isAltiVecPixel; |
| 395 | TSTLoc = Loc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 396 | return false; |
| 397 | } |
| 398 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 399 | bool DeclSpec::SetTypeSpecError() { |
| 400 | TypeSpecType = TST_error; |
John McCall | 9e46b8c | 2010-08-26 17:22:34 +0000 | [diff] [blame] | 401 | TypeSpecOwned = false; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 402 | TSTLoc = SourceLocation(); |
| 403 | return false; |
| 404 | } |
| 405 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 406 | bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 407 | unsigned &DiagID, const LangOptions &Lang) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 408 | // Duplicates turn into warnings pre-C99. |
| 409 | if ((TypeQualifiers & T) && !Lang.C99) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 410 | return BadSpecifier(T, T, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 411 | TypeQualifiers |= T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 412 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 413 | switch (T) { |
| 414 | default: assert(0 && "Unknown type qualifier!"); |
| 415 | case TQ_const: TQ_constLoc = Loc; break; |
| 416 | case TQ_restrict: TQ_restrictLoc = Loc; break; |
| 417 | case TQ_volatile: TQ_volatileLoc = Loc; break; |
| 418 | } |
| 419 | return false; |
| 420 | } |
| 421 | |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 422 | bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec, |
| 423 | unsigned &DiagID) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 424 | // 'inline inline' is ok. |
| 425 | FS_inline_specified = true; |
| 426 | FS_inlineLoc = Loc; |
| 427 | return false; |
| 428 | } |
| 429 | |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 430 | bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec, |
| 431 | unsigned &DiagID) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 432 | // 'virtual virtual' is ok. |
| 433 | FS_virtual_specified = true; |
| 434 | FS_virtualLoc = Loc; |
| 435 | return false; |
| 436 | } |
| 437 | |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 438 | bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec, |
| 439 | unsigned &DiagID) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 440 | // 'explicit explicit' is ok. |
| 441 | FS_explicit_specified = true; |
| 442 | FS_explicitLoc = Loc; |
| 443 | return false; |
| 444 | } |
| 445 | |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 446 | bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec, |
| 447 | unsigned &DiagID) { |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 448 | if (Friend_specified) { |
| 449 | PrevSpec = "friend"; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 450 | DiagID = diag::ext_duplicate_declspec; |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 451 | return true; |
| 452 | } |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 453 | |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 454 | Friend_specified = true; |
| 455 | FriendLoc = Loc; |
| 456 | return false; |
| 457 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 458 | |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 459 | bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec, |
| 460 | unsigned &DiagID) { |
| 461 | // 'constexpr constexpr' is ok. |
| 462 | Constexpr_specified = true; |
| 463 | ConstexprLoc = Loc; |
| 464 | return false; |
| 465 | } |
| 466 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 467 | void DeclSpec::setProtocolQualifiers(Decl * const *Protos, |
Argyrios Kyrtzidis | e3a535b | 2009-09-29 19:42:11 +0000 | [diff] [blame] | 468 | unsigned NP, |
| 469 | SourceLocation *ProtoLocs, |
| 470 | SourceLocation LAngleLoc) { |
| 471 | if (NP == 0) return; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 472 | ProtocolQualifiers = new Decl*[NP]; |
Argyrios Kyrtzidis | e3a535b | 2009-09-29 19:42:11 +0000 | [diff] [blame] | 473 | ProtocolLocs = new SourceLocation[NP]; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 474 | memcpy((void*)ProtocolQualifiers, Protos, sizeof(Decl*)*NP); |
Argyrios Kyrtzidis | e3a535b | 2009-09-29 19:42:11 +0000 | [diff] [blame] | 475 | memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP); |
| 476 | NumProtocolQualifiers = NP; |
| 477 | ProtocolLAngleLoc = LAngleLoc; |
| 478 | } |
| 479 | |
Douglas Gregor | ddf889a | 2010-01-18 18:04:31 +0000 | [diff] [blame] | 480 | void DeclSpec::SaveWrittenBuiltinSpecs() { |
| 481 | writtenBS.Sign = getTypeSpecSign(); |
| 482 | writtenBS.Width = getTypeSpecWidth(); |
| 483 | writtenBS.Type = getTypeSpecType(); |
| 484 | // Search the list of attributes for the presence of a mode attribute. |
| 485 | writtenBS.ModeAttr = false; |
| 486 | AttributeList* attrs = getAttributes(); |
| 487 | while (attrs) { |
| 488 | if (attrs->getKind() == AttributeList::AT_mode) { |
| 489 | writtenBS.ModeAttr = true; |
| 490 | break; |
| 491 | } |
| 492 | attrs = attrs->getNext(); |
| 493 | } |
| 494 | } |
| 495 | |
Abramo Bagnara | 35f9a19 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 496 | void DeclSpec::SaveStorageSpecifierAsWritten() { |
| 497 | if (SCS_extern_in_linkage_spec && StorageClassSpec == SCS_extern) |
| 498 | // If 'extern' is part of a linkage specification, |
| 499 | // then it is not a storage class "as written". |
| 500 | StorageClassSpecAsWritten = SCS_unspecified; |
| 501 | else |
| 502 | StorageClassSpecAsWritten = StorageClassSpec; |
| 503 | } |
| 504 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 505 | /// Finish - This does final analysis of the declspec, rejecting things like |
| 506 | /// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or |
| 507 | /// diag::NUM_DIAGNOSTICS if there is no error. After calling this method, |
| 508 | /// DeclSpec is guaranteed self-consistent, even if an error occurred. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 509 | void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) { |
Douglas Gregor | ddf889a | 2010-01-18 18:04:31 +0000 | [diff] [blame] | 510 | // Before possibly changing their values, save specs as written. |
| 511 | SaveWrittenBuiltinSpecs(); |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 512 | SaveStorageSpecifierAsWritten(); |
Douglas Gregor | ddf889a | 2010-01-18 18:04:31 +0000 | [diff] [blame] | 513 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 514 | // Check the type specifier components first. |
| 515 | |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 516 | // Validate and finalize AltiVec vector declspec. |
| 517 | if (TypeAltiVecVector) { |
| 518 | if (TypeAltiVecBool) { |
| 519 | // Sign specifiers are not allowed with vector bool. (PIM 2.1) |
| 520 | if (TypeSpecSign != TSS_unspecified) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame^] | 521 | Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec) |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 522 | << getSpecifierName((TSS)TypeSpecSign); |
| 523 | } |
| 524 | |
| 525 | // Only char/int are valid with vector bool. (PIM 2.1) |
Duncan Sands | 2e964a92 | 2010-06-23 19:34:52 +0000 | [diff] [blame] | 526 | if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) && |
| 527 | (TypeSpecType != TST_int)) || TypeAltiVecPixel) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame^] | 528 | Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec) |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 529 | << (TypeAltiVecPixel ? "__pixel" : |
| 530 | getSpecifierName((TST)TypeSpecType)); |
| 531 | } |
| 532 | |
| 533 | // Only 'short' is valid with vector bool. (PIM 2.1) |
| 534 | if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short)) |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame^] | 535 | Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec) |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 536 | << getSpecifierName((TSW)TypeSpecWidth); |
| 537 | |
| 538 | // Elements of vector bool are interpreted as unsigned. (PIM 2.1) |
| 539 | if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) || |
| 540 | (TypeSpecWidth != TSW_unspecified)) |
| 541 | TypeSpecSign = TSS_unsigned; |
| 542 | } |
| 543 | |
| 544 | if (TypeAltiVecPixel) { |
| 545 | //TODO: perform validation |
| 546 | TypeSpecType = TST_int; |
| 547 | TypeSpecSign = TSS_unsigned; |
| 548 | TypeSpecWidth = TSW_short; |
John McCall | 9e46b8c | 2010-08-26 17:22:34 +0000 | [diff] [blame] | 549 | TypeSpecOwned = false; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 553 | // signed/unsigned are only valid with int/char/wchar_t. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 554 | if (TypeSpecSign != TSS_unspecified) { |
| 555 | if (TypeSpecType == TST_unspecified) |
| 556 | TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int. |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 557 | else if (TypeSpecType != TST_int && |
| 558 | TypeSpecType != TST_char && TypeSpecType != TST_wchar) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame^] | 559 | Diag(D, TSSLoc, diag::err_invalid_sign_spec) |
Chris Lattner | 254be6a | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 560 | << getSpecifierName((TST)TypeSpecType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 561 | // signed double -> double. |
| 562 | TypeSpecSign = TSS_unspecified; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | // Validate the width of the type. |
| 567 | switch (TypeSpecWidth) { |
| 568 | case TSW_unspecified: break; |
| 569 | case TSW_short: // short int |
| 570 | case TSW_longlong: // long long int |
| 571 | if (TypeSpecType == TST_unspecified) |
| 572 | TypeSpecType = TST_int; // short -> short int, long long -> long long int. |
| 573 | else if (TypeSpecType != TST_int) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame^] | 574 | Diag(D, TSWLoc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 575 | TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec |
Chris Lattner | 254be6a | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 576 | : diag::err_invalid_longlong_spec) |
| 577 | << getSpecifierName((TST)TypeSpecType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 578 | TypeSpecType = TST_int; |
John McCall | 9e46b8c | 2010-08-26 17:22:34 +0000 | [diff] [blame] | 579 | TypeSpecOwned = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 580 | } |
| 581 | break; |
| 582 | case TSW_long: // long double, long int |
| 583 | if (TypeSpecType == TST_unspecified) |
| 584 | TypeSpecType = TST_int; // long -> long int. |
| 585 | else if (TypeSpecType != TST_int && TypeSpecType != TST_double) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame^] | 586 | Diag(D, TSWLoc, diag::err_invalid_long_spec) |
Chris Lattner | 254be6a | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 587 | << getSpecifierName((TST)TypeSpecType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 588 | TypeSpecType = TST_int; |
John McCall | 9e46b8c | 2010-08-26 17:22:34 +0000 | [diff] [blame] | 589 | TypeSpecOwned = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 590 | } |
| 591 | break; |
| 592 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 593 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 594 | // TODO: if the implementation does not implement _Complex or _Imaginary, |
| 595 | // disallow their use. Need information about the backend. |
| 596 | if (TypeSpecComplex != TSC_unspecified) { |
| 597 | if (TypeSpecType == TST_unspecified) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame^] | 598 | Diag(D, TSCLoc, diag::ext_plain_complex) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 599 | << FixItHint::CreateInsertion( |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 600 | PP.getLocForEndOfToken(getTypeSpecComplexLoc()), |
| 601 | " double"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 602 | TypeSpecType = TST_double; // _Complex -> _Complex double. |
| 603 | } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) { |
| 604 | // Note that this intentionally doesn't include _Complex _Bool. |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame^] | 605 | Diag(D, TSTLoc, diag::ext_integer_complex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 606 | } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame^] | 607 | Diag(D, TSCLoc, diag::err_invalid_complex_spec) |
Chris Lattner | 254be6a | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 608 | << getSpecifierName((TST)TypeSpecType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 609 | TypeSpecComplex = TSC_unspecified; |
| 610 | } |
| 611 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 612 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 613 | // C++ [class.friend]p6: |
| 614 | // No storage-class-specifier shall appear in the decl-specifier-seq |
| 615 | // of a friend declaration. |
| 616 | if (isFriendSpecified() && getStorageClassSpec()) { |
| 617 | DeclSpec::SCS SC = getStorageClassSpec(); |
| 618 | const char *SpecName = getSpecifierName(SC); |
| 619 | |
| 620 | SourceLocation SCLoc = getStorageClassSpecLoc(); |
| 621 | SourceLocation SCEndLoc = SCLoc.getFileLocWithOffset(strlen(SpecName)); |
| 622 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame^] | 623 | Diag(D, SCLoc, diag::err_friend_storage_spec) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 624 | << SpecName |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 625 | << FixItHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc)); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 626 | |
| 627 | ClearStorageClassSpecs(); |
| 628 | } |
| 629 | |
John McCall | 9e46b8c | 2010-08-26 17:22:34 +0000 | [diff] [blame] | 630 | assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType)); |
| 631 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 632 | // Okay, now we can infer the real type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 633 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 634 | // TODO: return "auto function" and other bad things based on the real type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 635 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 636 | // 'data definition has no type or storage class'? |
| 637 | } |
Daniel Dunbar | e4858a6 | 2008-08-11 03:45:03 +0000 | [diff] [blame] | 638 | |
Sebastian Redl | a4ed0d8 | 2008-12-28 15:28:59 +0000 | [diff] [blame] | 639 | bool DeclSpec::isMissingDeclaratorOk() { |
| 640 | TST tst = getTypeSpecType(); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 641 | return isDeclRep(tst) && getRepAsDecl() != 0 && |
| 642 | StorageClassSpec != DeclSpec::SCS_typedef; |
Sebastian Redl | a4ed0d8 | 2008-12-28 15:28:59 +0000 | [diff] [blame] | 643 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 644 | |
| 645 | void UnqualifiedId::clear() { |
| 646 | if (Kind == IK_TemplateId) |
| 647 | TemplateId->Destroy(); |
| 648 | |
| 649 | Kind = IK_Identifier; |
| 650 | Identifier = 0; |
| 651 | StartLocation = SourceLocation(); |
| 652 | EndLocation = SourceLocation(); |
| 653 | } |
| 654 | |
| 655 | void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc, |
| 656 | OverloadedOperatorKind Op, |
| 657 | SourceLocation SymbolLocations[3]) { |
| 658 | Kind = IK_OperatorFunctionId; |
| 659 | StartLocation = OperatorLoc; |
| 660 | EndLocation = OperatorLoc; |
| 661 | OperatorFunctionId.Operator = Op; |
| 662 | for (unsigned I = 0; I != 3; ++I) { |
| 663 | OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding(); |
| 664 | |
| 665 | if (SymbolLocations[I].isValid()) |
| 666 | EndLocation = SymbolLocations[I]; |
| 667 | } |
| 668 | } |