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