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