blob: c87f2cff54896cc1aeaef85337971e84f544bebc [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- SemaDeclSpec.cpp - Declaration Specifier Semantic Analysis -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for declaration specifiers.
11//
12//===----------------------------------------------------------------------===//
13
John McCall19510852010-08-20 18:27:03 +000014#include "clang/Parse/ParseDiagnostic.h" // FIXME: remove this back-dependency!
15#include "clang/Sema/DeclSpec.h"
Douglas Gregor555f57e2011-06-25 00:56:27 +000016#include "clang/Sema/LocInfoType.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/ParsedTemplate.h"
Douglas Gregorc34348a2011-02-24 17:54:50 +000018#include "clang/AST/ASTContext.h"
Douglas Gregor555f57e2011-06-25 00:56:27 +000019#include "clang/AST/Expr.h"
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000020#include "clang/AST/NestedNameSpecifier.h"
21#include "clang/AST/TypeLoc.h"
Douglas Gregor9b3064b2009-04-01 22:41:11 +000022#include "clang/Lex/Preprocessor.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023#include "clang/Basic/LangOptions.h"
Chris Lattner5af2f352009-01-20 19:11:22 +000024#include "llvm/ADT/STLExtras.h"
John McCall32d335e2009-08-03 18:47:27 +000025#include "llvm/Support/ErrorHandling.h"
Douglas Gregore4e5b052009-03-19 00:18:19 +000026#include <cstring>
Reid Spencer5f016e22007-07-11 17:01:13 +000027using namespace clang;
28
Chris Lattner254be6a2008-11-22 08:32:36 +000029
30static DiagnosticBuilder Diag(Diagnostic &D, SourceLocation Loc,
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000031 unsigned DiagID) {
32 return D.Report(Loc, DiagID);
Chris Lattner254be6a2008-11-22 08:32:36 +000033}
34
Douglas Gregor314b97f2009-11-10 19:49:08 +000035
36void UnqualifiedId::setTemplateId(TemplateIdAnnotation *TemplateId) {
37 assert(TemplateId && "NULL template-id annotation?");
38 Kind = IK_TemplateId;
39 this->TemplateId = TemplateId;
40 StartLocation = TemplateId->TemplateNameLoc;
41 EndLocation = TemplateId->RAngleLoc;
42}
43
Douglas Gregor0efc2c12010-01-13 17:31:36 +000044void UnqualifiedId::setConstructorTemplateId(TemplateIdAnnotation *TemplateId) {
45 assert(TemplateId && "NULL template-id annotation?");
46 Kind = IK_ConstructorTemplateId;
47 this->TemplateId = TemplateId;
48 StartLocation = TemplateId->TemplateNameLoc;
49 EndLocation = TemplateId->RAngleLoc;
50}
51
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000052void CXXScopeSpec::Extend(ASTContext &Context, SourceLocation TemplateKWLoc,
53 TypeLoc TL, SourceLocation ColonColonLoc) {
Douglas Gregor5f791bb2011-02-28 23:58:31 +000054 Builder.Extend(Context, TemplateKWLoc, TL, ColonColonLoc);
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000055 if (Range.getBegin().isInvalid())
56 Range.setBegin(TL.getBeginLoc());
57 Range.setEnd(ColonColonLoc);
Douglas Gregorc34348a2011-02-24 17:54:50 +000058
Douglas Gregor5f791bb2011-02-28 23:58:31 +000059 assert(Range == Builder.getSourceRange() &&
Douglas Gregorc34348a2011-02-24 17:54:50 +000060 "NestedNameSpecifierLoc range computation incorrect");
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000061}
62
63void CXXScopeSpec::Extend(ASTContext &Context, IdentifierInfo *Identifier,
64 SourceLocation IdentifierLoc,
65 SourceLocation ColonColonLoc) {
Douglas Gregor5f791bb2011-02-28 23:58:31 +000066 Builder.Extend(Context, Identifier, IdentifierLoc, ColonColonLoc);
67
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000068 if (Range.getBegin().isInvalid())
69 Range.setBegin(IdentifierLoc);
70 Range.setEnd(ColonColonLoc);
Douglas Gregorc34348a2011-02-24 17:54:50 +000071
Douglas Gregor5f791bb2011-02-28 23:58:31 +000072 assert(Range == Builder.getSourceRange() &&
Douglas Gregorc34348a2011-02-24 17:54:50 +000073 "NestedNameSpecifierLoc range computation incorrect");
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000074}
75
76void CXXScopeSpec::Extend(ASTContext &Context, NamespaceDecl *Namespace,
77 SourceLocation NamespaceLoc,
78 SourceLocation ColonColonLoc) {
Douglas Gregor5f791bb2011-02-28 23:58:31 +000079 Builder.Extend(Context, Namespace, NamespaceLoc, ColonColonLoc);
80
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000081 if (Range.getBegin().isInvalid())
82 Range.setBegin(NamespaceLoc);
83 Range.setEnd(ColonColonLoc);
Douglas Gregorc34348a2011-02-24 17:54:50 +000084
Douglas Gregor5f791bb2011-02-28 23:58:31 +000085 assert(Range == Builder.getSourceRange() &&
Douglas Gregorc34348a2011-02-24 17:54:50 +000086 "NestedNameSpecifierLoc range computation incorrect");
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000087}
88
Douglas Gregor14aba762011-02-24 02:36:08 +000089void CXXScopeSpec::Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
90 SourceLocation AliasLoc,
91 SourceLocation ColonColonLoc) {
Douglas Gregor5f791bb2011-02-28 23:58:31 +000092 Builder.Extend(Context, Alias, AliasLoc, ColonColonLoc);
93
Douglas Gregor14aba762011-02-24 02:36:08 +000094 if (Range.getBegin().isInvalid())
95 Range.setBegin(AliasLoc);
96 Range.setEnd(ColonColonLoc);
Douglas Gregorc34348a2011-02-24 17:54:50 +000097
Douglas Gregor5f791bb2011-02-28 23:58:31 +000098 assert(Range == Builder.getSourceRange() &&
Douglas Gregorc34348a2011-02-24 17:54:50 +000099 "NestedNameSpecifierLoc range computation incorrect");
Douglas Gregor14aba762011-02-24 02:36:08 +0000100}
101
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000102void CXXScopeSpec::MakeGlobal(ASTContext &Context,
103 SourceLocation ColonColonLoc) {
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000104 Builder.MakeGlobal(Context, ColonColonLoc);
105
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000106 Range = SourceRange(ColonColonLoc);
Douglas Gregorc34348a2011-02-24 17:54:50 +0000107
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000108 assert(Range == Builder.getSourceRange() &&
Douglas Gregorc34348a2011-02-24 17:54:50 +0000109 "NestedNameSpecifierLoc range computation incorrect");
110}
111
112void CXXScopeSpec::MakeTrivial(ASTContext &Context,
113 NestedNameSpecifier *Qualifier, SourceRange R) {
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000114 Builder.MakeTrivial(Context, Qualifier, R);
Douglas Gregorc34348a2011-02-24 17:54:50 +0000115 Range = R;
Douglas Gregorc34348a2011-02-24 17:54:50 +0000116}
117
118void CXXScopeSpec::Adopt(NestedNameSpecifierLoc Other) {
119 if (!Other) {
120 Range = SourceRange();
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000121 Builder.Clear();
Douglas Gregorc34348a2011-02-24 17:54:50 +0000122 return;
123 }
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000124
Douglas Gregorc34348a2011-02-24 17:54:50 +0000125 Range = Other.getSourceRange();
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000126 Builder.Adopt(Other);
Douglas Gregorc34348a2011-02-24 17:54:50 +0000127}
128
John McCall9dc71d22011-07-06 06:57:57 +0000129SourceLocation CXXScopeSpec::getLastQualifierNameLoc() const {
130 if (!Builder.getRepresentation())
131 return SourceLocation();
132 return Builder.getTemporary().getLocalBeginLoc();
133}
134
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000135NestedNameSpecifierLoc
136CXXScopeSpec::getWithLocInContext(ASTContext &Context) const {
Douglas Gregorb46ae392011-03-03 21:48:55 +0000137 if (!Builder.getRepresentation())
Douglas Gregorc34348a2011-02-24 17:54:50 +0000138 return NestedNameSpecifierLoc();
139
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000140 return Builder.getWithLocInContext(Context);
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000141}
142
Chris Lattner5af2f352009-01-20 19:11:22 +0000143/// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
144/// "TheDeclarator" is the declarator that this will be added to.
John McCall0b7e6782011-03-24 11:26:52 +0000145DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto, bool isVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +0000146 SourceLocation EllipsisLoc,
Chris Lattner5af2f352009-01-20 19:11:22 +0000147 ParamInfo *ArgInfo,
148 unsigned NumArgs,
149 unsigned TypeQuals,
Douglas Gregor83f51722011-01-26 03:43:54 +0000150 bool RefQualifierIsLvalueRef,
151 SourceLocation RefQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +0000152 SourceLocation MutableLoc,
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000153 ExceptionSpecificationType
154 ESpecType,
155 SourceLocation ESpecLoc,
John McCallb3d87482010-08-24 05:47:05 +0000156 ParsedType *Exceptions,
Sebastian Redlef65f062009-05-29 18:02:33 +0000157 SourceRange *ExceptionRanges,
Sebastian Redl7dc81342009-04-29 17:30:04 +0000158 unsigned NumExceptions,
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000159 Expr *NoexceptExpr,
Abramo Bagnara796aa442011-03-12 11:17:06 +0000160 SourceLocation LocalRangeBegin,
161 SourceLocation LocalRangeEnd,
Douglas Gregordab60ad2010-10-01 18:44:50 +0000162 Declarator &TheDeclarator,
163 ParsedType TrailingReturnType) {
Chris Lattner5af2f352009-01-20 19:11:22 +0000164 DeclaratorChunk I;
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000165 I.Kind = Function;
Abramo Bagnara796aa442011-03-12 11:17:06 +0000166 I.Loc = LocalRangeBegin;
167 I.EndLoc = LocalRangeEnd;
John McCall0b7e6782011-03-24 11:26:52 +0000168 I.Fun.AttrList = 0;
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000169 I.Fun.hasPrototype = hasProto;
170 I.Fun.isVariadic = isVariadic;
171 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
172 I.Fun.DeleteArgInfo = false;
173 I.Fun.TypeQuals = TypeQuals;
174 I.Fun.NumArgs = NumArgs;
175 I.Fun.ArgInfo = 0;
Douglas Gregor83f51722011-01-26 03:43:54 +0000176 I.Fun.RefQualifierIsLValueRef = RefQualifierIsLvalueRef;
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000177 I.Fun.RefQualifierLoc = RefQualifierLoc.getRawEncoding();
Douglas Gregor90ebed02011-07-13 21:47:47 +0000178 I.Fun.MutableLoc = MutableLoc.getRawEncoding();
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000179 I.Fun.ExceptionSpecType = ESpecType;
180 I.Fun.ExceptionSpecLoc = ESpecLoc.getRawEncoding();
181 I.Fun.NumExceptions = 0;
182 I.Fun.Exceptions = 0;
183 I.Fun.NoexceptExpr = 0;
Douglas Gregordab60ad2010-10-01 18:44:50 +0000184 I.Fun.TrailingReturnType = TrailingReturnType.getAsOpaquePtr();
Sebastian Redl7dc81342009-04-29 17:30:04 +0000185
Chris Lattner5af2f352009-01-20 19:11:22 +0000186 // new[] an argument array if needed.
187 if (NumArgs) {
188 // If the 'InlineParams' in Declarator is unused and big enough, put our
189 // parameter list there (in an effort to avoid new/delete traffic). If it
190 // is already used (consider a function returning a function pointer) or too
191 // small (function taking too many arguments), go to the heap.
Mike Stump1eb44332009-09-09 15:08:12 +0000192 if (!TheDeclarator.InlineParamsUsed &&
Chris Lattner5af2f352009-01-20 19:11:22 +0000193 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
194 I.Fun.ArgInfo = TheDeclarator.InlineParams;
195 I.Fun.DeleteArgInfo = false;
196 TheDeclarator.InlineParamsUsed = true;
197 } else {
198 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
199 I.Fun.DeleteArgInfo = true;
200 }
201 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
202 }
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000203
204 // Check what exception specification information we should actually store.
205 switch (ESpecType) {
206 default: break; // By default, save nothing.
207 case EST_Dynamic:
208 // new[] an exception array if needed
209 if (NumExceptions) {
210 I.Fun.NumExceptions = NumExceptions;
211 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
212 for (unsigned i = 0; i != NumExceptions; ++i) {
213 I.Fun.Exceptions[i].Ty = Exceptions[i];
214 I.Fun.Exceptions[i].Range = ExceptionRanges[i];
215 }
Sebastian Redlef65f062009-05-29 18:02:33 +0000216 }
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000217 break;
218
219 case EST_ComputedNoexcept:
220 I.Fun.NoexceptExpr = NoexceptExpr;
221 break;
Sebastian Redl7dc81342009-04-29 17:30:04 +0000222 }
Chris Lattner5af2f352009-01-20 19:11:22 +0000223 return I;
224}
Chris Lattner254be6a2008-11-22 08:32:36 +0000225
Douglas Gregor555f57e2011-06-25 00:56:27 +0000226bool Declarator::isDeclarationOfFunction() const {
Richard Smith1ab0d902011-06-25 02:28:38 +0000227 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
228 switch (DeclTypeInfo[i].Kind) {
229 case DeclaratorChunk::Function:
230 return true;
231 case DeclaratorChunk::Paren:
232 continue;
233 case DeclaratorChunk::Pointer:
234 case DeclaratorChunk::Reference:
235 case DeclaratorChunk::Array:
236 case DeclaratorChunk::BlockPointer:
237 case DeclaratorChunk::MemberPointer:
238 return false;
239 }
240 llvm_unreachable("Invalid type chunk");
241 return false;
242 }
Douglas Gregor555f57e2011-06-25 00:56:27 +0000243
244 switch (DS.getTypeSpecType()) {
245 case TST_auto:
246 case TST_bool:
247 case TST_char:
248 case TST_char16:
249 case TST_char32:
250 case TST_class:
251 case TST_decimal128:
252 case TST_decimal32:
253 case TST_decimal64:
254 case TST_double:
255 case TST_enum:
256 case TST_error:
257 case TST_float:
258 case TST_int:
259 case TST_struct:
260 case TST_union:
261 case TST_unknown_anytype:
262 case TST_unspecified:
263 case TST_void:
264 case TST_wchar:
265 return false;
266
267 case TST_decltype:
268 case TST_typeofExpr:
269 if (Expr *E = DS.getRepAsExpr())
270 return E->getType()->isFunctionType();
271 return false;
272
273 case TST_underlyingType:
274 case TST_typename:
275 case TST_typeofType: {
276 QualType QT = DS.getRepAsType().get();
277 if (QT.isNull())
278 return false;
279
280 if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT))
281 QT = LIT->getType();
282
283 if (QT.isNull())
284 return false;
285
286 return QT->isFunctionType();
287 }
288 }
289
290 return false;
291}
292
Reid Spencer5f016e22007-07-11 17:01:13 +0000293/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattner2a327d12009-02-27 18:35:46 +0000294/// declaration specifier includes.
Reid Spencer5f016e22007-07-11 17:01:13 +0000295///
296unsigned DeclSpec::getParsedSpecifiers() const {
297 unsigned Res = 0;
298 if (StorageClassSpec != SCS_unspecified ||
299 SCS_thread_specified)
300 Res |= PQ_StorageClassSpecifier;
Mike Stumpd4204332008-06-19 19:52:46 +0000301
Reid Spencer5f016e22007-07-11 17:01:13 +0000302 if (TypeQualifiers != TQ_unspecified)
303 Res |= PQ_TypeQualifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 if (hasTypeSpecifier())
306 Res |= PQ_TypeSpecifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Douglas Gregorb48fe382008-10-31 09:07:45 +0000308 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Reid Spencer5f016e22007-07-11 17:01:13 +0000309 Res |= PQ_FunctionSpecifier;
310 return Res;
311}
312
John McCallfec54012009-08-03 20:12:06 +0000313template <class T> static bool BadSpecifier(T TNew, T TPrev,
314 const char *&PrevSpec,
315 unsigned &DiagID) {
John McCall32d335e2009-08-03 18:47:27 +0000316 PrevSpec = DeclSpec::getSpecifierName(TPrev);
John McCallfec54012009-08-03 20:12:06 +0000317 DiagID = (TNew == TPrev ? diag::ext_duplicate_declspec
318 : diag::err_invalid_decl_spec_combination);
John McCall32d335e2009-08-03 18:47:27 +0000319 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000320}
John McCall32d335e2009-08-03 18:47:27 +0000321
Reid Spencer5f016e22007-07-11 17:01:13 +0000322const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
323 switch (S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 case DeclSpec::SCS_unspecified: return "unspecified";
325 case DeclSpec::SCS_typedef: return "typedef";
326 case DeclSpec::SCS_extern: return "extern";
327 case DeclSpec::SCS_static: return "static";
328 case DeclSpec::SCS_auto: return "auto";
329 case DeclSpec::SCS_register: return "register";
Eli Friedman63054b32009-04-19 20:27:55 +0000330 case DeclSpec::SCS_private_extern: return "__private_extern__";
Sebastian Redl669d5d72008-11-14 23:42:31 +0000331 case DeclSpec::SCS_mutable: return "mutable";
Reid Spencer5f016e22007-07-11 17:01:13 +0000332 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000333 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000334}
335
John McCall32d335e2009-08-03 18:47:27 +0000336const char *DeclSpec::getSpecifierName(TSW W) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 switch (W) {
John McCall32d335e2009-08-03 18:47:27 +0000338 case TSW_unspecified: return "unspecified";
339 case TSW_short: return "short";
340 case TSW_long: return "long";
341 case TSW_longlong: return "long long";
Reid Spencer5f016e22007-07-11 17:01:13 +0000342 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000343 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000344}
345
John McCall32d335e2009-08-03 18:47:27 +0000346const char *DeclSpec::getSpecifierName(TSC C) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000347 switch (C) {
John McCall32d335e2009-08-03 18:47:27 +0000348 case TSC_unspecified: return "unspecified";
349 case TSC_imaginary: return "imaginary";
350 case TSC_complex: return "complex";
Reid Spencer5f016e22007-07-11 17:01:13 +0000351 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000352 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000353}
354
355
John McCall32d335e2009-08-03 18:47:27 +0000356const char *DeclSpec::getSpecifierName(TSS S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000357 switch (S) {
John McCall32d335e2009-08-03 18:47:27 +0000358 case TSS_unspecified: return "unspecified";
359 case TSS_signed: return "signed";
360 case TSS_unsigned: return "unsigned";
Reid Spencer5f016e22007-07-11 17:01:13 +0000361 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000362 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000363}
364
365const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
366 switch (T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000367 case DeclSpec::TST_unspecified: return "unspecified";
368 case DeclSpec::TST_void: return "void";
369 case DeclSpec::TST_char: return "char";
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000370 case DeclSpec::TST_wchar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000371 case DeclSpec::TST_char16: return "char16_t";
372 case DeclSpec::TST_char32: return "char32_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000373 case DeclSpec::TST_int: return "int";
374 case DeclSpec::TST_float: return "float";
375 case DeclSpec::TST_double: return "double";
376 case DeclSpec::TST_bool: return "_Bool";
377 case DeclSpec::TST_decimal32: return "_Decimal32";
378 case DeclSpec::TST_decimal64: return "_Decimal64";
379 case DeclSpec::TST_decimal128: return "_Decimal128";
380 case DeclSpec::TST_enum: return "enum";
Chris Lattner99dc9142008-04-13 18:59:07 +0000381 case DeclSpec::TST_class: return "class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000382 case DeclSpec::TST_union: return "union";
383 case DeclSpec::TST_struct: return "struct";
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000384 case DeclSpec::TST_typename: return "type-name";
Steve Naroffd1861fd2007-07-31 12:34:36 +0000385 case DeclSpec::TST_typeofType:
386 case DeclSpec::TST_typeofExpr: return "typeof";
John McCall32d335e2009-08-03 18:47:27 +0000387 case DeclSpec::TST_auto: return "auto";
388 case DeclSpec::TST_decltype: return "(decltype)";
Sean Huntca63c202011-05-24 22:41:36 +0000389 case DeclSpec::TST_underlyingType: return "__underlying_type";
John McCalla5fc4722011-04-09 22:50:59 +0000390 case DeclSpec::TST_unknown_anytype: return "__unknown_anytype";
John McCall32d335e2009-08-03 18:47:27 +0000391 case DeclSpec::TST_error: return "(error)";
Reid Spencer5f016e22007-07-11 17:01:13 +0000392 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000393 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000394}
395
John McCall32d335e2009-08-03 18:47:27 +0000396const char *DeclSpec::getSpecifierName(TQ T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000397 switch (T) {
John McCall32d335e2009-08-03 18:47:27 +0000398 case DeclSpec::TQ_unspecified: return "unspecified";
399 case DeclSpec::TQ_const: return "const";
400 case DeclSpec::TQ_restrict: return "restrict";
401 case DeclSpec::TQ_volatile: return "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +0000402 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000403 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000404}
405
406bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000407 const char *&PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +0000408 unsigned &DiagID,
409 const LangOptions &Lang) {
410 // OpenCL prohibits extern, auto, register, and static
411 // It seems sensible to prohibit private_extern too
412 if (Lang.OpenCL) {
413 switch (S) {
414 case SCS_extern:
415 case SCS_private_extern:
416 case SCS_auto:
417 case SCS_register:
418 case SCS_static:
419 DiagID = diag::err_not_opencl_storage_class_specifier;
420 PrevSpec = getSpecifierName(S);
421 return true;
422 default:
423 break;
424 }
425 }
426
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000427 if (StorageClassSpec != SCS_unspecified) {
428 // Changing storage class is allowed only if the previous one
429 // was the 'extern' that is part of a linkage specification and
430 // the new storage class is 'typedef'.
431 if (!(SCS_extern_in_linkage_spec &&
432 StorageClassSpec == SCS_extern &&
433 S == SCS_typedef))
434 return BadSpecifier(S, (SCS)StorageClassSpec, PrevSpec, DiagID);
435 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000436 StorageClassSpec = S;
437 StorageClassSpecLoc = Loc;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000438 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Reid Spencer5f016e22007-07-11 17:01:13 +0000439 return false;
440}
441
Mike Stump1eb44332009-09-09 15:08:12 +0000442bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000443 const char *&PrevSpec,
444 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000445 if (SCS_thread_specified) {
446 PrevSpec = "__thread";
John McCallfec54012009-08-03 20:12:06 +0000447 DiagID = diag::ext_duplicate_declspec;
Reid Spencer5f016e22007-07-11 17:01:13 +0000448 return true;
449 }
450 SCS_thread_specified = true;
451 SCS_threadLoc = Loc;
452 return false;
453}
454
Reid Spencer5f016e22007-07-11 17:01:13 +0000455/// These methods set the specified attribute of the DeclSpec, but return true
456/// and ignore the request if invalid (e.g. "extern" then "auto" is
457/// specified).
458bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000459 const char *&PrevSpec,
460 unsigned &DiagID) {
Abramo Bagnara2553eaf2011-03-06 22:21:56 +0000461 // Overwrite TSWLoc only if TypeSpecWidth was unspecified, so that
462 // for 'long long' we will keep the source location of the first 'long'.
463 if (TypeSpecWidth == TSW_unspecified)
464 TSWLoc = Loc;
465 // Allow turning long -> long long.
466 else if (W != TSW_longlong || TypeSpecWidth != TSW_long)
John McCallfec54012009-08-03 20:12:06 +0000467 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000468 TypeSpecWidth = W;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000469 if (TypeAltiVecVector && !TypeAltiVecBool &&
470 ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) {
John Thompson82287d12010-02-05 00:12:22 +0000471 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
472 DiagID = diag::warn_vector_long_decl_spec_combination;
473 return true;
474 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000475 return false;
476}
477
Mike Stump1eb44332009-09-09 15:08:12 +0000478bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000479 const char *&PrevSpec,
480 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000481 if (TypeSpecComplex != TSC_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000482 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000483 TypeSpecComplex = C;
484 TSCLoc = Loc;
485 return false;
486}
487
Mike Stump1eb44332009-09-09 15:08:12 +0000488bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000489 const char *&PrevSpec,
490 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000491 if (TypeSpecSign != TSS_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000492 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000493 TypeSpecSign = S;
494 TSSLoc = Loc;
495 return false;
496}
497
498bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000499 const char *&PrevSpec,
500 unsigned &DiagID,
John McCallb3d87482010-08-24 05:47:05 +0000501 ParsedType Rep) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000502 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep);
503}
504
505bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
506 SourceLocation TagNameLoc,
507 const char *&PrevSpec,
508 unsigned &DiagID,
509 ParsedType Rep) {
John McCallb3d87482010-08-24 05:47:05 +0000510 assert(isTypeRep(T) && "T does not store a type");
511 assert(Rep && "no type provided!");
512 if (TypeSpecType != TST_unspecified) {
513 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
514 DiagID = diag::err_invalid_decl_spec_combination;
515 return true;
516 }
517 TypeSpecType = T;
518 TypeRep = Rep;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000519 TSTLoc = TagKwLoc;
520 TSTNameLoc = TagNameLoc;
John McCallb3d87482010-08-24 05:47:05 +0000521 TypeSpecOwned = false;
522 return false;
523}
524
525bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
526 const char *&PrevSpec,
527 unsigned &DiagID,
528 Expr *Rep) {
529 assert(isExprRep(T) && "T does not store an expr");
530 assert(Rep && "no expression provided!");
531 if (TypeSpecType != TST_unspecified) {
532 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
533 DiagID = diag::err_invalid_decl_spec_combination;
534 return true;
535 }
536 TypeSpecType = T;
537 ExprRep = Rep;
538 TSTLoc = Loc;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000539 TSTNameLoc = Loc;
John McCallb3d87482010-08-24 05:47:05 +0000540 TypeSpecOwned = false;
541 return false;
542}
543
544bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
545 const char *&PrevSpec,
546 unsigned &DiagID,
547 Decl *Rep, bool Owned) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000548 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Owned);
549}
550
551bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
552 SourceLocation TagNameLoc,
553 const char *&PrevSpec,
554 unsigned &DiagID,
555 Decl *Rep, bool Owned) {
John McCallb3d87482010-08-24 05:47:05 +0000556 assert(isDeclRep(T) && "T does not store a decl");
557 // Unlike the other cases, we don't assert that we actually get a decl.
558
559 if (TypeSpecType != TST_unspecified) {
560 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
561 DiagID = diag::err_invalid_decl_spec_combination;
562 return true;
563 }
564 TypeSpecType = T;
565 DeclRep = Rep;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000566 TSTLoc = TagKwLoc;
567 TSTNameLoc = TagNameLoc;
John McCallb3d87482010-08-24 05:47:05 +0000568 TypeSpecOwned = Owned;
569 return false;
570}
571
572bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
573 const char *&PrevSpec,
574 unsigned &DiagID) {
575 assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
576 "rep required for these type-spec kinds!");
John McCallfec54012009-08-03 20:12:06 +0000577 if (TypeSpecType != TST_unspecified) {
578 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
579 DiagID = diag::err_invalid_decl_spec_combination;
580 return true;
581 }
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000582 TSTLoc = Loc;
583 TSTNameLoc = Loc;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000584 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
585 TypeAltiVecBool = true;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000586 return false;
587 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000588 TypeSpecType = T;
John McCallb3d87482010-08-24 05:47:05 +0000589 TypeSpecOwned = false;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000590 if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) {
John Thompson82287d12010-02-05 00:12:22 +0000591 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
Chris Lattner788b0fd2010-06-23 06:00:24 +0000592 DiagID = diag::err_invalid_vector_decl_spec;
John Thompson82287d12010-02-05 00:12:22 +0000593 return true;
594 }
595 return false;
596}
597
598bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
599 const char *&PrevSpec, unsigned &DiagID) {
600 if (TypeSpecType != TST_unspecified) {
601 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
602 DiagID = diag::err_invalid_vector_decl_spec_combination;
603 return true;
604 }
605 TypeAltiVecVector = isAltiVecVector;
606 AltiVecLoc = Loc;
607 return false;
608}
609
610bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
611 const char *&PrevSpec, unsigned &DiagID) {
Chris Lattner788b0fd2010-06-23 06:00:24 +0000612 if (!TypeAltiVecVector || TypeAltiVecPixel ||
613 (TypeSpecType != TST_unspecified)) {
John Thompson82287d12010-02-05 00:12:22 +0000614 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
615 DiagID = diag::err_invalid_pixel_decl_spec_combination;
616 return true;
617 }
John Thompson82287d12010-02-05 00:12:22 +0000618 TypeAltiVecPixel = isAltiVecPixel;
619 TSTLoc = Loc;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000620 TSTNameLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000621 return false;
622}
623
Douglas Gregorddc29e12009-02-06 22:42:48 +0000624bool DeclSpec::SetTypeSpecError() {
625 TypeSpecType = TST_error;
John McCall9e46b8c2010-08-26 17:22:34 +0000626 TypeSpecOwned = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000627 TSTLoc = SourceLocation();
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000628 TSTNameLoc = SourceLocation();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000629 return false;
630}
631
Reid Spencer5f016e22007-07-11 17:01:13 +0000632bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000633 unsigned &DiagID, const LangOptions &Lang) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000634 // Duplicates turn into warnings pre-C99.
635 if ((TypeQualifiers & T) && !Lang.C99)
John McCallfec54012009-08-03 20:12:06 +0000636 return BadSpecifier(T, T, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000637 TypeQualifiers |= T;
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Reid Spencer5f016e22007-07-11 17:01:13 +0000639 switch (T) {
640 default: assert(0 && "Unknown type qualifier!");
641 case TQ_const: TQ_constLoc = Loc; break;
642 case TQ_restrict: TQ_restrictLoc = Loc; break;
643 case TQ_volatile: TQ_volatileLoc = Loc; break;
644 }
645 return false;
646}
647
John McCallfec54012009-08-03 20:12:06 +0000648bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
649 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000650 // 'inline inline' is ok.
651 FS_inline_specified = true;
652 FS_inlineLoc = Loc;
653 return false;
654}
655
John McCallfec54012009-08-03 20:12:06 +0000656bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
657 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000658 // 'virtual virtual' is ok.
659 FS_virtual_specified = true;
660 FS_virtualLoc = Loc;
661 return false;
662}
663
John McCallfec54012009-08-03 20:12:06 +0000664bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
665 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000666 // 'explicit explicit' is ok.
667 FS_explicit_specified = true;
668 FS_explicitLoc = Loc;
669 return false;
670}
671
John McCallfec54012009-08-03 20:12:06 +0000672bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
673 unsigned &DiagID) {
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000674 if (Friend_specified) {
675 PrevSpec = "friend";
John McCallfec54012009-08-03 20:12:06 +0000676 DiagID = diag::ext_duplicate_declspec;
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000677 return true;
678 }
John McCallfec54012009-08-03 20:12:06 +0000679
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000680 Friend_specified = true;
681 FriendLoc = Loc;
682 return false;
683}
Reid Spencer5f016e22007-07-11 17:01:13 +0000684
Sebastian Redl2ac67232009-11-05 15:47:02 +0000685bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
686 unsigned &DiagID) {
687 // 'constexpr constexpr' is ok.
688 Constexpr_specified = true;
689 ConstexprLoc = Loc;
690 return false;
691}
692
John McCalld226f652010-08-21 09:40:31 +0000693void DeclSpec::setProtocolQualifiers(Decl * const *Protos,
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000694 unsigned NP,
695 SourceLocation *ProtoLocs,
696 SourceLocation LAngleLoc) {
697 if (NP == 0) return;
John McCalld226f652010-08-21 09:40:31 +0000698 ProtocolQualifiers = new Decl*[NP];
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000699 ProtocolLocs = new SourceLocation[NP];
John McCalld226f652010-08-21 09:40:31 +0000700 memcpy((void*)ProtocolQualifiers, Protos, sizeof(Decl*)*NP);
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000701 memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
702 NumProtocolQualifiers = NP;
703 ProtocolLAngleLoc = LAngleLoc;
704}
705
Douglas Gregorddf889a2010-01-18 18:04:31 +0000706void DeclSpec::SaveWrittenBuiltinSpecs() {
707 writtenBS.Sign = getTypeSpecSign();
708 writtenBS.Width = getTypeSpecWidth();
709 writtenBS.Type = getTypeSpecType();
710 // Search the list of attributes for the presence of a mode attribute.
711 writtenBS.ModeAttr = false;
John McCall7f040a92010-12-24 02:08:15 +0000712 AttributeList* attrs = getAttributes().getList();
Douglas Gregorddf889a2010-01-18 18:04:31 +0000713 while (attrs) {
714 if (attrs->getKind() == AttributeList::AT_mode) {
715 writtenBS.ModeAttr = true;
716 break;
717 }
718 attrs = attrs->getNext();
719 }
720}
721
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000722void DeclSpec::SaveStorageSpecifierAsWritten() {
723 if (SCS_extern_in_linkage_spec && StorageClassSpec == SCS_extern)
724 // If 'extern' is part of a linkage specification,
725 // then it is not a storage class "as written".
726 StorageClassSpecAsWritten = SCS_unspecified;
727 else
728 StorageClassSpecAsWritten = StorageClassSpec;
729}
730
Reid Spencer5f016e22007-07-11 17:01:13 +0000731/// Finish - This does final analysis of the declspec, rejecting things like
732/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
733/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
734/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000735void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000736 // Before possibly changing their values, save specs as written.
737 SaveWrittenBuiltinSpecs();
Douglas Gregor16573fa2010-04-19 22:54:31 +0000738 SaveStorageSpecifierAsWritten();
Douglas Gregorddf889a2010-01-18 18:04:31 +0000739
Reid Spencer5f016e22007-07-11 17:01:13 +0000740 // Check the type specifier components first.
741
Chris Lattner788b0fd2010-06-23 06:00:24 +0000742 // Validate and finalize AltiVec vector declspec.
743 if (TypeAltiVecVector) {
744 if (TypeAltiVecBool) {
745 // Sign specifiers are not allowed with vector bool. (PIM 2.1)
746 if (TypeSpecSign != TSS_unspecified) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000747 Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000748 << getSpecifierName((TSS)TypeSpecSign);
749 }
750
751 // Only char/int are valid with vector bool. (PIM 2.1)
Duncan Sands2e964a922010-06-23 19:34:52 +0000752 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
753 (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000754 Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000755 << (TypeAltiVecPixel ? "__pixel" :
756 getSpecifierName((TST)TypeSpecType));
757 }
758
759 // Only 'short' is valid with vector bool. (PIM 2.1)
760 if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short))
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000761 Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000762 << getSpecifierName((TSW)TypeSpecWidth);
763
764 // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
765 if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
766 (TypeSpecWidth != TSW_unspecified))
767 TypeSpecSign = TSS_unsigned;
768 }
769
770 if (TypeAltiVecPixel) {
771 //TODO: perform validation
772 TypeSpecType = TST_int;
773 TypeSpecSign = TSS_unsigned;
774 TypeSpecWidth = TSW_short;
John McCall9e46b8c2010-08-26 17:22:34 +0000775 TypeSpecOwned = false;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000776 }
777 }
778
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000779 // signed/unsigned are only valid with int/char/wchar_t.
Reid Spencer5f016e22007-07-11 17:01:13 +0000780 if (TypeSpecSign != TSS_unspecified) {
781 if (TypeSpecType == TST_unspecified)
782 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000783 else if (TypeSpecType != TST_int &&
784 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000785 Diag(D, TSSLoc, diag::err_invalid_sign_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000786 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000787 // signed double -> double.
788 TypeSpecSign = TSS_unspecified;
789 }
790 }
791
792 // Validate the width of the type.
793 switch (TypeSpecWidth) {
794 case TSW_unspecified: break;
795 case TSW_short: // short int
796 case TSW_longlong: // long long int
797 if (TypeSpecType == TST_unspecified)
798 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
799 else if (TypeSpecType != TST_int) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000800 Diag(D, TSWLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000801 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner254be6a2008-11-22 08:32:36 +0000802 : diag::err_invalid_longlong_spec)
803 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000804 TypeSpecType = TST_int;
John McCall9e46b8c2010-08-26 17:22:34 +0000805 TypeSpecOwned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000806 }
807 break;
808 case TSW_long: // long double, long int
809 if (TypeSpecType == TST_unspecified)
810 TypeSpecType = TST_int; // long -> long int.
811 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000812 Diag(D, TSWLoc, diag::err_invalid_long_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000813 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000814 TypeSpecType = TST_int;
John McCall9e46b8c2010-08-26 17:22:34 +0000815 TypeSpecOwned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000816 }
817 break;
818 }
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Reid Spencer5f016e22007-07-11 17:01:13 +0000820 // TODO: if the implementation does not implement _Complex or _Imaginary,
821 // disallow their use. Need information about the backend.
822 if (TypeSpecComplex != TSC_unspecified) {
823 if (TypeSpecType == TST_unspecified) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000824 Diag(D, TSCLoc, diag::ext_plain_complex)
Douglas Gregor849b2432010-03-31 17:46:05 +0000825 << FixItHint::CreateInsertion(
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000826 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
827 " double");
Reid Spencer5f016e22007-07-11 17:01:13 +0000828 TypeSpecType = TST_double; // _Complex -> _Complex double.
829 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
830 // Note that this intentionally doesn't include _Complex _Bool.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000831 Diag(D, TSTLoc, diag::ext_integer_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000832 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000833 Diag(D, TSCLoc, diag::err_invalid_complex_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000834 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000835 TypeSpecComplex = TSC_unspecified;
836 }
837 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000838
John McCall67d1a672009-08-06 02:15:43 +0000839 // C++ [class.friend]p6:
840 // No storage-class-specifier shall appear in the decl-specifier-seq
841 // of a friend declaration.
842 if (isFriendSpecified() && getStorageClassSpec()) {
843 DeclSpec::SCS SC = getStorageClassSpec();
844 const char *SpecName = getSpecifierName(SC);
845
846 SourceLocation SCLoc = getStorageClassSpecLoc();
847 SourceLocation SCEndLoc = SCLoc.getFileLocWithOffset(strlen(SpecName));
848
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000849 Diag(D, SCLoc, diag::err_friend_storage_spec)
John McCall67d1a672009-08-06 02:15:43 +0000850 << SpecName
Douglas Gregor849b2432010-03-31 17:46:05 +0000851 << FixItHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc));
John McCall67d1a672009-08-06 02:15:43 +0000852
853 ClearStorageClassSpecs();
854 }
855
John McCall9e46b8c2010-08-26 17:22:34 +0000856 assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
857
Reid Spencer5f016e22007-07-11 17:01:13 +0000858 // Okay, now we can infer the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Reid Spencer5f016e22007-07-11 17:01:13 +0000860 // TODO: return "auto function" and other bad things based on the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000861
Reid Spencer5f016e22007-07-11 17:01:13 +0000862 // 'data definition has no type or storage class'?
863}
Daniel Dunbare4858a62008-08-11 03:45:03 +0000864
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000865bool DeclSpec::isMissingDeclaratorOk() {
866 TST tst = getTypeSpecType();
John McCallb3d87482010-08-24 05:47:05 +0000867 return isDeclRep(tst) && getRepAsDecl() != 0 &&
868 StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000869}
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000870
871void UnqualifiedId::clear() {
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000872 Kind = IK_Identifier;
873 Identifier = 0;
874 StartLocation = SourceLocation();
875 EndLocation = SourceLocation();
876}
877
878void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
879 OverloadedOperatorKind Op,
880 SourceLocation SymbolLocations[3]) {
881 Kind = IK_OperatorFunctionId;
882 StartLocation = OperatorLoc;
883 EndLocation = OperatorLoc;
884 OperatorFunctionId.Operator = Op;
885 for (unsigned I = 0; I != 3; ++I) {
886 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
887
888 if (SymbolLocations[I].isValid())
889 EndLocation = SymbolLocations[I];
890 }
891}
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000892
Anders Carlssoncc54d592011-01-22 16:56:46 +0000893bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
Anders Carlsson46127a92011-01-22 15:58:16 +0000894 const char *&PrevSpec) {
Douglas Gregorf5251602011-03-08 17:10:18 +0000895 LastLocation = Loc;
896
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000897 if (Specifiers & VS) {
898 PrevSpec = getSpecifierName(VS);
899 return true;
900 }
901
902 Specifiers |= VS;
903
904 switch (VS) {
905 default: assert(0 && "Unknown specifier!");
906 case VS_Override: VS_overrideLoc = Loc; break;
907 case VS_Final: VS_finalLoc = Loc; break;
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000908 }
Anders Carlsson46127a92011-01-22 15:58:16 +0000909
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000910 return false;
911}
912
Anders Carlssoncc54d592011-01-22 16:56:46 +0000913const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
Anders Carlssonc46bb7d2011-01-22 15:11:37 +0000914 switch (VS) {
915 default: assert(0 && "Unknown specifier");
916 case VS_Override: return "override";
917 case VS_Final: return "final";
Anders Carlssonc46bb7d2011-01-22 15:11:37 +0000918 }
919}