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