1 //===--- DeclSpec.cpp - Declaration Specifier Semantic Analysis -----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements semantic analysis for declaration specifiers.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/Sema/DeclSpec.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/DeclCXX.h"
17 #include "clang/AST/Expr.h"
18 #include "clang/AST/LocInfoType.h"
19 #include "clang/AST/TypeLoc.h"
20 #include "clang/Basic/LangOptions.h"
21 #include "clang/Basic/TargetInfo.h"
22 #include "clang/Sema/ParsedTemplate.h"
23 #include "clang/Sema/Sema.h"
24 #include "clang/Sema/SemaDiagnostic.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/ADT/SmallString.h"
27 #include <cstring>
28 using namespace clang;
29
30
setTemplateId(TemplateIdAnnotation * TemplateId)31 void UnqualifiedId::setTemplateId(TemplateIdAnnotation *TemplateId) {
32 assert(TemplateId && "NULL template-id annotation?");
33 Kind = IK_TemplateId;
34 this->TemplateId = TemplateId;
35 StartLocation = TemplateId->TemplateNameLoc;
36 EndLocation = TemplateId->RAngleLoc;
37 }
38
setConstructorTemplateId(TemplateIdAnnotation * TemplateId)39 void UnqualifiedId::setConstructorTemplateId(TemplateIdAnnotation *TemplateId) {
40 assert(TemplateId && "NULL template-id annotation?");
41 Kind = IK_ConstructorTemplateId;
42 this->TemplateId = TemplateId;
43 StartLocation = TemplateId->TemplateNameLoc;
44 EndLocation = TemplateId->RAngleLoc;
45 }
46
Extend(ASTContext & Context,SourceLocation TemplateKWLoc,TypeLoc TL,SourceLocation ColonColonLoc)47 void CXXScopeSpec::Extend(ASTContext &Context, SourceLocation TemplateKWLoc,
48 TypeLoc TL, SourceLocation ColonColonLoc) {
49 Builder.Extend(Context, TemplateKWLoc, TL, ColonColonLoc);
50 if (Range.getBegin().isInvalid())
51 Range.setBegin(TL.getBeginLoc());
52 Range.setEnd(ColonColonLoc);
53
54 assert(Range == Builder.getSourceRange() &&
55 "NestedNameSpecifierLoc range computation incorrect");
56 }
57
Extend(ASTContext & Context,IdentifierInfo * Identifier,SourceLocation IdentifierLoc,SourceLocation ColonColonLoc)58 void CXXScopeSpec::Extend(ASTContext &Context, IdentifierInfo *Identifier,
59 SourceLocation IdentifierLoc,
60 SourceLocation ColonColonLoc) {
61 Builder.Extend(Context, Identifier, IdentifierLoc, ColonColonLoc);
62
63 if (Range.getBegin().isInvalid())
64 Range.setBegin(IdentifierLoc);
65 Range.setEnd(ColonColonLoc);
66
67 assert(Range == Builder.getSourceRange() &&
68 "NestedNameSpecifierLoc range computation incorrect");
69 }
70
Extend(ASTContext & Context,NamespaceDecl * Namespace,SourceLocation NamespaceLoc,SourceLocation ColonColonLoc)71 void CXXScopeSpec::Extend(ASTContext &Context, NamespaceDecl *Namespace,
72 SourceLocation NamespaceLoc,
73 SourceLocation ColonColonLoc) {
74 Builder.Extend(Context, Namespace, NamespaceLoc, ColonColonLoc);
75
76 if (Range.getBegin().isInvalid())
77 Range.setBegin(NamespaceLoc);
78 Range.setEnd(ColonColonLoc);
79
80 assert(Range == Builder.getSourceRange() &&
81 "NestedNameSpecifierLoc range computation incorrect");
82 }
83
Extend(ASTContext & Context,NamespaceAliasDecl * Alias,SourceLocation AliasLoc,SourceLocation ColonColonLoc)84 void CXXScopeSpec::Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
85 SourceLocation AliasLoc,
86 SourceLocation ColonColonLoc) {
87 Builder.Extend(Context, Alias, AliasLoc, ColonColonLoc);
88
89 if (Range.getBegin().isInvalid())
90 Range.setBegin(AliasLoc);
91 Range.setEnd(ColonColonLoc);
92
93 assert(Range == Builder.getSourceRange() &&
94 "NestedNameSpecifierLoc range computation incorrect");
95 }
96
MakeGlobal(ASTContext & Context,SourceLocation ColonColonLoc)97 void CXXScopeSpec::MakeGlobal(ASTContext &Context,
98 SourceLocation ColonColonLoc) {
99 Builder.MakeGlobal(Context, ColonColonLoc);
100
101 Range = SourceRange(ColonColonLoc);
102
103 assert(Range == Builder.getSourceRange() &&
104 "NestedNameSpecifierLoc range computation incorrect");
105 }
106
MakeSuper(ASTContext & Context,CXXRecordDecl * RD,SourceLocation SuperLoc,SourceLocation ColonColonLoc)107 void CXXScopeSpec::MakeSuper(ASTContext &Context, CXXRecordDecl *RD,
108 SourceLocation SuperLoc,
109 SourceLocation ColonColonLoc) {
110 Builder.MakeSuper(Context, RD, SuperLoc, ColonColonLoc);
111
112 Range.setBegin(SuperLoc);
113 Range.setEnd(ColonColonLoc);
114
115 assert(Range == Builder.getSourceRange() &&
116 "NestedNameSpecifierLoc range computation incorrect");
117 }
118
MakeTrivial(ASTContext & Context,NestedNameSpecifier * Qualifier,SourceRange R)119 void CXXScopeSpec::MakeTrivial(ASTContext &Context,
120 NestedNameSpecifier *Qualifier, SourceRange R) {
121 Builder.MakeTrivial(Context, Qualifier, R);
122 Range = R;
123 }
124
Adopt(NestedNameSpecifierLoc Other)125 void CXXScopeSpec::Adopt(NestedNameSpecifierLoc Other) {
126 if (!Other) {
127 Range = SourceRange();
128 Builder.Clear();
129 return;
130 }
131
132 Range = Other.getSourceRange();
133 Builder.Adopt(Other);
134 }
135
getLastQualifierNameLoc() const136 SourceLocation CXXScopeSpec::getLastQualifierNameLoc() const {
137 if (!Builder.getRepresentation())
138 return SourceLocation();
139 return Builder.getTemporary().getLocalBeginLoc();
140 }
141
142 NestedNameSpecifierLoc
getWithLocInContext(ASTContext & Context) const143 CXXScopeSpec::getWithLocInContext(ASTContext &Context) const {
144 if (!Builder.getRepresentation())
145 return NestedNameSpecifierLoc();
146
147 return Builder.getWithLocInContext(Context);
148 }
149
150 /// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
151 /// "TheDeclarator" is the declarator that this will be added to.
getFunction(bool hasProto,bool isAmbiguous,SourceLocation LParenLoc,ParamInfo * Params,unsigned NumParams,SourceLocation EllipsisLoc,SourceLocation RParenLoc,unsigned TypeQuals,bool RefQualifierIsLvalueRef,SourceLocation RefQualifierLoc,SourceLocation ConstQualifierLoc,SourceLocation VolatileQualifierLoc,SourceLocation RestrictQualifierLoc,SourceLocation MutableLoc,ExceptionSpecificationType ESpecType,SourceRange ESpecRange,ParsedType * Exceptions,SourceRange * ExceptionRanges,unsigned NumExceptions,Expr * NoexceptExpr,CachedTokens * ExceptionSpecTokens,SourceLocation LocalRangeBegin,SourceLocation LocalRangeEnd,Declarator & TheDeclarator,TypeResult TrailingReturnType)152 DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto,
153 bool isAmbiguous,
154 SourceLocation LParenLoc,
155 ParamInfo *Params,
156 unsigned NumParams,
157 SourceLocation EllipsisLoc,
158 SourceLocation RParenLoc,
159 unsigned TypeQuals,
160 bool RefQualifierIsLvalueRef,
161 SourceLocation RefQualifierLoc,
162 SourceLocation ConstQualifierLoc,
163 SourceLocation
164 VolatileQualifierLoc,
165 SourceLocation
166 RestrictQualifierLoc,
167 SourceLocation MutableLoc,
168 ExceptionSpecificationType
169 ESpecType,
170 SourceRange ESpecRange,
171 ParsedType *Exceptions,
172 SourceRange *ExceptionRanges,
173 unsigned NumExceptions,
174 Expr *NoexceptExpr,
175 CachedTokens *ExceptionSpecTokens,
176 SourceLocation LocalRangeBegin,
177 SourceLocation LocalRangeEnd,
178 Declarator &TheDeclarator,
179 TypeResult TrailingReturnType) {
180 assert(!(TypeQuals & DeclSpec::TQ_atomic) &&
181 "function cannot have _Atomic qualifier");
182
183 DeclaratorChunk I;
184 I.Kind = Function;
185 I.Loc = LocalRangeBegin;
186 I.EndLoc = LocalRangeEnd;
187 I.Fun.AttrList = nullptr;
188 I.Fun.hasPrototype = hasProto;
189 I.Fun.isVariadic = EllipsisLoc.isValid();
190 I.Fun.isAmbiguous = isAmbiguous;
191 I.Fun.LParenLoc = LParenLoc.getRawEncoding();
192 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
193 I.Fun.RParenLoc = RParenLoc.getRawEncoding();
194 I.Fun.DeleteParams = false;
195 I.Fun.TypeQuals = TypeQuals;
196 I.Fun.NumParams = NumParams;
197 I.Fun.Params = nullptr;
198 I.Fun.RefQualifierIsLValueRef = RefQualifierIsLvalueRef;
199 I.Fun.RefQualifierLoc = RefQualifierLoc.getRawEncoding();
200 I.Fun.ConstQualifierLoc = ConstQualifierLoc.getRawEncoding();
201 I.Fun.VolatileQualifierLoc = VolatileQualifierLoc.getRawEncoding();
202 I.Fun.RestrictQualifierLoc = RestrictQualifierLoc.getRawEncoding();
203 I.Fun.MutableLoc = MutableLoc.getRawEncoding();
204 I.Fun.ExceptionSpecType = ESpecType;
205 I.Fun.ExceptionSpecLocBeg = ESpecRange.getBegin().getRawEncoding();
206 I.Fun.ExceptionSpecLocEnd = ESpecRange.getEnd().getRawEncoding();
207 I.Fun.NumExceptions = 0;
208 I.Fun.Exceptions = nullptr;
209 I.Fun.NoexceptExpr = nullptr;
210 I.Fun.HasTrailingReturnType = TrailingReturnType.isUsable() ||
211 TrailingReturnType.isInvalid();
212 I.Fun.TrailingReturnType = TrailingReturnType.get();
213
214 assert(I.Fun.TypeQuals == TypeQuals && "bitfield overflow");
215 assert(I.Fun.ExceptionSpecType == ESpecType && "bitfield overflow");
216
217 // new[] a parameter array if needed.
218 if (NumParams) {
219 // If the 'InlineParams' in Declarator is unused and big enough, put our
220 // parameter list there (in an effort to avoid new/delete traffic). If it
221 // is already used (consider a function returning a function pointer) or too
222 // small (function with too many parameters), go to the heap.
223 if (!TheDeclarator.InlineParamsUsed &&
224 NumParams <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
225 I.Fun.Params = TheDeclarator.InlineParams;
226 I.Fun.DeleteParams = false;
227 TheDeclarator.InlineParamsUsed = true;
228 } else {
229 I.Fun.Params = new DeclaratorChunk::ParamInfo[NumParams];
230 I.Fun.DeleteParams = true;
231 }
232 memcpy(I.Fun.Params, Params, sizeof(Params[0]) * NumParams);
233 }
234
235 // Check what exception specification information we should actually store.
236 switch (ESpecType) {
237 default: break; // By default, save nothing.
238 case EST_Dynamic:
239 // new[] an exception array if needed
240 if (NumExceptions) {
241 I.Fun.NumExceptions = NumExceptions;
242 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
243 for (unsigned i = 0; i != NumExceptions; ++i) {
244 I.Fun.Exceptions[i].Ty = Exceptions[i];
245 I.Fun.Exceptions[i].Range = ExceptionRanges[i];
246 }
247 }
248 break;
249
250 case EST_ComputedNoexcept:
251 I.Fun.NoexceptExpr = NoexceptExpr;
252 break;
253
254 case EST_Unparsed:
255 I.Fun.ExceptionSpecTokens = ExceptionSpecTokens;
256 break;
257 }
258 return I;
259 }
260
isDeclarationOfFunction() const261 bool Declarator::isDeclarationOfFunction() const {
262 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
263 switch (DeclTypeInfo[i].Kind) {
264 case DeclaratorChunk::Function:
265 return true;
266 case DeclaratorChunk::Paren:
267 continue;
268 case DeclaratorChunk::Pointer:
269 case DeclaratorChunk::Reference:
270 case DeclaratorChunk::Array:
271 case DeclaratorChunk::BlockPointer:
272 case DeclaratorChunk::MemberPointer:
273 case DeclaratorChunk::Pipe:
274 return false;
275 }
276 llvm_unreachable("Invalid type chunk");
277 }
278
279 switch (DS.getTypeSpecType()) {
280 case TST_atomic:
281 case TST_auto:
282 case TST_auto_type:
283 case TST_bool:
284 case TST_char:
285 case TST_char16:
286 case TST_char32:
287 case TST_class:
288 case TST_decimal128:
289 case TST_decimal32:
290 case TST_decimal64:
291 case TST_double:
292 case TST_float128:
293 case TST_enum:
294 case TST_error:
295 case TST_float:
296 case TST_half:
297 case TST_int:
298 case TST_int128:
299 case TST_struct:
300 case TST_interface:
301 case TST_union:
302 case TST_unknown_anytype:
303 case TST_unspecified:
304 case TST_void:
305 case TST_wchar:
306 #define GENERIC_IMAGE_TYPE(ImgType, Id) case TST_##ImgType##_t:
307 #include "clang/Basic/OpenCLImageTypes.def"
308 return false;
309
310 case TST_decltype_auto:
311 // This must have an initializer, so can't be a function declaration,
312 // even if the initializer has function type.
313 return false;
314
315 case TST_decltype:
316 case TST_typeofExpr:
317 if (Expr *E = DS.getRepAsExpr())
318 return E->getType()->isFunctionType();
319 return false;
320
321 case TST_underlyingType:
322 case TST_typename:
323 case TST_typeofType: {
324 QualType QT = DS.getRepAsType().get();
325 if (QT.isNull())
326 return false;
327
328 if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT))
329 QT = LIT->getType();
330
331 if (QT.isNull())
332 return false;
333
334 return QT->isFunctionType();
335 }
336 }
337
338 llvm_unreachable("Invalid TypeSpecType!");
339 }
340
isStaticMember()341 bool Declarator::isStaticMember() {
342 assert(getContext() == MemberContext);
343 return getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static ||
344 (getName().Kind == UnqualifiedId::IK_OperatorFunctionId &&
345 CXXMethodDecl::isStaticOverloadedOperator(
346 getName().OperatorFunctionId.Operator));
347 }
348
isCtorOrDtor()349 bool Declarator::isCtorOrDtor() {
350 return (getName().getKind() == UnqualifiedId::IK_ConstructorName) ||
351 (getName().getKind() == UnqualifiedId::IK_DestructorName);
352 }
353
hasTagDefinition() const354 bool DeclSpec::hasTagDefinition() const {
355 if (!TypeSpecOwned)
356 return false;
357 return cast<TagDecl>(getRepAsDecl())->isCompleteDefinition();
358 }
359
360 /// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
361 /// declaration specifier includes.
362 ///
getParsedSpecifiers() const363 unsigned DeclSpec::getParsedSpecifiers() const {
364 unsigned Res = 0;
365 if (StorageClassSpec != SCS_unspecified ||
366 ThreadStorageClassSpec != TSCS_unspecified)
367 Res |= PQ_StorageClassSpecifier;
368
369 if (TypeQualifiers != TQ_unspecified)
370 Res |= PQ_TypeQualifier;
371
372 if (hasTypeSpecifier())
373 Res |= PQ_TypeSpecifier;
374
375 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified ||
376 FS_noreturn_specified || FS_forceinline_specified)
377 Res |= PQ_FunctionSpecifier;
378 return Res;
379 }
380
BadSpecifier(T TNew,T TPrev,const char * & PrevSpec,unsigned & DiagID,bool IsExtension=true)381 template <class T> static bool BadSpecifier(T TNew, T TPrev,
382 const char *&PrevSpec,
383 unsigned &DiagID,
384 bool IsExtension = true) {
385 PrevSpec = DeclSpec::getSpecifierName(TPrev);
386 if (TNew != TPrev)
387 DiagID = diag::err_invalid_decl_spec_combination;
388 else
389 DiagID = IsExtension ? diag::ext_duplicate_declspec :
390 diag::warn_duplicate_declspec;
391 return true;
392 }
393
getSpecifierName(DeclSpec::SCS S)394 const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
395 switch (S) {
396 case DeclSpec::SCS_unspecified: return "unspecified";
397 case DeclSpec::SCS_typedef: return "typedef";
398 case DeclSpec::SCS_extern: return "extern";
399 case DeclSpec::SCS_static: return "static";
400 case DeclSpec::SCS_auto: return "auto";
401 case DeclSpec::SCS_register: return "register";
402 case DeclSpec::SCS_private_extern: return "__private_extern__";
403 case DeclSpec::SCS_mutable: return "mutable";
404 }
405 llvm_unreachable("Unknown typespec!");
406 }
407
getSpecifierName(DeclSpec::TSCS S)408 const char *DeclSpec::getSpecifierName(DeclSpec::TSCS S) {
409 switch (S) {
410 case DeclSpec::TSCS_unspecified: return "unspecified";
411 case DeclSpec::TSCS___thread: return "__thread";
412 case DeclSpec::TSCS_thread_local: return "thread_local";
413 case DeclSpec::TSCS__Thread_local: return "_Thread_local";
414 }
415 llvm_unreachable("Unknown typespec!");
416 }
417
getSpecifierName(TSW W)418 const char *DeclSpec::getSpecifierName(TSW W) {
419 switch (W) {
420 case TSW_unspecified: return "unspecified";
421 case TSW_short: return "short";
422 case TSW_long: return "long";
423 case TSW_longlong: return "long long";
424 }
425 llvm_unreachable("Unknown typespec!");
426 }
427
getSpecifierName(TSC C)428 const char *DeclSpec::getSpecifierName(TSC C) {
429 switch (C) {
430 case TSC_unspecified: return "unspecified";
431 case TSC_imaginary: return "imaginary";
432 case TSC_complex: return "complex";
433 }
434 llvm_unreachable("Unknown typespec!");
435 }
436
437
getSpecifierName(TSS S)438 const char *DeclSpec::getSpecifierName(TSS S) {
439 switch (S) {
440 case TSS_unspecified: return "unspecified";
441 case TSS_signed: return "signed";
442 case TSS_unsigned: return "unsigned";
443 }
444 llvm_unreachable("Unknown typespec!");
445 }
446
getSpecifierName(DeclSpec::TST T,const PrintingPolicy & Policy)447 const char *DeclSpec::getSpecifierName(DeclSpec::TST T,
448 const PrintingPolicy &Policy) {
449 switch (T) {
450 case DeclSpec::TST_unspecified: return "unspecified";
451 case DeclSpec::TST_void: return "void";
452 case DeclSpec::TST_char: return "char";
453 case DeclSpec::TST_wchar: return Policy.MSWChar ? "__wchar_t" : "wchar_t";
454 case DeclSpec::TST_char16: return "char16_t";
455 case DeclSpec::TST_char32: return "char32_t";
456 case DeclSpec::TST_int: return "int";
457 case DeclSpec::TST_int128: return "__int128";
458 case DeclSpec::TST_half: return "half";
459 case DeclSpec::TST_float: return "float";
460 case DeclSpec::TST_double: return "double";
461 case DeclSpec::TST_float128: return "__float128";
462 case DeclSpec::TST_bool: return Policy.Bool ? "bool" : "_Bool";
463 case DeclSpec::TST_decimal32: return "_Decimal32";
464 case DeclSpec::TST_decimal64: return "_Decimal64";
465 case DeclSpec::TST_decimal128: return "_Decimal128";
466 case DeclSpec::TST_enum: return "enum";
467 case DeclSpec::TST_class: return "class";
468 case DeclSpec::TST_union: return "union";
469 case DeclSpec::TST_struct: return "struct";
470 case DeclSpec::TST_interface: return "__interface";
471 case DeclSpec::TST_typename: return "type-name";
472 case DeclSpec::TST_typeofType:
473 case DeclSpec::TST_typeofExpr: return "typeof";
474 case DeclSpec::TST_auto: return "auto";
475 case DeclSpec::TST_auto_type: return "__auto_type";
476 case DeclSpec::TST_decltype: return "(decltype)";
477 case DeclSpec::TST_decltype_auto: return "decltype(auto)";
478 case DeclSpec::TST_underlyingType: return "__underlying_type";
479 case DeclSpec::TST_unknown_anytype: return "__unknown_anytype";
480 case DeclSpec::TST_atomic: return "_Atomic";
481 #define GENERIC_IMAGE_TYPE(ImgType, Id) \
482 case DeclSpec::TST_##ImgType##_t: \
483 return #ImgType "_t";
484 #include "clang/Basic/OpenCLImageTypes.def"
485 case DeclSpec::TST_error: return "(error)";
486 }
487 llvm_unreachable("Unknown typespec!");
488 }
489
getSpecifierName(TQ T)490 const char *DeclSpec::getSpecifierName(TQ T) {
491 switch (T) {
492 case DeclSpec::TQ_unspecified: return "unspecified";
493 case DeclSpec::TQ_const: return "const";
494 case DeclSpec::TQ_restrict: return "restrict";
495 case DeclSpec::TQ_volatile: return "volatile";
496 case DeclSpec::TQ_atomic: return "_Atomic";
497 case DeclSpec::TQ_unaligned: return "__unaligned";
498 }
499 llvm_unreachable("Unknown typespec!");
500 }
501
SetStorageClassSpec(Sema & S,SCS SC,SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID,const PrintingPolicy & Policy)502 bool DeclSpec::SetStorageClassSpec(Sema &S, SCS SC, SourceLocation Loc,
503 const char *&PrevSpec,
504 unsigned &DiagID,
505 const PrintingPolicy &Policy) {
506 // OpenCL v1.1 s6.8g: "The extern, static, auto and register storage-class
507 // specifiers are not supported.
508 // It seems sensible to prohibit private_extern too
509 // The cl_clang_storage_class_specifiers extension enables support for
510 // these storage-class specifiers.
511 // OpenCL v1.2 s6.8 changes this to "The auto and register storage-class
512 // specifiers are not supported."
513 if (S.getLangOpts().OpenCL &&
514 !S.getOpenCLOptions().cl_clang_storage_class_specifiers) {
515 switch (SC) {
516 case SCS_extern:
517 case SCS_private_extern:
518 case SCS_static:
519 if (S.getLangOpts().OpenCLVersion < 120) {
520 DiagID = diag::err_opencl_unknown_type_specifier;
521 PrevSpec = getSpecifierName(SC);
522 return true;
523 }
524 break;
525 case SCS_auto:
526 case SCS_register:
527 DiagID = diag::err_opencl_unknown_type_specifier;
528 PrevSpec = getSpecifierName(SC);
529 return true;
530 default:
531 break;
532 }
533 }
534
535 if (StorageClassSpec != SCS_unspecified) {
536 // Maybe this is an attempt to use C++11 'auto' outside of C++11 mode.
537 bool isInvalid = true;
538 if (TypeSpecType == TST_unspecified && S.getLangOpts().CPlusPlus) {
539 if (SC == SCS_auto)
540 return SetTypeSpecType(TST_auto, Loc, PrevSpec, DiagID, Policy);
541 if (StorageClassSpec == SCS_auto) {
542 isInvalid = SetTypeSpecType(TST_auto, StorageClassSpecLoc,
543 PrevSpec, DiagID, Policy);
544 assert(!isInvalid && "auto SCS -> TST recovery failed");
545 }
546 }
547
548 // Changing storage class is allowed only if the previous one
549 // was the 'extern' that is part of a linkage specification and
550 // the new storage class is 'typedef'.
551 if (isInvalid &&
552 !(SCS_extern_in_linkage_spec &&
553 StorageClassSpec == SCS_extern &&
554 SC == SCS_typedef))
555 return BadSpecifier(SC, (SCS)StorageClassSpec, PrevSpec, DiagID);
556 }
557 StorageClassSpec = SC;
558 StorageClassSpecLoc = Loc;
559 assert((unsigned)SC == StorageClassSpec && "SCS constants overflow bitfield");
560 return false;
561 }
562
SetStorageClassSpecThread(TSCS TSC,SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID)563 bool DeclSpec::SetStorageClassSpecThread(TSCS TSC, SourceLocation Loc,
564 const char *&PrevSpec,
565 unsigned &DiagID) {
566 if (ThreadStorageClassSpec != TSCS_unspecified)
567 return BadSpecifier(TSC, (TSCS)ThreadStorageClassSpec, PrevSpec, DiagID);
568
569 ThreadStorageClassSpec = TSC;
570 ThreadStorageClassSpecLoc = Loc;
571 return false;
572 }
573
574 /// These methods set the specified attribute of the DeclSpec, but return true
575 /// and ignore the request if invalid (e.g. "extern" then "auto" is
576 /// specified).
SetTypeSpecWidth(TSW W,SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID,const PrintingPolicy & Policy)577 bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
578 const char *&PrevSpec,
579 unsigned &DiagID,
580 const PrintingPolicy &Policy) {
581 // Overwrite TSWLoc only if TypeSpecWidth was unspecified, so that
582 // for 'long long' we will keep the source location of the first 'long'.
583 if (TypeSpecWidth == TSW_unspecified)
584 TSWLoc = Loc;
585 // Allow turning long -> long long.
586 else if (W != TSW_longlong || TypeSpecWidth != TSW_long)
587 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
588 TypeSpecWidth = W;
589 return false;
590 }
591
SetTypeSpecComplex(TSC C,SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID)592 bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
593 const char *&PrevSpec,
594 unsigned &DiagID) {
595 if (TypeSpecComplex != TSC_unspecified)
596 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
597 TypeSpecComplex = C;
598 TSCLoc = Loc;
599 return false;
600 }
601
SetTypeSpecSign(TSS S,SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID)602 bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
603 const char *&PrevSpec,
604 unsigned &DiagID) {
605 if (TypeSpecSign != TSS_unspecified)
606 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
607 TypeSpecSign = S;
608 TSSLoc = Loc;
609 return false;
610 }
611
SetTypeSpecType(TST T,SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID,ParsedType Rep,const PrintingPolicy & Policy)612 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
613 const char *&PrevSpec,
614 unsigned &DiagID,
615 ParsedType Rep,
616 const PrintingPolicy &Policy) {
617 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Policy);
618 }
619
SetTypeSpecType(TST T,SourceLocation TagKwLoc,SourceLocation TagNameLoc,const char * & PrevSpec,unsigned & DiagID,ParsedType Rep,const PrintingPolicy & Policy)620 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
621 SourceLocation TagNameLoc,
622 const char *&PrevSpec,
623 unsigned &DiagID,
624 ParsedType Rep,
625 const PrintingPolicy &Policy) {
626 assert(isTypeRep(T) && "T does not store a type");
627 assert(Rep && "no type provided!");
628 if (TypeSpecType != TST_unspecified) {
629 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
630 DiagID = diag::err_invalid_decl_spec_combination;
631 return true;
632 }
633 TypeSpecType = T;
634 TypeRep = Rep;
635 TSTLoc = TagKwLoc;
636 TSTNameLoc = TagNameLoc;
637 TypeSpecOwned = false;
638 return false;
639 }
640
SetTypeSpecType(TST T,SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID,Expr * Rep,const PrintingPolicy & Policy)641 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
642 const char *&PrevSpec,
643 unsigned &DiagID,
644 Expr *Rep,
645 const PrintingPolicy &Policy) {
646 assert(isExprRep(T) && "T does not store an expr");
647 assert(Rep && "no expression provided!");
648 if (TypeSpecType != TST_unspecified) {
649 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
650 DiagID = diag::err_invalid_decl_spec_combination;
651 return true;
652 }
653 TypeSpecType = T;
654 ExprRep = Rep;
655 TSTLoc = Loc;
656 TSTNameLoc = Loc;
657 TypeSpecOwned = false;
658 return false;
659 }
660
SetTypeSpecType(TST T,SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID,Decl * Rep,bool Owned,const PrintingPolicy & Policy)661 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
662 const char *&PrevSpec,
663 unsigned &DiagID,
664 Decl *Rep, bool Owned,
665 const PrintingPolicy &Policy) {
666 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Owned, Policy);
667 }
668
SetTypeSpecType(TST T,SourceLocation TagKwLoc,SourceLocation TagNameLoc,const char * & PrevSpec,unsigned & DiagID,Decl * Rep,bool Owned,const PrintingPolicy & Policy)669 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
670 SourceLocation TagNameLoc,
671 const char *&PrevSpec,
672 unsigned &DiagID,
673 Decl *Rep, bool Owned,
674 const PrintingPolicy &Policy) {
675 assert(isDeclRep(T) && "T does not store a decl");
676 // Unlike the other cases, we don't assert that we actually get a decl.
677
678 if (TypeSpecType != TST_unspecified) {
679 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
680 DiagID = diag::err_invalid_decl_spec_combination;
681 return true;
682 }
683 TypeSpecType = T;
684 DeclRep = Rep;
685 TSTLoc = TagKwLoc;
686 TSTNameLoc = TagNameLoc;
687 TypeSpecOwned = Owned && Rep != nullptr;
688 return false;
689 }
690
SetTypeSpecType(TST T,SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID,const PrintingPolicy & Policy)691 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
692 const char *&PrevSpec,
693 unsigned &DiagID,
694 const PrintingPolicy &Policy) {
695 assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
696 "rep required for these type-spec kinds!");
697 if (TypeSpecType != TST_unspecified) {
698 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
699 DiagID = diag::err_invalid_decl_spec_combination;
700 return true;
701 }
702 TSTLoc = Loc;
703 TSTNameLoc = Loc;
704 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
705 TypeAltiVecBool = true;
706 return false;
707 }
708 TypeSpecType = T;
709 TypeSpecOwned = false;
710 return false;
711 }
712
SetTypeAltiVecVector(bool isAltiVecVector,SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID,const PrintingPolicy & Policy)713 bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
714 const char *&PrevSpec, unsigned &DiagID,
715 const PrintingPolicy &Policy) {
716 if (TypeSpecType != TST_unspecified) {
717 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
718 DiagID = diag::err_invalid_vector_decl_spec_combination;
719 return true;
720 }
721 TypeAltiVecVector = isAltiVecVector;
722 AltiVecLoc = Loc;
723 return false;
724 }
725
SetTypePipe(bool isPipe,SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID,const PrintingPolicy & Policy)726 bool DeclSpec::SetTypePipe(bool isPipe, SourceLocation Loc,
727 const char *&PrevSpec, unsigned &DiagID,
728 const PrintingPolicy &Policy) {
729
730 if (TypeSpecType != TST_unspecified) {
731 PrevSpec = DeclSpec::getSpecifierName((TST)TypeSpecType, Policy);
732 DiagID = diag::err_invalid_decl_spec_combination;
733 return true;
734 }
735
736 if (isPipe) {
737 TypeSpecPipe = TSP_pipe;
738 }
739 return false;
740 }
741
SetTypeAltiVecPixel(bool isAltiVecPixel,SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID,const PrintingPolicy & Policy)742 bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
743 const char *&PrevSpec, unsigned &DiagID,
744 const PrintingPolicy &Policy) {
745 if (!TypeAltiVecVector || TypeAltiVecPixel ||
746 (TypeSpecType != TST_unspecified)) {
747 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
748 DiagID = diag::err_invalid_pixel_decl_spec_combination;
749 return true;
750 }
751 TypeAltiVecPixel = isAltiVecPixel;
752 TSTLoc = Loc;
753 TSTNameLoc = Loc;
754 return false;
755 }
756
SetTypeAltiVecBool(bool isAltiVecBool,SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID,const PrintingPolicy & Policy)757 bool DeclSpec::SetTypeAltiVecBool(bool isAltiVecBool, SourceLocation Loc,
758 const char *&PrevSpec, unsigned &DiagID,
759 const PrintingPolicy &Policy) {
760 if (!TypeAltiVecVector || TypeAltiVecBool ||
761 (TypeSpecType != TST_unspecified)) {
762 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
763 DiagID = diag::err_invalid_vector_bool_decl_spec;
764 return true;
765 }
766 TypeAltiVecBool = isAltiVecBool;
767 TSTLoc = Loc;
768 TSTNameLoc = Loc;
769 return false;
770 }
771
SetTypeSpecError()772 bool DeclSpec::SetTypeSpecError() {
773 TypeSpecType = TST_error;
774 TypeSpecOwned = false;
775 TSTLoc = SourceLocation();
776 TSTNameLoc = SourceLocation();
777 return false;
778 }
779
SetTypeQual(TQ T,SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID,const LangOptions & Lang)780 bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
781 unsigned &DiagID, const LangOptions &Lang) {
782 // Duplicates are permitted in C99 onwards, but are not permitted in C89 or
783 // C++. However, since this is likely not what the user intended, we will
784 // always warn. We do not need to set the qualifier's location since we
785 // already have it.
786 if (TypeQualifiers & T) {
787 bool IsExtension = true;
788 if (Lang.C99)
789 IsExtension = false;
790 return BadSpecifier(T, T, PrevSpec, DiagID, IsExtension);
791 }
792 TypeQualifiers |= T;
793
794 switch (T) {
795 case TQ_unspecified: break;
796 case TQ_const: TQ_constLoc = Loc; return false;
797 case TQ_restrict: TQ_restrictLoc = Loc; return false;
798 case TQ_volatile: TQ_volatileLoc = Loc; return false;
799 case TQ_unaligned: TQ_unalignedLoc = Loc; return false;
800 case TQ_atomic: TQ_atomicLoc = Loc; return false;
801 }
802
803 llvm_unreachable("Unknown type qualifier!");
804 }
805
setFunctionSpecInline(SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID)806 bool DeclSpec::setFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
807 unsigned &DiagID) {
808 // 'inline inline' is ok. However, since this is likely not what the user
809 // intended, we will always warn, similar to duplicates of type qualifiers.
810 if (FS_inline_specified) {
811 DiagID = diag::warn_duplicate_declspec;
812 PrevSpec = "inline";
813 return true;
814 }
815 FS_inline_specified = true;
816 FS_inlineLoc = Loc;
817 return false;
818 }
819
setFunctionSpecForceInline(SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID)820 bool DeclSpec::setFunctionSpecForceInline(SourceLocation Loc, const char *&PrevSpec,
821 unsigned &DiagID) {
822 if (FS_forceinline_specified) {
823 DiagID = diag::warn_duplicate_declspec;
824 PrevSpec = "__forceinline";
825 return true;
826 }
827 FS_forceinline_specified = true;
828 FS_forceinlineLoc = Loc;
829 return false;
830 }
831
setFunctionSpecVirtual(SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID)832 bool DeclSpec::setFunctionSpecVirtual(SourceLocation Loc,
833 const char *&PrevSpec,
834 unsigned &DiagID) {
835 // 'virtual virtual' is ok, but warn as this is likely not what the user
836 // intended.
837 if (FS_virtual_specified) {
838 DiagID = diag::warn_duplicate_declspec;
839 PrevSpec = "virtual";
840 return true;
841 }
842 FS_virtual_specified = true;
843 FS_virtualLoc = Loc;
844 return false;
845 }
846
setFunctionSpecExplicit(SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID)847 bool DeclSpec::setFunctionSpecExplicit(SourceLocation Loc,
848 const char *&PrevSpec,
849 unsigned &DiagID) {
850 // 'explicit explicit' is ok, but warn as this is likely not what the user
851 // intended.
852 if (FS_explicit_specified) {
853 DiagID = diag::warn_duplicate_declspec;
854 PrevSpec = "explicit";
855 return true;
856 }
857 FS_explicit_specified = true;
858 FS_explicitLoc = Loc;
859 return false;
860 }
861
setFunctionSpecNoreturn(SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID)862 bool DeclSpec::setFunctionSpecNoreturn(SourceLocation Loc,
863 const char *&PrevSpec,
864 unsigned &DiagID) {
865 // '_Noreturn _Noreturn' is ok, but warn as this is likely not what the user
866 // intended.
867 if (FS_noreturn_specified) {
868 DiagID = diag::warn_duplicate_declspec;
869 PrevSpec = "_Noreturn";
870 return true;
871 }
872 FS_noreturn_specified = true;
873 FS_noreturnLoc = Loc;
874 return false;
875 }
876
SetFriendSpec(SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID)877 bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
878 unsigned &DiagID) {
879 if (Friend_specified) {
880 PrevSpec = "friend";
881 // Keep the later location, so that we can later diagnose ill-formed
882 // declarations like 'friend class X friend;'. Per [class.friend]p3,
883 // 'friend' must be the first token in a friend declaration that is
884 // not a function declaration.
885 FriendLoc = Loc;
886 DiagID = diag::warn_duplicate_declspec;
887 return true;
888 }
889
890 Friend_specified = true;
891 FriendLoc = Loc;
892 return false;
893 }
894
setModulePrivateSpec(SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID)895 bool DeclSpec::setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec,
896 unsigned &DiagID) {
897 if (isModulePrivateSpecified()) {
898 PrevSpec = "__module_private__";
899 DiagID = diag::ext_duplicate_declspec;
900 return true;
901 }
902
903 ModulePrivateLoc = Loc;
904 return false;
905 }
906
SetConstexprSpec(SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID)907 bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
908 unsigned &DiagID) {
909 // 'constexpr constexpr' is ok, but warn as this is likely not what the user
910 // intended.
911 if (Constexpr_specified) {
912 DiagID = diag::warn_duplicate_declspec;
913 PrevSpec = "constexpr";
914 return true;
915 }
916 Constexpr_specified = true;
917 ConstexprLoc = Loc;
918 return false;
919 }
920
SetConceptSpec(SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID)921 bool DeclSpec::SetConceptSpec(SourceLocation Loc, const char *&PrevSpec,
922 unsigned &DiagID) {
923 if (Concept_specified) {
924 DiagID = diag::ext_duplicate_declspec;
925 PrevSpec = "concept";
926 return true;
927 }
928 Concept_specified = true;
929 ConceptLoc = Loc;
930 return false;
931 }
932
SaveWrittenBuiltinSpecs()933 void DeclSpec::SaveWrittenBuiltinSpecs() {
934 writtenBS.Sign = getTypeSpecSign();
935 writtenBS.Width = getTypeSpecWidth();
936 writtenBS.Type = getTypeSpecType();
937 // Search the list of attributes for the presence of a mode attribute.
938 writtenBS.ModeAttr = false;
939 AttributeList* attrs = getAttributes().getList();
940 while (attrs) {
941 if (attrs->getKind() == AttributeList::AT_Mode) {
942 writtenBS.ModeAttr = true;
943 break;
944 }
945 attrs = attrs->getNext();
946 }
947 }
948
949 /// Finish - This does final analysis of the declspec, rejecting things like
950 /// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
951 /// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
952 /// DeclSpec is guaranteed self-consistent, even if an error occurred.
Finish(Sema & S,const PrintingPolicy & Policy)953 void DeclSpec::Finish(Sema &S, const PrintingPolicy &Policy) {
954 // Before possibly changing their values, save specs as written.
955 SaveWrittenBuiltinSpecs();
956
957 // Check the type specifier components first.
958
959 // If decltype(auto) is used, no other type specifiers are permitted.
960 if (TypeSpecType == TST_decltype_auto &&
961 (TypeSpecWidth != TSW_unspecified ||
962 TypeSpecComplex != TSC_unspecified ||
963 TypeSpecSign != TSS_unspecified ||
964 TypeAltiVecVector || TypeAltiVecPixel || TypeAltiVecBool ||
965 TypeQualifiers)) {
966 const unsigned NumLocs = 9;
967 SourceLocation ExtraLocs[NumLocs] = {
968 TSWLoc, TSCLoc, TSSLoc, AltiVecLoc,
969 TQ_constLoc, TQ_restrictLoc, TQ_volatileLoc, TQ_atomicLoc, TQ_unalignedLoc
970 };
971 FixItHint Hints[NumLocs];
972 SourceLocation FirstLoc;
973 for (unsigned I = 0; I != NumLocs; ++I) {
974 if (ExtraLocs[I].isValid()) {
975 if (FirstLoc.isInvalid() ||
976 S.getSourceManager().isBeforeInTranslationUnit(ExtraLocs[I],
977 FirstLoc))
978 FirstLoc = ExtraLocs[I];
979 Hints[I] = FixItHint::CreateRemoval(ExtraLocs[I]);
980 }
981 }
982 TypeSpecWidth = TSW_unspecified;
983 TypeSpecComplex = TSC_unspecified;
984 TypeSpecSign = TSS_unspecified;
985 TypeAltiVecVector = TypeAltiVecPixel = TypeAltiVecBool = false;
986 TypeQualifiers = 0;
987 S.Diag(TSTLoc, diag::err_decltype_auto_cannot_be_combined)
988 << Hints[0] << Hints[1] << Hints[2] << Hints[3]
989 << Hints[4] << Hints[5] << Hints[6] << Hints[7];
990 }
991
992 // Validate and finalize AltiVec vector declspec.
993 if (TypeAltiVecVector) {
994 if (TypeAltiVecBool) {
995 // Sign specifiers are not allowed with vector bool. (PIM 2.1)
996 if (TypeSpecSign != TSS_unspecified) {
997 S.Diag(TSSLoc, diag::err_invalid_vector_bool_decl_spec)
998 << getSpecifierName((TSS)TypeSpecSign);
999 }
1000
1001 // Only char/int are valid with vector bool. (PIM 2.1)
1002 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
1003 (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
1004 S.Diag(TSTLoc, diag::err_invalid_vector_bool_decl_spec)
1005 << (TypeAltiVecPixel ? "__pixel" :
1006 getSpecifierName((TST)TypeSpecType, Policy));
1007 }
1008
1009 // Only 'short' and 'long long' are valid with vector bool. (PIM 2.1)
1010 if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short) &&
1011 (TypeSpecWidth != TSW_longlong))
1012 S.Diag(TSWLoc, diag::err_invalid_vector_bool_decl_spec)
1013 << getSpecifierName((TSW)TypeSpecWidth);
1014
1015 // vector bool long long requires VSX support or ZVector.
1016 if ((TypeSpecWidth == TSW_longlong) &&
1017 (!S.Context.getTargetInfo().hasFeature("vsx")) &&
1018 (!S.Context.getTargetInfo().hasFeature("power8-vector")) &&
1019 !S.getLangOpts().ZVector)
1020 S.Diag(TSTLoc, diag::err_invalid_vector_long_long_decl_spec);
1021
1022 // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
1023 if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
1024 (TypeSpecWidth != TSW_unspecified))
1025 TypeSpecSign = TSS_unsigned;
1026 } else if (TypeSpecType == TST_double) {
1027 // vector long double and vector long long double are never allowed.
1028 // vector double is OK for Power7 and later, and ZVector.
1029 if (TypeSpecWidth == TSW_long || TypeSpecWidth == TSW_longlong)
1030 S.Diag(TSWLoc, diag::err_invalid_vector_long_double_decl_spec);
1031 else if (!S.Context.getTargetInfo().hasFeature("vsx") &&
1032 !S.getLangOpts().ZVector)
1033 S.Diag(TSTLoc, diag::err_invalid_vector_double_decl_spec);
1034 } else if (TypeSpecType == TST_float) {
1035 // vector float is unsupported for ZVector.
1036 if (S.getLangOpts().ZVector)
1037 S.Diag(TSTLoc, diag::err_invalid_vector_float_decl_spec);
1038 } else if (TypeSpecWidth == TSW_long) {
1039 // vector long is unsupported for ZVector and deprecated for AltiVec.
1040 if (S.getLangOpts().ZVector)
1041 S.Diag(TSWLoc, diag::err_invalid_vector_long_decl_spec);
1042 else
1043 S.Diag(TSWLoc, diag::warn_vector_long_decl_spec_combination)
1044 << getSpecifierName((TST)TypeSpecType, Policy);
1045 }
1046
1047 if (TypeAltiVecPixel) {
1048 //TODO: perform validation
1049 TypeSpecType = TST_int;
1050 TypeSpecSign = TSS_unsigned;
1051 TypeSpecWidth = TSW_short;
1052 TypeSpecOwned = false;
1053 }
1054 }
1055
1056 // signed/unsigned are only valid with int/char/wchar_t.
1057 if (TypeSpecSign != TSS_unspecified) {
1058 if (TypeSpecType == TST_unspecified)
1059 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
1060 else if (TypeSpecType != TST_int && TypeSpecType != TST_int128 &&
1061 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
1062 S.Diag(TSSLoc, diag::err_invalid_sign_spec)
1063 << getSpecifierName((TST)TypeSpecType, Policy);
1064 // signed double -> double.
1065 TypeSpecSign = TSS_unspecified;
1066 }
1067 }
1068
1069 // Validate the width of the type.
1070 switch (TypeSpecWidth) {
1071 case TSW_unspecified: break;
1072 case TSW_short: // short int
1073 case TSW_longlong: // long long int
1074 if (TypeSpecType == TST_unspecified)
1075 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
1076 else if (TypeSpecType != TST_int) {
1077 S.Diag(TSWLoc, diag::err_invalid_width_spec) << (int)TypeSpecWidth
1078 << getSpecifierName((TST)TypeSpecType, Policy);
1079 TypeSpecType = TST_int;
1080 TypeSpecOwned = false;
1081 }
1082 break;
1083 case TSW_long: // long double, long int
1084 if (TypeSpecType == TST_unspecified)
1085 TypeSpecType = TST_int; // long -> long int.
1086 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
1087 S.Diag(TSWLoc, diag::err_invalid_width_spec) << (int)TypeSpecWidth
1088 << getSpecifierName((TST)TypeSpecType, Policy);
1089 TypeSpecType = TST_int;
1090 TypeSpecOwned = false;
1091 }
1092 break;
1093 }
1094
1095 // TODO: if the implementation does not implement _Complex or _Imaginary,
1096 // disallow their use. Need information about the backend.
1097 if (TypeSpecComplex != TSC_unspecified) {
1098 if (TypeSpecType == TST_unspecified) {
1099 S.Diag(TSCLoc, diag::ext_plain_complex)
1100 << FixItHint::CreateInsertion(
1101 S.getLocForEndOfToken(getTypeSpecComplexLoc()),
1102 " double");
1103 TypeSpecType = TST_double; // _Complex -> _Complex double.
1104 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
1105 // Note that this intentionally doesn't include _Complex _Bool.
1106 if (!S.getLangOpts().CPlusPlus)
1107 S.Diag(TSTLoc, diag::ext_integer_complex);
1108 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
1109 S.Diag(TSCLoc, diag::err_invalid_complex_spec)
1110 << getSpecifierName((TST)TypeSpecType, Policy);
1111 TypeSpecComplex = TSC_unspecified;
1112 }
1113 }
1114
1115 // C11 6.7.1/3, C++11 [dcl.stc]p1, GNU TLS: __thread, thread_local and
1116 // _Thread_local can only appear with the 'static' and 'extern' storage class
1117 // specifiers. We also allow __private_extern__ as an extension.
1118 if (ThreadStorageClassSpec != TSCS_unspecified) {
1119 switch (StorageClassSpec) {
1120 case SCS_unspecified:
1121 case SCS_extern:
1122 case SCS_private_extern:
1123 case SCS_static:
1124 break;
1125 default:
1126 if (S.getSourceManager().isBeforeInTranslationUnit(
1127 getThreadStorageClassSpecLoc(), getStorageClassSpecLoc()))
1128 S.Diag(getStorageClassSpecLoc(),
1129 diag::err_invalid_decl_spec_combination)
1130 << DeclSpec::getSpecifierName(getThreadStorageClassSpec())
1131 << SourceRange(getThreadStorageClassSpecLoc());
1132 else
1133 S.Diag(getThreadStorageClassSpecLoc(),
1134 diag::err_invalid_decl_spec_combination)
1135 << DeclSpec::getSpecifierName(getStorageClassSpec())
1136 << SourceRange(getStorageClassSpecLoc());
1137 // Discard the thread storage class specifier to recover.
1138 ThreadStorageClassSpec = TSCS_unspecified;
1139 ThreadStorageClassSpecLoc = SourceLocation();
1140 }
1141 }
1142
1143 // If no type specifier was provided and we're parsing a language where
1144 // the type specifier is not optional, but we got 'auto' as a storage
1145 // class specifier, then assume this is an attempt to use C++0x's 'auto'
1146 // type specifier.
1147 if (S.getLangOpts().CPlusPlus &&
1148 TypeSpecType == TST_unspecified && StorageClassSpec == SCS_auto) {
1149 TypeSpecType = TST_auto;
1150 StorageClassSpec = SCS_unspecified;
1151 TSTLoc = TSTNameLoc = StorageClassSpecLoc;
1152 StorageClassSpecLoc = SourceLocation();
1153 }
1154 // Diagnose if we've recovered from an ill-formed 'auto' storage class
1155 // specifier in a pre-C++11 dialect of C++.
1156 if (!S.getLangOpts().CPlusPlus11 && TypeSpecType == TST_auto)
1157 S.Diag(TSTLoc, diag::ext_auto_type_specifier);
1158 if (S.getLangOpts().CPlusPlus && !S.getLangOpts().CPlusPlus11 &&
1159 StorageClassSpec == SCS_auto)
1160 S.Diag(StorageClassSpecLoc, diag::warn_auto_storage_class)
1161 << FixItHint::CreateRemoval(StorageClassSpecLoc);
1162 if (TypeSpecType == TST_char16 || TypeSpecType == TST_char32)
1163 S.Diag(TSTLoc, diag::warn_cxx98_compat_unicode_type)
1164 << (TypeSpecType == TST_char16 ? "char16_t" : "char32_t");
1165 if (Constexpr_specified)
1166 S.Diag(ConstexprLoc, diag::warn_cxx98_compat_constexpr);
1167
1168 // C++ [class.friend]p6:
1169 // No storage-class-specifier shall appear in the decl-specifier-seq
1170 // of a friend declaration.
1171 if (isFriendSpecified() &&
1172 (getStorageClassSpec() || getThreadStorageClassSpec())) {
1173 SmallString<32> SpecName;
1174 SourceLocation SCLoc;
1175 FixItHint StorageHint, ThreadHint;
1176
1177 if (DeclSpec::SCS SC = getStorageClassSpec()) {
1178 SpecName = getSpecifierName(SC);
1179 SCLoc = getStorageClassSpecLoc();
1180 StorageHint = FixItHint::CreateRemoval(SCLoc);
1181 }
1182
1183 if (DeclSpec::TSCS TSC = getThreadStorageClassSpec()) {
1184 if (!SpecName.empty()) SpecName += " ";
1185 SpecName += getSpecifierName(TSC);
1186 SCLoc = getThreadStorageClassSpecLoc();
1187 ThreadHint = FixItHint::CreateRemoval(SCLoc);
1188 }
1189
1190 S.Diag(SCLoc, diag::err_friend_decl_spec)
1191 << SpecName << StorageHint << ThreadHint;
1192
1193 ClearStorageClassSpecs();
1194 }
1195
1196 // C++11 [dcl.fct.spec]p5:
1197 // The virtual specifier shall be used only in the initial
1198 // declaration of a non-static class member function;
1199 // C++11 [dcl.fct.spec]p6:
1200 // The explicit specifier shall be used only in the declaration of
1201 // a constructor or conversion function within its class
1202 // definition;
1203 if (isFriendSpecified() && (isVirtualSpecified() || isExplicitSpecified())) {
1204 StringRef Keyword;
1205 SourceLocation SCLoc;
1206
1207 if (isVirtualSpecified()) {
1208 Keyword = "virtual";
1209 SCLoc = getVirtualSpecLoc();
1210 } else {
1211 Keyword = "explicit";
1212 SCLoc = getExplicitSpecLoc();
1213 }
1214
1215 FixItHint Hint = FixItHint::CreateRemoval(SCLoc);
1216 S.Diag(SCLoc, diag::err_friend_decl_spec)
1217 << Keyword << Hint;
1218
1219 FS_virtual_specified = FS_explicit_specified = false;
1220 FS_virtualLoc = FS_explicitLoc = SourceLocation();
1221 }
1222
1223 assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
1224
1225 // Okay, now we can infer the real type.
1226
1227 // TODO: return "auto function" and other bad things based on the real type.
1228
1229 // 'data definition has no type or storage class'?
1230 }
1231
isMissingDeclaratorOk()1232 bool DeclSpec::isMissingDeclaratorOk() {
1233 TST tst = getTypeSpecType();
1234 return isDeclRep(tst) && getRepAsDecl() != nullptr &&
1235 StorageClassSpec != DeclSpec::SCS_typedef;
1236 }
1237
setOperatorFunctionId(SourceLocation OperatorLoc,OverloadedOperatorKind Op,SourceLocation SymbolLocations[3])1238 void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
1239 OverloadedOperatorKind Op,
1240 SourceLocation SymbolLocations[3]) {
1241 Kind = IK_OperatorFunctionId;
1242 StartLocation = OperatorLoc;
1243 EndLocation = OperatorLoc;
1244 OperatorFunctionId.Operator = Op;
1245 for (unsigned I = 0; I != 3; ++I) {
1246 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
1247
1248 if (SymbolLocations[I].isValid())
1249 EndLocation = SymbolLocations[I];
1250 }
1251 }
1252
SetSpecifier(Specifier VS,SourceLocation Loc,const char * & PrevSpec)1253 bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
1254 const char *&PrevSpec) {
1255 if (!FirstLocation.isValid())
1256 FirstLocation = Loc;
1257 LastLocation = Loc;
1258 LastSpecifier = VS;
1259
1260 if (Specifiers & VS) {
1261 PrevSpec = getSpecifierName(VS);
1262 return true;
1263 }
1264
1265 Specifiers |= VS;
1266
1267 switch (VS) {
1268 default: llvm_unreachable("Unknown specifier!");
1269 case VS_Override: VS_overrideLoc = Loc; break;
1270 case VS_Sealed:
1271 case VS_Final: VS_finalLoc = Loc; break;
1272 }
1273
1274 return false;
1275 }
1276
getSpecifierName(Specifier VS)1277 const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
1278 switch (VS) {
1279 default: llvm_unreachable("Unknown specifier");
1280 case VS_Override: return "override";
1281 case VS_Final: return "final";
1282 case VS_Sealed: return "sealed";
1283 }
1284 }
1285