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