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