Chris Lattner | 289ab7b | 2006-11-08 06:54:53 +0000 | [diff] [blame] | 1 | //===--- SemaDeclSpec.cpp - Declaration Specifier Semantic Analysis -------===// |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 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. |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | 289ab7b | 2006-11-08 06:54:53 +0000 | [diff] [blame] | 10 | // This file implements semantic analysis for declaration specifiers. |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 8b0666c | 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 | e3e01a2 | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 17 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 18 | #include "clang/Basic/LangOptions.h" |
Chris Lattner | 1ce41ed | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/STLExtras.h" |
John McCall | 898cd0f | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ErrorHandling.h" |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 21 | #include <cstring> |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Chris Lattner | 3b0f3ef | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 24 | |
| 25 | static DiagnosticBuilder Diag(Diagnostic &D, SourceLocation Loc, |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 26 | unsigned DiagID) { |
| 27 | return D.Report(Loc, DiagID); |
Chris Lattner | 3b0f3ef | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Douglas Gregor | b53edfb | 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 | 9de54ea | 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 | 1ce41ed | 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 | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 49 | DeclaratorChunk DeclaratorChunk::getFunction(const ParsedAttributes &attrs, |
| 50 | bool hasProto, bool isVariadic, |
Douglas Gregor | 94349fd | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 51 | SourceLocation EllipsisLoc, |
Chris Lattner | 1ce41ed | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 52 | ParamInfo *ArgInfo, |
| 53 | unsigned NumArgs, |
| 54 | unsigned TypeQuals, |
Douglas Gregor | 5499235 | 2011-01-26 03:43:54 +0000 | [diff] [blame^] | 55 | bool RefQualifierIsLvalueRef, |
| 56 | SourceLocation RefQualifierLoc, |
Sebastian Redl | 2b9cacb | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 57 | bool hasExceptionSpec, |
Sebastian Redl | fb3f179 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 58 | SourceLocation ThrowLoc, |
Sebastian Redl | 2b9cacb | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 59 | bool hasAnyExceptionSpec, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 60 | ParsedType *Exceptions, |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 61 | SourceRange *ExceptionRanges, |
Sebastian Redl | 2b9cacb | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 62 | unsigned NumExceptions, |
Argyrios Kyrtzidis | 20cf191 | 2009-08-19 23:14:54 +0000 | [diff] [blame] | 63 | SourceLocation LPLoc, |
| 64 | SourceLocation RPLoc, |
Douglas Gregor | 7fb2541 | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 65 | Declarator &TheDeclarator, |
| 66 | ParsedType TrailingReturnType) { |
Chris Lattner | 1ce41ed | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 67 | DeclaratorChunk I; |
Sebastian Redl | 2b9cacb | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 68 | I.Kind = Function; |
Argyrios Kyrtzidis | 20cf191 | 2009-08-19 23:14:54 +0000 | [diff] [blame] | 69 | I.Loc = LPLoc; |
| 70 | I.EndLoc = RPLoc; |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 71 | I.Fun.AttrList = attrs.getList(); |
Sebastian Redl | 2b9cacb | 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 | 5499235 | 2011-01-26 03:43:54 +0000 | [diff] [blame^] | 79 | I.Fun.RefQualifierIsLValueRef = RefQualifierIsLvalueRef; |
| 80 | I.Fun.RefQualifierLoc = RefQualifierLoc.getRawEncoding(); |
Sebastian Redl | 2b9cacb | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 81 | I.Fun.hasExceptionSpec = hasExceptionSpec; |
Sebastian Redl | fb3f179 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 82 | I.Fun.ThrowLoc = ThrowLoc.getRawEncoding(); |
Sebastian Redl | 2b9cacb | 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 | 7fb2541 | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 86 | I.Fun.TrailingReturnType = TrailingReturnType.getAsOpaquePtr(); |
Sebastian Redl | 2b9cacb | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 87 | |
Chris Lattner | 1ce41ed | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | if (!TheDeclarator.InlineParamsUsed && |
Chris Lattner | 1ce41ed | 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 | 2b9cacb | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 105 | // new[] an exception array if needed |
| 106 | if (NumExceptions) { |
Sebastian Redl | d643456 | 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 | 2b9cacb | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 112 | } |
Chris Lattner | 1ce41ed | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 113 | return I; |
| 114 | } |
Chris Lattner | 3b0f3ef | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 115 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 116 | /// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this |
Chris Lattner | e0c5116 | 2009-02-27 18:35:46 +0000 | [diff] [blame] | 117 | /// declaration specifier includes. |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 118 | /// |
| 119 | unsigned DeclSpec::getParsedSpecifiers() const { |
| 120 | unsigned Res = 0; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 121 | if (StorageClassSpec != SCS_unspecified || |
| 122 | SCS_thread_specified) |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 123 | Res |= PQ_StorageClassSpecifier; |
Mike Stump | 65643c6 | 2008-06-19 19:52:46 +0000 | [diff] [blame] | 124 | |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 125 | if (TypeQualifiers != TQ_unspecified) |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 126 | Res |= PQ_TypeQualifier; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 | |
Chris Lattner | f055d43 | 2006-11-28 04:28:12 +0000 | [diff] [blame] | 128 | if (hasTypeSpecifier()) |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 129 | Res |= PQ_TypeSpecifier; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 130 | |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 131 | if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified) |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 132 | Res |= PQ_FunctionSpecifier; |
| 133 | return Res; |
| 134 | } |
| 135 | |
John McCall | 49bfce4 | 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 | 898cd0f | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 139 | PrevSpec = DeclSpec::getSpecifierName(TPrev); |
John McCall | 49bfce4 | 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 | 898cd0f | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 142 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | } |
John McCall | 898cd0f | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 144 | |
Chris Lattner | 7bd11fe | 2007-01-23 04:35:33 +0000 | [diff] [blame] | 145 | const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) { |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 146 | switch (S) { |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +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 | d5c0eed | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 153 | case DeclSpec::SCS_private_extern: return "__private_extern__"; |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 154 | case DeclSpec::SCS_mutable: return "mutable"; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 155 | } |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 156 | llvm_unreachable("Unknown typespec!"); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 157 | } |
| 158 | |
John McCall | 898cd0f | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 159 | const char *DeclSpec::getSpecifierName(TSW W) { |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 160 | switch (W) { |
John McCall | 898cd0f | 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"; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 165 | } |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 166 | llvm_unreachable("Unknown typespec!"); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 167 | } |
| 168 | |
John McCall | 898cd0f | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 169 | const char *DeclSpec::getSpecifierName(TSC C) { |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 170 | switch (C) { |
John McCall | 898cd0f | 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"; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 174 | } |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 175 | llvm_unreachable("Unknown typespec!"); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | |
John McCall | 898cd0f | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 179 | const char *DeclSpec::getSpecifierName(TSS S) { |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 180 | switch (S) { |
John McCall | 898cd0f | 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"; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 184 | } |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 185 | llvm_unreachable("Unknown typespec!"); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Chris Lattner | 69680ea | 2007-01-23 04:34:43 +0000 | [diff] [blame] | 188 | const char *DeclSpec::getSpecifierName(DeclSpec::TST T) { |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 189 | switch (T) { |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +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 | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 193 | case DeclSpec::TST_wchar: return "wchar_t"; |
Alisdair Meredith | a9ad47d | 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"; |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +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"; |
Chris Lattner | da72c82 | 2006-08-13 22:16:42 +0000 | [diff] [blame] | 203 | case DeclSpec::TST_enum: return "enum"; |
Chris Lattner | 861a226 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 204 | case DeclSpec::TST_class: return "class"; |
Chris Lattner | da72c82 | 2006-08-13 22:16:42 +0000 | [diff] [blame] | 205 | case DeclSpec::TST_union: return "union"; |
| 206 | case DeclSpec::TST_struct: return "struct"; |
Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 207 | case DeclSpec::TST_typename: return "type-name"; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 208 | case DeclSpec::TST_typeofType: |
| 209 | case DeclSpec::TST_typeofExpr: return "typeof"; |
John McCall | 898cd0f | 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)"; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 213 | } |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 214 | llvm_unreachable("Unknown typespec!"); |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 215 | } |
| 216 | |
John McCall | 898cd0f | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 217 | const char *DeclSpec::getSpecifierName(TQ T) { |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 218 | switch (T) { |
John McCall | 898cd0f | 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"; |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 223 | } |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 224 | llvm_unreachable("Unknown typespec!"); |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 225 | } |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 226 | |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 227 | bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc, |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 228 | const char *&PrevSpec, |
| 229 | unsigned &DiagID) { |
Abramo Bagnara | ed5b689 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 230 | if (StorageClassSpec != SCS_unspecified) { |
| 231 | // Changing storage class is allowed only if the previous one |
| 232 | // was the 'extern' that is part of a linkage specification and |
| 233 | // the new storage class is 'typedef'. |
| 234 | if (!(SCS_extern_in_linkage_spec && |
| 235 | StorageClassSpec == SCS_extern && |
| 236 | S == SCS_typedef)) |
| 237 | return BadSpecifier(S, (SCS)StorageClassSpec, PrevSpec, DiagID); |
| 238 | } |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 239 | StorageClassSpec = S; |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 240 | StorageClassSpecLoc = Loc; |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 241 | assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield"); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 242 | return false; |
| 243 | } |
| 244 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 245 | bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc, |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 246 | const char *&PrevSpec, |
| 247 | unsigned &DiagID) { |
Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 248 | if (SCS_thread_specified) { |
| 249 | PrevSpec = "__thread"; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 250 | DiagID = diag::ext_duplicate_declspec; |
Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 251 | return true; |
| 252 | } |
| 253 | SCS_thread_specified = true; |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 254 | SCS_threadLoc = Loc; |
Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 255 | return false; |
| 256 | } |
| 257 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 258 | /// These methods set the specified attribute of the DeclSpec, but return true |
| 259 | /// and ignore the request if invalid (e.g. "extern" then "auto" is |
| 260 | /// specified). |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 261 | bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc, |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 262 | const char *&PrevSpec, |
| 263 | unsigned &DiagID) { |
Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 264 | if (TypeSpecWidth != TSW_unspecified && |
| 265 | // Allow turning long -> long long. |
| 266 | (W != TSW_longlong || TypeSpecWidth != TSW_long)) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 267 | return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 268 | TypeSpecWidth = W; |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 269 | TSWLoc = Loc; |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 270 | if (TypeAltiVecVector && !TypeAltiVecBool && |
| 271 | ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) { |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 272 | PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); |
| 273 | DiagID = diag::warn_vector_long_decl_spec_combination; |
| 274 | return true; |
| 275 | } |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 276 | return false; |
| 277 | } |
| 278 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 279 | bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc, |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 280 | const char *&PrevSpec, |
| 281 | unsigned &DiagID) { |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 282 | if (TypeSpecComplex != TSC_unspecified) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 283 | return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 284 | TypeSpecComplex = C; |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 285 | TSCLoc = Loc; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 286 | return false; |
| 287 | } |
| 288 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 289 | bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc, |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 290 | const char *&PrevSpec, |
| 291 | unsigned &DiagID) { |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 292 | if (TypeSpecSign != TSS_unspecified) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 293 | return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID); |
Chris Lattner | deb42f5 | 2006-08-04 05:26:52 +0000 | [diff] [blame] | 294 | TypeSpecSign = S; |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 295 | TSSLoc = Loc; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 296 | return false; |
| 297 | } |
| 298 | |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 299 | bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc, |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 300 | const char *&PrevSpec, |
| 301 | unsigned &DiagID, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 302 | ParsedType Rep) { |
| 303 | assert(isTypeRep(T) && "T does not store a type"); |
| 304 | assert(Rep && "no type provided!"); |
| 305 | if (TypeSpecType != TST_unspecified) { |
| 306 | PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); |
| 307 | DiagID = diag::err_invalid_decl_spec_combination; |
| 308 | return true; |
| 309 | } |
| 310 | TypeSpecType = T; |
| 311 | TypeRep = Rep; |
| 312 | TSTLoc = Loc; |
| 313 | TypeSpecOwned = false; |
| 314 | return false; |
| 315 | } |
| 316 | |
| 317 | bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc, |
| 318 | const char *&PrevSpec, |
| 319 | unsigned &DiagID, |
| 320 | Expr *Rep) { |
| 321 | assert(isExprRep(T) && "T does not store an expr"); |
| 322 | assert(Rep && "no expression 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 | ExprRep = 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 | Decl *Rep, bool Owned) { |
| 339 | assert(isDeclRep(T) && "T does not store a decl"); |
| 340 | // Unlike the other cases, we don't assert that we actually get a decl. |
| 341 | |
| 342 | if (TypeSpecType != TST_unspecified) { |
| 343 | PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); |
| 344 | DiagID = diag::err_invalid_decl_spec_combination; |
| 345 | return true; |
| 346 | } |
| 347 | TypeSpecType = T; |
| 348 | DeclRep = Rep; |
| 349 | TSTLoc = Loc; |
| 350 | TypeSpecOwned = Owned; |
| 351 | return false; |
| 352 | } |
| 353 | |
| 354 | bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc, |
| 355 | const char *&PrevSpec, |
| 356 | unsigned &DiagID) { |
| 357 | assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) && |
| 358 | "rep required for these type-spec kinds!"); |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 359 | if (TypeSpecType != TST_unspecified) { |
| 360 | PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); |
| 361 | DiagID = diag::err_invalid_decl_spec_combination; |
| 362 | return true; |
| 363 | } |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 364 | if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) { |
| 365 | TypeAltiVecBool = true; |
| 366 | TSTLoc = Loc; |
| 367 | return false; |
| 368 | } |
Chris Lattner | deb42f5 | 2006-08-04 05:26:52 +0000 | [diff] [blame] | 369 | TypeSpecType = T; |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 370 | TSTLoc = Loc; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 371 | TypeSpecOwned = false; |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 372 | if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) { |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 373 | PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 374 | DiagID = diag::err_invalid_vector_decl_spec; |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 375 | return true; |
| 376 | } |
| 377 | return false; |
| 378 | } |
| 379 | |
| 380 | bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc, |
| 381 | const char *&PrevSpec, unsigned &DiagID) { |
| 382 | if (TypeSpecType != TST_unspecified) { |
| 383 | PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); |
| 384 | DiagID = diag::err_invalid_vector_decl_spec_combination; |
| 385 | return true; |
| 386 | } |
| 387 | TypeAltiVecVector = isAltiVecVector; |
| 388 | AltiVecLoc = Loc; |
| 389 | return false; |
| 390 | } |
| 391 | |
| 392 | bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc, |
| 393 | const char *&PrevSpec, unsigned &DiagID) { |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 394 | if (!TypeAltiVecVector || TypeAltiVecPixel || |
| 395 | (TypeSpecType != TST_unspecified)) { |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 396 | PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); |
| 397 | DiagID = diag::err_invalid_pixel_decl_spec_combination; |
| 398 | return true; |
| 399 | } |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 400 | TypeAltiVecPixel = isAltiVecPixel; |
| 401 | TSTLoc = Loc; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 402 | return false; |
| 403 | } |
| 404 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 405 | bool DeclSpec::SetTypeSpecError() { |
| 406 | TypeSpecType = TST_error; |
John McCall | a3707cc | 2010-08-26 17:22:34 +0000 | [diff] [blame] | 407 | TypeSpecOwned = false; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 408 | TSTLoc = SourceLocation(); |
| 409 | return false; |
| 410 | } |
| 411 | |
Chris Lattner | 60809f5 | 2006-11-28 05:18:46 +0000 | [diff] [blame] | 412 | bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec, |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 413 | unsigned &DiagID, const LangOptions &Lang) { |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 414 | // Duplicates turn into warnings pre-C99. |
| 415 | if ((TypeQualifiers & T) && !Lang.C99) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 416 | return BadSpecifier(T, T, PrevSpec, DiagID); |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 417 | TypeQualifiers |= T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 418 | |
Chris Lattner | 60809f5 | 2006-11-28 05:18:46 +0000 | [diff] [blame] | 419 | switch (T) { |
| 420 | default: assert(0 && "Unknown type qualifier!"); |
| 421 | case TQ_const: TQ_constLoc = Loc; break; |
| 422 | case TQ_restrict: TQ_restrictLoc = Loc; break; |
| 423 | case TQ_volatile: TQ_volatileLoc = Loc; break; |
| 424 | } |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 425 | return false; |
| 426 | } |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 427 | |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 428 | bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec, |
| 429 | unsigned &DiagID) { |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 430 | // 'inline inline' is ok. |
| 431 | FS_inline_specified = true; |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 432 | FS_inlineLoc = Loc; |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 433 | return false; |
| 434 | } |
| 435 | |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 436 | bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec, |
| 437 | unsigned &DiagID) { |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 438 | // 'virtual virtual' is ok. |
| 439 | FS_virtual_specified = true; |
| 440 | FS_virtualLoc = Loc; |
| 441 | return false; |
| 442 | } |
| 443 | |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 444 | bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec, |
| 445 | unsigned &DiagID) { |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 446 | // 'explicit explicit' is ok. |
| 447 | FS_explicit_specified = true; |
| 448 | FS_explicitLoc = Loc; |
| 449 | return false; |
| 450 | } |
| 451 | |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 452 | bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec, |
| 453 | unsigned &DiagID) { |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 454 | if (Friend_specified) { |
| 455 | PrevSpec = "friend"; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 456 | DiagID = diag::ext_duplicate_declspec; |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 457 | return true; |
| 458 | } |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 459 | |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 460 | Friend_specified = true; |
| 461 | FriendLoc = Loc; |
| 462 | return false; |
| 463 | } |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 464 | |
Sebastian Redl | 39c2a8b | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 465 | bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec, |
| 466 | unsigned &DiagID) { |
| 467 | // 'constexpr constexpr' is ok. |
| 468 | Constexpr_specified = true; |
| 469 | ConstexprLoc = Loc; |
| 470 | return false; |
| 471 | } |
| 472 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 473 | void DeclSpec::setProtocolQualifiers(Decl * const *Protos, |
Argyrios Kyrtzidis | 5ec645b | 2009-09-29 19:42:11 +0000 | [diff] [blame] | 474 | unsigned NP, |
| 475 | SourceLocation *ProtoLocs, |
| 476 | SourceLocation LAngleLoc) { |
| 477 | if (NP == 0) return; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 478 | ProtocolQualifiers = new Decl*[NP]; |
Argyrios Kyrtzidis | 5ec645b | 2009-09-29 19:42:11 +0000 | [diff] [blame] | 479 | ProtocolLocs = new SourceLocation[NP]; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 480 | memcpy((void*)ProtocolQualifiers, Protos, sizeof(Decl*)*NP); |
Argyrios Kyrtzidis | 5ec645b | 2009-09-29 19:42:11 +0000 | [diff] [blame] | 481 | memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP); |
| 482 | NumProtocolQualifiers = NP; |
| 483 | ProtocolLAngleLoc = LAngleLoc; |
| 484 | } |
| 485 | |
Douglas Gregor | c9b7a59 | 2010-01-18 18:04:31 +0000 | [diff] [blame] | 486 | void DeclSpec::SaveWrittenBuiltinSpecs() { |
| 487 | writtenBS.Sign = getTypeSpecSign(); |
| 488 | writtenBS.Width = getTypeSpecWidth(); |
| 489 | writtenBS.Type = getTypeSpecType(); |
| 490 | // Search the list of attributes for the presence of a mode attribute. |
| 491 | writtenBS.ModeAttr = false; |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 492 | AttributeList* attrs = getAttributes().getList(); |
Douglas Gregor | c9b7a59 | 2010-01-18 18:04:31 +0000 | [diff] [blame] | 493 | while (attrs) { |
| 494 | if (attrs->getKind() == AttributeList::AT_mode) { |
| 495 | writtenBS.ModeAttr = true; |
| 496 | break; |
| 497 | } |
| 498 | attrs = attrs->getNext(); |
| 499 | } |
| 500 | } |
| 501 | |
Abramo Bagnara | ed5b689 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 502 | void DeclSpec::SaveStorageSpecifierAsWritten() { |
| 503 | if (SCS_extern_in_linkage_spec && StorageClassSpec == SCS_extern) |
| 504 | // If 'extern' is part of a linkage specification, |
| 505 | // then it is not a storage class "as written". |
| 506 | StorageClassSpecAsWritten = SCS_unspecified; |
| 507 | else |
| 508 | StorageClassSpecAsWritten = StorageClassSpec; |
| 509 | } |
| 510 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 511 | /// Finish - This does final analysis of the declspec, rejecting things like |
| 512 | /// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or |
| 513 | /// diag::NUM_DIAGNOSTICS if there is no error. After calling this method, |
| 514 | /// DeclSpec is guaranteed self-consistent, even if an error occurred. |
Douglas Gregor | e3e01a2 | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 515 | void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) { |
Douglas Gregor | c9b7a59 | 2010-01-18 18:04:31 +0000 | [diff] [blame] | 516 | // Before possibly changing their values, save specs as written. |
| 517 | SaveWrittenBuiltinSpecs(); |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 518 | SaveStorageSpecifierAsWritten(); |
Douglas Gregor | c9b7a59 | 2010-01-18 18:04:31 +0000 | [diff] [blame] | 519 | |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 520 | // Check the type specifier components first. |
| 521 | |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 522 | // Validate and finalize AltiVec vector declspec. |
| 523 | if (TypeAltiVecVector) { |
| 524 | if (TypeAltiVecBool) { |
| 525 | // Sign specifiers are not allowed with vector bool. (PIM 2.1) |
| 526 | if (TypeSpecSign != TSS_unspecified) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 527 | Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec) |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 528 | << getSpecifierName((TSS)TypeSpecSign); |
| 529 | } |
| 530 | |
| 531 | // Only char/int are valid with vector bool. (PIM 2.1) |
Duncan Sands | d3e231e | 2010-06-23 19:34:52 +0000 | [diff] [blame] | 532 | if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) && |
| 533 | (TypeSpecType != TST_int)) || TypeAltiVecPixel) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 534 | Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec) |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 535 | << (TypeAltiVecPixel ? "__pixel" : |
| 536 | getSpecifierName((TST)TypeSpecType)); |
| 537 | } |
| 538 | |
| 539 | // Only 'short' is valid with vector bool. (PIM 2.1) |
| 540 | if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short)) |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 541 | Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec) |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 542 | << getSpecifierName((TSW)TypeSpecWidth); |
| 543 | |
| 544 | // Elements of vector bool are interpreted as unsigned. (PIM 2.1) |
| 545 | if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) || |
| 546 | (TypeSpecWidth != TSW_unspecified)) |
| 547 | TypeSpecSign = TSS_unsigned; |
| 548 | } |
| 549 | |
| 550 | if (TypeAltiVecPixel) { |
| 551 | //TODO: perform validation |
| 552 | TypeSpecType = TST_int; |
| 553 | TypeSpecSign = TSS_unsigned; |
| 554 | TypeSpecWidth = TSW_short; |
John McCall | a3707cc | 2010-08-26 17:22:34 +0000 | [diff] [blame] | 555 | TypeSpecOwned = false; |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 559 | // signed/unsigned are only valid with int/char/wchar_t. |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 560 | if (TypeSpecSign != TSS_unspecified) { |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 561 | if (TypeSpecType == TST_unspecified) |
| 562 | TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int. |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 563 | else if (TypeSpecType != TST_int && |
| 564 | TypeSpecType != TST_char && TypeSpecType != TST_wchar) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 565 | Diag(D, TSSLoc, diag::err_invalid_sign_spec) |
Chris Lattner | 3b0f3ef | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 566 | << getSpecifierName((TST)TypeSpecType); |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 567 | // signed double -> double. |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 568 | TypeSpecSign = TSS_unspecified; |
| 569 | } |
| 570 | } |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 571 | |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 572 | // Validate the width of the type. |
| 573 | switch (TypeSpecWidth) { |
| 574 | case TSW_unspecified: break; |
| 575 | case TSW_short: // short int |
| 576 | case TSW_longlong: // long long int |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 577 | if (TypeSpecType == TST_unspecified) |
| 578 | TypeSpecType = TST_int; // short -> short int, long long -> long long int. |
| 579 | else if (TypeSpecType != TST_int) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 580 | Diag(D, TSWLoc, |
Chris Lattner | 36982e4 | 2007-05-16 17:49:37 +0000 | [diff] [blame] | 581 | TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec |
Chris Lattner | 3b0f3ef | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 582 | : diag::err_invalid_longlong_spec) |
| 583 | << getSpecifierName((TST)TypeSpecType); |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 584 | TypeSpecType = TST_int; |
John McCall | a3707cc | 2010-08-26 17:22:34 +0000 | [diff] [blame] | 585 | TypeSpecOwned = false; |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 586 | } |
| 587 | break; |
| 588 | case TSW_long: // long double, long int |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 589 | if (TypeSpecType == TST_unspecified) |
| 590 | TypeSpecType = TST_int; // long -> long int. |
| 591 | else if (TypeSpecType != TST_int && TypeSpecType != TST_double) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 592 | Diag(D, TSWLoc, diag::err_invalid_long_spec) |
Chris Lattner | 3b0f3ef | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 593 | << getSpecifierName((TST)TypeSpecType); |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 594 | TypeSpecType = TST_int; |
John McCall | a3707cc | 2010-08-26 17:22:34 +0000 | [diff] [blame] | 595 | TypeSpecOwned = false; |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 596 | } |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 597 | break; |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 598 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 599 | |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 600 | // TODO: if the implementation does not implement _Complex or _Imaginary, |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 601 | // disallow their use. Need information about the backend. |
| 602 | if (TypeSpecComplex != TSC_unspecified) { |
| 603 | if (TypeSpecType == TST_unspecified) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 604 | Diag(D, TSCLoc, diag::ext_plain_complex) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 605 | << FixItHint::CreateInsertion( |
Douglas Gregor | e3e01a2 | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 606 | PP.getLocForEndOfToken(getTypeSpecComplexLoc()), |
| 607 | " double"); |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 608 | TypeSpecType = TST_double; // _Complex -> _Complex double. |
| 609 | } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) { |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 610 | // Note that this intentionally doesn't include _Complex _Bool. |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 611 | Diag(D, TSTLoc, diag::ext_integer_complex); |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 612 | } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 613 | Diag(D, TSCLoc, diag::err_invalid_complex_spec) |
Chris Lattner | 3b0f3ef | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 614 | << getSpecifierName((TST)TypeSpecType); |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 615 | TypeSpecComplex = TSC_unspecified; |
| 616 | } |
| 617 | } |
Chris Lattner | 8e90ef6 | 2006-08-05 03:30:45 +0000 | [diff] [blame] | 618 | |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 619 | // C++ [class.friend]p6: |
| 620 | // No storage-class-specifier shall appear in the decl-specifier-seq |
| 621 | // of a friend declaration. |
| 622 | if (isFriendSpecified() && getStorageClassSpec()) { |
| 623 | DeclSpec::SCS SC = getStorageClassSpec(); |
| 624 | const char *SpecName = getSpecifierName(SC); |
| 625 | |
| 626 | SourceLocation SCLoc = getStorageClassSpecLoc(); |
| 627 | SourceLocation SCEndLoc = SCLoc.getFileLocWithOffset(strlen(SpecName)); |
| 628 | |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 629 | Diag(D, SCLoc, diag::err_friend_storage_spec) |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 630 | << SpecName |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 631 | << FixItHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc)); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 632 | |
| 633 | ClearStorageClassSpecs(); |
| 634 | } |
| 635 | |
John McCall | a3707cc | 2010-08-26 17:22:34 +0000 | [diff] [blame] | 636 | assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType)); |
| 637 | |
Chris Lattner | 8e90ef6 | 2006-08-05 03:30:45 +0000 | [diff] [blame] | 638 | // Okay, now we can infer the real type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 639 | |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 640 | // TODO: return "auto function" and other bad things based on the real type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 641 | |
Chris Lattner | 1ae2329 | 2006-08-04 06:42:31 +0000 | [diff] [blame] | 642 | // 'data definition has no type or storage class'? |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 643 | } |
Daniel Dunbar | c74b5cc | 2008-08-11 03:45:03 +0000 | [diff] [blame] | 644 | |
Sebastian Redl | a2b5e31 | 2008-12-28 15:28:59 +0000 | [diff] [blame] | 645 | bool DeclSpec::isMissingDeclaratorOk() { |
| 646 | TST tst = getTypeSpecType(); |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 647 | return isDeclRep(tst) && getRepAsDecl() != 0 && |
| 648 | StorageClassSpec != DeclSpec::SCS_typedef; |
Sebastian Redl | a2b5e31 | 2008-12-28 15:28:59 +0000 | [diff] [blame] | 649 | } |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 650 | |
| 651 | void UnqualifiedId::clear() { |
| 652 | if (Kind == IK_TemplateId) |
| 653 | TemplateId->Destroy(); |
| 654 | |
| 655 | Kind = IK_Identifier; |
| 656 | Identifier = 0; |
| 657 | StartLocation = SourceLocation(); |
| 658 | EndLocation = SourceLocation(); |
| 659 | } |
| 660 | |
| 661 | void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc, |
| 662 | OverloadedOperatorKind Op, |
| 663 | SourceLocation SymbolLocations[3]) { |
| 664 | Kind = IK_OperatorFunctionId; |
| 665 | StartLocation = OperatorLoc; |
| 666 | EndLocation = OperatorLoc; |
| 667 | OperatorFunctionId.Operator = Op; |
| 668 | for (unsigned I = 0; I != 3; ++I) { |
| 669 | OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding(); |
| 670 | |
| 671 | if (SymbolLocations[I].isValid()) |
| 672 | EndLocation = SymbolLocations[I]; |
| 673 | } |
| 674 | } |
Anders Carlsson | 5610490 | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 675 | |
Anders Carlsson | 4b63d0e | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 676 | bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc, |
Anders Carlsson | f2ca389 | 2011-01-22 15:58:16 +0000 | [diff] [blame] | 677 | const char *&PrevSpec) { |
Anders Carlsson | 5610490 | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 678 | if (Specifiers & VS) { |
| 679 | PrevSpec = getSpecifierName(VS); |
| 680 | return true; |
| 681 | } |
| 682 | |
| 683 | Specifiers |= VS; |
| 684 | |
| 685 | switch (VS) { |
| 686 | default: assert(0 && "Unknown specifier!"); |
| 687 | case VS_Override: VS_overrideLoc = Loc; break; |
| 688 | case VS_Final: VS_finalLoc = Loc; break; |
| 689 | case VS_New: VS_newLoc = Loc; break; |
| 690 | } |
Anders Carlsson | f2ca389 | 2011-01-22 15:58:16 +0000 | [diff] [blame] | 691 | |
Anders Carlsson | 5610490 | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 692 | return false; |
| 693 | } |
| 694 | |
Anders Carlsson | 4b63d0e | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 695 | const char *VirtSpecifiers::getSpecifierName(Specifier VS) { |
Anders Carlsson | a6d3501 | 2011-01-22 15:11:37 +0000 | [diff] [blame] | 696 | switch (VS) { |
| 697 | default: assert(0 && "Unknown specifier"); |
| 698 | case VS_Override: return "override"; |
| 699 | case VS_Final: return "final"; |
| 700 | case VS_New: return "new"; |
| 701 | } |
| 702 | } |
Anders Carlsson | f2ca389 | 2011-01-22 15:58:16 +0000 | [diff] [blame] | 703 | |
Anders Carlsson | 4b63d0e | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 704 | bool ClassVirtSpecifiers::SetSpecifier(Specifier CVS, SourceLocation Loc, |
Anders Carlsson | f2ca389 | 2011-01-22 15:58:16 +0000 | [diff] [blame] | 705 | const char *&PrevSpec) { |
| 706 | if (Specifiers & CVS) { |
| 707 | PrevSpec = getSpecifierName(CVS); |
| 708 | return true; |
| 709 | } |
| 710 | |
| 711 | Specifiers |= CVS; |
| 712 | |
| 713 | switch (CVS) { |
| 714 | default: assert(0 && "Unknown specifier!"); |
| 715 | case CVS_Final: CVS_finalLoc = Loc; break; |
| 716 | case CVS_Explicit: CVS_explicitLoc = Loc; break; |
| 717 | } |
| 718 | |
| 719 | return false; |
| 720 | } |
| 721 | |
Anders Carlsson | 4b63d0e | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 722 | const char *ClassVirtSpecifiers::getSpecifierName(Specifier CVS) { |
Anders Carlsson | f2ca389 | 2011-01-22 15:58:16 +0000 | [diff] [blame] | 723 | switch (CVS) { |
| 724 | default: assert(0 && "Unknown specifier"); |
| 725 | case CVS_Final: return "final"; |
| 726 | case CVS_Explicit: return "explicit"; |
| 727 | } |
| 728 | } |
| 729 | |