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