clang API Documentation
00001 //===--- SemaDeclSpec.cpp - Declaration Specifier Semantic Analysis -------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements semantic analysis for declaration specifiers. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "clang/Sema/DeclSpec.h" 00015 #include "clang/AST/ASTContext.h" 00016 #include "clang/AST/DeclCXX.h" 00017 #include "clang/AST/Expr.h" 00018 #include "clang/AST/NestedNameSpecifier.h" 00019 #include "clang/AST/TypeLoc.h" 00020 #include "clang/Basic/LangOptions.h" 00021 #include "clang/Basic/TargetInfo.h" 00022 #include "clang/Lex/Preprocessor.h" 00023 #include "clang/Parse/ParseDiagnostic.h" // FIXME: remove this back-dependency! 00024 #include "clang/Sema/LocInfoType.h" 00025 #include "clang/Sema/ParsedTemplate.h" 00026 #include "clang/Sema/Sema.h" 00027 #include "clang/Sema/SemaDiagnostic.h" 00028 #include "llvm/ADT/STLExtras.h" 00029 #include "llvm/ADT/SmallString.h" 00030 #include "llvm/Support/ErrorHandling.h" 00031 #include <cstring> 00032 using namespace clang; 00033 00034 00035 static DiagnosticBuilder Diag(DiagnosticsEngine &D, SourceLocation Loc, 00036 unsigned DiagID) { 00037 return D.Report(Loc, DiagID); 00038 } 00039 00040 00041 void UnqualifiedId::setTemplateId(TemplateIdAnnotation *TemplateId) { 00042 assert(TemplateId && "NULL template-id annotation?"); 00043 Kind = IK_TemplateId; 00044 this->TemplateId = TemplateId; 00045 StartLocation = TemplateId->TemplateNameLoc; 00046 EndLocation = TemplateId->RAngleLoc; 00047 } 00048 00049 void UnqualifiedId::setConstructorTemplateId(TemplateIdAnnotation *TemplateId) { 00050 assert(TemplateId && "NULL template-id annotation?"); 00051 Kind = IK_ConstructorTemplateId; 00052 this->TemplateId = TemplateId; 00053 StartLocation = TemplateId->TemplateNameLoc; 00054 EndLocation = TemplateId->RAngleLoc; 00055 } 00056 00057 void CXXScopeSpec::Extend(ASTContext &Context, SourceLocation TemplateKWLoc, 00058 TypeLoc TL, SourceLocation ColonColonLoc) { 00059 Builder.Extend(Context, TemplateKWLoc, TL, ColonColonLoc); 00060 if (Range.getBegin().isInvalid()) 00061 Range.setBegin(TL.getBeginLoc()); 00062 Range.setEnd(ColonColonLoc); 00063 00064 assert(Range == Builder.getSourceRange() && 00065 "NestedNameSpecifierLoc range computation incorrect"); 00066 } 00067 00068 void CXXScopeSpec::Extend(ASTContext &Context, IdentifierInfo *Identifier, 00069 SourceLocation IdentifierLoc, 00070 SourceLocation ColonColonLoc) { 00071 Builder.Extend(Context, Identifier, IdentifierLoc, ColonColonLoc); 00072 00073 if (Range.getBegin().isInvalid()) 00074 Range.setBegin(IdentifierLoc); 00075 Range.setEnd(ColonColonLoc); 00076 00077 assert(Range == Builder.getSourceRange() && 00078 "NestedNameSpecifierLoc range computation incorrect"); 00079 } 00080 00081 void CXXScopeSpec::Extend(ASTContext &Context, NamespaceDecl *Namespace, 00082 SourceLocation NamespaceLoc, 00083 SourceLocation ColonColonLoc) { 00084 Builder.Extend(Context, Namespace, NamespaceLoc, ColonColonLoc); 00085 00086 if (Range.getBegin().isInvalid()) 00087 Range.setBegin(NamespaceLoc); 00088 Range.setEnd(ColonColonLoc); 00089 00090 assert(Range == Builder.getSourceRange() && 00091 "NestedNameSpecifierLoc range computation incorrect"); 00092 } 00093 00094 void CXXScopeSpec::Extend(ASTContext &Context, NamespaceAliasDecl *Alias, 00095 SourceLocation AliasLoc, 00096 SourceLocation ColonColonLoc) { 00097 Builder.Extend(Context, Alias, AliasLoc, ColonColonLoc); 00098 00099 if (Range.getBegin().isInvalid()) 00100 Range.setBegin(AliasLoc); 00101 Range.setEnd(ColonColonLoc); 00102 00103 assert(Range == Builder.getSourceRange() && 00104 "NestedNameSpecifierLoc range computation incorrect"); 00105 } 00106 00107 void CXXScopeSpec::MakeGlobal(ASTContext &Context, 00108 SourceLocation ColonColonLoc) { 00109 Builder.MakeGlobal(Context, ColonColonLoc); 00110 00111 Range = SourceRange(ColonColonLoc); 00112 00113 assert(Range == Builder.getSourceRange() && 00114 "NestedNameSpecifierLoc range computation incorrect"); 00115 } 00116 00117 void CXXScopeSpec::MakeSuper(ASTContext &Context, CXXRecordDecl *RD, 00118 SourceLocation SuperLoc, 00119 SourceLocation ColonColonLoc) { 00120 Builder.MakeSuper(Context, RD, SuperLoc, ColonColonLoc); 00121 00122 Range.setBegin(SuperLoc); 00123 Range.setEnd(ColonColonLoc); 00124 00125 assert(Range == Builder.getSourceRange() && 00126 "NestedNameSpecifierLoc range computation incorrect"); 00127 } 00128 00129 void CXXScopeSpec::MakeTrivial(ASTContext &Context, 00130 NestedNameSpecifier *Qualifier, SourceRange R) { 00131 Builder.MakeTrivial(Context, Qualifier, R); 00132 Range = R; 00133 } 00134 00135 void CXXScopeSpec::Adopt(NestedNameSpecifierLoc Other) { 00136 if (!Other) { 00137 Range = SourceRange(); 00138 Builder.Clear(); 00139 return; 00140 } 00141 00142 Range = Other.getSourceRange(); 00143 Builder.Adopt(Other); 00144 } 00145 00146 SourceLocation CXXScopeSpec::getLastQualifierNameLoc() const { 00147 if (!Builder.getRepresentation()) 00148 return SourceLocation(); 00149 return Builder.getTemporary().getLocalBeginLoc(); 00150 } 00151 00152 NestedNameSpecifierLoc 00153 CXXScopeSpec::getWithLocInContext(ASTContext &Context) const { 00154 if (!Builder.getRepresentation()) 00155 return NestedNameSpecifierLoc(); 00156 00157 return Builder.getWithLocInContext(Context); 00158 } 00159 00160 /// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function. 00161 /// "TheDeclarator" is the declarator that this will be added to. 00162 DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto, 00163 bool isAmbiguous, 00164 SourceLocation LParenLoc, 00165 ParamInfo *Params, 00166 unsigned NumParams, 00167 SourceLocation EllipsisLoc, 00168 SourceLocation RParenLoc, 00169 unsigned TypeQuals, 00170 bool RefQualifierIsLvalueRef, 00171 SourceLocation RefQualifierLoc, 00172 SourceLocation ConstQualifierLoc, 00173 SourceLocation 00174 VolatileQualifierLoc, 00175 SourceLocation 00176 RestrictQualifierLoc, 00177 SourceLocation MutableLoc, 00178 ExceptionSpecificationType 00179 ESpecType, 00180 SourceLocation ESpecLoc, 00181 ParsedType *Exceptions, 00182 SourceRange *ExceptionRanges, 00183 unsigned NumExceptions, 00184 Expr *NoexceptExpr, 00185 CachedTokens *ExceptionSpecTokens, 00186 SourceLocation LocalRangeBegin, 00187 SourceLocation LocalRangeEnd, 00188 Declarator &TheDeclarator, 00189 TypeResult TrailingReturnType) { 00190 assert(!(TypeQuals & DeclSpec::TQ_atomic) && 00191 "function cannot have _Atomic qualifier"); 00192 00193 DeclaratorChunk I; 00194 I.Kind = Function; 00195 I.Loc = LocalRangeBegin; 00196 I.EndLoc = LocalRangeEnd; 00197 I.Fun.AttrList = nullptr; 00198 I.Fun.hasPrototype = hasProto; 00199 I.Fun.isVariadic = EllipsisLoc.isValid(); 00200 I.Fun.isAmbiguous = isAmbiguous; 00201 I.Fun.LParenLoc = LParenLoc.getRawEncoding(); 00202 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding(); 00203 I.Fun.RParenLoc = RParenLoc.getRawEncoding(); 00204 I.Fun.DeleteParams = false; 00205 I.Fun.TypeQuals = TypeQuals; 00206 I.Fun.NumParams = NumParams; 00207 I.Fun.Params = nullptr; 00208 I.Fun.RefQualifierIsLValueRef = RefQualifierIsLvalueRef; 00209 I.Fun.RefQualifierLoc = RefQualifierLoc.getRawEncoding(); 00210 I.Fun.ConstQualifierLoc = ConstQualifierLoc.getRawEncoding(); 00211 I.Fun.VolatileQualifierLoc = VolatileQualifierLoc.getRawEncoding(); 00212 I.Fun.RestrictQualifierLoc = RestrictQualifierLoc.getRawEncoding(); 00213 I.Fun.MutableLoc = MutableLoc.getRawEncoding(); 00214 I.Fun.ExceptionSpecType = ESpecType; 00215 I.Fun.ExceptionSpecLoc = ESpecLoc.getRawEncoding(); 00216 I.Fun.NumExceptions = 0; 00217 I.Fun.Exceptions = nullptr; 00218 I.Fun.NoexceptExpr = nullptr; 00219 I.Fun.HasTrailingReturnType = TrailingReturnType.isUsable() || 00220 TrailingReturnType.isInvalid(); 00221 I.Fun.TrailingReturnType = TrailingReturnType.get(); 00222 00223 assert(I.Fun.TypeQuals == TypeQuals && "bitfield overflow"); 00224 assert(I.Fun.ExceptionSpecType == ESpecType && "bitfield overflow"); 00225 00226 // new[] a parameter array if needed. 00227 if (NumParams) { 00228 // If the 'InlineParams' in Declarator is unused and big enough, put our 00229 // parameter list there (in an effort to avoid new/delete traffic). If it 00230 // is already used (consider a function returning a function pointer) or too 00231 // small (function with too many parameters), go to the heap. 00232 if (!TheDeclarator.InlineParamsUsed && 00233 NumParams <= llvm::array_lengthof(TheDeclarator.InlineParams)) { 00234 I.Fun.Params = TheDeclarator.InlineParams; 00235 I.Fun.DeleteParams = false; 00236 TheDeclarator.InlineParamsUsed = true; 00237 } else { 00238 I.Fun.Params = new DeclaratorChunk::ParamInfo[NumParams]; 00239 I.Fun.DeleteParams = true; 00240 } 00241 memcpy(I.Fun.Params, Params, sizeof(Params[0]) * NumParams); 00242 } 00243 00244 // Check what exception specification information we should actually store. 00245 switch (ESpecType) { 00246 default: break; // By default, save nothing. 00247 case EST_Dynamic: 00248 // new[] an exception array if needed 00249 if (NumExceptions) { 00250 I.Fun.NumExceptions = NumExceptions; 00251 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions]; 00252 for (unsigned i = 0; i != NumExceptions; ++i) { 00253 I.Fun.Exceptions[i].Ty = Exceptions[i]; 00254 I.Fun.Exceptions[i].Range = ExceptionRanges[i]; 00255 } 00256 } 00257 break; 00258 00259 case EST_ComputedNoexcept: 00260 I.Fun.NoexceptExpr = NoexceptExpr; 00261 break; 00262 00263 case EST_Unparsed: 00264 I.Fun.ExceptionSpecTokens = ExceptionSpecTokens; 00265 break; 00266 } 00267 return I; 00268 } 00269 00270 bool Declarator::isDeclarationOfFunction() const { 00271 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) { 00272 switch (DeclTypeInfo[i].Kind) { 00273 case DeclaratorChunk::Function: 00274 return true; 00275 case DeclaratorChunk::Paren: 00276 continue; 00277 case DeclaratorChunk::Pointer: 00278 case DeclaratorChunk::Reference: 00279 case DeclaratorChunk::Array: 00280 case DeclaratorChunk::BlockPointer: 00281 case DeclaratorChunk::MemberPointer: 00282 return false; 00283 } 00284 llvm_unreachable("Invalid type chunk"); 00285 } 00286 00287 switch (DS.getTypeSpecType()) { 00288 case TST_atomic: 00289 case TST_auto: 00290 case TST_bool: 00291 case TST_char: 00292 case TST_char16: 00293 case TST_char32: 00294 case TST_class: 00295 case TST_decimal128: 00296 case TST_decimal32: 00297 case TST_decimal64: 00298 case TST_double: 00299 case TST_enum: 00300 case TST_error: 00301 case TST_float: 00302 case TST_half: 00303 case TST_int: 00304 case TST_int128: 00305 case TST_struct: 00306 case TST_interface: 00307 case TST_union: 00308 case TST_unknown_anytype: 00309 case TST_unspecified: 00310 case TST_void: 00311 case TST_wchar: 00312 return false; 00313 00314 case TST_decltype_auto: 00315 // This must have an initializer, so can't be a function declaration, 00316 // even if the initializer has function type. 00317 return false; 00318 00319 case TST_decltype: 00320 case TST_typeofExpr: 00321 if (Expr *E = DS.getRepAsExpr()) 00322 return E->getType()->isFunctionType(); 00323 return false; 00324 00325 case TST_underlyingType: 00326 case TST_typename: 00327 case TST_typeofType: { 00328 QualType QT = DS.getRepAsType().get(); 00329 if (QT.isNull()) 00330 return false; 00331 00332 if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) 00333 QT = LIT->getType(); 00334 00335 if (QT.isNull()) 00336 return false; 00337 00338 return QT->isFunctionType(); 00339 } 00340 } 00341 00342 llvm_unreachable("Invalid TypeSpecType!"); 00343 } 00344 00345 bool Declarator::isStaticMember() { 00346 assert(getContext() == MemberContext); 00347 return getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static || 00348 CXXMethodDecl::isStaticOverloadedOperator( 00349 getName().OperatorFunctionId.Operator); 00350 } 00351 00352 bool DeclSpec::hasTagDefinition() const { 00353 if (!TypeSpecOwned) 00354 return false; 00355 return cast<TagDecl>(getRepAsDecl())->isCompleteDefinition(); 00356 } 00357 00358 /// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this 00359 /// declaration specifier includes. 00360 /// 00361 unsigned DeclSpec::getParsedSpecifiers() const { 00362 unsigned Res = 0; 00363 if (StorageClassSpec != SCS_unspecified || 00364 ThreadStorageClassSpec != TSCS_unspecified) 00365 Res |= PQ_StorageClassSpecifier; 00366 00367 if (TypeQualifiers != TQ_unspecified) 00368 Res |= PQ_TypeQualifier; 00369 00370 if (hasTypeSpecifier()) 00371 Res |= PQ_TypeSpecifier; 00372 00373 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified || 00374 FS_noreturn_specified || FS_forceinline_specified) 00375 Res |= PQ_FunctionSpecifier; 00376 return Res; 00377 } 00378 00379 template <class T> static bool BadSpecifier(T TNew, T TPrev, 00380 const char *&PrevSpec, 00381 unsigned &DiagID, 00382 bool IsExtension = true) { 00383 PrevSpec = DeclSpec::getSpecifierName(TPrev); 00384 if (TNew != TPrev) 00385 DiagID = diag::err_invalid_decl_spec_combination; 00386 else 00387 DiagID = IsExtension ? diag::ext_duplicate_declspec : 00388 diag::warn_duplicate_declspec; 00389 return true; 00390 } 00391 00392 const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) { 00393 switch (S) { 00394 case DeclSpec::SCS_unspecified: return "unspecified"; 00395 case DeclSpec::SCS_typedef: return "typedef"; 00396 case DeclSpec::SCS_extern: return "extern"; 00397 case DeclSpec::SCS_static: return "static"; 00398 case DeclSpec::SCS_auto: return "auto"; 00399 case DeclSpec::SCS_register: return "register"; 00400 case DeclSpec::SCS_private_extern: return "__private_extern__"; 00401 case DeclSpec::SCS_mutable: return "mutable"; 00402 } 00403 llvm_unreachable("Unknown typespec!"); 00404 } 00405 00406 const char *DeclSpec::getSpecifierName(DeclSpec::TSCS S) { 00407 switch (S) { 00408 case DeclSpec::TSCS_unspecified: return "unspecified"; 00409 case DeclSpec::TSCS___thread: return "__thread"; 00410 case DeclSpec::TSCS_thread_local: return "thread_local"; 00411 case DeclSpec::TSCS__Thread_local: return "_Thread_local"; 00412 } 00413 llvm_unreachable("Unknown typespec!"); 00414 } 00415 00416 const char *DeclSpec::getSpecifierName(TSW W) { 00417 switch (W) { 00418 case TSW_unspecified: return "unspecified"; 00419 case TSW_short: return "short"; 00420 case TSW_long: return "long"; 00421 case TSW_longlong: return "long long"; 00422 } 00423 llvm_unreachable("Unknown typespec!"); 00424 } 00425 00426 const char *DeclSpec::getSpecifierName(TSC C) { 00427 switch (C) { 00428 case TSC_unspecified: return "unspecified"; 00429 case TSC_imaginary: return "imaginary"; 00430 case TSC_complex: return "complex"; 00431 } 00432 llvm_unreachable("Unknown typespec!"); 00433 } 00434 00435 00436 const char *DeclSpec::getSpecifierName(TSS S) { 00437 switch (S) { 00438 case TSS_unspecified: return "unspecified"; 00439 case TSS_signed: return "signed"; 00440 case TSS_unsigned: return "unsigned"; 00441 } 00442 llvm_unreachable("Unknown typespec!"); 00443 } 00444 00445 const char *DeclSpec::getSpecifierName(DeclSpec::TST T, 00446 const PrintingPolicy &Policy) { 00447 switch (T) { 00448 case DeclSpec::TST_unspecified: return "unspecified"; 00449 case DeclSpec::TST_void: return "void"; 00450 case DeclSpec::TST_char: return "char"; 00451 case DeclSpec::TST_wchar: return Policy.MSWChar ? "__wchar_t" : "wchar_t"; 00452 case DeclSpec::TST_char16: return "char16_t"; 00453 case DeclSpec::TST_char32: return "char32_t"; 00454 case DeclSpec::TST_int: return "int"; 00455 case DeclSpec::TST_int128: return "__int128"; 00456 case DeclSpec::TST_half: return "half"; 00457 case DeclSpec::TST_float: return "float"; 00458 case DeclSpec::TST_double: return "double"; 00459 case DeclSpec::TST_bool: return Policy.Bool ? "bool" : "_Bool"; 00460 case DeclSpec::TST_decimal32: return "_Decimal32"; 00461 case DeclSpec::TST_decimal64: return "_Decimal64"; 00462 case DeclSpec::TST_decimal128: return "_Decimal128"; 00463 case DeclSpec::TST_enum: return "enum"; 00464 case DeclSpec::TST_class: return "class"; 00465 case DeclSpec::TST_union: return "union"; 00466 case DeclSpec::TST_struct: return "struct"; 00467 case DeclSpec::TST_interface: return "__interface"; 00468 case DeclSpec::TST_typename: return "type-name"; 00469 case DeclSpec::TST_typeofType: 00470 case DeclSpec::TST_typeofExpr: return "typeof"; 00471 case DeclSpec::TST_auto: return "auto"; 00472 case DeclSpec::TST_decltype: return "(decltype)"; 00473 case DeclSpec::TST_decltype_auto: return "decltype(auto)"; 00474 case DeclSpec::TST_underlyingType: return "__underlying_type"; 00475 case DeclSpec::TST_unknown_anytype: return "__unknown_anytype"; 00476 case DeclSpec::TST_atomic: return "_Atomic"; 00477 case DeclSpec::TST_error: return "(error)"; 00478 } 00479 llvm_unreachable("Unknown typespec!"); 00480 } 00481 00482 const char *DeclSpec::getSpecifierName(TQ T) { 00483 switch (T) { 00484 case DeclSpec::TQ_unspecified: return "unspecified"; 00485 case DeclSpec::TQ_const: return "const"; 00486 case DeclSpec::TQ_restrict: return "restrict"; 00487 case DeclSpec::TQ_volatile: return "volatile"; 00488 case DeclSpec::TQ_atomic: return "_Atomic"; 00489 } 00490 llvm_unreachable("Unknown typespec!"); 00491 } 00492 00493 bool DeclSpec::SetStorageClassSpec(Sema &S, SCS SC, SourceLocation Loc, 00494 const char *&PrevSpec, 00495 unsigned &DiagID, 00496 const PrintingPolicy &Policy) { 00497 // OpenCL v1.1 s6.8g: "The extern, static, auto and register storage-class 00498 // specifiers are not supported. 00499 // It seems sensible to prohibit private_extern too 00500 // The cl_clang_storage_class_specifiers extension enables support for 00501 // these storage-class specifiers. 00502 // OpenCL v1.2 s6.8 changes this to "The auto and register storage-class 00503 // specifiers are not supported." 00504 if (S.getLangOpts().OpenCL && 00505 !S.getOpenCLOptions().cl_clang_storage_class_specifiers) { 00506 switch (SC) { 00507 case SCS_extern: 00508 case SCS_private_extern: 00509 case SCS_static: 00510 if (S.getLangOpts().OpenCLVersion < 120) { 00511 DiagID = diag::err_not_opencl_storage_class_specifier; 00512 PrevSpec = getSpecifierName(SC); 00513 return true; 00514 } 00515 break; 00516 case SCS_auto: 00517 case SCS_register: 00518 DiagID = diag::err_not_opencl_storage_class_specifier; 00519 PrevSpec = getSpecifierName(SC); 00520 return true; 00521 default: 00522 break; 00523 } 00524 } 00525 00526 if (StorageClassSpec != SCS_unspecified) { 00527 // Maybe this is an attempt to use C++11 'auto' outside of C++11 mode. 00528 bool isInvalid = true; 00529 if (TypeSpecType == TST_unspecified && S.getLangOpts().CPlusPlus) { 00530 if (SC == SCS_auto) 00531 return SetTypeSpecType(TST_auto, Loc, PrevSpec, DiagID, Policy); 00532 if (StorageClassSpec == SCS_auto) { 00533 isInvalid = SetTypeSpecType(TST_auto, StorageClassSpecLoc, 00534 PrevSpec, DiagID, Policy); 00535 assert(!isInvalid && "auto SCS -> TST recovery failed"); 00536 } 00537 } 00538 00539 // Changing storage class is allowed only if the previous one 00540 // was the 'extern' that is part of a linkage specification and 00541 // the new storage class is 'typedef'. 00542 if (isInvalid && 00543 !(SCS_extern_in_linkage_spec && 00544 StorageClassSpec == SCS_extern && 00545 SC == SCS_typedef)) 00546 return BadSpecifier(SC, (SCS)StorageClassSpec, PrevSpec, DiagID); 00547 } 00548 StorageClassSpec = SC; 00549 StorageClassSpecLoc = Loc; 00550 assert((unsigned)SC == StorageClassSpec && "SCS constants overflow bitfield"); 00551 return false; 00552 } 00553 00554 bool DeclSpec::SetStorageClassSpecThread(TSCS TSC, SourceLocation Loc, 00555 const char *&PrevSpec, 00556 unsigned &DiagID) { 00557 if (ThreadStorageClassSpec != TSCS_unspecified) 00558 return BadSpecifier(TSC, (TSCS)ThreadStorageClassSpec, PrevSpec, DiagID); 00559 00560 ThreadStorageClassSpec = TSC; 00561 ThreadStorageClassSpecLoc = Loc; 00562 return false; 00563 } 00564 00565 /// These methods set the specified attribute of the DeclSpec, but return true 00566 /// and ignore the request if invalid (e.g. "extern" then "auto" is 00567 /// specified). 00568 bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc, 00569 const char *&PrevSpec, 00570 unsigned &DiagID, 00571 const PrintingPolicy &Policy) { 00572 // Overwrite TSWLoc only if TypeSpecWidth was unspecified, so that 00573 // for 'long long' we will keep the source location of the first 'long'. 00574 if (TypeSpecWidth == TSW_unspecified) 00575 TSWLoc = Loc; 00576 // Allow turning long -> long long. 00577 else if (W != TSW_longlong || TypeSpecWidth != TSW_long) 00578 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID); 00579 TypeSpecWidth = W; 00580 return false; 00581 } 00582 00583 bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc, 00584 const char *&PrevSpec, 00585 unsigned &DiagID) { 00586 if (TypeSpecComplex != TSC_unspecified) 00587 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID); 00588 TypeSpecComplex = C; 00589 TSCLoc = Loc; 00590 return false; 00591 } 00592 00593 bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc, 00594 const char *&PrevSpec, 00595 unsigned &DiagID) { 00596 if (TypeSpecSign != TSS_unspecified) 00597 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID); 00598 TypeSpecSign = S; 00599 TSSLoc = Loc; 00600 return false; 00601 } 00602 00603 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc, 00604 const char *&PrevSpec, 00605 unsigned &DiagID, 00606 ParsedType Rep, 00607 const PrintingPolicy &Policy) { 00608 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Policy); 00609 } 00610 00611 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc, 00612 SourceLocation TagNameLoc, 00613 const char *&PrevSpec, 00614 unsigned &DiagID, 00615 ParsedType Rep, 00616 const PrintingPolicy &Policy) { 00617 assert(isTypeRep(T) && "T does not store a type"); 00618 assert(Rep && "no type provided!"); 00619 if (TypeSpecType != TST_unspecified) { 00620 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy); 00621 DiagID = diag::err_invalid_decl_spec_combination; 00622 return true; 00623 } 00624 TypeSpecType = T; 00625 TypeRep = Rep; 00626 TSTLoc = TagKwLoc; 00627 TSTNameLoc = TagNameLoc; 00628 TypeSpecOwned = false; 00629 return false; 00630 } 00631 00632 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc, 00633 const char *&PrevSpec, 00634 unsigned &DiagID, 00635 Expr *Rep, 00636 const PrintingPolicy &Policy) { 00637 assert(isExprRep(T) && "T does not store an expr"); 00638 assert(Rep && "no expression provided!"); 00639 if (TypeSpecType != TST_unspecified) { 00640 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy); 00641 DiagID = diag::err_invalid_decl_spec_combination; 00642 return true; 00643 } 00644 TypeSpecType = T; 00645 ExprRep = Rep; 00646 TSTLoc = Loc; 00647 TSTNameLoc = Loc; 00648 TypeSpecOwned = false; 00649 return false; 00650 } 00651 00652 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc, 00653 const char *&PrevSpec, 00654 unsigned &DiagID, 00655 Decl *Rep, bool Owned, 00656 const PrintingPolicy &Policy) { 00657 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Owned, Policy); 00658 } 00659 00660 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc, 00661 SourceLocation TagNameLoc, 00662 const char *&PrevSpec, 00663 unsigned &DiagID, 00664 Decl *Rep, bool Owned, 00665 const PrintingPolicy &Policy) { 00666 assert(isDeclRep(T) && "T does not store a decl"); 00667 // Unlike the other cases, we don't assert that we actually get a decl. 00668 00669 if (TypeSpecType != TST_unspecified) { 00670 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy); 00671 DiagID = diag::err_invalid_decl_spec_combination; 00672 return true; 00673 } 00674 TypeSpecType = T; 00675 DeclRep = Rep; 00676 TSTLoc = TagKwLoc; 00677 TSTNameLoc = TagNameLoc; 00678 TypeSpecOwned = Owned && Rep != nullptr; 00679 return false; 00680 } 00681 00682 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc, 00683 const char *&PrevSpec, 00684 unsigned &DiagID, 00685 const PrintingPolicy &Policy) { 00686 assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) && 00687 "rep required for these type-spec kinds!"); 00688 if (TypeSpecType != TST_unspecified) { 00689 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy); 00690 DiagID = diag::err_invalid_decl_spec_combination; 00691 return true; 00692 } 00693 TSTLoc = Loc; 00694 TSTNameLoc = Loc; 00695 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) { 00696 TypeAltiVecBool = true; 00697 return false; 00698 } 00699 TypeSpecType = T; 00700 TypeSpecOwned = false; 00701 return false; 00702 } 00703 00704 bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc, 00705 const char *&PrevSpec, unsigned &DiagID, 00706 const PrintingPolicy &Policy) { 00707 if (TypeSpecType != TST_unspecified) { 00708 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy); 00709 DiagID = diag::err_invalid_vector_decl_spec_combination; 00710 return true; 00711 } 00712 TypeAltiVecVector = isAltiVecVector; 00713 AltiVecLoc = Loc; 00714 return false; 00715 } 00716 00717 bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc, 00718 const char *&PrevSpec, unsigned &DiagID, 00719 const PrintingPolicy &Policy) { 00720 if (!TypeAltiVecVector || TypeAltiVecPixel || 00721 (TypeSpecType != TST_unspecified)) { 00722 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy); 00723 DiagID = diag::err_invalid_pixel_decl_spec_combination; 00724 return true; 00725 } 00726 TypeAltiVecPixel = isAltiVecPixel; 00727 TSTLoc = Loc; 00728 TSTNameLoc = Loc; 00729 return false; 00730 } 00731 00732 bool DeclSpec::SetTypeAltiVecBool(bool isAltiVecBool, SourceLocation Loc, 00733 const char *&PrevSpec, unsigned &DiagID, 00734 const PrintingPolicy &Policy) { 00735 if (!TypeAltiVecVector || TypeAltiVecBool || 00736 (TypeSpecType != TST_unspecified)) { 00737 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy); 00738 DiagID = diag::err_invalid_vector_bool_decl_spec; 00739 return true; 00740 } 00741 TypeAltiVecBool = isAltiVecBool; 00742 TSTLoc = Loc; 00743 TSTNameLoc = Loc; 00744 return false; 00745 } 00746 00747 bool DeclSpec::SetTypeSpecError() { 00748 TypeSpecType = TST_error; 00749 TypeSpecOwned = false; 00750 TSTLoc = SourceLocation(); 00751 TSTNameLoc = SourceLocation(); 00752 return false; 00753 } 00754 00755 bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec, 00756 unsigned &DiagID, const LangOptions &Lang) { 00757 // Duplicates are permitted in C99 onwards, but are not permitted in C89 or 00758 // C++. However, since this is likely not what the user intended, we will 00759 // always warn. We do not need to set the qualifier's location since we 00760 // already have it. 00761 if (TypeQualifiers & T) { 00762 bool IsExtension = true; 00763 if (Lang.C99) 00764 IsExtension = false; 00765 return BadSpecifier(T, T, PrevSpec, DiagID, IsExtension); 00766 } 00767 TypeQualifiers |= T; 00768 00769 switch (T) { 00770 case TQ_unspecified: break; 00771 case TQ_const: TQ_constLoc = Loc; return false; 00772 case TQ_restrict: TQ_restrictLoc = Loc; return false; 00773 case TQ_volatile: TQ_volatileLoc = Loc; return false; 00774 case TQ_atomic: TQ_atomicLoc = Loc; return false; 00775 } 00776 00777 llvm_unreachable("Unknown type qualifier!"); 00778 } 00779 00780 bool DeclSpec::setFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec, 00781 unsigned &DiagID) { 00782 // 'inline inline' is ok. However, since this is likely not what the user 00783 // intended, we will always warn, similar to duplicates of type qualifiers. 00784 if (FS_inline_specified) { 00785 DiagID = diag::warn_duplicate_declspec; 00786 PrevSpec = "inline"; 00787 return true; 00788 } 00789 FS_inline_specified = true; 00790 FS_inlineLoc = Loc; 00791 return false; 00792 } 00793 00794 bool DeclSpec::setFunctionSpecForceInline(SourceLocation Loc, const char *&PrevSpec, 00795 unsigned &DiagID) { 00796 if (FS_forceinline_specified) { 00797 DiagID = diag::warn_duplicate_declspec; 00798 PrevSpec = "__forceinline"; 00799 return true; 00800 } 00801 FS_forceinline_specified = true; 00802 FS_forceinlineLoc = Loc; 00803 return false; 00804 } 00805 00806 bool DeclSpec::setFunctionSpecVirtual(SourceLocation Loc, 00807 const char *&PrevSpec, 00808 unsigned &DiagID) { 00809 // 'virtual virtual' is ok, but warn as this is likely not what the user 00810 // intended. 00811 if (FS_virtual_specified) { 00812 DiagID = diag::warn_duplicate_declspec; 00813 PrevSpec = "virtual"; 00814 return true; 00815 } 00816 FS_virtual_specified = true; 00817 FS_virtualLoc = Loc; 00818 return false; 00819 } 00820 00821 bool DeclSpec::setFunctionSpecExplicit(SourceLocation Loc, 00822 const char *&PrevSpec, 00823 unsigned &DiagID) { 00824 // 'explicit explicit' is ok, but warn as this is likely not what the user 00825 // intended. 00826 if (FS_explicit_specified) { 00827 DiagID = diag::warn_duplicate_declspec; 00828 PrevSpec = "explicit"; 00829 return true; 00830 } 00831 FS_explicit_specified = true; 00832 FS_explicitLoc = Loc; 00833 return false; 00834 } 00835 00836 bool DeclSpec::setFunctionSpecNoreturn(SourceLocation Loc, 00837 const char *&PrevSpec, 00838 unsigned &DiagID) { 00839 // '_Noreturn _Noreturn' is ok, but warn as this is likely not what the user 00840 // intended. 00841 if (FS_noreturn_specified) { 00842 DiagID = diag::warn_duplicate_declspec; 00843 PrevSpec = "_Noreturn"; 00844 return true; 00845 } 00846 FS_noreturn_specified = true; 00847 FS_noreturnLoc = Loc; 00848 return false; 00849 } 00850 00851 bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec, 00852 unsigned &DiagID) { 00853 if (Friend_specified) { 00854 PrevSpec = "friend"; 00855 // Keep the later location, so that we can later diagnose ill-formed 00856 // declarations like 'friend class X friend;'. Per [class.friend]p3, 00857 // 'friend' must be the first token in a friend declaration that is 00858 // not a function declaration. 00859 FriendLoc = Loc; 00860 DiagID = diag::warn_duplicate_declspec; 00861 return true; 00862 } 00863 00864 Friend_specified = true; 00865 FriendLoc = Loc; 00866 return false; 00867 } 00868 00869 bool DeclSpec::setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec, 00870 unsigned &DiagID) { 00871 if (isModulePrivateSpecified()) { 00872 PrevSpec = "__module_private__"; 00873 DiagID = diag::ext_duplicate_declspec; 00874 return true; 00875 } 00876 00877 ModulePrivateLoc = Loc; 00878 return false; 00879 } 00880 00881 bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec, 00882 unsigned &DiagID) { 00883 // 'constexpr constexpr' is ok, but warn as this is likely not what the user 00884 // intended. 00885 if (Constexpr_specified) { 00886 DiagID = diag::warn_duplicate_declspec; 00887 PrevSpec = "constexpr"; 00888 return true; 00889 } 00890 Constexpr_specified = true; 00891 ConstexprLoc = Loc; 00892 return false; 00893 } 00894 00895 void DeclSpec::setProtocolQualifiers(Decl * const *Protos, 00896 unsigned NP, 00897 SourceLocation *ProtoLocs, 00898 SourceLocation LAngleLoc) { 00899 if (NP == 0) return; 00900 Decl **ProtoQuals = new Decl*[NP]; 00901 memcpy(ProtoQuals, Protos, sizeof(Decl*)*NP); 00902 ProtocolQualifiers = ProtoQuals; 00903 ProtocolLocs = new SourceLocation[NP]; 00904 memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP); 00905 NumProtocolQualifiers = NP; 00906 ProtocolLAngleLoc = LAngleLoc; 00907 } 00908 00909 void DeclSpec::SaveWrittenBuiltinSpecs() { 00910 writtenBS.Sign = getTypeSpecSign(); 00911 writtenBS.Width = getTypeSpecWidth(); 00912 writtenBS.Type = getTypeSpecType(); 00913 // Search the list of attributes for the presence of a mode attribute. 00914 writtenBS.ModeAttr = false; 00915 AttributeList* attrs = getAttributes().getList(); 00916 while (attrs) { 00917 if (attrs->getKind() == AttributeList::AT_Mode) { 00918 writtenBS.ModeAttr = true; 00919 break; 00920 } 00921 attrs = attrs->getNext(); 00922 } 00923 } 00924 00925 /// Finish - This does final analysis of the declspec, rejecting things like 00926 /// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or 00927 /// diag::NUM_DIAGNOSTICS if there is no error. After calling this method, 00928 /// DeclSpec is guaranteed self-consistent, even if an error occurred. 00929 void DeclSpec::Finish(DiagnosticsEngine &D, Preprocessor &PP, const PrintingPolicy &Policy) { 00930 // Before possibly changing their values, save specs as written. 00931 SaveWrittenBuiltinSpecs(); 00932 00933 // Check the type specifier components first. 00934 00935 // If decltype(auto) is used, no other type specifiers are permitted. 00936 if (TypeSpecType == TST_decltype_auto && 00937 (TypeSpecWidth != TSW_unspecified || 00938 TypeSpecComplex != TSC_unspecified || 00939 TypeSpecSign != TSS_unspecified || 00940 TypeAltiVecVector || TypeAltiVecPixel || TypeAltiVecBool || 00941 TypeQualifiers)) { 00942 const unsigned NumLocs = 8; 00943 SourceLocation ExtraLocs[NumLocs] = { 00944 TSWLoc, TSCLoc, TSSLoc, AltiVecLoc, 00945 TQ_constLoc, TQ_restrictLoc, TQ_volatileLoc, TQ_atomicLoc 00946 }; 00947 FixItHint Hints[NumLocs]; 00948 SourceLocation FirstLoc; 00949 for (unsigned I = 0; I != NumLocs; ++I) { 00950 if (!ExtraLocs[I].isInvalid()) { 00951 if (FirstLoc.isInvalid() || 00952 PP.getSourceManager().isBeforeInTranslationUnit(ExtraLocs[I], 00953 FirstLoc)) 00954 FirstLoc = ExtraLocs[I]; 00955 Hints[I] = FixItHint::CreateRemoval(ExtraLocs[I]); 00956 } 00957 } 00958 TypeSpecWidth = TSW_unspecified; 00959 TypeSpecComplex = TSC_unspecified; 00960 TypeSpecSign = TSS_unspecified; 00961 TypeAltiVecVector = TypeAltiVecPixel = TypeAltiVecBool = false; 00962 TypeQualifiers = 0; 00963 Diag(D, TSTLoc, diag::err_decltype_auto_cannot_be_combined) 00964 << Hints[0] << Hints[1] << Hints[2] << Hints[3] 00965 << Hints[4] << Hints[5] << Hints[6] << Hints[7]; 00966 } 00967 00968 // Validate and finalize AltiVec vector declspec. 00969 if (TypeAltiVecVector) { 00970 if (TypeAltiVecBool) { 00971 // Sign specifiers are not allowed with vector bool. (PIM 2.1) 00972 if (TypeSpecSign != TSS_unspecified) { 00973 Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec) 00974 << getSpecifierName((TSS)TypeSpecSign); 00975 } 00976 00977 // Only char/int are valid with vector bool. (PIM 2.1) 00978 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) && 00979 (TypeSpecType != TST_int)) || TypeAltiVecPixel) { 00980 Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec) 00981 << (TypeAltiVecPixel ? "__pixel" : 00982 getSpecifierName((TST)TypeSpecType, Policy)); 00983 } 00984 00985 // Only 'short' is valid with vector bool. (PIM 2.1) 00986 if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short)) 00987 Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec) 00988 << getSpecifierName((TSW)TypeSpecWidth); 00989 00990 // Elements of vector bool are interpreted as unsigned. (PIM 2.1) 00991 if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) || 00992 (TypeSpecWidth != TSW_unspecified)) 00993 TypeSpecSign = TSS_unsigned; 00994 } else if (TypeSpecType == TST_double) { 00995 // vector long double and vector long long double are never allowed. 00996 // vector double is OK for Power7 and later. 00997 if (TypeSpecWidth == TSW_long || TypeSpecWidth == TSW_longlong) 00998 Diag(D, TSWLoc, diag::err_invalid_vector_long_double_decl_spec); 00999 else if (!PP.getTargetInfo().hasFeature("vsx")) 01000 Diag(D, TSTLoc, diag::err_invalid_vector_double_decl_spec); 01001 } else if (TypeSpecWidth == TSW_long) { 01002 Diag(D, TSWLoc, diag::warn_vector_long_decl_spec_combination) 01003 << getSpecifierName((TST)TypeSpecType, Policy); 01004 } 01005 01006 if (TypeAltiVecPixel) { 01007 //TODO: perform validation 01008 TypeSpecType = TST_int; 01009 TypeSpecSign = TSS_unsigned; 01010 TypeSpecWidth = TSW_short; 01011 TypeSpecOwned = false; 01012 } 01013 } 01014 01015 // signed/unsigned are only valid with int/char/wchar_t. 01016 if (TypeSpecSign != TSS_unspecified) { 01017 if (TypeSpecType == TST_unspecified) 01018 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int. 01019 else if (TypeSpecType != TST_int && TypeSpecType != TST_int128 && 01020 TypeSpecType != TST_char && TypeSpecType != TST_wchar) { 01021 Diag(D, TSSLoc, diag::err_invalid_sign_spec) 01022 << getSpecifierName((TST)TypeSpecType, Policy); 01023 // signed double -> double. 01024 TypeSpecSign = TSS_unspecified; 01025 } 01026 } 01027 01028 // Validate the width of the type. 01029 switch (TypeSpecWidth) { 01030 case TSW_unspecified: break; 01031 case TSW_short: // short int 01032 case TSW_longlong: // long long int 01033 if (TypeSpecType == TST_unspecified) 01034 TypeSpecType = TST_int; // short -> short int, long long -> long long int. 01035 else if (TypeSpecType != TST_int) { 01036 Diag(D, TSWLoc, 01037 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec 01038 : diag::err_invalid_longlong_spec) 01039 << getSpecifierName((TST)TypeSpecType, Policy); 01040 TypeSpecType = TST_int; 01041 TypeSpecOwned = false; 01042 } 01043 break; 01044 case TSW_long: // long double, long int 01045 if (TypeSpecType == TST_unspecified) 01046 TypeSpecType = TST_int; // long -> long int. 01047 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) { 01048 Diag(D, TSWLoc, diag::err_invalid_long_spec) 01049 << getSpecifierName((TST)TypeSpecType, Policy); 01050 TypeSpecType = TST_int; 01051 TypeSpecOwned = false; 01052 } 01053 break; 01054 } 01055 01056 // TODO: if the implementation does not implement _Complex or _Imaginary, 01057 // disallow their use. Need information about the backend. 01058 if (TypeSpecComplex != TSC_unspecified) { 01059 if (TypeSpecType == TST_unspecified) { 01060 Diag(D, TSCLoc, diag::ext_plain_complex) 01061 << FixItHint::CreateInsertion( 01062 PP.getLocForEndOfToken(getTypeSpecComplexLoc()), 01063 " double"); 01064 TypeSpecType = TST_double; // _Complex -> _Complex double. 01065 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) { 01066 // Note that this intentionally doesn't include _Complex _Bool. 01067 if (!PP.getLangOpts().CPlusPlus) 01068 Diag(D, TSTLoc, diag::ext_integer_complex); 01069 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) { 01070 Diag(D, TSCLoc, diag::err_invalid_complex_spec) 01071 << getSpecifierName((TST)TypeSpecType, Policy); 01072 TypeSpecComplex = TSC_unspecified; 01073 } 01074 } 01075 01076 // C11 6.7.1/3, C++11 [dcl.stc]p1, GNU TLS: __thread, thread_local and 01077 // _Thread_local can only appear with the 'static' and 'extern' storage class 01078 // specifiers. We also allow __private_extern__ as an extension. 01079 if (ThreadStorageClassSpec != TSCS_unspecified) { 01080 switch (StorageClassSpec) { 01081 case SCS_unspecified: 01082 case SCS_extern: 01083 case SCS_private_extern: 01084 case SCS_static: 01085 break; 01086 default: 01087 if (PP.getSourceManager().isBeforeInTranslationUnit( 01088 getThreadStorageClassSpecLoc(), getStorageClassSpecLoc())) 01089 Diag(D, getStorageClassSpecLoc(), 01090 diag::err_invalid_decl_spec_combination) 01091 << DeclSpec::getSpecifierName(getThreadStorageClassSpec()) 01092 << SourceRange(getThreadStorageClassSpecLoc()); 01093 else 01094 Diag(D, getThreadStorageClassSpecLoc(), 01095 diag::err_invalid_decl_spec_combination) 01096 << DeclSpec::getSpecifierName(getStorageClassSpec()) 01097 << SourceRange(getStorageClassSpecLoc()); 01098 // Discard the thread storage class specifier to recover. 01099 ThreadStorageClassSpec = TSCS_unspecified; 01100 ThreadStorageClassSpecLoc = SourceLocation(); 01101 } 01102 } 01103 01104 // If no type specifier was provided and we're parsing a language where 01105 // the type specifier is not optional, but we got 'auto' as a storage 01106 // class specifier, then assume this is an attempt to use C++0x's 'auto' 01107 // type specifier. 01108 if (PP.getLangOpts().CPlusPlus && 01109 TypeSpecType == TST_unspecified && StorageClassSpec == SCS_auto) { 01110 TypeSpecType = TST_auto; 01111 StorageClassSpec = SCS_unspecified; 01112 TSTLoc = TSTNameLoc = StorageClassSpecLoc; 01113 StorageClassSpecLoc = SourceLocation(); 01114 } 01115 // Diagnose if we've recovered from an ill-formed 'auto' storage class 01116 // specifier in a pre-C++11 dialect of C++. 01117 if (!PP.getLangOpts().CPlusPlus11 && TypeSpecType == TST_auto) 01118 Diag(D, TSTLoc, diag::ext_auto_type_specifier); 01119 if (PP.getLangOpts().CPlusPlus && !PP.getLangOpts().CPlusPlus11 && 01120 StorageClassSpec == SCS_auto) 01121 Diag(D, StorageClassSpecLoc, diag::warn_auto_storage_class) 01122 << FixItHint::CreateRemoval(StorageClassSpecLoc); 01123 if (TypeSpecType == TST_char16 || TypeSpecType == TST_char32) 01124 Diag(D, TSTLoc, diag::warn_cxx98_compat_unicode_type) 01125 << (TypeSpecType == TST_char16 ? "char16_t" : "char32_t"); 01126 if (Constexpr_specified) 01127 Diag(D, ConstexprLoc, diag::warn_cxx98_compat_constexpr); 01128 01129 // C++ [class.friend]p6: 01130 // No storage-class-specifier shall appear in the decl-specifier-seq 01131 // of a friend declaration. 01132 if (isFriendSpecified() && 01133 (getStorageClassSpec() || getThreadStorageClassSpec())) { 01134 SmallString<32> SpecName; 01135 SourceLocation SCLoc; 01136 FixItHint StorageHint, ThreadHint; 01137 01138 if (DeclSpec::SCS SC = getStorageClassSpec()) { 01139 SpecName = getSpecifierName(SC); 01140 SCLoc = getStorageClassSpecLoc(); 01141 StorageHint = FixItHint::CreateRemoval(SCLoc); 01142 } 01143 01144 if (DeclSpec::TSCS TSC = getThreadStorageClassSpec()) { 01145 if (!SpecName.empty()) SpecName += " "; 01146 SpecName += getSpecifierName(TSC); 01147 SCLoc = getThreadStorageClassSpecLoc(); 01148 ThreadHint = FixItHint::CreateRemoval(SCLoc); 01149 } 01150 01151 Diag(D, SCLoc, diag::err_friend_decl_spec) 01152 << SpecName << StorageHint << ThreadHint; 01153 01154 ClearStorageClassSpecs(); 01155 } 01156 01157 // C++11 [dcl.fct.spec]p5: 01158 // The virtual specifier shall be used only in the initial 01159 // declaration of a non-static class member function; 01160 // C++11 [dcl.fct.spec]p6: 01161 // The explicit specifier shall be used only in the declaration of 01162 // a constructor or conversion function within its class 01163 // definition; 01164 if (isFriendSpecified() && (isVirtualSpecified() || isExplicitSpecified())) { 01165 StringRef Keyword; 01166 SourceLocation SCLoc; 01167 01168 if (isVirtualSpecified()) { 01169 Keyword = "virtual"; 01170 SCLoc = getVirtualSpecLoc(); 01171 } else { 01172 Keyword = "explicit"; 01173 SCLoc = getExplicitSpecLoc(); 01174 } 01175 01176 FixItHint Hint = FixItHint::CreateRemoval(SCLoc); 01177 Diag(D, SCLoc, diag::err_friend_decl_spec) 01178 << Keyword << Hint; 01179 01180 FS_virtual_specified = FS_explicit_specified = false; 01181 FS_virtualLoc = FS_explicitLoc = SourceLocation(); 01182 } 01183 01184 assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType)); 01185 01186 // Okay, now we can infer the real type. 01187 01188 // TODO: return "auto function" and other bad things based on the real type. 01189 01190 // 'data definition has no type or storage class'? 01191 } 01192 01193 bool DeclSpec::isMissingDeclaratorOk() { 01194 TST tst = getTypeSpecType(); 01195 return isDeclRep(tst) && getRepAsDecl() != nullptr && 01196 StorageClassSpec != DeclSpec::SCS_typedef; 01197 } 01198 01199 void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc, 01200 OverloadedOperatorKind Op, 01201 SourceLocation SymbolLocations[3]) { 01202 Kind = IK_OperatorFunctionId; 01203 StartLocation = OperatorLoc; 01204 EndLocation = OperatorLoc; 01205 OperatorFunctionId.Operator = Op; 01206 for (unsigned I = 0; I != 3; ++I) { 01207 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding(); 01208 01209 if (SymbolLocations[I].isValid()) 01210 EndLocation = SymbolLocations[I]; 01211 } 01212 } 01213 01214 bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc, 01215 const char *&PrevSpec) { 01216 LastLocation = Loc; 01217 01218 if (Specifiers & VS) { 01219 PrevSpec = getSpecifierName(VS); 01220 return true; 01221 } 01222 01223 Specifiers |= VS; 01224 01225 switch (VS) { 01226 default: llvm_unreachable("Unknown specifier!"); 01227 case VS_Override: VS_overrideLoc = Loc; break; 01228 case VS_Sealed: 01229 case VS_Final: VS_finalLoc = Loc; break; 01230 } 01231 01232 return false; 01233 } 01234 01235 const char *VirtSpecifiers::getSpecifierName(Specifier VS) { 01236 switch (VS) { 01237 default: llvm_unreachable("Unknown specifier"); 01238 case VS_Override: return "override"; 01239 case VS_Final: return "final"; 01240 case VS_Sealed: return "sealed"; 01241 } 01242 }