diff options
Diffstat (limited to 'contrib/llvm/tools/clang/lib/Sema/SemaExpr.cpp')
-rw-r--r-- | contrib/llvm/tools/clang/lib/Sema/SemaExpr.cpp | 2250 |
1 files changed, 1309 insertions, 941 deletions
diff --git a/contrib/llvm/tools/clang/lib/Sema/SemaExpr.cpp b/contrib/llvm/tools/clang/lib/Sema/SemaExpr.cpp index e1f65f4..35dad82 100644 --- a/contrib/llvm/tools/clang/lib/Sema/SemaExpr.cpp +++ b/contrib/llvm/tools/clang/lib/Sema/SemaExpr.cpp @@ -59,8 +59,8 @@ bool Sema::CanUseDecl(NamedDecl *D) { // If the function has a deduced return type, and we can't deduce it, // then we can't use it either. - if (getLangOpts().CPlusPlus1y && FD->getResultType()->isUndeducedType() && - DeduceReturnType(FD, SourceLocation(), /*Diagnose*/false)) + if (getLangOpts().CPlusPlus1y && FD->getReturnType()->isUndeducedType() && + DeduceReturnType(FD, SourceLocation(), /*Diagnose*/ false)) return false; } @@ -83,9 +83,16 @@ static void DiagnoseUnusedOfDecl(Sema &S, NamedDecl *D, SourceLocation Loc) { static AvailabilityResult DiagnoseAvailabilityOfDecl(Sema &S, NamedDecl *D, SourceLocation Loc, - const ObjCInterfaceDecl *UnknownObjCClass) { + const ObjCInterfaceDecl *UnknownObjCClass, + bool ObjCPropertyAccess) { // See if this declaration is unavailable or deprecated. std::string Message; + + // Forward class declarations get their attributes from their definition. + if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(D)) { + if (IDecl->getDefinition()) + D = IDecl->getDefinition(); + } AvailabilityResult Result = D->getAvailability(&Message); if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) if (Result == AR_Available) { @@ -94,11 +101,11 @@ static AvailabilityResult DiagnoseAvailabilityOfDecl(Sema &S, Result = TheEnumDecl->getAvailability(&Message); } - const ObjCPropertyDecl *ObjCPDecl = 0; + const ObjCPropertyDecl *ObjCPDecl = nullptr; if (Result == AR_Deprecated || Result == AR_Unavailable) { if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { if (const ObjCPropertyDecl *PD = MD->findPropertyDecl()) { - AvailabilityResult PDeclResult = PD->getAvailability(0); + AvailabilityResult PDeclResult = PD->getAvailability(nullptr); if (PDeclResult == Result) ObjCPDecl = PD; } @@ -111,32 +118,19 @@ static AvailabilityResult DiagnoseAvailabilityOfDecl(Sema &S, break; case AR_Deprecated: - S.EmitDeprecationWarning(D, Message, Loc, UnknownObjCClass, ObjCPDecl); + if (S.getCurContextAvailability() != AR_Deprecated) + S.EmitAvailabilityWarning(Sema::AD_Deprecation, + D, Message, Loc, UnknownObjCClass, ObjCPDecl, + ObjCPropertyAccess); break; - + case AR_Unavailable: - if (S.getCurContextAvailability() != AR_Unavailable) { - if (Message.empty()) { - if (!UnknownObjCClass) { - S.Diag(Loc, diag::err_unavailable) << D->getDeclName(); - if (ObjCPDecl) - S.Diag(ObjCPDecl->getLocation(), diag::note_property_attribute) - << ObjCPDecl->getDeclName() << 1; - } - else - S.Diag(Loc, diag::warn_unavailable_fwdclass_message) - << D->getDeclName(); - } - else - S.Diag(Loc, diag::err_unavailable_message) - << D->getDeclName() << Message; - S.Diag(D->getLocation(), diag::note_unavailable_here) - << isa<FunctionDecl>(D) << false; - if (ObjCPDecl) - S.Diag(ObjCPDecl->getLocation(), diag::note_property_attribute) - << ObjCPDecl->getDeclName() << 1; - } + if (S.getCurContextAvailability() != AR_Unavailable) + S.EmitAvailabilityWarning(Sema::AD_Unavailable, + D, Message, Loc, UnknownObjCClass, ObjCPDecl, + ObjCPropertyAccess); break; + } return Result; } @@ -176,16 +170,14 @@ void Sema::NoteDeletedFunction(FunctionDecl *Decl) { } } - Diag(Decl->getLocation(), diag::note_unavailable_here) - << 1 << true; + Diag(Decl->getLocation(), diag::note_availability_specified_here) + << Decl << true; } /// \brief Determine whether a FunctionDecl was ever declared with an /// explicit storage class. static bool hasAnyExplicitStorageClass(const FunctionDecl *D) { - for (FunctionDecl::redecl_iterator I = D->redecls_begin(), - E = D->redecls_end(); - I != E; ++I) { + for (auto I : D->redecls()) { if (I->getStorageClass() != SC_None) return true; } @@ -234,15 +226,14 @@ static void diagnoseUseOfInternalDeclInInlineFunction(Sema &S, if (!DowngradeWarning && UsedFn) DowngradeWarning = UsedFn->isInlined() || UsedFn->hasAttr<ConstAttr>(); - S.Diag(Loc, DowngradeWarning ? diag::ext_internal_in_extern_inline - : diag::warn_internal_in_extern_inline) + S.Diag(Loc, DowngradeWarning ? diag::ext_internal_in_extern_inline_quiet + : diag::ext_internal_in_extern_inline) << /*IsVar=*/!UsedFn << D; S.MaybeSuggestAddingStaticToDecl(Current); - S.Diag(D->getCanonicalDecl()->getLocation(), - diag::note_internal_decl_declared_here) - << D; + S.Diag(D->getCanonicalDecl()->getLocation(), diag::note_entity_declared_at) + << D; } void Sema::MaybeSuggestAddingStaticToDecl(const FunctionDecl *Cur) { @@ -269,7 +260,8 @@ void Sema::MaybeSuggestAddingStaticToDecl(const FunctionDecl *Cur) { /// referenced), false otherwise. /// bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc, - const ObjCInterfaceDecl *UnknownObjCClass) { + const ObjCInterfaceDecl *UnknownObjCClass, + bool ObjCPropertyAccess) { if (getLangOpts().CPlusPlus && isa<FunctionDecl>(D)) { // If there were any diagnostics suppressed by template argument deduction, // emit them now. @@ -279,13 +271,18 @@ bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc, SmallVectorImpl<PartialDiagnosticAt> &Suppressed = Pos->second; for (unsigned I = 0, N = Suppressed.size(); I != N; ++I) Diag(Suppressed[I].first, Suppressed[I].second); - + // Clear out the list of suppressed diagnostics, so that we don't emit // them again for this specialization. However, we don't obsolete this // entry from the table, because we want to avoid ever emitting these // diagnostics again. Suppressed.clear(); } + + // C++ [basic.start.main]p3: + // The function 'main' shall not be used within a program. + if (cast<FunctionDecl>(D)->isMain()) + Diag(Loc, diag::ext_main_used); } // See if this is an auto-typed variable whose initializer we are parsing. @@ -305,11 +302,11 @@ bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc, // If the function has a deduced return type, and we can't deduce it, // then we can't use it either. - if (getLangOpts().CPlusPlus1y && FD->getResultType()->isUndeducedType() && + if (getLangOpts().CPlusPlus1y && FD->getReturnType()->isUndeducedType() && DeduceReturnType(FD, Loc)) return true; } - DiagnoseAvailabilityOfDecl(*this, D, Loc, UnknownObjCClass); + DiagnoseAvailabilityOfDecl(*this, D, Loc, UnknownObjCClass, ObjCPropertyAccess); DiagnoseUnusedOfDecl(*this, D, Loc); @@ -354,7 +351,7 @@ void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc, calleeType = CT_Function; } else if (isa<VarDecl>(D)) { QualType type = cast<ValueDecl>(D)->getType(); - const FunctionType *fn = 0; + const FunctionType *fn = nullptr; if (const PointerType *ptr = type->getAs<PointerType>()) { fn = ptr->getPointeeType()->getAs<FunctionType>(); if (!fn) return; @@ -367,7 +364,7 @@ void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc, } if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fn)) { - numFormalParams = proto->getNumArgs(); + numFormalParams = proto->getNumParams(); } else { numFormalParams = 0; } @@ -438,16 +435,22 @@ ExprResult Sema::DefaultFunctionArrayConversion(Expr *E) { if (E->getType()->isPlaceholderType()) { ExprResult result = CheckPlaceholderExpr(E); if (result.isInvalid()) return ExprError(); - E = result.take(); + E = result.get(); } QualType Ty = E->getType(); assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); - if (Ty->isFunctionType()) + if (Ty->isFunctionType()) { + // If we are here, we are not calling a function but taking + // its address (which is not allowed in OpenCL v1.0 s6.8.a.3). + if (getLangOpts().OpenCL) { + Diag(E->getExprLoc(), diag::err_opencl_taking_function_address); + return ExprError(); + } E = ImpCastExprToType(E, Context.getPointerType(Ty), - CK_FunctionToPointerDecay).take(); - else if (Ty->isArrayType()) { + CK_FunctionToPointerDecay).get(); + } else if (Ty->isArrayType()) { // In C90 mode, arrays only promote to pointers if the array expression is // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has // type 'array of type' is converted to an expression that has type 'pointer @@ -461,9 +464,9 @@ ExprResult Sema::DefaultFunctionArrayConversion(Expr *E) { // if (getLangOpts().C99 || getLangOpts().CPlusPlus || E->isLValue()) E = ImpCastExprToType(E, Context.getArrayDecayedType(Ty), - CK_ArrayToPointerDecay).take(); + CK_ArrayToPointerDecay).get(); } - return Owned(E); + return E; } static void CheckForNullPointerDereference(Sema &S, Expr *E) { @@ -503,7 +506,7 @@ static void DiagnoseDirectIsaAccess(Sema &S, const ObjCIvarRefExpr *OIRE, BaseType = BaseType->getPointeeType(); if (const ObjCObjectType *OTy = BaseType->getAs<ObjCObjectType>()) if (ObjCInterfaceDecl *IDecl = OTy->getInterface()) { - ObjCInterfaceDecl *ClassDeclared = 0; + ObjCInterfaceDecl *ClassDeclared = nullptr; ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared); if (!ClassDeclared->getSuperClass() && (*ClassDeclared->ivar_begin()) == IV) { @@ -546,13 +549,13 @@ ExprResult Sema::DefaultLvalueConversion(Expr *E) { if (E->getType()->isPlaceholderType()) { ExprResult result = CheckPlaceholderExpr(E); if (result.isInvalid()) return ExprError(); - E = result.take(); + E = result.get(); } // C++ [conv.lval]p1: // A glvalue of a non-function, non-array type T can be // converted to a prvalue. - if (!E->isGLValue()) return Owned(E); + if (!E->isGLValue()) return E; QualType T = E->getType(); assert(!T.isNull() && "r-value conversion on typeless expression?"); @@ -563,7 +566,7 @@ ExprResult Sema::DefaultLvalueConversion(Expr *E) { (E->getType() == Context.OverloadTy || T->isDependentType() || T->isRecordType())) - return Owned(E); + return E; // The C standard is actually really unclear on this point, and // DR106 tells us what the result should be but not why. It's @@ -571,7 +574,7 @@ ExprResult Sema::DefaultLvalueConversion(Expr *E) { // lvalue-to-rvalue at all. Note that expressions of unqualified // 'void' type are never l-values, but qualified void can be. if (T->isVoidType()) - return Owned(E); + return E; // OpenCL usually rejects direct accesses to values of 'half' type. if (getLangOpts().OpenCL && !getOpenCLOptions().cl_khr_fp16 && @@ -596,8 +599,8 @@ ExprResult Sema::DefaultLvalueConversion(Expr *E) { } else if (const ObjCIvarRefExpr *OIRE = dyn_cast<ObjCIvarRefExpr>(E->IgnoreParenCasts())) - DiagnoseDirectIsaAccess(*this, OIRE, SourceLocation(), /* Expr*/0); - + DiagnoseDirectIsaAccess(*this, OIRE, SourceLocation(), /* Expr*/nullptr); + // C++ [conv.lval]p1: // [...] If T is a non-class type, the type of the prvalue is the // cv-unqualified version of T. Otherwise, the type of the @@ -618,16 +621,16 @@ ExprResult Sema::DefaultLvalueConversion(Expr *E) { E->getType().getObjCLifetime() == Qualifiers::OCL_Weak) ExprNeedsCleanups = true; - ExprResult Res = Owned(ImplicitCastExpr::Create(Context, T, CK_LValueToRValue, - E, 0, VK_RValue)); + ExprResult Res = ImplicitCastExpr::Create(Context, T, CK_LValueToRValue, E, + nullptr, VK_RValue); // C11 6.3.2.1p2: // ... if the lvalue has atomic type, the value has the non-atomic version // of the type of the lvalue ... if (const AtomicType *Atomic = T->getAs<AtomicType>()) { T = Atomic->getValueType().getUnqualifiedType(); - Res = Owned(ImplicitCastExpr::Create(Context, T, CK_AtomicToNonAtomic, - Res.get(), 0, VK_RValue)); + Res = ImplicitCastExpr::Create(Context, T, CK_AtomicToNonAtomic, Res.get(), + nullptr, VK_RValue); } return Res; @@ -637,12 +640,30 @@ ExprResult Sema::DefaultFunctionArrayLvalueConversion(Expr *E) { ExprResult Res = DefaultFunctionArrayConversion(E); if (Res.isInvalid()) return ExprError(); - Res = DefaultLvalueConversion(Res.take()); + Res = DefaultLvalueConversion(Res.get()); if (Res.isInvalid()) return ExprError(); return Res; } +/// CallExprUnaryConversions - a special case of an unary conversion +/// performed on a function designator of a call expression. +ExprResult Sema::CallExprUnaryConversions(Expr *E) { + QualType Ty = E->getType(); + ExprResult Res = E; + // Only do implicit cast for a function type, but not for a pointer + // to function type. + if (Ty->isFunctionType()) { + Res = ImpCastExprToType(E, Context.getPointerType(Ty), + CK_FunctionToPointerDecay).get(); + if (Res.isInvalid()) + return ExprError(); + } + Res = DefaultLvalueConversion(Res.get()); + if (Res.isInvalid()) + return ExprError(); + return Res.get(); +} /// UsualUnaryConversions - Performs various conversions that are common to most /// operators (C99 6.3). The conversions of array and function types are @@ -654,14 +675,14 @@ ExprResult Sema::UsualUnaryConversions(Expr *E) { ExprResult Res = DefaultFunctionArrayLvalueConversion(E); if (Res.isInvalid()) return ExprError(); - E = Res.take(); + E = Res.get(); QualType Ty = E->getType(); assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); // Half FP have to be promoted to float unless it is natively supported if (Ty->isHalfType() && !getLangOpts().NativeHalfType) - return ImpCastExprToType(Res.take(), Context.FloatTy, CK_FloatingCast); + return ImpCastExprToType(Res.get(), Context.FloatTy, CK_FloatingCast); // Try to perform integral promotions if the object has a theoretically // promotable type. @@ -682,16 +703,16 @@ ExprResult Sema::UsualUnaryConversions(Expr *E) { QualType PTy = Context.isPromotableBitField(E); if (!PTy.isNull()) { - E = ImpCastExprToType(E, PTy, CK_IntegralCast).take(); - return Owned(E); + E = ImpCastExprToType(E, PTy, CK_IntegralCast).get(); + return E; } if (Ty->isPromotableIntegerType()) { QualType PT = Context.getPromotedIntegerType(Ty); - E = ImpCastExprToType(E, PT, CK_IntegralCast).take(); - return Owned(E); + E = ImpCastExprToType(E, PT, CK_IntegralCast).get(); + return E; } } - return Owned(E); + return E; } /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that @@ -705,14 +726,14 @@ ExprResult Sema::DefaultArgumentPromotion(Expr *E) { ExprResult Res = UsualUnaryConversions(E); if (Res.isInvalid()) return ExprError(); - E = Res.take(); + E = Res.get(); // If this is a 'float' or '__fp16' (CVR qualified or typedef) promote to // double. const BuiltinType *BTy = Ty->getAs<BuiltinType>(); if (BTy && (BTy->getKind() == BuiltinType::Half || BTy->getKind() == BuiltinType::Float)) - E = ImpCastExprToType(E, Context.DoubleTy, CK_FloatingCast).take(); + E = ImpCastExprToType(E, Context.DoubleTy, CK_FloatingCast).get(); // C++ performs lvalue-to-rvalue conversion as a default argument // promotion, even on class types, but note: @@ -728,14 +749,13 @@ ExprResult Sema::DefaultArgumentPromotion(Expr *E) { if (getLangOpts().CPlusPlus && E->isGLValue() && !isUnevaluatedContext()) { ExprResult Temp = PerformCopyInitialization( InitializedEntity::InitializeTemporary(E->getType()), - E->getExprLoc(), - Owned(E)); + E->getExprLoc(), E); if (Temp.isInvalid()) return ExprError(); E = Temp.get(); } - return Owned(E); + return E; } /// Determine the degree of POD-ness for an expression. @@ -792,19 +812,25 @@ void Sema::checkVariadicArgument(const Expr *E, VariadicCallType CT) { // Complain about passing non-POD types through varargs. switch (VAK) { - case VAK_Valid: - break; - case VAK_ValidInCXX11: DiagRuntimeBehavior( - E->getLocStart(), 0, + E->getLocStart(), nullptr, PDiag(diag::warn_cxx98_compat_pass_non_pod_arg_to_vararg) - << E->getType() << CT); + << Ty << CT); + // Fall through. + case VAK_Valid: + if (Ty->isRecordType()) { + // This is unlikely to be what the user intended. If the class has a + // 'c_str' member function, the user probably meant to call that. + DiagRuntimeBehavior(E->getLocStart(), nullptr, + PDiag(diag::warn_pass_class_arg_to_vararg) + << Ty << CT << hasCStrMethod(E) << ".c_str()"); + } break; case VAK_Undefined: DiagRuntimeBehavior( - E->getLocStart(), 0, + E->getLocStart(), nullptr, PDiag(diag::warn_cannot_pass_non_pod_arg_to_vararg) << getLangOpts().CPlusPlus11 << Ty << CT); break; @@ -812,7 +838,7 @@ void Sema::checkVariadicArgument(const Expr *E, VariadicCallType CT) { case VAK_Invalid: if (Ty->isObjCObjectType()) DiagRuntimeBehavior( - E->getLocStart(), 0, + E->getLocStart(), nullptr, PDiag(diag::err_cannot_pass_objc_interface_to_vararg) << Ty << CT); else @@ -838,14 +864,14 @@ ExprResult Sema::DefaultVariadicArgumentPromotion(Expr *E, VariadicCallType CT, ExprResult ExprRes = CheckPlaceholderExpr(E); if (ExprRes.isInvalid()) return ExprError(); - E = ExprRes.take(); + E = ExprRes.get(); } } ExprResult ExprRes = DefaultArgumentPromotion(E); if (ExprRes.isInvalid()) return ExprError(); - E = ExprRes.take(); + E = ExprRes.get(); // Diagnostics regarding non-POD argument types are // emitted along with format string checking in Sema::CheckFunctionCall(). @@ -879,7 +905,7 @@ ExprResult Sema::DefaultVariadicArgumentPromotion(Expr *E, VariadicCallType CT, diag::err_call_incomplete_argument)) return ExprError(); - return Owned(E); + return E; } /// \brief Converts an integer to complex float type. Helper function of @@ -896,12 +922,12 @@ static bool handleIntegerToComplexFloatConversion(Sema &S, ExprResult &IntExpr, if (SkipCast) return false; if (IntTy->isIntegerType()) { QualType fpTy = cast<ComplexType>(ComplexTy)->getElementType(); - IntExpr = S.ImpCastExprToType(IntExpr.take(), fpTy, CK_IntegralToFloating); - IntExpr = S.ImpCastExprToType(IntExpr.take(), ComplexTy, + IntExpr = S.ImpCastExprToType(IntExpr.get(), fpTy, CK_IntegralToFloating); + IntExpr = S.ImpCastExprToType(IntExpr.get(), ComplexTy, CK_FloatingRealToComplex); } else { assert(IntTy->isComplexIntegerType()); - IntExpr = S.ImpCastExprToType(IntExpr.take(), ComplexTy, + IntExpr = S.ImpCastExprToType(IntExpr.get(), ComplexTy, CK_IntegralComplexToFloatingComplex); } return false; @@ -919,12 +945,12 @@ handleComplexFloatToComplexFloatConverstion(Sema &S, ExprResult &LHS, if (order < 0) { // _Complex float -> _Complex double if (!IsCompAssign) - LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_FloatingComplexCast); + LHS = S.ImpCastExprToType(LHS.get(), RHSType, CK_FloatingComplexCast); return RHSType; } if (order > 0) // _Complex float -> _Complex double - RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_FloatingComplexCast); + RHS = S.ImpCastExprToType(RHS.get(), LHSType, CK_FloatingComplexCast); return LHSType; } @@ -945,8 +971,8 @@ static QualType handleOtherComplexFloatConversion(Sema &S, // float -> _Complex double if (ConvertOtherExpr) { QualType fp = cast<ComplexType>(ComplexTy)->getElementType(); - OtherExpr = S.ImpCastExprToType(OtherExpr.take(), fp, CK_FloatingCast); - OtherExpr = S.ImpCastExprToType(OtherExpr.take(), ComplexTy, + OtherExpr = S.ImpCastExprToType(OtherExpr.get(), fp, CK_FloatingCast); + OtherExpr = S.ImpCastExprToType(OtherExpr.get(), ComplexTy, CK_FloatingRealToComplex); } return ComplexTy; @@ -958,12 +984,12 @@ static QualType handleOtherComplexFloatConversion(Sema &S, // double -> _Complex double if (ConvertOtherExpr) - OtherExpr = S.ImpCastExprToType(OtherExpr.take(), result, + OtherExpr = S.ImpCastExprToType(OtherExpr.get(), result, CK_FloatingRealToComplex); // _Complex float -> _Complex double if (ConvertComplexExpr && order < 0) - ComplexExpr = S.ImpCastExprToType(ComplexExpr.take(), result, + ComplexExpr = S.ImpCastExprToType(ComplexExpr.get(), result, CK_FloatingComplexCast); return result; @@ -1025,7 +1051,7 @@ static QualType handleIntToFloatConversion(Sema &S, ExprResult &FloatExpr, if (IntTy->isIntegerType()) { if (ConvertInt) // Convert intExpr to the lhs floating point type. - IntExpr = S.ImpCastExprToType(IntExpr.take(), FloatTy, + IntExpr = S.ImpCastExprToType(IntExpr.get(), FloatTy, CK_IntegralToFloating); return FloatTy; } @@ -1036,12 +1062,12 @@ static QualType handleIntToFloatConversion(Sema &S, ExprResult &FloatExpr, // _Complex int -> _Complex float if (ConvertInt) - IntExpr = S.ImpCastExprToType(IntExpr.take(), result, + IntExpr = S.ImpCastExprToType(IntExpr.get(), result, CK_IntegralComplexToFloatingComplex); // float -> _Complex float if (ConvertFloat) - FloatExpr = S.ImpCastExprToType(FloatExpr.take(), result, + FloatExpr = S.ImpCastExprToType(FloatExpr.get(), result, CK_FloatingRealToComplex); return result; @@ -1060,13 +1086,13 @@ static QualType handleFloatConversion(Sema &S, ExprResult &LHS, if (LHSFloat && RHSFloat) { int order = S.Context.getFloatingTypeOrder(LHSType, RHSType); if (order > 0) { - RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_FloatingCast); + RHS = S.ImpCastExprToType(RHS.get(), LHSType, CK_FloatingCast); return LHSType; } assert(order < 0 && "illegal float comparison"); if (!IsCompAssign) - LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_FloatingCast); + LHS = S.ImpCastExprToType(LHS.get(), RHSType, CK_FloatingCast); return RHSType; } @@ -1108,29 +1134,29 @@ static QualType handleIntegerConversion(Sema &S, ExprResult &LHS, if (LHSSigned == RHSSigned) { // Same signedness; use the higher-ranked type if (order >= 0) { - RHS = (*doRHSCast)(S, RHS.take(), LHSType); + RHS = (*doRHSCast)(S, RHS.get(), LHSType); return LHSType; } else if (!IsCompAssign) - LHS = (*doLHSCast)(S, LHS.take(), RHSType); + LHS = (*doLHSCast)(S, LHS.get(), RHSType); return RHSType; } else if (order != (LHSSigned ? 1 : -1)) { // The unsigned type has greater than or equal rank to the // signed type, so use the unsigned type if (RHSSigned) { - RHS = (*doRHSCast)(S, RHS.take(), LHSType); + RHS = (*doRHSCast)(S, RHS.get(), LHSType); return LHSType; } else if (!IsCompAssign) - LHS = (*doLHSCast)(S, LHS.take(), RHSType); + LHS = (*doLHSCast)(S, LHS.get(), RHSType); return RHSType; } else if (S.Context.getIntWidth(LHSType) != S.Context.getIntWidth(RHSType)) { // The two types are different widths; if we are here, that // means the signed type is larger than the unsigned type, so // use the signed type. if (LHSSigned) { - RHS = (*doRHSCast)(S, RHS.take(), LHSType); + RHS = (*doRHSCast)(S, RHS.get(), LHSType); return LHSType; } else if (!IsCompAssign) - LHS = (*doLHSCast)(S, LHS.take(), RHSType); + LHS = (*doLHSCast)(S, LHS.get(), RHSType); return RHSType; } else { // The signed type is higher-ranked than the unsigned type, @@ -1139,9 +1165,9 @@ static QualType handleIntegerConversion(Sema &S, ExprResult &LHS, // to the signed type. QualType result = S.Context.getCorrespondingUnsignedType(LHSSigned ? LHSType : RHSType); - RHS = (*doRHSCast)(S, RHS.take(), result); + RHS = (*doRHSCast)(S, RHS.get(), result); if (!IsCompAssign) - LHS = (*doLHSCast)(S, LHS.take(), result); + LHS = (*doLHSCast)(S, LHS.get(), result); return result; } } @@ -1171,7 +1197,7 @@ static QualType handleComplexIntConversion(Sema &S, ExprResult &LHS, handleIntegerConversion<doComplexIntegralCast, doIntegralCast> (S, LHS, RHS, LHSEltType, RHSType, IsCompAssign); QualType ComplexType = S.Context.getComplexType(ScalarType); - RHS = S.ImpCastExprToType(RHS.take(), ComplexType, + RHS = S.ImpCastExprToType(RHS.get(), ComplexType, CK_IntegralRealToComplex); return ComplexType; @@ -1186,7 +1212,7 @@ static QualType handleComplexIntConversion(Sema &S, ExprResult &LHS, QualType ComplexType = S.Context.getComplexType(ScalarType); if (!IsCompAssign) - LHS = S.ImpCastExprToType(LHS.take(), ComplexType, + LHS = S.ImpCastExprToType(LHS.get(), ComplexType, CK_IntegralRealToComplex); return ComplexType; } @@ -1198,12 +1224,12 @@ static QualType handleComplexIntConversion(Sema &S, ExprResult &LHS, QualType Sema::UsualArithmeticConversions(ExprResult &LHS, ExprResult &RHS, bool IsCompAssign) { if (!IsCompAssign) { - LHS = UsualUnaryConversions(LHS.take()); + LHS = UsualUnaryConversions(LHS.get()); if (LHS.isInvalid()) return QualType(); } - RHS = UsualUnaryConversions(RHS.take()); + RHS = UsualUnaryConversions(RHS.get()); if (RHS.isInvalid()) return QualType(); @@ -1235,7 +1261,7 @@ QualType Sema::UsualArithmeticConversions(ExprResult &LHS, ExprResult &RHS, if (!LHSBitfieldPromoteTy.isNull()) LHSType = LHSBitfieldPromoteTy; if (LHSType != LHSUnpromotedType && !IsCompAssign) - LHS = ImpCastExprToType(LHS.take(), LHSType, CK_IntegralCast); + LHS = ImpCastExprToType(LHS.get(), LHSType, CK_IntegralCast); // If both types are identical, no conversion is needed. if (LHSType == RHSType) @@ -1284,7 +1310,7 @@ Sema::ActOnGenericSelectionExpr(SourceLocation KeyLoc, if (ArgTypes[i]) (void) GetTypeFromParser(ArgTypes[i], &Types[i]); else - Types[i] = 0; + Types[i] = nullptr; } ExprResult ER = CreateGenericSelectionExpr(KeyLoc, DefaultLoc, RParenLoc, @@ -1307,7 +1333,7 @@ Sema::CreateGenericSelectionExpr(SourceLocation KeyLoc, if (ControllingExpr->getType()->isPlaceholderType()) { ExprResult result = CheckPlaceholderExpr(ControllingExpr); if (result.isInvalid()) return ExprError(); - ControllingExpr = result.take(); + ControllingExpr = result.get(); } bool TypeErrorFound = false, @@ -1369,10 +1395,9 @@ Sema::CreateGenericSelectionExpr(SourceLocation KeyLoc, // If we determined that the generic selection is result-dependent, don't // try to compute the result expression. if (IsResultDependent) - return Owned(new (Context) GenericSelectionExpr( - Context, KeyLoc, ControllingExpr, - Types, Exprs, - DefaultLoc, RParenLoc, ContainsUnexpandedParameterPack)); + return new (Context) GenericSelectionExpr( + Context, KeyLoc, ControllingExpr, Types, Exprs, DefaultLoc, RParenLoc, + ContainsUnexpandedParameterPack); SmallVector<unsigned, 1> CompatIndices; unsigned DefaultIndex = -1U; @@ -1424,11 +1449,9 @@ Sema::CreateGenericSelectionExpr(SourceLocation KeyLoc, unsigned ResultIndex = CompatIndices.size() ? CompatIndices[0] : DefaultIndex; - return Owned(new (Context) GenericSelectionExpr( - Context, KeyLoc, ControllingExpr, - Types, Exprs, - DefaultLoc, RParenLoc, ContainsUnexpandedParameterPack, - ResultIndex)); + return new (Context) GenericSelectionExpr( + Context, KeyLoc, ControllingExpr, Types, Exprs, DefaultLoc, RParenLoc, + ContainsUnexpandedParameterPack, ResultIndex); } /// getUDSuffixLoc - Create a SourceLocation for a ud-suffix, given the @@ -1476,16 +1499,15 @@ static ExprResult BuildCookedLiteralOperatorCall(Sema &S, Scope *Scope, /// string. /// ExprResult -Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks, - Scope *UDLScope) { - assert(NumStringToks && "Must have at least one string!"); +Sema::ActOnStringLiteral(ArrayRef<Token> StringToks, Scope *UDLScope) { + assert(!StringToks.empty() && "Must have at least one string!"); - StringLiteralParser Literal(StringToks, NumStringToks, PP); + StringLiteralParser Literal(StringToks, PP); if (Literal.hadError) return ExprError(); SmallVector<SourceLocation, 4> StringTokLocs; - for (unsigned i = 0; i != NumStringToks; ++i) + for (unsigned i = 0; i != StringToks.size(); ++i) StringTokLocs.push_back(StringToks[i].getLocation()); QualType CharTy = Context.CharTy; @@ -1528,7 +1550,7 @@ Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks, &StringTokLocs[0], StringTokLocs.size()); if (Literal.getUDSuffix().empty()) - return Owned(Lit); + return Lit; // We're building a user-defined literal. IdentifierInfo *UDSuffix = &Context.Idents.get(Literal.getUDSuffix()); @@ -1643,7 +1665,7 @@ Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, NameInfo.getLoc(), Ty, VK, FoundD, TemplateArgs); } else { assert(!TemplateArgs && "No template arguments for non-variable" - " template specialization referrences"); + " template specialization references"); E = DeclRefExpr::Create( Context, SS ? SS->getWithLocInContext(Context) : NestedNameSpecifierLoc(), @@ -1653,20 +1675,16 @@ Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, MarkDeclRefReferenced(E); if (getLangOpts().ObjCARCWeak && isa<VarDecl>(D) && - Ty.getObjCLifetime() == Qualifiers::OCL_Weak) { - DiagnosticsEngine::Level Level = - Diags.getDiagnosticLevel(diag::warn_arc_repeated_use_of_weak, - E->getLocStart()); - if (Level != DiagnosticsEngine::Ignored) + Ty.getObjCLifetime() == Qualifiers::OCL_Weak && + !Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, E->getLocStart())) recordUseOfEvaluatedWeak(E); - } // Just in case we're building an illegal pointer-to-member. FieldDecl *FD = dyn_cast<FieldDecl>(D); if (FD && FD->isBitField()) E->setObjectKind(OK_BitField); - return Owned(E); + return E; } /// Decomposes the given name into a DeclarationNameInfo, its location, and @@ -1697,7 +1715,7 @@ Sema::DecomposeUnqualifiedId(const UnqualifiedId &Id, TemplateArgs = &Buffer; } else { NameInfo = GetNameFromUnqualifiedId(Id); - TemplateArgs = 0; + TemplateArgs = nullptr; } } @@ -1724,7 +1742,7 @@ bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, // original lookup would not have found something because it was a // dependent name. DeclContext *DC = (SS.isEmpty() && !CallsUndergoingInstantiation.empty()) - ? CurContext : 0; + ? CurContext : nullptr; while (DC) { if (isa<CXXRecordDecl>(DC)) { LookupQualifiedName(R, DC); @@ -1748,8 +1766,8 @@ bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, // Give a code modification hint to insert 'this->'. // TODO: fixit for inserting 'Base<T>::' in the other cases. // Actually quite difficult! - if (getLangOpts().MicrosoftMode) - diagnostic = diag::warn_found_via_dependent_bases_lookup; + if (getLangOpts().MSVCCompat) + diagnostic = diag::ext_found_via_dependent_bases_lookup; if (isInstance) { Diag(R.getNameLoc(), diagnostic) << Name << FixItHint::CreateInsertion(R.getNameLoc(), "this->"); @@ -1782,9 +1800,9 @@ bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, CXXDependentScopeMemberExpr::Create( Context, DepThis, DepThisType, true, SourceLocation(), SS.getWithLocInContext(Context), - ULE->getTemplateKeywordLoc(), 0, + ULE->getTemplateKeywordLoc(), nullptr, R.getLookupNameInfo(), - ULE->hasExplicitTemplateArgs() ? &TList : 0); + ULE->hasExplicitTemplateArgs() ? &TList : nullptr); CallsUndergoingInstantiation.back()->setCallee(DepExpr); } else { Diag(R.getNameLoc(), diagnostic) << Name; @@ -1814,7 +1832,7 @@ bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, // function definition declared at class scope then we must set // DC to the lexical parent to be able to search into the parent // class. - if (getLangOpts().MicrosoftMode && isa<FunctionDecl>(DC) && + if (getLangOpts().MSVCCompat && isa<FunctionDecl>(DC) && cast<FunctionDecl>(DC)->getFriendObjectKind() && DC->getLexicalParent()->isRecord()) DC = DC->getLexicalParent(); @@ -1825,7 +1843,7 @@ bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, // We didn't find anything, so try to correct for a typo. TypoCorrection Corrected; if (S && (Corrected = CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), - S, &SS, CCC))) { + S, &SS, CCC, CTK_ErrorRecovery))) { std::string CorrectedStr(Corrected.getAsString(getLangOpts())); bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && Name.getAsString() == CorrectedStr; @@ -1836,7 +1854,8 @@ bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, NamedDecl *ND = Corrected.getCorrectionDecl(); if (ND) { if (Corrected.isOverloaded()) { - OverloadCandidateSet OCS(R.getNameLoc()); + OverloadCandidateSet OCS(R.getNameLoc(), + OverloadCandidateSet::CSK_Normal); OverloadCandidateSet::iterator Best; for (TypoCorrection::decl_iterator CD = Corrected.begin(), CDEnd = Corrected.end(); @@ -1863,6 +1882,17 @@ bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, } } R.addDecl(ND); + if (getLangOpts().CPlusPlus && ND->isCXXClassMember()) { + CXXRecordDecl *Record = nullptr; + if (Corrected.getCorrectionSpecifier()) { + const Type *Ty = Corrected.getCorrectionSpecifier()->getAsType(); + Record = Ty->getAsCXXRecordDecl(); + } + if (!Record) + Record = cast<CXXRecordDecl>( + ND->getDeclContext()->getRedeclContext()); + R.setNamingClass(Record); + } AcceptableWithRecovery = isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND); @@ -1913,6 +1943,53 @@ bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, return true; } +/// In Microsoft mode, if we are inside a template class whose parent class has +/// dependent base classes, and we can't resolve an unqualified identifier, then +/// assume the identifier is a member of a dependent base class. We can only +/// recover successfully in static methods, instance methods, and other contexts +/// where 'this' is available. This doesn't precisely match MSVC's +/// instantiation model, but it's close enough. +static Expr * +recoverFromMSUnqualifiedLookup(Sema &S, ASTContext &Context, + DeclarationNameInfo &NameInfo, + SourceLocation TemplateKWLoc, + const TemplateArgumentListInfo *TemplateArgs) { + // Only try to recover from lookup into dependent bases in static methods or + // contexts where 'this' is available. + QualType ThisType = S.getCurrentThisType(); + const CXXRecordDecl *RD = nullptr; + if (!ThisType.isNull()) + RD = ThisType->getPointeeType()->getAsCXXRecordDecl(); + else if (auto *MD = dyn_cast<CXXMethodDecl>(S.CurContext)) + RD = MD->getParent(); + if (!RD || !RD->hasAnyDependentBases()) + return nullptr; + + // Diagnose this as unqualified lookup into a dependent base class. If 'this' + // is available, suggest inserting 'this->' as a fixit. + SourceLocation Loc = NameInfo.getLoc(); + auto DB = S.Diag(Loc, diag::ext_undeclared_unqual_id_with_dependent_base); + DB << NameInfo.getName() << RD; + + if (!ThisType.isNull()) { + DB << FixItHint::CreateInsertion(Loc, "this->"); + return CXXDependentScopeMemberExpr::Create( + Context, /*This=*/nullptr, ThisType, /*IsArrow=*/true, + /*Op=*/SourceLocation(), NestedNameSpecifierLoc(), TemplateKWLoc, + /*FirstQualifierInScope=*/nullptr, NameInfo, TemplateArgs); + } + + // Synthesize a fake NNS that points to the derived class. This will + // perform name lookup during template instantiation. + CXXScopeSpec SS; + auto *NNS = + NestedNameSpecifier::Create(Context, nullptr, true, RD->getTypeForDecl()); + SS.MakeTrivial(Context, NNS, SourceRange(Loc, Loc)); + return DependentScopeDeclRefExpr::Create( + Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, + TemplateArgs); +} + ExprResult Sema::ActOnIdExpression(Scope *S, CXXScopeSpec &SS, SourceLocation TemplateKWLoc, @@ -2000,77 +2077,59 @@ ExprResult Sema::ActOnIdExpression(Scope *S, if (E.isInvalid()) return ExprError(); - if (Expr *Ex = E.takeAs<Expr>()) - return Owned(Ex); + if (Expr *Ex = E.getAs<Expr>()) + return Ex; } } if (R.isAmbiguous()) return ExprError(); + // This could be an implicitly declared function reference (legal in C90, + // extension in C99, forbidden in C++). + if (R.empty() && HasTrailingLParen && II && !getLangOpts().CPlusPlus) { + NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S); + if (D) R.addDecl(D); + } + // Determine whether this name might be a candidate for // argument-dependent lookup. bool ADL = UseArgumentDependentLookup(SS, R, HasTrailingLParen); if (R.empty() && !ADL) { - - // Otherwise, this could be an implicitly declared function reference (legal - // in C90, extension in C99, forbidden in C++). - if (HasTrailingLParen && II && !getLangOpts().CPlusPlus) { - NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S); - if (D) R.addDecl(D); + if (SS.isEmpty() && getLangOpts().MSVCCompat) { + if (Expr *E = recoverFromMSUnqualifiedLookup(*this, Context, NameInfo, + TemplateKWLoc, TemplateArgs)) + return E; } + // Don't diagnose an empty lookup for inline assembly. + if (IsInlineAsmIdentifier) + return ExprError(); + // If this name wasn't predeclared and if this is not a function // call, diagnose the problem. - if (R.empty()) { - // In Microsoft mode, if we are inside a template class member function - // whose parent class has dependent base classes, and we can't resolve - // an identifier, then assume the identifier is a member of a dependent - // base class. The goal is to postpone name lookup to instantiation time - // to be able to search into the type dependent base classes. - // FIXME: If we want 100% compatibility with MSVC, we will have delay all - // unqualified name lookup. Any name lookup during template parsing means - // clang might find something that MSVC doesn't. For now, we only handle - // the common case of members of a dependent base class. - if (getLangOpts().MicrosoftMode) { - CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext); - if (MD && MD->isInstance() && MD->getParent()->hasAnyDependentBases()) { - assert(SS.isEmpty() && "qualifiers should be already handled"); - QualType ThisType = MD->getThisType(Context); - // Since the 'this' expression is synthesized, we don't need to - // perform the double-lookup check. - NamedDecl *FirstQualifierInScope = 0; - return Owned(CXXDependentScopeMemberExpr::Create( - Context, /*This=*/0, ThisType, /*IsArrow=*/true, - /*Op=*/SourceLocation(), SS.getWithLocInContext(Context), - TemplateKWLoc, FirstQualifierInScope, NameInfo, TemplateArgs)); - } - } + CorrectionCandidateCallback DefaultValidator; + DefaultValidator.IsAddressOfOperand = IsAddressOfOperand; + assert((!CCC || CCC->IsAddressOfOperand == IsAddressOfOperand) && + "Typo correction callback misconfigured"); + if (DiagnoseEmptyLookup(S, SS, R, CCC ? *CCC : DefaultValidator)) + return ExprError(); - // Don't diagnose an empty lookup for inline assmebly. - if (IsInlineAsmIdentifier) - return ExprError(); + assert(!R.empty() && + "DiagnoseEmptyLookup returned false but added no results"); - CorrectionCandidateCallback DefaultValidator; - if (DiagnoseEmptyLookup(S, SS, R, CCC ? *CCC : DefaultValidator)) + // If we found an Objective-C instance variable, let + // LookupInObjCMethod build the appropriate expression to + // reference the ivar. + if (ObjCIvarDecl *Ivar = R.getAsSingle<ObjCIvarDecl>()) { + R.clear(); + ExprResult E(LookupInObjCMethod(R, S, Ivar->getIdentifier())); + // In a hopelessly buggy code, Objective-C instance variable + // lookup fails and no expression will be built to reference it. + if (!E.isInvalid() && !E.get()) return ExprError(); - - assert(!R.empty() && - "DiagnoseEmptyLookup returned false but added no results"); - - // If we found an Objective-C instance variable, let - // LookupInObjCMethod build the appropriate expression to - // reference the ivar. - if (ObjCIvarDecl *Ivar = R.getAsSingle<ObjCIvarDecl>()) { - R.clear(); - ExprResult E(LookupInObjCMethod(R, S, Ivar->getIdentifier())); - // In a hopelessly buggy code, Objective-C instance variable - // lookup fails and no expression will be built to reference it. - if (!E.isInvalid() && !E.get()) - return ExprError(); - return E; - } + return E; } } @@ -2145,11 +2204,12 @@ ExprResult Sema::ActOnIdExpression(Scope *S, ExprResult Sema::BuildQualifiedDeclarationNameExpr(CXXScopeSpec &SS, const DeclarationNameInfo &NameInfo, - bool IsAddressOfOperand) { + bool IsAddressOfOperand, + TypeSourceInfo **RecoveryTSI) { DeclContext *DC = computeDeclContext(SS, false); if (!DC) return BuildDependentDeclRefExpr(SS, /*TemplateKWLoc=*/SourceLocation(), - NameInfo, /*TemplateArgs=*/0); + NameInfo, /*TemplateArgs=*/nullptr); if (RequireCompleteDeclContext(SS, DC)) return ExprError(); @@ -2162,7 +2222,7 @@ Sema::BuildQualifiedDeclarationNameExpr(CXXScopeSpec &SS, if (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation) return BuildDependentDeclRefExpr(SS, /*TemplateKWLoc=*/SourceLocation(), - NameInfo, /*TemplateArgs=*/0); + NameInfo, /*TemplateArgs=*/nullptr); if (R.empty()) { Diag(NameInfo.getLoc(), diag::err_no_member) @@ -2170,6 +2230,41 @@ Sema::BuildQualifiedDeclarationNameExpr(CXXScopeSpec &SS, return ExprError(); } + if (const TypeDecl *TD = R.getAsSingle<TypeDecl>()) { + // Diagnose a missing typename if this resolved unambiguously to a type in + // a dependent context. If we can recover with a type, downgrade this to + // a warning in Microsoft compatibility mode. + unsigned DiagID = diag::err_typename_missing; + if (RecoveryTSI && getLangOpts().MSVCCompat) + DiagID = diag::ext_typename_missing; + SourceLocation Loc = SS.getBeginLoc(); + auto D = Diag(Loc, DiagID); + D << SS.getScopeRep() << NameInfo.getName().getAsString() + << SourceRange(Loc, NameInfo.getEndLoc()); + + // Don't recover if the caller isn't expecting us to or if we're in a SFINAE + // context. + if (!RecoveryTSI) + return ExprError(); + + // Only issue the fixit if we're prepared to recover. + D << FixItHint::CreateInsertion(Loc, "typename "); + + // Recover by pretending this was an elaborated type. + QualType Ty = Context.getTypeDeclType(TD); + TypeLocBuilder TLB; + TLB.pushTypeSpec(Ty).setNameLoc(NameInfo.getLoc()); + + QualType ET = getElaboratedType(ETK_None, SS, Ty); + ElaboratedTypeLoc QTL = TLB.push<ElaboratedTypeLoc>(ET); + QTL.setElaboratedKeywordLoc(SourceLocation()); + QTL.setQualifierLoc(SS.getWithLocInContext(Context)); + + *RecoveryTSI = TLB.getTypeSourceInfo(Context, ET); + + return ExprEmpty(); + } + // Defend against this resolving to an implicit member access. We usually // won't get here if this might be a legitimate a class member (we end up in // BuildMemberReferenceExpr instead), but this can be valid if we're forming @@ -2177,7 +2272,7 @@ Sema::BuildQualifiedDeclarationNameExpr(CXXScopeSpec &SS, if (!R.empty() && (*R.begin())->isCXXClassMember() && !IsAddressOfOperand) return BuildPossibleImplicitMemberExpr(SS, /*TemplateKWLoc=*/SourceLocation(), - R, /*TemplateArgs=*/0); + R, /*TemplateArgs=*/nullptr); return BuildDeclarationNameExpr(SS, R, /* ADL */ false); } @@ -2219,11 +2314,11 @@ Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, else LookForIvars = (Lookup.isSingleResult() && Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()); - ObjCInterfaceDecl *IFace = 0; + ObjCInterfaceDecl *IFace = nullptr; if (LookForIvars) { IFace = CurMethod->getClassInterface(); ObjCInterfaceDecl *ClassDeclared; - ObjCIvarDecl *IV = 0; + ObjCIvarDecl *IV = nullptr; if (IFace && (IV = IFace->lookupInstanceVariable(II, ClassDeclared))) { // Diagnose using an ivar in a class method. if (IsClassMethod) @@ -2258,20 +2353,12 @@ Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, if (SelfExpr.isInvalid()) return ExprError(); - SelfExpr = DefaultLvalueConversion(SelfExpr.take()); + SelfExpr = DefaultLvalueConversion(SelfExpr.get()); if (SelfExpr.isInvalid()) return ExprError(); MarkAnyDeclReferenced(Loc, IV, true); - if (!IV->getBackingIvarReferencedInAccessor()) { - // Mark this ivar 'referenced' in this method, if it is a backing ivar - // of a property and current method is one of its property accessor. - const ObjCPropertyDecl *PDecl; - const ObjCIvarDecl *BIV = GetIvarBackingPropertyAccessor(CurMethod, PDecl); - if (BIV && BIV == IV) - IV->setBackingIvarReferencedInAccessor(true); - } - + ObjCMethodFamily MF = CurMethod->getMethodFamily(); if (MF != OMF_init && MF != OMF_dealloc && MF != OMF_finalize && !IvarBacksCurrentMethodAccessor(IFace, CurMethod, IV)) @@ -2279,14 +2366,12 @@ Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, ObjCIvarRefExpr *Result = new (Context) ObjCIvarRefExpr(IV, IV->getType(), Loc, IV->getLocation(), - SelfExpr.take(), + SelfExpr.get(), true, true); if (getLangOpts().ObjCAutoRefCount) { if (IV->getType().getObjCLifetime() == Qualifiers::OCL_Weak) { - DiagnosticsEngine::Level Level = - Diags.getDiagnosticLevel(diag::warn_arc_repeated_use_of_weak, Loc); - if (Level != DiagnosticsEngine::Ignored) + if (!Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, Loc)) recordUseOfEvaluatedWeak(Result); } if (CurContext->isClosure()) @@ -2294,7 +2379,7 @@ Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, << FixItHint::CreateInsertion(Loc, "self->"); } - return Owned(Result); + return Result; } } else if (CurMethod->isInstanceMethod()) { // We should warn if a local variable hides an ivar. @@ -2327,7 +2412,7 @@ Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, } } // Sentinel value saying that we didn't do anything special. - return Owned((Expr*) 0); + return ExprResult((Expr *)nullptr); } /// \brief Cast a base object to a member's actual type. @@ -2354,7 +2439,7 @@ Sema::PerformObjectMemberConversion(Expr *From, NamedDecl *Member) { CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Member->getDeclContext()); if (!RD) - return Owned(From); + return From; QualType DestRecordType; QualType DestType; @@ -2374,7 +2459,7 @@ Sema::PerformObjectMemberConversion(Expr *From, } } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) { if (Method->isStatic()) - return Owned(From); + return From; DestType = Method->getThisType(Context); DestRecordType = DestType->getPointeeType(); @@ -2388,15 +2473,15 @@ Sema::PerformObjectMemberConversion(Expr *From, } } else { // No conversion necessary. - return Owned(From); + return From; } if (DestType->isDependentType() || FromType->isDependentType()) - return Owned(From); + return From; // If the unqualified types are the same, no conversion is necessary. if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) - return Owned(From); + return From; SourceRange FromRange = From->getSourceRange(); SourceLocation FromLoc = FromRange.getBegin(); @@ -2439,7 +2524,7 @@ Sema::PerformObjectMemberConversion(Expr *From, if (PointerConversions) QType = Context.getPointerType(QType); From = ImpCastExprToType(From, QType, CK_UncheckedDerivedToBase, - VK, &BasePath).take(); + VK, &BasePath).get(); FromType = QType; FromRecordType = QRecordType; @@ -2447,7 +2532,7 @@ Sema::PerformObjectMemberConversion(Expr *From, // If the qualifier type was the same as the destination type, // we're done. if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) - return Owned(From); + return From; } } @@ -2476,7 +2561,7 @@ Sema::PerformObjectMemberConversion(Expr *From, if (PointerConversions) UType = Context.getPointerType(UType); From = ImpCastExprToType(From, UType, CK_UncheckedDerivedToBase, - VK, &BasePath).take(); + VK, &BasePath).get(); FromType = UType; FromRecordType = URecordType; } @@ -2604,7 +2689,7 @@ Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, NeedsADL, R.isOverloadedResult(), R.begin(), R.end()); - return Owned(ULE); + return ULE; } /// \brief Complete semantic analysis for a reference to the given declaration. @@ -2755,7 +2840,7 @@ ExprResult Sema::BuildDeclarationNameExpr( // If we're referring to a function with an __unknown_anytype // result type, make the entire expression __unknown_anytype. - if (fty->getResultType() == Context.UnknownAnyTy) { + if (fty->getReturnType() == Context.UnknownAnyTy) { type = Context.UnknownAnyTy; valueKind = VK_RValue; break; @@ -2774,7 +2859,7 @@ ExprResult Sema::BuildDeclarationNameExpr( // type. if (!cast<FunctionDecl>(VD)->hasPrototype() && isa<FunctionProtoType>(fty)) - type = Context.getFunctionNoProtoType(fty->getResultType(), + type = Context.getFunctionNoProtoType(fty->getReturnType(), fty->getExtInfo()); // Functions are r-values in C. @@ -2792,7 +2877,7 @@ ExprResult Sema::BuildDeclarationNameExpr( // This should only be possible with a type written directly. if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(VD->getType())) - if (proto->getResultType() == Context.UnknownAnyTy) { + if (proto->getReturnType() == Context.UnknownAnyTy) { type = Context.UnknownAnyTy; valueKind = VK_RValue; break; @@ -2820,7 +2905,7 @@ ExprResult Sema::BuildDeclarationNameExpr( ExprResult Sema::BuildPredefinedExpr(SourceLocation Loc, PredefinedExpr::IdentType IT) { // Pick the current block, lambda, captured statement or function. - Decl *currentDecl = 0; + Decl *currentDecl = nullptr; if (const BlockScopeInfo *BSI = getCurBlock()) currentDecl = BSI->TheDecl; else if (const LambdaScopeInfo *LSI = getCurLambda()) @@ -2851,7 +2936,7 @@ ExprResult Sema::BuildPredefinedExpr(SourceLocation Loc, ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); } - return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); + return new (Context) PredefinedExpr(Loc, ResTy, IT); } ExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, tok::TokenKind Kind) { @@ -2862,6 +2947,7 @@ ExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, tok::TokenKind Kind) { case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; case tok::kw___FUNCDNAME__: IT = PredefinedExpr::FuncDName; break; // [MS] + case tok::kw___FUNCSIG__: IT = PredefinedExpr::FuncSig; break; // [MS] case tok::kw_L__FUNCTION__: IT = PredefinedExpr::LFunction; break; case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; } @@ -2905,7 +2991,7 @@ ExprResult Sema::ActOnCharacterConstant(const Token &Tok, Scope *UDLScope) { Tok.getLocation()); if (Literal.getUDSuffix().empty()) - return Owned(Lit); + return Lit; // We're building a user-defined literal. IdentifierInfo *UDSuffix = &Context.Idents.get(Literal.getUDSuffix()); @@ -2924,8 +3010,8 @@ ExprResult Sema::ActOnCharacterConstant(const Token &Tok, Scope *UDLScope) { ExprResult Sema::ActOnIntegerConstant(SourceLocation Loc, uint64_t Val) { unsigned IntSize = Context.getTargetInfo().getIntWidth(); - return Owned(IntegerLiteral::Create(Context, llvm::APInt(IntSize, Val), - Context.IntTy, Loc)); + return IntegerLiteral::Create(Context, llvm::APInt(IntSize, Val), + Context.IntTy, Loc); } static Expr *BuildFloatingLiteral(Sema &S, NumericLiteralParser &Literal, @@ -3090,10 +3176,10 @@ ExprResult Sema::ActOnNumericConstant(const Token &Tok, Scope *UDLScope) { if (Ty == Context.DoubleTy) { if (getLangOpts().SinglePrecisionConstants) { - Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); + Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).get(); } else if (getLangOpts().OpenCL && !getOpenCLOptions().cl_khr_fp64) { Diag(Tok.getLocation(), diag::warn_double_const_requires_fp64); - Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); + Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).get(); } } } else if (!Literal.isIntegerLiteral()) { @@ -3117,8 +3203,8 @@ ExprResult Sema::ActOnNumericConstant(const Token &Tok, Scope *UDLScope) { // may be wider than [u]intmax_t. // FIXME: Actually, they don't. We seem to have accidentally invented the // i128 suffix. - if (Literal.isMicrosoftInteger && MaxWidth < 128 && - PP.getTargetInfo().hasInt128Type()) + if (Literal.MicrosoftInteger == 128 && MaxWidth < 128 && + Context.getTargetInfo().hasInt128Type()) MaxWidth = 128; llvm::APInt ResultVal(MaxWidth, 0); @@ -3138,7 +3224,22 @@ ExprResult Sema::ActOnNumericConstant(const Token &Tok, Scope *UDLScope) { // Check from smallest to largest, picking the smallest type we can. unsigned Width = 0; - if (!Literal.isLong && !Literal.isLongLong) { + + // Microsoft specific integer suffixes are explicitly sized. + if (Literal.MicrosoftInteger) { + if (Literal.MicrosoftInteger > MaxWidth) { + // If this target doesn't support __int128, error and force to ull. + Diag(Tok.getLocation(), diag::err_int128_unsupported); + Width = MaxWidth; + Ty = Context.getIntMaxType(); + } else { + Width = Literal.MicrosoftInteger; + Ty = Context.getIntTypeForBitwidth(Width, + /*Signed=*/!Literal.isUnsigned); + } + } + + if (Ty.isNull() && !Literal.isLong && !Literal.isLongLong) { // Are int/unsigned possibilities? unsigned IntSize = Context.getTargetInfo().getIntWidth(); @@ -3185,22 +3286,11 @@ ExprResult Sema::ActOnNumericConstant(const Token &Tok, Scope *UDLScope) { Width = LongLongSize; } } - - // If it doesn't fit in unsigned long long, and we're using Microsoft - // extensions, then its a 128-bit integer literal. - if (Ty.isNull() && Literal.isMicrosoftInteger && - PP.getTargetInfo().hasInt128Type()) { - if (Literal.isUnsigned) - Ty = Context.UnsignedInt128Ty; - else - Ty = Context.Int128Ty; - Width = 128; - } // If we still couldn't decide a type, we probably have something that // does not fit in a signed long long, but has no U suffix. if (Ty.isNull()) { - Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); + Diag(Tok.getLocation(), diag::ext_integer_too_large_for_signed); Ty = Context.UnsignedLongLongTy; Width = Context.getTargetInfo().getLongLongWidth(); } @@ -3216,12 +3306,12 @@ ExprResult Sema::ActOnNumericConstant(const Token &Tok, Scope *UDLScope) { Res = new (Context) ImaginaryLiteral(Res, Context.getComplexType(Res->getType())); - return Owned(Res); + return Res; } ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R, Expr *E) { - assert((E != 0) && "ActOnParenExpr() missing expr"); - return Owned(new (Context) ParenExpr(L, R, E)); + assert(E && "ActOnParenExpr() missing expr"); + return new (Context) ParenExpr(L, R, E); } static bool CheckVecStepTraitOperandType(Sema &S, QualType T, @@ -3259,9 +3349,12 @@ static bool CheckExtensionTraitOperandType(Sema &S, QualType T, return false; } - // Allow sizeof(void)/alignof(void) as an extension. + // Allow sizeof(void)/alignof(void) as an extension, unless in OpenCL where + // this is an error (OpenCL v1.1 s6.3.k) if (T->isVoidType()) { - S.Diag(Loc, diag::ext_sizeof_alignof_void_type) << TraitKind << ArgRange; + unsigned DiagID = S.LangOpts.OpenCL ? diag::err_opencl_sizeof_alignof_type + : diag::ext_sizeof_alignof_void_type; + S.Diag(Loc, DiagID) << TraitKind << ArgRange; return false; } @@ -3302,7 +3395,7 @@ static void warnOnSizeofOnArrayDecay(Sema &S, SourceLocation Loc, QualType T, << ICE->getSubExpr()->getType(); } -/// \brief Check the constrains on expression operands to unary type expression +/// \brief Check the constraints on expression operands to unary type expression /// and type traits. /// /// Completes any types necessary and validates the constraints on the operand @@ -3323,10 +3416,21 @@ bool Sema::CheckUnaryExprOrTypeTraitOperand(Expr *E, E->getSourceRange(), ExprKind)) return false; - if (RequireCompleteExprType(E, - diag::err_sizeof_alignof_incomplete_type, - ExprKind, E->getSourceRange())) - return true; + // 'alignof' applied to an expression only requires the base element type of + // the expression to be complete. 'sizeof' requires the expression's type to + // be complete (and will attempt to complete it if it's an array of unknown + // bound). + if (ExprKind == UETT_AlignOf) { + if (RequireCompleteType(E->getExprLoc(), + Context.getBaseElementType(E->getType()), + diag::err_sizeof_alignof_incomplete_type, ExprKind, + E->getSourceRange())) + return true; + } else { + if (RequireCompleteExprType(E, diag::err_sizeof_alignof_incomplete_type, + ExprKind, E->getSourceRange())) + return true; + } // Completing the expression's type may have changed it. ExprTy = E->getType(); @@ -3391,13 +3495,21 @@ bool Sema::CheckUnaryExprOrTypeTraitOperand(QualType ExprType, if (ExprType->isDependentType()) return false; - // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, - // the result is the size of the referenced type." - // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the - // result shall be the alignment of the referenced type." + // C++ [expr.sizeof]p2: + // When applied to a reference or a reference type, the result + // is the size of the referenced type. + // C++11 [expr.alignof]p3: + // When alignof is applied to a reference type, the result + // shall be the alignment of the referenced type. if (const ReferenceType *Ref = ExprType->getAs<ReferenceType>()) ExprType = Ref->getPointeeType(); + // C11 6.5.3.4/3, C++11 [expr.alignof]p3: + // When alignof or _Alignof is applied to an array type, the result + // is the alignment of the element type. + if (ExprKind == UETT_AlignOf) + ExprType = Context.getBaseElementType(ExprType); + if (ExprKind == UETT_VecStep) return CheckVecStepTraitOperandType(*this, ExprType, OpLoc, ExprRange); @@ -3437,7 +3549,7 @@ static bool CheckAlignOfExpr(Sema &S, Expr *E) { return true; } - ValueDecl *D = 0; + ValueDecl *D = nullptr; if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { D = DRE->getDecl(); } else if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) { @@ -3447,21 +3559,10 @@ static bool CheckAlignOfExpr(Sema &S, Expr *E) { // If it's a field, require the containing struct to have a // complete definition so that we can compute the layout. // - // This requires a very particular set of circumstances. For a - // field to be contained within an incomplete type, we must in the - // process of parsing that type. To have an expression refer to a - // field, it must be an id-expression or a member-expression, but - // the latter are always ill-formed when the base type is - // incomplete, including only being partially complete. An - // id-expression can never refer to a field in C because fields - // are not in the ordinary namespace. In C++, an id-expression - // can implicitly be a member access, but only if there's an - // implicit 'this' value, and all such contexts are subject to - // delayed parsing --- except for trailing return types in C++11. - // And if an id-expression referring to a field occurs in a - // context that lacks a 'this' value, it's ill-formed --- except, - // agian, in C++11, where such references are allowed in an - // unevaluated context. So C++11 introduces some new complexity. + // This can happen in C++11 onwards, either by naming the member + // in a way that is not transformed into a member access expression + // (in an unevaluated operand, for instance), or by naming the member + // in a trailing-return-type. // // For the record, since __alignof__ on expressions is a GCC // extension, GCC seems to permit this but always gives the @@ -3518,9 +3619,8 @@ Sema::CreateUnaryExprOrTypeTraitExpr(TypeSourceInfo *TInfo, return ExprError(); // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. - return Owned(new (Context) UnaryExprOrTypeTraitExpr(ExprKind, TInfo, - Context.getSizeType(), - OpLoc, R.getEnd())); + return new (Context) UnaryExprOrTypeTraitExpr( + ExprKind, TInfo, Context.getSizeType(), OpLoc, R.getEnd()); } /// \brief Build a sizeof or alignof expression given an expression @@ -3555,13 +3655,12 @@ Sema::CreateUnaryExprOrTypeTraitExpr(Expr *E, SourceLocation OpLoc, if (ExprKind == UETT_SizeOf && E->getType()->isVariableArrayType()) { PE = TransformToPotentiallyEvaluated(E); if (PE.isInvalid()) return ExprError(); - E = PE.take(); + E = PE.get(); } // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. - return Owned(new (Context) UnaryExprOrTypeTraitExpr( - ExprKind, E, Context.getSizeType(), OpLoc, - E->getSourceRange().getEnd())); + return new (Context) UnaryExprOrTypeTraitExpr( + ExprKind, E, Context.getSizeType(), OpLoc, E->getSourceRange().getEnd()); } /// ActOnUnaryExprOrTypeTraitExpr - Handle @c sizeof(type) and @c sizeof @c @@ -3572,7 +3671,7 @@ Sema::ActOnUnaryExprOrTypeTraitExpr(SourceLocation OpLoc, UnaryExprOrTypeTrait ExprKind, bool IsType, void *TyOrEx, const SourceRange &ArgRange) { // If error parsing type, ignore. - if (TyOrEx == 0) return ExprError(); + if (!TyOrEx) return ExprError(); if (IsType) { TypeSourceInfo *TInfo; @@ -3592,7 +3691,7 @@ static QualType CheckRealImagOperand(Sema &S, ExprResult &V, SourceLocation Loc, // _Real and _Imag are only l-values for normal l-values. if (V.get()->getObjectKind() != OK_Ordinary) { - V = S.DefaultLvalueConversion(V.take()); + V = S.DefaultLvalueConversion(V.get()); if (V.isInvalid()) return QualType(); } @@ -3634,7 +3733,7 @@ Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, // Since this might is a postfix expression, get rid of ParenListExprs. ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Input); if (Result.isInvalid()) return ExprError(); - Input = Result.take(); + Input = Result.get(); return BuildUnaryOp(S, OpLoc, Opc, Input); } @@ -3663,7 +3762,7 @@ Sema::ActOnArraySubscriptExpr(Scope *S, Expr *base, SourceLocation lbLoc, if (isa<ParenListExpr>(base)) { ExprResult result = MaybeConvertParenListExprToParenExpr(S, base); if (result.isInvalid()) return ExprError(); - base = result.take(); + base = result.get(); } // Handle any non-overload placeholder types in the base and index @@ -3674,21 +3773,19 @@ Sema::ActOnArraySubscriptExpr(Scope *S, Expr *base, SourceLocation lbLoc, if (base->getType()->isNonOverloadPlaceholderType()) { ExprResult result = CheckPlaceholderExpr(base); if (result.isInvalid()) return ExprError(); - base = result.take(); + base = result.get(); } if (idx->getType()->isNonOverloadPlaceholderType()) { ExprResult result = CheckPlaceholderExpr(idx); if (result.isInvalid()) return ExprError(); - idx = result.take(); + idx = result.get(); } // Build an unanalyzed expression if either operand is type-dependent. if (getLangOpts().CPlusPlus && (base->isTypeDependent() || idx->isTypeDependent())) { - return Owned(new (Context) ArraySubscriptExpr(base, idx, - Context.DependentTy, - VK_LValue, OK_Ordinary, - rbLoc)); + return new (Context) ArraySubscriptExpr(base, idx, Context.DependentTy, + VK_LValue, OK_Ordinary, rbLoc); } // Use C++ overloaded-operator rules if either operand has record @@ -3720,12 +3817,12 @@ Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, ExprResult Result = DefaultFunctionArrayLvalueConversion(LHSExp); if (Result.isInvalid()) return ExprError(); - LHSExp = Result.take(); + LHSExp = Result.get(); } ExprResult Result = DefaultFunctionArrayLvalueConversion(RHSExp); if (Result.isInvalid()) return ExprError(); - RHSExp = Result.take(); + RHSExp = Result.get(); QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); ExprValueKind VK = VK_LValue; @@ -3753,7 +3850,8 @@ Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, // Use custom logic if this should be the pseudo-object subscript // expression. if (!LangOpts.isSubscriptPointerArithmetic()) - return BuildObjCSubscriptExpression(RLoc, BaseExpr, IndexExpr, 0, 0); + return BuildObjCSubscriptExpression(RLoc, BaseExpr, IndexExpr, nullptr, + nullptr); ResultType = PTy->getPointeeType(); } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { @@ -3790,7 +3888,7 @@ Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << LHSExp->getSourceRange(); LHSExp = ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy), - CK_ArrayToPointerDecay).take(); + CK_ArrayToPointerDecay).get(); LHSTy = LHSExp->getType(); BaseExpr = LHSExp; @@ -3801,7 +3899,7 @@ Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << RHSExp->getSourceRange(); RHSExp = ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy), - CK_ArrayToPointerDecay).take(); + CK_ArrayToPointerDecay).get(); RHSTy = RHSExp->getType(); BaseExpr = RHSExp; @@ -3847,8 +3945,8 @@ Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, assert(VK == VK_RValue || LangOpts.CPlusPlus || !ResultType.isCForbiddenLValueType()); - return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, - ResultType, VK, OK, RLoc)); + return new (Context) + ArraySubscriptExpr(LHSExp, RHSExp, ResultType, VK, OK, RLoc); } ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, @@ -3871,7 +3969,7 @@ ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, // Instantiate the expression. MultiLevelTemplateArgumentList MutiLevelArgList - = getTemplateInstantiationArgs(FD, 0, /*RelativeToPrimary=*/true); + = getTemplateInstantiationArgs(FD, nullptr, /*RelativeToPrimary=*/true); InstantiatingTemplate Inst(*this, CallLoc, Param, MutiLevelArgList.getInnermost()); @@ -3897,17 +3995,17 @@ ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, InitializationKind Kind = InitializationKind::CreateCopy(Param->getLocation(), /*FIXME:EqualLoc*/UninstExpr->getLocStart()); - Expr *ResultE = Result.takeAs<Expr>(); + Expr *ResultE = Result.getAs<Expr>(); InitializationSequence InitSeq(*this, Entity, Kind, ResultE); Result = InitSeq.Perform(*this, Entity, Kind, ResultE); if (Result.isInvalid()) return ExprError(); - Expr *Arg = Result.takeAs<Expr>(); + Expr *Arg = Result.getAs<Expr>(); CheckCompletedExpr(Arg, Param->getOuterLocStart()); // Build the default argument expression. - return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param, Arg)); + return CXXDefaultArgExpr::Create(Context, CallLoc, Param, Arg); } // If the default expression creates temporaries, we need to @@ -3934,7 +4032,7 @@ ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, // as being "referenced". MarkDeclarationsReferencedInExpr(Param->getDefaultArg(), /*SkipLocalVariables=*/true); - return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param)); + return CXXDefaultArgExpr::Create(Context, CallLoc, Param); } @@ -3961,11 +4059,11 @@ namespace { class FunctionCallCCC : public FunctionCallFilterCCC { public: FunctionCallCCC(Sema &SemaRef, const IdentifierInfo *FuncName, - unsigned NumArgs, bool HasExplicitTemplateArgs) - : FunctionCallFilterCCC(SemaRef, NumArgs, HasExplicitTemplateArgs), + unsigned NumArgs, MemberExpr *ME) + : FunctionCallFilterCCC(SemaRef, NumArgs, false, ME), FunctionName(FuncName) {} - virtual bool ValidateCandidate(const TypoCorrection &candidate) { + bool ValidateCandidate(const TypoCorrection &candidate) override { if (!candidate.getCorrectionSpecifier() || candidate.getCorrectionAsIdentifierInfo() != FunctionName) { return false; @@ -3979,17 +4077,21 @@ private: }; } -static TypoCorrection TryTypoCorrectionForCall(Sema &S, - DeclarationNameInfo FuncName, +static TypoCorrection TryTypoCorrectionForCall(Sema &S, Expr *Fn, + FunctionDecl *FDecl, ArrayRef<Expr *> Args) { - FunctionCallCCC CCC(S, FuncName.getName().getAsIdentifierInfo(), - Args.size(), false); - if (TypoCorrection Corrected = - S.CorrectTypo(FuncName, Sema::LookupOrdinaryName, - S.getScopeForContext(S.CurContext), NULL, CCC)) { + MemberExpr *ME = dyn_cast<MemberExpr>(Fn); + DeclarationName FuncName = FDecl->getDeclName(); + SourceLocation NameLoc = ME ? ME->getMemberLoc() : Fn->getLocStart(); + FunctionCallCCC CCC(S, FuncName.getAsIdentifierInfo(), Args.size(), ME); + + if (TypoCorrection Corrected = S.CorrectTypo( + DeclarationNameInfo(FuncName, NameLoc), Sema::LookupOrdinaryName, + S.getScopeForContext(S.CurContext), nullptr, CCC, + Sema::CTK_ErrorRecovery)) { if (NamedDecl *ND = Corrected.getCorrectionDecl()) { if (Corrected.isOverloaded()) { - OverloadCandidateSet OCS(FuncName.getLoc()); + OverloadCandidateSet OCS(NameLoc, OverloadCandidateSet::CSK_Normal); OverloadCandidateSet::iterator Best; for (TypoCorrection::decl_iterator CD = Corrected.begin(), CDEnd = Corrected.end(); @@ -3998,7 +4100,7 @@ static TypoCorrection TryTypoCorrectionForCall(Sema &S, S.AddOverloadCandidate(FD, DeclAccessPair::make(FD, AS_none), Args, OCS); } - switch (OCS.BestViableFunction(S, FuncName.getLoc(), Best)) { + switch (OCS.BestViableFunction(S, NameLoc, Best)) { case OR_Success: ND = Best->Function; Corrected.setCorrectionDecl(ND); @@ -4037,9 +4139,9 @@ Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by // assignment, to the types of the corresponding parameter, ... - unsigned NumArgsInProto = Proto->getNumArgs(); + unsigned NumParams = Proto->getNumParams(); bool Invalid = false; - unsigned MinArgs = FDecl ? FDecl->getMinRequiredArguments() : NumArgsInProto; + unsigned MinArgs = FDecl ? FDecl->getMinRequiredArguments() : NumParams; unsigned FnKind = Fn->getType()->isBlockPointerType() ? 1 /* block */ : (IsExecConfig ? 3 /* kernel function (exec config) */ @@ -4047,35 +4149,29 @@ Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, // If too few arguments are available (and we don't have default // arguments for the remaining parameters), don't make the call. - if (Args.size() < NumArgsInProto) { + if (Args.size() < NumParams) { if (Args.size() < MinArgs) { - MemberExpr *ME = dyn_cast<MemberExpr>(Fn); TypoCorrection TC; - if (FDecl && (TC = TryTypoCorrectionForCall( - *this, DeclarationNameInfo(FDecl->getDeclName(), - (ME ? ME->getMemberLoc() - : Fn->getLocStart())), - Args))) { + if (FDecl && (TC = TryTypoCorrectionForCall(*this, Fn, FDecl, Args))) { unsigned diag_id = - MinArgs == NumArgsInProto && !Proto->isVariadic() + MinArgs == NumParams && !Proto->isVariadic() ? diag::err_typecheck_call_too_few_args_suggest : diag::err_typecheck_call_too_few_args_at_least_suggest; diagnoseTypo(TC, PDiag(diag_id) << FnKind << MinArgs << static_cast<unsigned>(Args.size()) - << Fn->getSourceRange()); + << TC.getCorrectionRange()); } else if (MinArgs == 1 && FDecl && FDecl->getParamDecl(0)->getDeclName()) - Diag(RParenLoc, MinArgs == NumArgsInProto && !Proto->isVariadic() - ? diag::err_typecheck_call_too_few_args_one - : diag::err_typecheck_call_too_few_args_at_least_one) - << FnKind - << FDecl->getParamDecl(0) << Fn->getSourceRange(); + Diag(RParenLoc, + MinArgs == NumParams && !Proto->isVariadic() + ? diag::err_typecheck_call_too_few_args_one + : diag::err_typecheck_call_too_few_args_at_least_one) + << FnKind << FDecl->getParamDecl(0) << Fn->getSourceRange(); else - Diag(RParenLoc, MinArgs == NumArgsInProto && !Proto->isVariadic() - ? diag::err_typecheck_call_too_few_args - : diag::err_typecheck_call_too_few_args_at_least) - << FnKind - << MinArgs << static_cast<unsigned>(Args.size()) - << Fn->getSourceRange(); + Diag(RParenLoc, MinArgs == NumParams && !Proto->isVariadic() + ? diag::err_typecheck_call_too_few_args + : diag::err_typecheck_call_too_few_args_at_least) + << FnKind << MinArgs << static_cast<unsigned>(Args.size()) + << Fn->getSourceRange(); // Emit the location of the prototype. if (!TC && FDecl && !FDecl->getBuiltinID() && !IsExecConfig) @@ -4084,46 +4180,41 @@ Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, return true; } - Call->setNumArgs(Context, NumArgsInProto); + Call->setNumArgs(Context, NumParams); } // If too many are passed and not variadic, error on the extras and drop // them. - if (Args.size() > NumArgsInProto) { + if (Args.size() > NumParams) { if (!Proto->isVariadic()) { TypoCorrection TC; - if (FDecl && (TC = TryTypoCorrectionForCall( - *this, DeclarationNameInfo(FDecl->getDeclName(), - Fn->getLocStart()), - Args))) { + if (FDecl && (TC = TryTypoCorrectionForCall(*this, Fn, FDecl, Args))) { unsigned diag_id = - MinArgs == NumArgsInProto && !Proto->isVariadic() + MinArgs == NumParams && !Proto->isVariadic() ? diag::err_typecheck_call_too_many_args_suggest : diag::err_typecheck_call_too_many_args_at_most_suggest; - diagnoseTypo(TC, PDiag(diag_id) << FnKind << NumArgsInProto + diagnoseTypo(TC, PDiag(diag_id) << FnKind << NumParams << static_cast<unsigned>(Args.size()) - << Fn->getSourceRange()); - } else if (NumArgsInProto == 1 && FDecl && + << TC.getCorrectionRange()); + } else if (NumParams == 1 && FDecl && FDecl->getParamDecl(0)->getDeclName()) - Diag(Args[NumArgsInProto]->getLocStart(), - MinArgs == NumArgsInProto - ? diag::err_typecheck_call_too_many_args_one - : diag::err_typecheck_call_too_many_args_at_most_one) - << FnKind - << FDecl->getParamDecl(0) << static_cast<unsigned>(Args.size()) - << Fn->getSourceRange() - << SourceRange(Args[NumArgsInProto]->getLocStart(), - Args.back()->getLocEnd()); + Diag(Args[NumParams]->getLocStart(), + MinArgs == NumParams + ? diag::err_typecheck_call_too_many_args_one + : diag::err_typecheck_call_too_many_args_at_most_one) + << FnKind << FDecl->getParamDecl(0) + << static_cast<unsigned>(Args.size()) << Fn->getSourceRange() + << SourceRange(Args[NumParams]->getLocStart(), + Args.back()->getLocEnd()); else - Diag(Args[NumArgsInProto]->getLocStart(), - MinArgs == NumArgsInProto - ? diag::err_typecheck_call_too_many_args - : diag::err_typecheck_call_too_many_args_at_most) - << FnKind - << NumArgsInProto << static_cast<unsigned>(Args.size()) - << Fn->getSourceRange() - << SourceRange(Args[NumArgsInProto]->getLocStart(), - Args.back()->getLocEnd()); + Diag(Args[NumParams]->getLocStart(), + MinArgs == NumParams + ? diag::err_typecheck_call_too_many_args + : diag::err_typecheck_call_too_many_args_at_most) + << FnKind << NumParams << static_cast<unsigned>(Args.size()) + << Fn->getSourceRange() + << SourceRange(Args[NumParams]->getLocStart(), + Args.back()->getLocEnd()); // Emit the location of the prototype. if (!TC && FDecl && !FDecl->getBuiltinID() && !IsExecConfig) @@ -4131,7 +4222,7 @@ Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, << FDecl; // This deletes the extra arguments. - Call->setNumArgs(Context, NumArgsInProto); + Call->setNumArgs(Context, NumParams); return true; } } @@ -4149,28 +4240,21 @@ Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, return false; } -bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, - FunctionDecl *FDecl, +bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, FunctionDecl *FDecl, const FunctionProtoType *Proto, - unsigned FirstProtoArg, - ArrayRef<Expr *> Args, + unsigned FirstParam, ArrayRef<Expr *> Args, SmallVectorImpl<Expr *> &AllArgs, - VariadicCallType CallType, - bool AllowExplicit, + VariadicCallType CallType, bool AllowExplicit, bool IsListInitialization) { - unsigned NumArgsInProto = Proto->getNumArgs(); - unsigned NumArgsToCheck = Args.size(); + unsigned NumParams = Proto->getNumParams(); bool Invalid = false; - if (Args.size() != NumArgsInProto) - // Use default arguments for missing arguments - NumArgsToCheck = NumArgsInProto; unsigned ArgIx = 0; // Continue to check argument types (even if we have too few/many args). - for (unsigned i = FirstProtoArg; i != NumArgsToCheck; i++) { - QualType ProtoArgType = Proto->getArgType(i); + for (unsigned i = FirstParam; i < NumParams; i++) { + QualType ProtoArgType = Proto->getParamType(i); Expr *Arg; - ParmVarDecl *Param; + ParmVarDecl *Param = FDecl ? FDecl->getParamDecl(i) : nullptr; if (ArgIx < Args.size()) { Arg = Args[ArgIx++]; @@ -4179,11 +4263,6 @@ bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, diag::err_call_incomplete_argument, Arg)) return true; - // Pass the argument - Param = 0; - if (FDecl && i < FDecl->getNumParams()) - Param = FDecl->getParamDecl(i); - // Strip the unbridged-cast placeholder expression off, if applicable. bool CFAudited = false; if (Arg->getType() == Context.ARCUnbridgedCastTy && @@ -4195,34 +4274,31 @@ bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, (!Param || !Param->hasAttr<CFConsumedAttr>())) CFAudited = true; - InitializedEntity Entity = Param ? - InitializedEntity::InitializeParameter(Context, Param, ProtoArgType) - : InitializedEntity::InitializeParameter(Context, ProtoArgType, - Proto->isArgConsumed(i)); - + InitializedEntity Entity = + Param ? InitializedEntity::InitializeParameter(Context, Param, + ProtoArgType) + : InitializedEntity::InitializeParameter( + Context, ProtoArgType, Proto->isParamConsumed(i)); + // Remember that parameter belongs to a CF audited API. if (CFAudited) Entity.setParameterCFAudited(); - - ExprResult ArgE = PerformCopyInitialization(Entity, - SourceLocation(), - Owned(Arg), - IsListInitialization, - AllowExplicit); + + ExprResult ArgE = PerformCopyInitialization( + Entity, SourceLocation(), Arg, IsListInitialization, AllowExplicit); if (ArgE.isInvalid()) return true; - Arg = ArgE.takeAs<Expr>(); + Arg = ArgE.getAs<Expr>(); } else { - assert(FDecl && "can't use default arguments without a known callee"); - Param = FDecl->getParamDecl(i); + assert(Param && "can't use default arguments without a known callee"); ExprResult ArgExpr = BuildCXXDefaultArgExpr(CallLoc, FDecl, Param); if (ArgExpr.isInvalid()) return true; - Arg = ArgExpr.takeAs<Expr>(); + Arg = ArgExpr.getAs<Expr>(); } // Check for array bounds violations for each argument to the call. This @@ -4240,13 +4316,13 @@ bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, if (CallType != VariadicDoesNotApply) { // Assume that extern "C" functions with variadic arguments that // return __unknown_anytype aren't *really* variadic. - if (Proto->getResultType() == Context.UnknownAnyTy && - FDecl && FDecl->isExternC()) { + if (Proto->getReturnType() == Context.UnknownAnyTy && FDecl && + FDecl->isExternC()) { for (unsigned i = ArgIx, e = Args.size(); i != e; ++i) { QualType paramType; // ignored ExprResult arg = checkUnknownAnyArg(CallLoc, Args[i], paramType); Invalid |= arg.isInvalid(); - AllArgs.push_back(arg.take()); + AllArgs.push_back(arg.get()); } // Otherwise do argument promotion, (C99 6.5.2.2p7). @@ -4255,7 +4331,7 @@ bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], CallType, FDecl); Invalid |= Arg.isInvalid(); - AllArgs.push_back(Arg.take()); + AllArgs.push_back(Arg.get()); } } @@ -4377,7 +4453,7 @@ static bool checkArgsForPlaceholders(Sema &S, MultiExprArg args) { if (isPlaceholderToRemoveAsArg(args[i]->getType())) { ExprResult result = S.CheckPlaceholderExpr(args[i]); if (result.isInvalid()) hasInvalid = true; - else args[i] = result.take(); + else args[i] = result.get(); } } return hasInvalid; @@ -4393,7 +4469,7 @@ Sema::ActOnCallExpr(Scope *S, Expr *Fn, SourceLocation LParenLoc, // Since this might be a postfix expression, get rid of ParenListExprs. ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Fn); if (Result.isInvalid()) return ExprError(); - Fn = Result.take(); + Fn = Result.get(); if (checkArgsForPlaceholders(*this, ArgExprs)) return ExprError(); @@ -4409,14 +4485,13 @@ Sema::ActOnCallExpr(Scope *S, Expr *Fn, SourceLocation LParenLoc, ArgExprs.back()->getLocEnd())); } - return Owned(new (Context) CallExpr(Context, Fn, None, - Context.VoidTy, VK_RValue, - RParenLoc)); + return new (Context) + CallExpr(Context, Fn, None, Context.VoidTy, VK_RValue, RParenLoc); } if (Fn->getType() == Context.PseudoObjectTy) { ExprResult result = CheckPlaceholderExpr(Fn); if (result.isInvalid()) return ExprError(); - Fn = result.take(); + Fn = result.get(); } // Determine whether this is a dependent call inside a C++ template, @@ -4431,25 +4506,24 @@ Sema::ActOnCallExpr(Scope *S, Expr *Fn, SourceLocation LParenLoc, if (Dependent) { if (ExecConfig) { - return Owned(new (Context) CUDAKernelCallExpr( + return new (Context) CUDAKernelCallExpr( Context, Fn, cast<CallExpr>(ExecConfig), ArgExprs, - Context.DependentTy, VK_RValue, RParenLoc)); + Context.DependentTy, VK_RValue, RParenLoc); } else { - return Owned(new (Context) CallExpr(Context, Fn, ArgExprs, - Context.DependentTy, VK_RValue, - RParenLoc)); + return new (Context) CallExpr( + Context, Fn, ArgExprs, Context.DependentTy, VK_RValue, RParenLoc); } } // Determine whether this is a call to an object (C++ [over.call.object]). if (Fn->getType()->isRecordType()) - return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, - ArgExprs, RParenLoc)); + return BuildCallToObjectOfClassType(S, Fn, LParenLoc, ArgExprs, + RParenLoc); if (Fn->getType() == Context.UnknownAnyTy) { ExprResult result = rebuildUnknownAnyFunction(*this, Fn); if (result.isInvalid()) return ExprError(); - Fn = result.take(); + Fn = result.get(); } if (Fn->getType() == Context.BoundMemberTy) { @@ -4479,12 +4553,12 @@ Sema::ActOnCallExpr(Scope *S, Expr *Fn, SourceLocation LParenLoc, if (Fn->getType() == Context.UnknownAnyTy) { ExprResult result = rebuildUnknownAnyFunction(*this, Fn); if (result.isInvalid()) return ExprError(); - Fn = result.take(); + Fn = result.get(); } Expr *NakedFn = Fn->IgnoreParens(); - NamedDecl *NDecl = 0; + NamedDecl *NDecl = nullptr; if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(NakedFn)) if (UnOp->getOpcode() == UO_AddrOf) NakedFn = UnOp->getSubExpr()->IgnoreParens(); @@ -4494,6 +4568,21 @@ Sema::ActOnCallExpr(Scope *S, Expr *Fn, SourceLocation LParenLoc, else if (isa<MemberExpr>(NakedFn)) NDecl = cast<MemberExpr>(NakedFn)->getMemberDecl(); + if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(NDecl)) { + if (FD->hasAttr<EnableIfAttr>()) { + if (const EnableIfAttr *Attr = CheckEnableIf(FD, ArgExprs, true)) { + Diag(Fn->getLocStart(), + isa<CXXMethodDecl>(FD) ? + diag::err_ovl_no_viable_member_function_in_call : + diag::err_ovl_no_viable_function_in_call) + << FD << FD->getSourceRange(); + Diag(FD->getLocation(), + diag::note_ovl_candidate_disabled_by_enable_if_attr) + << Attr->getCond()->getSourceRange() << Attr->getMessage(); + } + } + } + return BuildResolvedCallExpr(Fn, NDecl, LParenLoc, ArgExprs, RParenLoc, ExecConfig, IsExecConfig); } @@ -4511,7 +4600,7 @@ Sema::ActOnCUDAExecConfigExpr(Scope *S, SourceLocation LLLLoc, ConfigDecl, false, ConfigQTy, VK_LValue, LLLLoc); MarkFunctionReferenced(LLLLoc, ConfigDecl); - return ActOnCallExpr(S, ConfigDR, LLLLoc, ExecConfig, GGGLoc, 0, + return ActOnCallExpr(S, ConfigDR, LLLLoc, ExecConfig, GGGLoc, nullptr, /*IsExecConfig=*/true); } @@ -4532,8 +4621,7 @@ ExprResult Sema::ActOnAsTypeExpr(Expr *E, ParsedType ParsedDestTy, << DstTy << SrcTy << E->getSourceRange()); - return Owned(new (Context) AsTypeExpr(E, DstTy, VK, OK, BuiltinLoc, - RParenLoc)); + return new (Context) AsTypeExpr(E, DstTy, VK, OK, BuiltinLoc, RParenLoc); } /// ActOnConvertVectorExpr - create a new convert-vector expression from the @@ -4571,13 +4659,13 @@ Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, if (BuiltinID && Fn->getType()->isSpecificBuiltinType(BuiltinType::BuiltinFn)) { Result = ImpCastExprToType(Fn, Context.getPointerType(FDecl->getType()), - CK_BuiltinFnToFnPtr).take(); + CK_BuiltinFnToFnPtr).get(); } else { - Result = UsualUnaryConversions(Fn); + Result = CallExprUnaryConversions(Fn); } if (Result.isInvalid()) return ExprError(); - Fn = Result.take(); + Fn = Result.get(); // Make the call expr early, before semantic checks. This guarantees cleanup // of arguments and function on error. @@ -4601,7 +4689,7 @@ Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, // C99 6.5.2.2p1 - "The expression that denotes the called function shall // have type pointer to function". FuncT = PT->getPointeeType()->getAs<FunctionType>(); - if (FuncT == 0) + if (!FuncT) return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) << Fn->getType() << Fn->getSourceRange()); } else if (const BlockPointerType *BPT = @@ -4612,7 +4700,7 @@ Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, if (Fn->getType() == Context.UnknownAnyTy) { ExprResult rewrite = rebuildUnknownAnyFunction(*this, Fn); if (rewrite.isInvalid()) return ExprError(); - Fn = rewrite.take(); + Fn = rewrite.get(); TheCall->setCallee(Fn); goto retry; } @@ -4629,7 +4717,7 @@ Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, << FDecl->getName() << Fn->getSourceRange()); // CUDA: Kernel function must have 'void' return type - if (!FuncT->getResultType()->isVoidType()) + if (!FuncT->getReturnType()->isVoidType()) return ExprError(Diag(LParenLoc, diag::err_kern_type_not_void_return) << Fn->getType() << Fn->getSourceRange()); } else { @@ -4641,14 +4729,13 @@ Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, } // Check for a valid return type - if (CheckCallReturnType(FuncT->getResultType(), - Fn->getLocStart(), TheCall, + if (CheckCallReturnType(FuncT->getReturnType(), Fn->getLocStart(), TheCall, FDecl)) return ExprError(); // We know the result type of the call, set it. TheCall->setType(FuncT->getCallResultType(Context)); - TheCall->setValueKind(Expr::getValueKindForType(FuncT->getResultType())); + TheCall->setValueKind(Expr::getValueKindForType(FuncT->getReturnType())); const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT); if (Proto) { @@ -4661,7 +4748,7 @@ Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, if (FDecl) { // Check if we have too few/too many template arguments, based // on our knowledge of the function definition. - const FunctionDecl *Def = 0; + const FunctionDecl *Def = nullptr; if (FDecl->hasBody(Def) && Args.size() != Def->param_size()) { Proto = Def->getType()->getAs<FunctionProtoType>(); if (!Proto || !(Proto->isVariadic() && Args.size() >= Def->param_size())) @@ -4679,18 +4766,15 @@ Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, for (unsigned i = 0, e = Args.size(); i != e; i++) { Expr *Arg = Args[i]; - if (Proto && i < Proto->getNumArgs()) { - InitializedEntity Entity - = InitializedEntity::InitializeParameter(Context, - Proto->getArgType(i), - Proto->isArgConsumed(i)); - ExprResult ArgE = PerformCopyInitialization(Entity, - SourceLocation(), - Owned(Arg)); + if (Proto && i < Proto->getNumParams()) { + InitializedEntity Entity = InitializedEntity::InitializeParameter( + Context, Proto->getParamType(i), Proto->isParamConsumed(i)); + ExprResult ArgE = + PerformCopyInitialization(Entity, SourceLocation(), Arg); if (ArgE.isInvalid()) return true; - Arg = ArgE.takeAs<Expr>(); + Arg = ArgE.getAs<Expr>(); } else { ExprResult ArgE = DefaultArgumentPromotion(Arg); @@ -4698,7 +4782,7 @@ Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, if (ArgE.isInvalid()) return true; - Arg = ArgE.takeAs<Expr>(); + Arg = ArgE.getAs<Expr>(); } if (RequireCompleteType(Arg->getLocStart(), @@ -4785,7 +4869,7 @@ Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo, return ExprError(); LiteralExpr = Result.get(); - bool isFileScope = getCurFunctionOrMethodDecl() == 0; + bool isFileScope = getCurFunctionOrMethodDecl() == nullptr; if (isFileScope && !LiteralExpr->isTypeDependent() && !LiteralExpr->isValueDependent() && @@ -4815,7 +4899,7 @@ Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg InitArgList, // of one failure would be terrible for indexing/etc. if (result.isInvalid()) continue; - InitArgList[I] = result.take(); + InitArgList[I] = result.get(); } } @@ -4825,7 +4909,7 @@ Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg InitArgList, InitListExpr *E = new (Context) InitListExpr(Context, LBraceLoc, InitArgList, RBraceLoc); E->setType(Context.VoidTy); // FIXME: just a place holder for now. - return Owned(E); + return E; } /// Do an explicit extend of the given block pointer if we're in ARC. @@ -4838,7 +4922,7 @@ static void maybeExtendBlockObject(Sema &S, ExprResult &E) { E = ImplicitCastExpr::Create(S.Context, E.get()->getType(), CK_ARCExtendBlockObject, E.get(), - /*base path*/ 0, VK_RValue); + /*base path*/ nullptr, VK_RValue); S.ExprNeedsCleanups = true; } @@ -4876,8 +4960,13 @@ CastKind Sema::PrepareScalarCast(ExprResult &Src, QualType DestTy) { case Type::STK_BlockPointer: case Type::STK_ObjCObjectPointer: switch (DestTy->getScalarTypeKind()) { - case Type::STK_CPointer: + case Type::STK_CPointer: { + unsigned SrcAS = SrcTy->getPointeeType().getAddressSpace(); + unsigned DestAS = DestTy->getPointeeType().getAddressSpace(); + if (SrcAS != DestAS) + return CK_AddressSpaceConversion; return CK_BitCast; + } case Type::STK_BlockPointer: return (SrcKind == Type::STK_BlockPointer ? CK_BitCast : CK_AnyPointerToBlockPointerCast); @@ -4917,12 +5006,12 @@ CastKind Sema::PrepareScalarCast(ExprResult &Src, QualType DestTy) { case Type::STK_Floating: return CK_IntegralToFloating; case Type::STK_IntegralComplex: - Src = ImpCastExprToType(Src.take(), + Src = ImpCastExprToType(Src.get(), DestTy->castAs<ComplexType>()->getElementType(), CK_IntegralCast); return CK_IntegralRealToComplex; case Type::STK_FloatingComplex: - Src = ImpCastExprToType(Src.take(), + Src = ImpCastExprToType(Src.get(), DestTy->castAs<ComplexType>()->getElementType(), CK_IntegralToFloating); return CK_FloatingRealToComplex; @@ -4940,12 +5029,12 @@ CastKind Sema::PrepareScalarCast(ExprResult &Src, QualType DestTy) { case Type::STK_Integral: return CK_FloatingToIntegral; case Type::STK_FloatingComplex: - Src = ImpCastExprToType(Src.take(), + Src = ImpCastExprToType(Src.get(), DestTy->castAs<ComplexType>()->getElementType(), CK_FloatingCast); return CK_FloatingRealToComplex; case Type::STK_IntegralComplex: - Src = ImpCastExprToType(Src.take(), + Src = ImpCastExprToType(Src.get(), DestTy->castAs<ComplexType>()->getElementType(), CK_FloatingToIntegral); return CK_IntegralRealToComplex; @@ -4968,13 +5057,13 @@ CastKind Sema::PrepareScalarCast(ExprResult &Src, QualType DestTy) { QualType ET = SrcTy->castAs<ComplexType>()->getElementType(); if (Context.hasSameType(ET, DestTy)) return CK_FloatingComplexToReal; - Src = ImpCastExprToType(Src.take(), ET, CK_FloatingComplexToReal); + Src = ImpCastExprToType(Src.get(), ET, CK_FloatingComplexToReal); return CK_FloatingCast; } case Type::STK_Bool: return CK_FloatingComplexToBoolean; case Type::STK_Integral: - Src = ImpCastExprToType(Src.take(), + Src = ImpCastExprToType(Src.get(), SrcTy->castAs<ComplexType>()->getElementType(), CK_FloatingComplexToReal); return CK_FloatingToIntegral; @@ -4997,13 +5086,13 @@ CastKind Sema::PrepareScalarCast(ExprResult &Src, QualType DestTy) { QualType ET = SrcTy->castAs<ComplexType>()->getElementType(); if (Context.hasSameType(ET, DestTy)) return CK_IntegralComplexToReal; - Src = ImpCastExprToType(Src.take(), ET, CK_IntegralComplexToReal); + Src = ImpCastExprToType(Src.get(), ET, CK_IntegralComplexToReal); return CK_IntegralCast; } case Type::STK_Bool: return CK_IntegralComplexToBoolean; case Type::STK_Floating: - Src = ImpCastExprToType(Src.take(), + Src = ImpCastExprToType(Src.get(), SrcTy->castAs<ComplexType>()->getElementType(), CK_IntegralComplexToReal); return CK_IntegralToFloating; @@ -5020,12 +5109,55 @@ CastKind Sema::PrepareScalarCast(ExprResult &Src, QualType DestTy) { llvm_unreachable("Unhandled scalar cast"); } +static bool breakDownVectorType(QualType type, uint64_t &len, + QualType &eltType) { + // Vectors are simple. + if (const VectorType *vecType = type->getAs<VectorType>()) { + len = vecType->getNumElements(); + eltType = vecType->getElementType(); + assert(eltType->isScalarType()); + return true; + } + + // We allow lax conversion to and from non-vector types, but only if + // they're real types (i.e. non-complex, non-pointer scalar types). + if (!type->isRealType()) return false; + + len = 1; + eltType = type; + return true; +} + +static bool VectorTypesMatch(Sema &S, QualType srcTy, QualType destTy) { + uint64_t srcLen, destLen; + QualType srcElt, destElt; + if (!breakDownVectorType(srcTy, srcLen, srcElt)) return false; + if (!breakDownVectorType(destTy, destLen, destElt)) return false; + + // ASTContext::getTypeSize will return the size rounded up to a + // power of 2, so instead of using that, we need to use the raw + // element size multiplied by the element count. + uint64_t srcEltSize = S.Context.getTypeSize(srcElt); + uint64_t destEltSize = S.Context.getTypeSize(destElt); + + return (srcLen * srcEltSize == destLen * destEltSize); +} + +/// Is this a legal conversion between two known vector types? +bool Sema::isLaxVectorConversion(QualType srcTy, QualType destTy) { + assert(destTy->isVectorType() || srcTy->isVectorType()); + + if (!Context.getLangOpts().LaxVectorConversions) + return false; + return VectorTypesMatch(*this, srcTy, destTy); +} + bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty, CastKind &Kind) { assert(VectorTy->isVectorType() && "Not a vector type!"); if (Ty->isVectorType() || Ty->isIntegerType()) { - if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) + if (!VectorTypesMatch(*this, Ty, VectorTy)) return Diag(R.getBegin(), Ty->isVectorType() ? diag::err_invalid_conversion_between_vectors : @@ -5051,7 +5183,7 @@ ExprResult Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, // In OpenCL, casts between vectors of different types are not allowed. // (See OpenCL 6.2). if (SrcTy->isVectorType()) { - if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy) + if (!VectorTypesMatch(*this, SrcTy, DestTy) || (getLangOpts().OpenCL && (DestTy.getCanonicalType() != SrcTy.getCanonicalType()))) { Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) @@ -5059,7 +5191,7 @@ ExprResult Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, return ExprError(); } Kind = CK_BitCast; - return Owned(CastExpr); + return CastExpr; } // All non-pointer scalars can be cast to ExtVector type. The appropriate @@ -5071,21 +5203,21 @@ ExprResult Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, << DestTy << SrcTy << R; QualType DestElemTy = DestTy->getAs<ExtVectorType>()->getElementType(); - ExprResult CastExprRes = Owned(CastExpr); + ExprResult CastExprRes = CastExpr; CastKind CK = PrepareScalarCast(CastExprRes, DestElemTy); if (CastExprRes.isInvalid()) return ExprError(); - CastExpr = ImpCastExprToType(CastExprRes.take(), DestElemTy, CK).take(); + CastExpr = ImpCastExprToType(CastExprRes.get(), DestElemTy, CK).get(); Kind = CK_VectorSplat; - return Owned(CastExpr); + return CastExpr; } ExprResult Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, Declarator &D, ParsedType &Ty, SourceLocation RParenLoc, Expr *CastExpr) { - assert(!D.isInvalidType() && (CastExpr != 0) && + assert(!D.isInvalidType() && (CastExpr != nullptr) && "ActOnCastExpr(): missing type or expr"); TypeSourceInfo *castTInfo = GetTypeForDeclaratorCast(D, CastExpr->getType()); @@ -5134,9 +5266,17 @@ Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, if (isa<ParenListExpr>(CastExpr)) { ExprResult Result = MaybeConvertParenListExprToParenExpr(S, CastExpr); if (Result.isInvalid()) return ExprError(); - CastExpr = Result.take(); + CastExpr = Result.get(); } + if (getLangOpts().CPlusPlus && !castType->isVoidType() && + !getSourceManager().isInSystemMacro(LParenLoc)) + Diag(LParenLoc, diag::warn_old_style_cast) << CastExpr->getSourceRange(); + + CheckTollFreeBridgeCast(castType, CastExpr); + + CheckObjCBridgeRelatedCast(castType, CastExpr); + return BuildCStyleCastExpr(LParenLoc, castTInfo, RParenLoc, CastExpr); } @@ -5183,9 +5323,9 @@ ExprResult Sema::BuildVectorLiteral(SourceLocation LParenLoc, ExprResult Literal = DefaultLvalueConversion(exprs[0]); if (Literal.isInvalid()) return ExprError(); - Literal = ImpCastExprToType(Literal.take(), ElemTy, + Literal = ImpCastExprToType(Literal.get(), ElemTy, PrepareScalarCast(Literal, ElemTy)); - return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take()); + return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.get()); } else if (numExprs < numElems) { Diag(E->getExprLoc(), @@ -5205,9 +5345,9 @@ ExprResult Sema::BuildVectorLiteral(SourceLocation LParenLoc, ExprResult Literal = DefaultLvalueConversion(exprs[0]); if (Literal.isInvalid()) return ExprError(); - Literal = ImpCastExprToType(Literal.take(), ElemTy, + Literal = ImpCastExprToType(Literal.get(), ElemTy, PrepareScalarCast(Literal, ElemTy)); - return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take()); + return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.get()); } initExprs.append(exprs, exprs + numExprs); @@ -5226,7 +5366,7 @@ ExprResult Sema::MaybeConvertParenListExprToParenExpr(Scope *S, Expr *OrigExpr) { ParenListExpr *E = dyn_cast<ParenListExpr>(OrigExpr); if (!E) - return Owned(OrigExpr); + return OrigExpr; ExprResult Result(E->getExpr(0)); @@ -5243,7 +5383,7 @@ ExprResult Sema::ActOnParenListExpr(SourceLocation L, SourceLocation R, MultiExprArg Val) { Expr *expr = new (Context) ParenListExpr(Context, L, Val, R); - return Owned(expr); + return expr; } /// \brief Emit a specialized diagnostic when one expression is a null pointer @@ -5324,8 +5464,8 @@ static bool checkConditionalConvertScalarsToVectors(Sema &S, ExprResult &LHS, } // Implicity convert these scalars to the type of the condition. - LHS = S.ImpCastExprToType(LHS.take(), CondTy, CK_IntegralCast); - RHS = S.ImpCastExprToType(RHS.take(), CondTy, CK_IntegralCast); + LHS = S.ImpCastExprToType(LHS.get(), CondTy, CK_IntegralCast); + RHS = S.ImpCastExprToType(RHS.get(), CondTy, CK_IntegralCast); return false; } @@ -5341,8 +5481,8 @@ static QualType checkConditionalVoidType(Sema &S, ExprResult &LHS, if (!RHSExpr->getType()->isVoidType()) S.Diag(LHSExpr->getLocStart(), diag::ext_typecheck_cond_one_void) << LHSExpr->getSourceRange(); - LHS = S.ImpCastExprToType(LHS.take(), S.Context.VoidTy, CK_ToVoid); - RHS = S.ImpCastExprToType(RHS.take(), S.Context.VoidTy, CK_ToVoid); + LHS = S.ImpCastExprToType(LHS.get(), S.Context.VoidTy, CK_ToVoid); + RHS = S.ImpCastExprToType(RHS.get(), S.Context.VoidTy, CK_ToVoid); return S.Context.VoidTy; } @@ -5355,7 +5495,7 @@ static bool checkConditionalNullPointer(Sema &S, ExprResult &NullExpr, Expr::NPC_ValueDependentIsNull)) return true; - NullExpr = S.ImpCastExprToType(NullExpr.take(), PointerTy, CK_NullToPointer); + NullExpr = S.ImpCastExprToType(NullExpr.get(), PointerTy, CK_NullToPointer); return false; } @@ -5407,15 +5547,15 @@ static QualType checkConditionalPointerCompatibility(Sema &S, ExprResult &LHS, QualType CompositeTy = S.Context.mergeTypes(lhptee, rhptee); if (CompositeTy.isNull()) { - S.Diag(Loc, diag::warn_typecheck_cond_incompatible_pointers) + S.Diag(Loc, diag::ext_typecheck_cond_incompatible_pointers) << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); // In this situation, we assume void* type. No especially good // reason, but this is what gcc does, and we do have to pick // to get a consistent AST. QualType incompatTy = S.Context.getPointerType(S.Context.VoidTy); - LHS = S.ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); - RHS = S.ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); + LHS = S.ImpCastExprToType(LHS.get(), incompatTy, CK_BitCast); + RHS = S.ImpCastExprToType(RHS.get(), incompatTy, CK_BitCast); return incompatTy; } @@ -5426,11 +5566,41 @@ static QualType checkConditionalPointerCompatibility(Sema &S, ExprResult &LHS, else ResultTy = S.Context.getPointerType(ResultTy); - LHS = S.ImpCastExprToType(LHS.take(), ResultTy, CK_BitCast); - RHS = S.ImpCastExprToType(RHS.take(), ResultTy, CK_BitCast); + LHS = S.ImpCastExprToType(LHS.get(), ResultTy, CK_BitCast); + RHS = S.ImpCastExprToType(RHS.get(), ResultTy, CK_BitCast); return ResultTy; } +/// \brief Returns true if QT is quelified-id and implements 'NSObject' and/or +/// 'NSCopying' protocols (and nothing else); or QT is an NSObject and optionally +/// implements 'NSObject' and/or NSCopying' protocols (and nothing else). +static bool isObjCPtrBlockCompatible(Sema &S, ASTContext &C, QualType QT) { + if (QT->isObjCIdType()) + return true; + + const ObjCObjectPointerType *OPT = QT->getAs<ObjCObjectPointerType>(); + if (!OPT) + return false; + + if (ObjCInterfaceDecl *ID = OPT->getInterfaceDecl()) + if (ID->getIdentifier() != &C.Idents.get("NSObject")) + return false; + + ObjCProtocolDecl* PNSCopying = + S.LookupProtocol(&C.Idents.get("NSCopying"), SourceLocation()); + ObjCProtocolDecl* PNSObject = + S.LookupProtocol(&C.Idents.get("NSObject"), SourceLocation()); + + for (auto *Proto : OPT->quals()) { + if ((PNSCopying && declaresSameEntity(Proto, PNSCopying)) || + (PNSObject && declaresSameEntity(Proto, PNSObject))) + ; + else + return false; + } + return true; +} + /// \brief Return the resulting type when the operands are both block pointers. static QualType checkConditionalBlockPointerCompatibility(Sema &S, ExprResult &LHS, @@ -5442,8 +5612,8 @@ static QualType checkConditionalBlockPointerCompatibility(Sema &S, if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { QualType destType = S.Context.getPointerType(S.Context.VoidTy); - LHS = S.ImpCastExprToType(LHS.take(), destType, CK_BitCast); - RHS = S.ImpCastExprToType(RHS.take(), destType, CK_BitCast); + LHS = S.ImpCastExprToType(LHS.get(), destType, CK_BitCast); + RHS = S.ImpCastExprToType(RHS.get(), destType, CK_BitCast); return destType; } S.Diag(Loc, diag::err_typecheck_cond_incompatible_operands) @@ -5476,9 +5646,9 @@ checkConditionalObjectPointersCompatibility(Sema &S, ExprResult &LHS, = S.Context.getQualifiedType(lhptee, rhptee.getQualifiers()); QualType destType = S.Context.getPointerType(destPointee); // Add qualifiers if necessary. - LHS = S.ImpCastExprToType(LHS.take(), destType, CK_NoOp); + LHS = S.ImpCastExprToType(LHS.get(), destType, CK_NoOp); // Promote to void*. - RHS = S.ImpCastExprToType(RHS.take(), destType, CK_BitCast); + RHS = S.ImpCastExprToType(RHS.get(), destType, CK_BitCast); return destType; } if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { @@ -5486,9 +5656,9 @@ checkConditionalObjectPointersCompatibility(Sema &S, ExprResult &LHS, = S.Context.getQualifiedType(rhptee, lhptee.getQualifiers()); QualType destType = S.Context.getPointerType(destPointee); // Add qualifiers if necessary. - RHS = S.ImpCastExprToType(RHS.take(), destType, CK_NoOp); + RHS = S.ImpCastExprToType(RHS.get(), destType, CK_NoOp); // Promote to void*. - LHS = S.ImpCastExprToType(LHS.take(), destType, CK_BitCast); + LHS = S.ImpCastExprToType(LHS.get(), destType, CK_BitCast); return destType; } @@ -5507,10 +5677,10 @@ static bool checkPointerIntegerMismatch(Sema &S, ExprResult &Int, Expr *Expr1 = IsIntFirstExpr ? Int.get() : PointerExpr; Expr *Expr2 = IsIntFirstExpr ? PointerExpr : Int.get(); - S.Diag(Loc, diag::warn_typecheck_cond_pointer_integer_mismatch) + S.Diag(Loc, diag::ext_typecheck_cond_pointer_integer_mismatch) << Expr1->getType() << Expr2->getType() << Expr1->getSourceRange() << Expr2->getSourceRange(); - Int = S.ImpCastExprToType(Int.take(), PointerExpr->getType(), + Int = S.ImpCastExprToType(Int.get(), PointerExpr->getType(), CK_IntegralToPointer); return true; } @@ -5539,7 +5709,7 @@ QualType Sema::CheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, OK = OK_Ordinary; // First, check the condition. - Cond = UsualUnaryConversions(Cond.take()); + Cond = UsualUnaryConversions(Cond.get()); if (Cond.isInvalid()) return QualType(); if (checkCondition(*this, Cond.get())) @@ -5645,34 +5815,34 @@ QualType Sema::FindCompositeObjCPointerType(ExprResult &LHS, ExprResult &RHS, // redefinition type if an attempt is made to access its fields. if (LHSTy->isObjCClassType() && (Context.hasSameType(RHSTy, Context.getObjCClassRedefinitionType()))) { - RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_CPointerToObjCPointerCast); + RHS = ImpCastExprToType(RHS.get(), LHSTy, CK_CPointerToObjCPointerCast); return LHSTy; } if (RHSTy->isObjCClassType() && (Context.hasSameType(LHSTy, Context.getObjCClassRedefinitionType()))) { - LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_CPointerToObjCPointerCast); + LHS = ImpCastExprToType(LHS.get(), RHSTy, CK_CPointerToObjCPointerCast); return RHSTy; } // And the same for struct objc_object* / id if (LHSTy->isObjCIdType() && (Context.hasSameType(RHSTy, Context.getObjCIdRedefinitionType()))) { - RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_CPointerToObjCPointerCast); + RHS = ImpCastExprToType(RHS.get(), LHSTy, CK_CPointerToObjCPointerCast); return LHSTy; } if (RHSTy->isObjCIdType() && (Context.hasSameType(LHSTy, Context.getObjCIdRedefinitionType()))) { - LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_CPointerToObjCPointerCast); + LHS = ImpCastExprToType(LHS.get(), RHSTy, CK_CPointerToObjCPointerCast); return RHSTy; } // And the same for struct objc_selector* / SEL if (Context.isObjCSelType(LHSTy) && (Context.hasSameType(RHSTy, Context.getObjCSelRedefinitionType()))) { - RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); + RHS = ImpCastExprToType(RHS.get(), LHSTy, CK_BitCast); return LHSTy; } if (Context.isObjCSelType(RHSTy) && (Context.hasSameType(LHSTy, Context.getObjCSelRedefinitionType()))) { - LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); + LHS = ImpCastExprToType(LHS.get(), RHSTy, CK_BitCast); return RHSTy; } // Check constraints for Objective-C object pointers types. @@ -5721,13 +5891,13 @@ QualType Sema::FindCompositeObjCPointerType(ExprResult &LHS, ExprResult &RHS, << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); QualType incompatTy = Context.getObjCIdType(); - LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); - RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); + LHS = ImpCastExprToType(LHS.get(), incompatTy, CK_BitCast); + RHS = ImpCastExprToType(RHS.get(), incompatTy, CK_BitCast); return incompatTy; } // The object pointer types are compatible. - LHS = ImpCastExprToType(LHS.take(), compositeType, CK_BitCast); - RHS = ImpCastExprToType(RHS.take(), compositeType, CK_BitCast); + LHS = ImpCastExprToType(LHS.get(), compositeType, CK_BitCast); + RHS = ImpCastExprToType(RHS.get(), compositeType, CK_BitCast); return compositeType; } // Check Objective-C object pointer types and 'void *' @@ -5746,9 +5916,9 @@ QualType Sema::FindCompositeObjCPointerType(ExprResult &LHS, ExprResult &RHS, = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); QualType destType = Context.getPointerType(destPointee); // Add qualifiers if necessary. - LHS = ImpCastExprToType(LHS.take(), destType, CK_NoOp); + LHS = ImpCastExprToType(LHS.get(), destType, CK_NoOp); // Promote to void*. - RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); + RHS = ImpCastExprToType(RHS.get(), destType, CK_BitCast); return destType; } if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { @@ -5766,9 +5936,9 @@ QualType Sema::FindCompositeObjCPointerType(ExprResult &LHS, ExprResult &RHS, = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); QualType destType = Context.getPointerType(destPointee); // Add qualifiers if necessary. - RHS = ImpCastExprToType(RHS.take(), destType, CK_NoOp); + RHS = ImpCastExprToType(RHS.get(), destType, CK_NoOp); // Promote to void*. - LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); + LHS = ImpCastExprToType(LHS.get(), destType, CK_BitCast); return destType; } return QualType(); @@ -5900,9 +6070,9 @@ ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, Expr *RHSExpr) { // If this is the gnu "x ?: y" extension, analyze the types as though the LHS // was the condition. - OpaqueValueExpr *opaqueValue = 0; - Expr *commonExpr = 0; - if (LHSExpr == 0) { + OpaqueValueExpr *opaqueValue = nullptr; + Expr *commonExpr = nullptr; + if (!LHSExpr) { commonExpr = CondExpr; // Lower out placeholder types first. This is important so that we don't // try to capture a placeholder. This happens in few cases in C++; such @@ -5910,7 +6080,7 @@ ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, if (commonExpr->hasPlaceholderType()) { ExprResult result = CheckPlaceholderExpr(commonExpr); if (!result.isUsable()) return ExprError(); - commonExpr = result.take(); + commonExpr = result.get(); } // We usually want to apply unary conversions *before* saving, except // in the special case of a C++ l-value conditional. @@ -5924,7 +6094,7 @@ ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, ExprResult commonRes = UsualUnaryConversions(commonExpr); if (commonRes.isInvalid()) return ExprError(); - commonExpr = commonRes.take(); + commonExpr = commonRes.get(); } opaqueValue = new (Context) OpaqueValueExpr(commonExpr->getExprLoc(), @@ -5937,7 +6107,7 @@ ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, ExprValueKind VK = VK_RValue; ExprObjectKind OK = OK_Ordinary; - ExprResult Cond = Owned(CondExpr), LHS = Owned(LHSExpr), RHS = Owned(RHSExpr); + ExprResult Cond = CondExpr, LHS = LHSExpr, RHS = RHSExpr; QualType result = CheckConditionalOperands(Cond, LHS, RHS, VK, OK, QuestionLoc); if (result.isNull() || Cond.isInvalid() || LHS.isInvalid() || @@ -5948,14 +6118,13 @@ ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, RHS.get()); if (!commonExpr) - return Owned(new (Context) ConditionalOperator(Cond.take(), QuestionLoc, - LHS.take(), ColonLoc, - RHS.take(), result, VK, OK)); + return new (Context) + ConditionalOperator(Cond.get(), QuestionLoc, LHS.get(), ColonLoc, + RHS.get(), result, VK, OK); - return Owned(new (Context) - BinaryConditionalOperator(commonExpr, opaqueValue, Cond.take(), LHS.take(), - RHS.take(), QuestionLoc, ColonLoc, result, VK, - OK)); + return new (Context) BinaryConditionalOperator( + commonExpr, opaqueValue, Cond.get(), LHS.get(), RHS.get(), QuestionLoc, + ColonLoc, result, VK, OK); } // checkPointerTypesForAssignment - This is a very tricky routine (despite @@ -5971,15 +6140,16 @@ checkPointerTypesForAssignment(Sema &S, QualType LHSType, QualType RHSType) { // get the "pointed to" type (ignoring qualifiers at the top level) const Type *lhptee, *rhptee; Qualifiers lhq, rhq; - llvm::tie(lhptee, lhq) = cast<PointerType>(LHSType)->getPointeeType().split(); - llvm::tie(rhptee, rhq) = cast<PointerType>(RHSType)->getPointeeType().split(); + std::tie(lhptee, lhq) = + cast<PointerType>(LHSType)->getPointeeType().split().asPair(); + std::tie(rhptee, rhq) = + cast<PointerType>(RHSType)->getPointeeType().split().asPair(); Sema::AssignConvertType ConvTy = Sema::Compatible; // C99 6.5.16.1p1: This following citation is common to constraints // 3 & 4 (below). ...and the type *pointed to* by the left has all the // qualifiers of the type *pointed to* by the right; - Qualifiers lq; // As a special case, 'non-__weak A *' -> 'non-__weak const *' is okay. if (lhq.getObjCLifetime() != rhq.getObjCLifetime() && @@ -6206,7 +6376,7 @@ Sema::CheckAssignmentConstraints(QualType LHSType, ExprResult &RHS, if (result != Compatible) return result; if (Kind != CK_NoOp) - RHS = ImpCastExprToType(RHS.take(), AtomicTy->getValueType(), Kind); + RHS = ImpCastExprToType(RHS.get(), AtomicTy->getValueType(), Kind); Kind = CK_NonAtomicToAtomic; return Compatible; } @@ -6237,7 +6407,7 @@ Sema::CheckAssignmentConstraints(QualType LHSType, ExprResult &RHS, QualType elType = cast<ExtVectorType>(LHSType)->getElementType(); if (elType != RHSType) { Kind = PrepareScalarCast(RHS, elType); - RHS = ImpCastExprToType(RHS.take(), elType, Kind); + RHS = ImpCastExprToType(RHS.get(), elType, Kind); } Kind = CK_VectorSplat; return Compatible; @@ -6257,8 +6427,7 @@ Sema::CheckAssignmentConstraints(QualType LHSType, ExprResult &RHS, // If we are allowing lax vector conversions, and LHS and RHS are both // vectors, the total size only needs to be the same. This is a bitcast; // no bits are changed but the result type is different. - if (getLangOpts().LaxVectorConversions && - (Context.getTypeSize(LHSType) == Context.getTypeSize(RHSType))) { + if (isLaxVectorConversion(RHSType, LHSType)) { Kind = CK_BitCast; return IncompatibleVectors; } @@ -6389,8 +6558,9 @@ Sema::CheckAssignmentConstraints(QualType LHSType, ExprResult &RHS, return IncompatiblePointer; } - // T^ -> A* - if (RHSType->isBlockPointerType()) { + // Only under strict condition T^ is compatible with an Objective-C pointer. + if (RHSType->isBlockPointerType() && + isObjCPtrBlockCompatible(*this, Context, LHSType)) { maybeExtendBlockObject(*this, RHS); Kind = CK_BlockPointerToObjCPointerCast; return Compatible; @@ -6451,7 +6621,7 @@ static void ConstructTransparentUnion(Sema &S, ASTContext &C, FieldDecl *Field) { // Build an initializer list that designates the appropriate member // of the transparent union. - Expr *E = EResult.take(); + Expr *E = EResult.get(); InitListExpr *Initializer = new (C) InitListExpr(C, SourceLocation(), E, SourceLocation()); Initializer->setType(UnionType); @@ -6460,9 +6630,8 @@ static void ConstructTransparentUnion(Sema &S, ASTContext &C, // Build a compound literal constructing a value of the transparent // union type from this initializer list. TypeSourceInfo *unionTInfo = C.getTrivialTypeSourceInfo(UnionType); - EResult = S.Owned( - new (C) CompoundLiteralExpr(SourceLocation(), unionTInfo, UnionType, - VK_RValue, Initializer, false)); + EResult = new (C) CompoundLiteralExpr(SourceLocation(), unionTInfo, UnionType, + VK_RValue, Initializer, false); } Sema::AssignConvertType @@ -6478,27 +6647,25 @@ Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, // The field to initialize within the transparent union. RecordDecl *UD = UT->getDecl(); - FieldDecl *InitField = 0; + FieldDecl *InitField = nullptr; // It's compatible if the expression matches any of the fields. - for (RecordDecl::field_iterator it = UD->field_begin(), - itend = UD->field_end(); - it != itend; ++it) { + for (auto *it : UD->fields()) { if (it->getType()->isPointerType()) { // If the transparent union contains a pointer type, we allow: // 1) void pointer // 2) null pointer constant if (RHSType->isPointerType()) if (RHSType->castAs<PointerType>()->getPointeeType()->isVoidType()) { - RHS = ImpCastExprToType(RHS.take(), it->getType(), CK_BitCast); - InitField = *it; + RHS = ImpCastExprToType(RHS.get(), it->getType(), CK_BitCast); + InitField = it; break; } if (RHS.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { - RHS = ImpCastExprToType(RHS.take(), it->getType(), + RHS = ImpCastExprToType(RHS.get(), it->getType(), CK_NullToPointer); - InitField = *it; + InitField = it; break; } } @@ -6506,8 +6673,8 @@ Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, CastKind Kind = CK_Invalid; if (CheckAssignmentConstraints(it->getType(), RHS, Kind) == Compatible) { - RHS = ImpCastExprToType(RHS.take(), it->getType(), Kind); - InitField = *it; + RHS = ImpCastExprToType(RHS.get(), it->getType(), Kind); + InitField = it; break; } } @@ -6571,7 +6738,7 @@ Sema::CheckSingleAssignmentConstraints(QualType LHSType, ExprResult &RHS, CastKind Kind; CXXCastPath Path; CheckPointerConversion(RHS.get(), LHSType, Kind, Path, false); - RHS = ImpCastExprToType(RHS.take(), LHSType, Kind, VK_RValue, &Path); + RHS = ImpCastExprToType(RHS.get(), LHSType, Kind, VK_RValue, &Path); return Compatible; } @@ -6582,7 +6749,7 @@ Sema::CheckSingleAssignmentConstraints(QualType LHSType, ExprResult &RHS, // // Suppress this for references: C++ 8.5.3p5. if (!LHSType->isReferenceType()) { - RHS = DefaultFunctionArrayLvalueConversion(RHS.take()); + RHS = DefaultFunctionArrayLvalueConversion(RHS.get()); if (RHS.isInvalid()) return Incompatible; } @@ -6599,10 +6766,18 @@ Sema::CheckSingleAssignmentConstraints(QualType LHSType, ExprResult &RHS, // does not have reference type. if (result != Incompatible && RHS.get()->getType() != LHSType) { QualType Ty = LHSType.getNonLValueExprType(Context); - Expr *E = RHS.take(); + Expr *E = RHS.get(); if (getLangOpts().ObjCAutoRefCount) CheckObjCARCConversion(SourceRange(), Ty, E, CCK_ImplicitConversion, DiagnoseCFAudited); + if (getLangOpts().ObjC1 && + (CheckObjCBridgeRelatedConversions(E->getLocStart(), + LHSType, E->getType(), E) || + ConversionToObjCStringLiteralCheck(LHSType, E))) { + RHS = E; + return Compatible; + } + RHS = ImpCastExprToType(E, Ty, Kind); } return result; @@ -6616,97 +6791,128 @@ QualType Sema::InvalidOperands(SourceLocation Loc, ExprResult &LHS, return QualType(); } +/// Try to convert a value of non-vector type to a vector type by converting +/// the type to the element type of the vector and then performing a splat. +/// If the language is OpenCL, we only use conversions that promote scalar +/// rank; for C, Obj-C, and C++ we allow any real scalar conversion except +/// for float->int. +/// +/// \param scalar - if non-null, actually perform the conversions +/// \return true if the operation fails (but without diagnosing the failure) +static bool tryVectorConvertAndSplat(Sema &S, ExprResult *scalar, + QualType scalarTy, + QualType vectorEltTy, + QualType vectorTy) { + // The conversion to apply to the scalar before splatting it, + // if necessary. + CastKind scalarCast = CK_Invalid; + + if (vectorEltTy->isIntegralType(S.Context)) { + if (!scalarTy->isIntegralType(S.Context)) + return true; + if (S.getLangOpts().OpenCL && + S.Context.getIntegerTypeOrder(vectorEltTy, scalarTy) < 0) + return true; + scalarCast = CK_IntegralCast; + } else if (vectorEltTy->isRealFloatingType()) { + if (scalarTy->isRealFloatingType()) { + if (S.getLangOpts().OpenCL && + S.Context.getFloatingTypeOrder(vectorEltTy, scalarTy) < 0) + return true; + scalarCast = CK_FloatingCast; + } + else if (scalarTy->isIntegralType(S.Context)) + scalarCast = CK_IntegralToFloating; + else + return true; + } else { + return true; + } + + // Adjust scalar if desired. + if (scalar) { + if (scalarCast != CK_Invalid) + *scalar = S.ImpCastExprToType(scalar->get(), vectorEltTy, scalarCast); + *scalar = S.ImpCastExprToType(scalar->get(), vectorTy, CK_VectorSplat); + } + return false; +} + QualType Sema::CheckVectorOperands(ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, bool IsCompAssign) { if (!IsCompAssign) { - LHS = DefaultFunctionArrayLvalueConversion(LHS.take()); + LHS = DefaultFunctionArrayLvalueConversion(LHS.get()); if (LHS.isInvalid()) return QualType(); } - RHS = DefaultFunctionArrayLvalueConversion(RHS.take()); + RHS = DefaultFunctionArrayLvalueConversion(RHS.get()); if (RHS.isInvalid()) return QualType(); // For conversion purposes, we ignore any qualifiers. // For example, "const float" and "float" are equivalent. - QualType LHSType = - Context.getCanonicalType(LHS.get()->getType()).getUnqualifiedType(); - QualType RHSType = - Context.getCanonicalType(RHS.get()->getType()).getUnqualifiedType(); + QualType LHSType = LHS.get()->getType().getUnqualifiedType(); + QualType RHSType = RHS.get()->getType().getUnqualifiedType(); // If the vector types are identical, return. - if (LHSType == RHSType) + if (Context.hasSameType(LHSType, RHSType)) return LHSType; - // Handle the case of equivalent AltiVec and GCC vector types - if (LHSType->isVectorType() && RHSType->isVectorType() && + const VectorType *LHSVecType = LHSType->getAs<VectorType>(); + const VectorType *RHSVecType = RHSType->getAs<VectorType>(); + assert(LHSVecType || RHSVecType); + + // If we have compatible AltiVec and GCC vector types, use the AltiVec type. + if (LHSVecType && RHSVecType && Context.areCompatibleVectorTypes(LHSType, RHSType)) { - if (LHSType->isExtVectorType()) { - RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); + if (isa<ExtVectorType>(LHSVecType)) { + RHS = ImpCastExprToType(RHS.get(), LHSType, CK_BitCast); return LHSType; } if (!IsCompAssign) - LHS = ImpCastExprToType(LHS.take(), RHSType, CK_BitCast); + LHS = ImpCastExprToType(LHS.get(), RHSType, CK_BitCast); return RHSType; } - if (getLangOpts().LaxVectorConversions && - Context.getTypeSize(LHSType) == Context.getTypeSize(RHSType)) { - // If we are allowing lax vector conversions, and LHS and RHS are both - // vectors, the total size only needs to be the same. This is a - // bitcast; no bits are changed but the result type is different. - // FIXME: Should we really be allowing this? - RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); - return LHSType; + // If there's an ext-vector type and a scalar, try to convert the scalar to + // the vector element type and splat. + if (!RHSVecType && isa<ExtVectorType>(LHSVecType)) { + if (!tryVectorConvertAndSplat(*this, &RHS, RHSType, + LHSVecType->getElementType(), LHSType)) + return LHSType; + } + if (!LHSVecType && isa<ExtVectorType>(RHSVecType)) { + if (!tryVectorConvertAndSplat(*this, (IsCompAssign ? nullptr : &LHS), + LHSType, RHSVecType->getElementType(), + RHSType)) + return RHSType; } - // Canonicalize the ExtVector to the LHS, remember if we swapped so we can - // swap back (so that we don't reverse the inputs to a subtract, for instance. - bool swapped = false; - if (RHSType->isExtVectorType() && !IsCompAssign) { - swapped = true; - std::swap(RHS, LHS); - std::swap(RHSType, LHSType); - } - - // Handle the case of an ext vector and scalar. - if (const ExtVectorType *LV = LHSType->getAs<ExtVectorType>()) { - QualType EltTy = LV->getElementType(); - if (EltTy->isIntegralType(Context) && RHSType->isIntegralType(Context)) { - int order = Context.getIntegerTypeOrder(EltTy, RHSType); - if (order > 0) - RHS = ImpCastExprToType(RHS.take(), EltTy, CK_IntegralCast); - if (order >= 0) { - RHS = ImpCastExprToType(RHS.take(), LHSType, CK_VectorSplat); - if (swapped) std::swap(RHS, LHS); - return LHSType; - } - } - if (EltTy->isRealFloatingType() && RHSType->isScalarType()) { - if (RHSType->isRealFloatingType()) { - int order = Context.getFloatingTypeOrder(EltTy, RHSType); - if (order > 0) - RHS = ImpCastExprToType(RHS.take(), EltTy, CK_FloatingCast); - if (order >= 0) { - RHS = ImpCastExprToType(RHS.take(), LHSType, CK_VectorSplat); - if (swapped) std::swap(RHS, LHS); - return LHSType; - } - } - if (RHSType->isIntegralType(Context)) { - RHS = ImpCastExprToType(RHS.take(), EltTy, CK_IntegralToFloating); - RHS = ImpCastExprToType(RHS.take(), LHSType, CK_VectorSplat); - if (swapped) std::swap(RHS, LHS); - return LHSType; - } - } + // If we're allowing lax vector conversions, only the total (data) size + // needs to be the same. + // FIXME: Should we really be allowing this? + // FIXME: We really just pick the LHS type arbitrarily? + if (isLaxVectorConversion(RHSType, LHSType)) { + QualType resultType = LHSType; + RHS = ImpCastExprToType(RHS.get(), resultType, CK_BitCast); + return resultType; } - // Vectors of different size or scalar and non-ext-vector are errors. - if (swapped) std::swap(RHS, LHS); + // Okay, the expression is invalid. + + // If there's a non-vector, non-real operand, diagnose that. + if ((!RHSVecType && !RHSType->isRealType()) || + (!LHSVecType && !LHSType->isRealType())) { + Diag(Loc, diag::err_typecheck_vector_not_convertable_non_scalar) + << LHSType << RHSType + << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); + return QualType(); + } + + // Otherwise, use the generic diagnostic. Diag(Loc, diag::err_typecheck_vector_not_convertable) - << LHS.get()->getType() << RHS.get()->getType() + << LHSType << RHSType << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); return QualType(); } @@ -7159,7 +7365,7 @@ QualType Sema::CheckSubtractionOperands(ExprResult &LHS, ExprResult &RHS, return QualType(); // Check array bounds for pointer arithemtic - CheckArrayAccess(LHS.get(), RHS.get(), /*ArraySubscriptExpr*/0, + CheckArrayAccess(LHS.get(), RHS.get(), /*ArraySubscriptExpr*/nullptr, /*AllowOnePastEnd*/true, /*IndexNegated*/true); if (CompLHSTy) *CompLHSTy = LHS.get()->getType(); @@ -7302,14 +7508,14 @@ QualType Sema::CheckShiftOperands(ExprResult &LHS, ExprResult &RHS, // For the LHS, do usual unary conversions, but then reset them away // if this is a compound assignment. ExprResult OldLHS = LHS; - LHS = UsualUnaryConversions(LHS.take()); + LHS = UsualUnaryConversions(LHS.get()); if (LHS.isInvalid()) return QualType(); QualType LHSType = LHS.get()->getType(); if (IsCompAssign) LHS = OldLHS; // The RHS is simpler. - RHS = UsualUnaryConversions(RHS.take()); + RHS = UsualUnaryConversions(RHS.get()); if (RHS.isInvalid()) return QualType(); QualType RHSType = RHS.get()->getType(); @@ -7409,7 +7615,7 @@ static bool convertPointersToCompositeType(Sema &S, SourceLocation Loc, (LHSType->isMemberPointerType() && RHSType->isMemberPointerType())); bool NonStandardCompositeType = false; - bool *BoolPtr = S.isSFINAEContext() ? 0 : &NonStandardCompositeType; + bool *BoolPtr = S.isSFINAEContext() ? nullptr : &NonStandardCompositeType; QualType T = S.FindCompositePointerType(Loc, LHS, RHS, BoolPtr); if (T.isNull()) { diagnoseDistinctPointerComparison(S, Loc, LHS, RHS, /*isError*/true); @@ -7421,8 +7627,8 @@ static bool convertPointersToCompositeType(Sema &S, SourceLocation Loc, << LHSType << RHSType << T << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); - LHS = S.ImpCastExprToType(LHS.take(), T, CK_BitCast); - RHS = S.ImpCastExprToType(RHS.take(), T, CK_BitCast); + LHS = S.ImpCastExprToType(LHS.get(), T, CK_BitCast); + RHS = S.ImpCastExprToType(RHS.get(), T, CK_BitCast); return false; } @@ -7488,11 +7694,11 @@ static bool hasIsEqualMethod(Sema &S, const Expr *LHS, const Expr *RHS) { if (!Method) return false; - QualType T = Method->param_begin()[0]->getType(); + QualType T = Method->parameters()[0]->getType(); if (!T->isObjCObjectPointerType()) return false; - - QualType R = Method->getResultType(); + + QualType R = Method->getReturnType(); if (!R->isScalarType()) return false; @@ -7646,7 +7852,7 @@ static ValueDecl *getCompareDecl(Expr *E) { if (Mem->isImplicitAccess()) return Mem->getMemberDecl(); } - return 0; + return nullptr; } // C99 6.5.8, C++ [expr.rel] @@ -7689,7 +7895,7 @@ QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS, ValueDecl *DL = getCompareDecl(LHSStripped); ValueDecl *DR = getCompareDecl(RHSStripped); if (DL && DR && DL == DR && !IsWithinTemplateSpecialization(DL)) { - DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) + DiagRuntimeBehavior(Loc, nullptr, PDiag(diag::warn_comparison_always) << 0 // self- << (Opc == BO_EQ || Opc == BO_LE @@ -7711,7 +7917,7 @@ QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS, always_evals_to = 2; // e.g. array1 <= array2 break; } - DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) + DiagRuntimeBehavior(Loc, nullptr, PDiag(diag::warn_comparison_always) << 1 // array << always_evals_to); } @@ -7723,8 +7929,8 @@ QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS, // Warn about comparisons against a string constant (unless the other // operand is null), the user probably wants strcmp. - Expr *literalString = 0; - Expr *literalStringStripped = 0; + Expr *literalString = nullptr; + Expr *literalStringStripped = nullptr; if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && !RHSStripped->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { @@ -7739,7 +7945,7 @@ QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS, } if (literalString) { - DiagRuntimeBehavior(Loc, 0, + DiagRuntimeBehavior(Loc, nullptr, PDiag(diag::warn_stringcompare) << isa<ObjCEncodeExpr>(literalStringStripped) << literalString->getSourceRange()); @@ -7769,10 +7975,22 @@ QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS, return ResultTy; } - bool LHSIsNull = LHS.get()->isNullPointerConstant(Context, - Expr::NPC_ValueDependentIsNull); - bool RHSIsNull = RHS.get()->isNullPointerConstant(Context, - Expr::NPC_ValueDependentIsNull); + const Expr::NullPointerConstantKind LHSNullKind = + LHS.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull); + const Expr::NullPointerConstantKind RHSNullKind = + RHS.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull); + bool LHSIsNull = LHSNullKind != Expr::NPCK_NotNull; + bool RHSIsNull = RHSNullKind != Expr::NPCK_NotNull; + + if (!IsRelational && LHSIsNull != RHSIsNull) { + bool IsEquality = Opc == BO_EQ; + if (RHSIsNull) + DiagnoseAlwaysNonNullPointer(LHS.get(), RHSNullKind, IsEquality, + RHS.get()->getSourceRange()); + else + DiagnoseAlwaysNonNullPointer(RHS.get(), LHSNullKind, IsEquality, + LHS.get()->getSourceRange()); + } // All of the following pointer-related warnings are GCC extensions, except // when handling null pointer constants. @@ -7799,7 +8017,7 @@ QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS, if (isSFINAEContext()) return QualType(); - RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); + RHS = ImpCastExprToType(RHS.get(), LHSType, CK_BitCast); return ResultTy; } } @@ -7830,10 +8048,14 @@ QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS, diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, /*isError*/false); } if (LCanPointeeTy != RCanPointeeTy) { + unsigned AddrSpaceL = LCanPointeeTy.getAddressSpace(); + unsigned AddrSpaceR = RCanPointeeTy.getAddressSpace(); + CastKind Kind = AddrSpaceL != AddrSpaceR ? CK_AddressSpaceConversion + : CK_BitCast; if (LHSIsNull && !RHSIsNull) - LHS = ImpCastExprToType(LHS.take(), RHSType, CK_BitCast); + LHS = ImpCastExprToType(LHS.get(), RHSType, Kind); else - RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); + RHS = ImpCastExprToType(RHS.get(), LHSType, Kind); } return ResultTy; } @@ -7849,7 +8071,7 @@ QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS, ((LHSType->isAnyPointerType() || LHSType->isNullPtrType()) || (!IsRelational && (LHSType->isMemberPointerType() || LHSType->isBlockPointerType())))) { - RHS = ImpCastExprToType(RHS.take(), LHSType, + RHS = ImpCastExprToType(RHS.get(), LHSType, LHSType->isMemberPointerType() ? CK_NullToMemberPointer : CK_NullToPointer); @@ -7859,7 +8081,7 @@ QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS, ((RHSType->isAnyPointerType() || RHSType->isNullPtrType()) || (!IsRelational && (RHSType->isMemberPointerType() || RHSType->isBlockPointerType())))) { - LHS = ImpCastExprToType(LHS.take(), RHSType, + LHS = ImpCastExprToType(LHS.get(), RHSType, RHSType->isMemberPointerType() ? CK_NullToMemberPointer : CK_NullToPointer); @@ -7895,7 +8117,7 @@ QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS, << LHSType << RHSType << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); } - RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); + RHS = ImpCastExprToType(RHS.get(), LHSType, CK_BitCast); return ResultTy; } @@ -7913,11 +8135,11 @@ QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS, << RHS.get()->getSourceRange(); } if (LHSIsNull && !RHSIsNull) - LHS = ImpCastExprToType(LHS.take(), RHSType, + LHS = ImpCastExprToType(LHS.get(), RHSType, RHSType->isPointerType() ? CK_BitCast : CK_AnyPointerToBlockPointerCast); else - RHS = ImpCastExprToType(RHS.take(), LHSType, + RHS = ImpCastExprToType(RHS.get(), LHSType, LHSType->isPointerType() ? CK_BitCast : CK_AnyPointerToBlockPointerCast); return ResultTy; @@ -7937,16 +8159,17 @@ QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS, /*isError*/false); } if (LHSIsNull && !RHSIsNull) { - Expr *E = LHS.take(); + Expr *E = LHS.get(); if (getLangOpts().ObjCAutoRefCount) CheckObjCARCConversion(SourceRange(), RHSType, E, CCK_ImplicitConversion); LHS = ImpCastExprToType(E, RHSType, RPT ? CK_BitCast :CK_CPointerToObjCPointerCast); } else { - Expr *E = RHS.take(); + Expr *E = RHS.get(); if (getLangOpts().ObjCAutoRefCount) - CheckObjCARCConversion(SourceRange(), LHSType, E, CCK_ImplicitConversion); + CheckObjCARCConversion(SourceRange(), LHSType, E, CCK_ImplicitConversion, false, + Opc); RHS = ImpCastExprToType(E, LHSType, LPT ? CK_BitCast :CK_CPointerToObjCPointerCast); } @@ -7961,9 +8184,9 @@ QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS, diagnoseObjCLiteralComparison(*this, Loc, LHS, RHS, Opc); if (LHSIsNull && !RHSIsNull) - LHS = ImpCastExprToType(LHS.take(), RHSType, CK_BitCast); + LHS = ImpCastExprToType(LHS.get(), RHSType, CK_BitCast); else - RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); + RHS = ImpCastExprToType(RHS.get(), LHSType, CK_BitCast); return ResultTy; } } @@ -7995,10 +8218,10 @@ QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS, } if (LHSType->isIntegerType()) - LHS = ImpCastExprToType(LHS.take(), RHSType, + LHS = ImpCastExprToType(LHS.get(), RHSType, LHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); else - RHS = ImpCastExprToType(RHS.take(), LHSType, + RHS = ImpCastExprToType(RHS.get(), LHSType, RHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); return ResultTy; } @@ -8006,12 +8229,12 @@ QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS, // Handle block pointers. if (!IsRelational && RHSIsNull && LHSType->isBlockPointerType() && RHSType->isIntegerType()) { - RHS = ImpCastExprToType(RHS.take(), LHSType, CK_NullToPointer); + RHS = ImpCastExprToType(RHS.get(), LHSType, CK_NullToPointer); return ResultTy; } if (!IsRelational && LHSIsNull && LHSType->isIntegerType() && RHSType->isBlockPointerType()) { - LHS = ImpCastExprToType(LHS.take(), RHSType, CK_NullToPointer); + LHS = ImpCastExprToType(LHS.get(), RHSType, CK_NullToPointer); return ResultTy; } @@ -8068,7 +8291,7 @@ QualType Sema::CheckVectorCompareOperands(ExprResult &LHS, ExprResult &RHS, if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHS.get()->IgnoreParenImpCasts())) if (DRL->getDecl() == DRR->getDecl()) - DiagRuntimeBehavior(Loc, 0, + DiagRuntimeBehavior(Loc, nullptr, PDiag(diag::warn_comparison_always) << 0 // self- << 2 // "a constant" @@ -8112,13 +8335,13 @@ inline QualType Sema::CheckBitwiseOperands( return InvalidOperands(Loc, LHS, RHS); } - ExprResult LHSResult = Owned(LHS), RHSResult = Owned(RHS); + ExprResult LHSResult = LHS, RHSResult = RHS; QualType compType = UsualArithmeticConversions(LHSResult, RHSResult, IsCompAssign); if (LHSResult.isInvalid() || RHSResult.isInvalid()) return QualType(); - LHS = LHSResult.take(); - RHS = RHSResult.take(); + LHS = LHSResult.get(); + RHS = RHSResult.get(); if (!compType.isNull() && compType->isIntegralOrUnscopedEnumerationType()) return compType; @@ -8146,7 +8369,8 @@ inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] // Parens on the RHS are ignored. llvm::APSInt Result; if (RHS.get()->EvaluateAsInt(Result, Context)) - if ((getLangOpts().Bool && !RHS.get()->getType()->isBooleanType()) || + if ((getLangOpts().Bool && !RHS.get()->getType()->isBooleanType() && + !RHS.get()->getExprLoc().isMacroID()) || (Result != 0 && Result != 1)) { Diag(Loc, diag::warn_logical_instead_of_bitwise) << RHS.get()->getSourceRange() @@ -8180,11 +8404,11 @@ inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] return InvalidOperands(Loc, LHS, RHS); } - LHS = UsualUnaryConversions(LHS.take()); + LHS = UsualUnaryConversions(LHS.get()); if (LHS.isInvalid()) return QualType(); - RHS = UsualUnaryConversions(RHS.take()); + RHS = UsualUnaryConversions(RHS.get()); if (RHS.isInvalid()) return QualType(); @@ -8224,7 +8448,7 @@ static bool IsReadonlyMessage(Expr *E, Sema &S) { ObjCMessageExpr *Base = dyn_cast<ObjCMessageExpr>(ME->getBase()->IgnoreParenImpCasts()); if (!Base) return false; - return Base->getMethodDecl() != 0; + return Base->getMethodDecl() != nullptr; } /// Is the given expression (which must be 'const') a reference to a @@ -8247,7 +8471,7 @@ static NonConstCaptureKind isReferenceToNonConstCapture(Sema &S, Expr *E) { assert(var->hasLocalStorage() && "capture added 'const' to non-local?"); // Decide whether the first capture was for a block or a lambda. - DeclContext *DC = S.CurContext, *Prev = 0; + DeclContext *DC = S.CurContext, *Prev = nullptr; while (DC != var->getDeclContext()) { Prev = DC; DC = DC->getParent(); @@ -8464,10 +8688,8 @@ QualType Sema::CheckAssignmentOperands(Expr *LHSExpr, ExprResult &RHS, // we do not warn to warn spuriously when 'x' and 'y' are on separate // paths through the function. This should be revisited if // -Wrepeated-use-of-weak is made flow-sensitive. - DiagnosticsEngine::Level Level = - Diags.getDiagnosticLevel(diag::warn_arc_repeated_use_of_weak, - RHS.get()->getLocStart()); - if (Level != DiagnosticsEngine::Ignored) + if (!Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, + RHS.get()->getLocStart())) getCurFunction()->markSafeWeakUse(RHS.get()); } else if (getLangOpts().ObjCAutoRefCount) { @@ -8499,8 +8721,8 @@ QualType Sema::CheckAssignmentOperands(Expr *LHSExpr, ExprResult &RHS, // C99 6.5.17 static QualType CheckCommaOperands(Sema &S, ExprResult &LHS, ExprResult &RHS, SourceLocation Loc) { - LHS = S.CheckPlaceholderExpr(LHS.take()); - RHS = S.CheckPlaceholderExpr(RHS.take()); + LHS = S.CheckPlaceholderExpr(LHS.get()); + RHS = S.CheckPlaceholderExpr(RHS.get()); if (LHS.isInvalid() || RHS.isInvalid()) return QualType(); @@ -8510,14 +8732,14 @@ static QualType CheckCommaOperands(Sema &S, ExprResult &LHS, ExprResult &RHS, // So we treat the LHS as a ignored value, and in C++ we allow the // containing site to determine what should be done with the RHS. - LHS = S.IgnoredValueConversions(LHS.take()); + LHS = S.IgnoredValueConversions(LHS.get()); if (LHS.isInvalid()) return QualType(); S.DiagnoseUnusedExprResult(LHS.get()); if (!S.getLangOpts().CPlusPlus) { - RHS = S.DefaultFunctionArrayLvalueConversion(RHS.take()); + RHS = S.DefaultFunctionArrayLvalueConversion(RHS.get()); if (RHS.isInvalid()) return QualType(); if (!RHS.get()->getType()->isVoidType()) @@ -8577,7 +8799,7 @@ static QualType CheckIncrementDecrementOperand(Sema &S, Expr *Op, } else if (ResType->isPlaceholderType()) { ExprResult PR = S.CheckPlaceholderExpr(Op); if (PR.isInvalid()) return QualType(); - return CheckIncrementDecrementOperand(S, PR.take(), VK, OpLoc, + return CheckIncrementDecrementOperand(S, PR.get(), VK, OpLoc, IsInc, IsPrefix); } else if (S.getLangOpts().AltiVec && ResType->isVectorType()) { // OK! ( C/C++ Language Extensions for CBEA(Version 2.6) 10.3 ) @@ -8627,7 +8849,7 @@ static ValueDecl *getPrimaryDecl(Expr *E) { // the base's value, so the object the base refers to is // irrelevant. if (cast<MemberExpr>(E)->isArrow()) - return 0; + return nullptr; // Otherwise, the expression refers to a part of the base return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); case Stmt::ArraySubscriptExprClass: { @@ -8638,7 +8860,7 @@ static ValueDecl *getPrimaryDecl(Expr *E) { if (ICE->getSubExpr()->getType()->isArrayType()) return getPrimaryDecl(ICE->getSubExpr()); } - return 0; + return nullptr; } case Stmt::UnaryOperatorClass: { UnaryOperator *UO = cast<UnaryOperator>(E); @@ -8649,7 +8871,7 @@ static ValueDecl *getPrimaryDecl(Expr *E) { case UO_Extension: return getPrimaryDecl(UO->getSubExpr()); default: - return 0; + return nullptr; } } case Stmt::ParenExprClass: @@ -8659,7 +8881,7 @@ static ValueDecl *getPrimaryDecl(Expr *E) { // the sub-expression; otherwise, the result here doesn't matter. return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); default: - return 0; + return nullptr; } } @@ -8718,7 +8940,7 @@ QualType Sema::CheckAddressOfOperand(ExprResult &OrigOp, SourceLocation OpLoc) { return QualType(); } - OrigOp = CheckPlaceholderExpr(OrigOp.take()); + OrigOp = CheckPlaceholderExpr(OrigOp.get()); if (OrigOp.isInvalid()) return QualType(); } @@ -8730,6 +8952,12 @@ QualType Sema::CheckAddressOfOperand(ExprResult &OrigOp, SourceLocation OpLoc) { // Make sure to ignore parentheses in subsequent checks Expr *op = OrigOp.get()->IgnoreParens(); + // OpenCL v1.0 s6.8.a.3: Pointers to functions are not allowed. + if (LangOpts.OpenCL && op->getType()->isFunctionType()) { + Diag(op->getExprLoc(), diag::err_opencl_taking_function_address); + return QualType(); + } + if (getLangOpts().C99) { // Implement C99-only parts of addressof rules. if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { @@ -8754,7 +8982,7 @@ QualType Sema::CheckAddressOfOperand(ExprResult &OrigOp, SourceLocation OpLoc) { return QualType(); // Materialize the temporary as an lvalue so that we can take its address. OrigOp = op = new (Context) - MaterializeTemporaryExpr(op->getType(), OrigOp.take(), true, 0); + MaterializeTemporaryExpr(op->getType(), OrigOp.get(), true); } else if (isa<ObjCSelectorExpr>(op)) { return Context.getPointerType(op->getType()); } else if (lval == Expr::LV_MemberFunction) { @@ -8793,8 +9021,11 @@ QualType Sema::CheckAddressOfOperand(ExprResult &OrigOp, SourceLocation OpLoc) { if (isa<CXXDestructorDecl>(MD)) Diag(OpLoc, diag::err_typecheck_addrof_dtor) << op->getSourceRange(); - return Context.getMemberPointerType(op->getType(), - Context.getTypeDeclType(MD->getParent()).getTypePtr()); + QualType MPTy = Context.getMemberPointerType( + op->getType(), Context.getTypeDeclType(MD->getParent()).getTypePtr()); + if (Context.getTargetInfo().getCXXABI().isMicrosoft()) + RequireCompleteType(OpLoc, MPTy, 0); + return MPTy; } else if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { // C99 6.5.3.2p1 // The operand must be either an l-value or a function designator @@ -8842,8 +9073,13 @@ QualType Sema::CheckAddressOfOperand(ExprResult &OrigOp, SourceLocation OpLoc) { while (cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()) Ctx = Ctx->getParent(); - return Context.getMemberPointerType(op->getType(), - Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); + + QualType MPTy = Context.getMemberPointerType( + op->getType(), + Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); + if (Context.getTargetInfo().getCXXABI().isMicrosoft()) + RequireCompleteType(OpLoc, MPTy, 0); + return MPTy; } } } else if (!isa<FunctionDecl>(dcl) && !isa<NonTypeTemplateParmDecl>(dcl)) @@ -8877,7 +9113,7 @@ static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK, ExprResult ConvResult = S.UsualUnaryConversions(Op); if (ConvResult.isInvalid()) return QualType(); - Op = ConvResult.take(); + Op = ConvResult.get(); QualType OpTy = Op->getType(); QualType Result; @@ -8887,10 +9123,6 @@ static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK, Op->getSourceRange()); } - // Note that per both C89 and C99, indirection is always legal, even if OpTy - // is an incomplete type or void. It would be possible to warn about - // dereferencing a void pointer, but it's completely well-defined, and such a - // warning is unlikely to catch any mistakes. if (const PointerType *PT = OpTy->getAs<PointerType>()) Result = PT->getPointeeType(); else if (const ObjCObjectPointerType *OPT = @@ -8899,8 +9131,8 @@ static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK, else { ExprResult PR = S.CheckPlaceholderExpr(Op); if (PR.isInvalid()) return QualType(); - if (PR.take() != Op) - return CheckIndirectionOperand(S, PR.take(), VK, OpLoc); + if (PR.get() != Op) + return CheckIndirectionOperand(S, PR.get(), VK, OpLoc); } if (Result.isNull()) { @@ -8909,6 +9141,19 @@ static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK, return QualType(); } + // Note that per both C89 and C99, indirection is always legal, even if Result + // is an incomplete type or void. It would be possible to warn about + // dereferencing a void pointer, but it's completely well-defined, and such a + // warning is unlikely to catch any mistakes. In C++, indirection is not valid + // for pointers to 'void' but is fine for any other pointer type: + // + // C++ [expr.unary.op]p1: + // [...] the expression to which [the unary * operator] is applied shall + // be a pointer to an object type, or a pointer to a function type + if (S.getLangOpts().CPlusPlus && Result->isVoidType()) + S.Diag(OpLoc, diag::ext_typecheck_indirection_through_void_pointer) + << OpTy << Op->getSourceRange(); + // Dereferences are usually l-values... VK = VK_LValue; @@ -9021,7 +9266,7 @@ static void checkObjCPointerIntrospection(Sema &S, ExprResult &L, ExprResult &R, if (!S.getLangOpts().ObjC1) return; - const Expr *ObjCPointerExpr = 0, *OtherExpr = 0; + const Expr *ObjCPointerExpr = nullptr, *OtherExpr = nullptr; const Expr *LHS = L.get(); const Expr *RHS = R.get(); @@ -9078,10 +9323,10 @@ ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, ExprResult Init = InitSeq.Perform(*this, Entity, Kind, RHSExpr); if (Init.isInvalid()) return Init; - RHSExpr = Init.take(); + RHSExpr = Init.get(); } - ExprResult LHS = Owned(LHSExpr), RHS = Owned(RHSExpr); + ExprResult LHS = LHSExpr, RHS = RHSExpr; QualType ResultTy; // Result type of the binary operator. // The following two variables are used for compound assignment operators QualType CompLHSTy; // Type of LHS after promotions for computation @@ -9175,8 +9420,9 @@ ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); break; case BO_AndAssign: + case BO_OrAssign: // fallthrough + DiagnoseSelfAssignment(*this, LHS.get(), RHS.get(), OpLoc); case BO_XorAssign: - case BO_OrAssign: CompResultTy = CheckBitwiseOperands(LHS, RHS, OpLoc, true); CompLHSTy = CompResultTy; if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) @@ -9216,18 +9462,16 @@ ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, DiagnoseDirectIsaAccess(*this, OIRE, OpLoc, RHS.get()); if (CompResultTy.isNull()) - return Owned(new (Context) BinaryOperator(LHS.take(), RHS.take(), Opc, - ResultTy, VK, OK, OpLoc, - FPFeatures.fp_contract)); + return new (Context) BinaryOperator(LHS.get(), RHS.get(), Opc, ResultTy, VK, + OK, OpLoc, FPFeatures.fp_contract); if (getLangOpts().CPlusPlus && LHS.get()->getObjectKind() != OK_ObjCProperty) { VK = VK_LValue; OK = LHS.get()->getObjectKind(); } - return Owned(new (Context) CompoundAssignOperator(LHS.take(), RHS.take(), Opc, - ResultTy, VK, OK, CompLHSTy, - CompResultTy, OpLoc, - FPFeatures.fp_contract)); + return new (Context) CompoundAssignOperator( + LHS.get(), RHS.get(), Opc, ResultTy, VK, OK, CompLHSTy, CompResultTy, + OpLoc, FPFeatures.fp_contract); } /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison @@ -9445,8 +9689,8 @@ ExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, tok::TokenKind Kind, Expr *LHSExpr, Expr *RHSExpr) { BinaryOperatorKind Opc = ConvertTokenKindToBinaryOpcode(Kind); - assert((LHSExpr != 0) && "ActOnBinOp(): missing left expression"); - assert((RHSExpr != 0) && "ActOnBinOp(): missing right expression"); + assert(LHSExpr && "ActOnBinOp(): missing left expression"); + assert(RHSExpr && "ActOnBinOp(): missing right expression"); // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" DiagnoseBinOpPrecedence(*this, Opc, TokLoc, LHSExpr, RHSExpr); @@ -9499,7 +9743,7 @@ ExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, // instantiates to having an overloadable type. ExprResult resolvedRHS = CheckPlaceholderExpr(RHSExpr); if (resolvedRHS.isInvalid()) return ExprError(); - RHSExpr = resolvedRHS.take(); + RHSExpr = resolvedRHS.get(); if (RHSExpr->isTypeDependent() || RHSExpr->getType()->isOverloadableType()) @@ -9508,7 +9752,7 @@ ExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, ExprResult LHS = CheckPlaceholderExpr(LHSExpr); if (LHS.isInvalid()) return ExprError(); - LHSExpr = LHS.take(); + LHSExpr = LHS.get(); } // Handle pseudo-objects in the RHS. @@ -9532,7 +9776,7 @@ ExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, ExprResult resolvedRHS = CheckPlaceholderExpr(RHSExpr); if (!resolvedRHS.isUsable()) return ExprError(); - RHSExpr = resolvedRHS.take(); + RHSExpr = resolvedRHS.get(); } if (getLangOpts().CPlusPlus) { @@ -9555,7 +9799,7 @@ ExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, UnaryOperatorKind Opc, Expr *InputExpr) { - ExprResult Input = Owned(InputExpr); + ExprResult Input = InputExpr; ExprValueKind VK = VK_RValue; ExprObjectKind OK = OK_Ordinary; QualType resultType; @@ -9574,14 +9818,14 @@ ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, resultType = CheckAddressOfOperand(Input, OpLoc); break; case UO_Deref: { - Input = DefaultFunctionArrayLvalueConversion(Input.take()); + Input = DefaultFunctionArrayLvalueConversion(Input.get()); if (Input.isInvalid()) return ExprError(); resultType = CheckIndirectionOperand(*this, Input.get(), VK, OpLoc); break; } case UO_Plus: case UO_Minus: - Input = UsualUnaryConversions(Input.take()); + Input = UsualUnaryConversions(Input.get()); if (Input.isInvalid()) return ExprError(); resultType = Input.get()->getType(); if (resultType->isDependentType()) @@ -9598,7 +9842,7 @@ ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, << resultType << Input.get()->getSourceRange()); case UO_Not: // bitwise complement - Input = UsualUnaryConversions(Input.take()); + Input = UsualUnaryConversions(Input.get()); if (Input.isInvalid()) return ExprError(); resultType = Input.get()->getType(); @@ -9629,24 +9873,24 @@ ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, case UO_LNot: // logical negation // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). - Input = DefaultFunctionArrayLvalueConversion(Input.take()); + Input = DefaultFunctionArrayLvalueConversion(Input.get()); if (Input.isInvalid()) return ExprError(); resultType = Input.get()->getType(); // Though we still have to promote half FP to float... if (resultType->isHalfType() && !Context.getLangOpts().NativeHalfType) { - Input = ImpCastExprToType(Input.take(), Context.FloatTy, CK_FloatingCast).take(); + Input = ImpCastExprToType(Input.get(), Context.FloatTy, CK_FloatingCast).get(); resultType = Context.FloatTy; } if (resultType->isDependentType()) break; - if (resultType->isScalarType()) { + if (resultType->isScalarType() && !isScopedEnumerationType(resultType)) { // C99 6.5.3.3p1: ok, fallthrough; if (Context.getLangOpts().CPlusPlus) { // C++03 [expr.unary.op]p8, C++0x [expr.unary.op]p9: // operand contextually converted to bool. - Input = ImpCastExprToType(Input.take(), Context.BoolTy, + Input = ImpCastExprToType(Input.get(), Context.BoolTy, ScalarTypeToBooleanCastKind(resultType)); } else if (Context.getLangOpts().OpenCL && Context.getLangOpts().OpenCLVersion < 120) { @@ -9690,7 +9934,7 @@ ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, VK = Input.get()->getValueKind(); } else if (!getLangOpts().CPlusPlus) { // In C, a volatile scalar is read by __imag. In C++, it is not. - Input = DefaultLvalueConversion(Input.take()); + Input = DefaultLvalueConversion(Input.get()); } break; case UO_Extension: @@ -9709,8 +9953,8 @@ ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, if (Opc != UO_AddrOf && Opc != UO_Deref) CheckArrayAccess(Input.get()); - return Owned(new (Context) UnaryOperator(Input.take(), Opc, resultType, - VK, OK, OpLoc)); + return new (Context) + UnaryOperator(Input.get(), Opc, resultType, VK, OK, OpLoc); } /// \brief Determine whether the given expression is a qualified member @@ -9780,7 +10024,7 @@ ExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, // Anything else needs to be handled now. ExprResult Result = CheckPlaceholderExpr(Input); if (Result.isInvalid()) return ExprError(); - Input = Result.take(); + Input = Result.get(); } if (getLangOpts().CPlusPlus && Input->getType()->isOverloadableType() && @@ -9813,8 +10057,8 @@ ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, SourceLocation LabLoc, LabelDecl *TheDecl) { TheDecl->markUsed(Context); // Create the AST node. The address of a label always has type 'void*'. - return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, TheDecl, - Context.getPointerType(Context.VoidTy))); + return new (Context) AddrLabelExpr(OpLoc, LabLoc, TheDecl, + Context.getPointerType(Context.VoidTy)); } /// Given the last statement in a statement-expression, check whether @@ -9825,11 +10069,11 @@ ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, SourceLocation LabLoc, static Expr *maybeRebuildARCConsumingStmt(Stmt *Statement) { // Should always be wrapped with one of these. ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(Statement); - if (!cleanups) return 0; + if (!cleanups) return nullptr; ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(cleanups->getSubExpr()); if (!cast || cast->getCastKind() != CK_ARCConsumeObject) - return 0; + return nullptr; // Splice out the cast. This shouldn't modify any interesting // features of the statement. @@ -9864,7 +10108,7 @@ Sema::ActOnStmtExpr(SourceLocation LPLoc, Stmt *SubStmt, PopExpressionEvaluationContext(); bool isFileScope - = (getCurFunctionOrMethodDecl() == 0) && (getCurBlock() == 0); + = (getCurFunctionOrMethodDecl() == nullptr) && (getCurBlock() == nullptr); if (isFileScope) return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); @@ -9872,13 +10116,13 @@ Sema::ActOnStmtExpr(SourceLocation LPLoc, Stmt *SubStmt, // example, it is not possible to goto into a stmt expression apparently. // More semantic analysis is needed. - // If there are sub stmts in the compound stmt, take the type of the last one + // If there are sub-stmts in the compound stmt, take the type of the last one // as the type of the stmtexpr. QualType Ty = Context.VoidTy; bool StmtExprMayBindToTemp = false; if (!Compound->body_empty()) { Stmt *LastStmt = Compound->body_back(); - LabelStmt *LastLabelStmt = 0; + LabelStmt *LastLabelStmt = nullptr; // If LastStmt is a label, skip down through into the body. while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) { LastLabelStmt = Label; @@ -9914,11 +10158,11 @@ Sema::ActOnStmtExpr(SourceLocation LPLoc, Stmt *SubStmt, if (LastExpr.isInvalid()) return ExprError(); - if (LastExpr.get() != 0) { + if (LastExpr.get() != nullptr) { if (!LastLabelStmt) - Compound->setLastStmt(LastExpr.take()); + Compound->setLastStmt(LastExpr.get()); else - LastLabelStmt->setSubStmt(LastExpr.take()); + LastLabelStmt->setSubStmt(LastExpr.get()); StmtExprMayBindToTemp = true; } } @@ -9930,7 +10174,7 @@ Sema::ActOnStmtExpr(SourceLocation LPLoc, Stmt *SubStmt, Expr *ResStmtExpr = new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc); if (StmtExprMayBindToTemp) return MaybeBindToTemporary(ResStmtExpr); - return Owned(ResStmtExpr); + return ResStmtExpr; } ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc, @@ -9985,7 +10229,7 @@ ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc, ExprResult IdxRval = DefaultLvalueConversion(static_cast<Expr*>(OC.U.E)); if (IdxRval.isInvalid()) return ExprError(); - Expr *Idx = IdxRval.take(); + Expr *Idx = IdxRval.get(); // The expression must be an integral expression. // FIXME: An integral constant expression? @@ -10032,11 +10276,11 @@ ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc, if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { bool IsSafe = LangOpts.CPlusPlus11? CRD->isStandardLayout() : CRD->isPOD(); unsigned DiagID = - LangOpts.CPlusPlus11? diag::warn_offsetof_non_standardlayout_type - : diag::warn_offsetof_non_pod_type; + LangOpts.CPlusPlus11? diag::ext_offsetof_non_standardlayout_type + : diag::ext_offsetof_non_pod_type; if (!IsSafe && !DidWarnAboutNonPOD && - DiagRuntimeBehavior(BuiltinLoc, 0, + DiagRuntimeBehavior(BuiltinLoc, nullptr, PDiag(DiagID) << SourceRange(CompPtr[0].LocStart, OC.LocEnd) << CurrentType)) @@ -10047,7 +10291,7 @@ ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc, LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); LookupQualifiedName(R, RD); FieldDecl *MemberDecl = R.getAsSingle<FieldDecl>(); - IndirectFieldDecl *IndirectMemberDecl = 0; + IndirectFieldDecl *IndirectMemberDecl = nullptr; if (!MemberDecl) { if ((IndirectMemberDecl = R.getAsSingle<IndirectFieldDecl>())) MemberDecl = IndirectMemberDecl->getAnonField(); @@ -10092,12 +10336,10 @@ ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc, } if (IndirectMemberDecl) { - for (IndirectFieldDecl::chain_iterator FI = - IndirectMemberDecl->chain_begin(), - FEnd = IndirectMemberDecl->chain_end(); FI != FEnd; FI++) { - assert(isa<FieldDecl>(*FI)); + for (auto *FI : IndirectMemberDecl->chain()) { + assert(isa<FieldDecl>(FI)); Comps.push_back(OffsetOfNode(OC.LocStart, - cast<FieldDecl>(*FI), OC.LocEnd)); + cast<FieldDecl>(FI), OC.LocEnd)); } } else Comps.push_back(OffsetOfNode(OC.LocStart, MemberDecl, OC.LocEnd)); @@ -10105,8 +10347,8 @@ ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc, CurrentType = MemberDecl->getType().getNonReferenceType(); } - return Owned(OffsetOfExpr::Create(Context, Context.getSizeType(), BuiltinLoc, - TInfo, Comps, Exprs, RParenLoc)); + return OffsetOfExpr::Create(Context, Context.getSizeType(), BuiltinLoc, TInfo, + Comps, Exprs, RParenLoc); } ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, @@ -10152,7 +10394,7 @@ ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, diag::err_typecheck_choose_expr_requires_constant, false); if (CondICE.isInvalid()) return ExprError(); - CondExpr = CondICE.take(); + CondExpr = CondICE.get(); CondIsTrue = condEval.getZExtValue(); // If the condition is > zero, then the AST type is the same as the LSHExpr. @@ -10164,10 +10406,9 @@ ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, OK = ActiveExpr->getObjectKind(); } - return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, - resType, VK, OK, RPLoc, CondIsTrue, - resType->isDependentType(), - ValueDependent)); + return new (Context) + ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, VK, OK, RPLoc, + CondIsTrue, resType->isDependentType(), ValueDependent); } //===----------------------------------------------------------------------===// @@ -10204,7 +10445,8 @@ void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *CurScope) { void Sema::ActOnBlockArguments(SourceLocation CaretLoc, Declarator &ParamInfo, Scope *CurScope) { - assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); + assert(ParamInfo.getIdentifier() == nullptr && + "block-id should have no identifier!"); assert(ParamInfo.getContext() == Declarator::BlockLiteralContext); BlockScopeInfo *CurBlock = getCurBlock(); @@ -10241,7 +10483,7 @@ void Sema::ActOnBlockArguments(SourceLocation CaretLoc, Declarator &ParamInfo, ExplicitSignature.getLocalRangeEnd()) { // This would be much cheaper if we stored TypeLocs instead of // TypeSourceInfos. - TypeLoc Result = ExplicitSignature.getResultLoc(); + TypeLoc Result = ExplicitSignature.getReturnLoc(); unsigned Size = Result.getFullDataSize(); Sig = Context.CreateTypeSourceInfo(Result.getType(), Size); Sig->getTypeLoc().initializeFullCopy(Result, Size); @@ -10254,7 +10496,7 @@ void Sema::ActOnBlockArguments(SourceLocation CaretLoc, Declarator &ParamInfo, CurBlock->FunctionType = T; const FunctionType *Fn = T->getAs<FunctionType>(); - QualType RetTy = Fn->getResultType(); + QualType RetTy = Fn->getReturnType(); bool isVariadic = (isa<FunctionProtoType>(Fn) && cast<FunctionProtoType>(Fn)->isVariadic()); @@ -10273,9 +10515,9 @@ void Sema::ActOnBlockArguments(SourceLocation CaretLoc, Declarator &ParamInfo, // Push block parameters from the declarator if we had them. SmallVector<ParmVarDecl*, 8> Params; if (ExplicitSignature) { - for (unsigned I = 0, E = ExplicitSignature.getNumArgs(); I != E; ++I) { - ParmVarDecl *Param = ExplicitSignature.getArg(I); - if (Param->getIdentifier() == 0 && + for (unsigned I = 0, E = ExplicitSignature.getNumParams(); I != E; ++I) { + ParmVarDecl *Param = ExplicitSignature.getParam(I); + if (Param->getIdentifier() == nullptr && !Param->isImplicit() && !Param->isInvalidDecl() && !getLangOpts().CPlusPlus) @@ -10286,12 +10528,9 @@ void Sema::ActOnBlockArguments(SourceLocation CaretLoc, Declarator &ParamInfo, // Fake up parameter variables if we have a typedef, like // ^ fntype { ... } } else if (const FunctionProtoType *Fn = T->getAs<FunctionProtoType>()) { - for (FunctionProtoType::arg_type_iterator - I = Fn->arg_type_begin(), E = Fn->arg_type_end(); I != E; ++I) { - ParmVarDecl *Param = - BuildParmVarDeclForTypedef(CurBlock->TheDecl, - ParamInfo.getLocStart(), - *I); + for (const auto &I : Fn->param_types()) { + ParmVarDecl *Param = BuildParmVarDeclForTypedef( + CurBlock->TheDecl, ParamInfo.getLocStart(), I); Params.push_back(Param); } } @@ -10308,15 +10547,14 @@ void Sema::ActOnBlockArguments(SourceLocation CaretLoc, Declarator &ParamInfo, ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); // Put the parameter variables in scope. - for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), - E = CurBlock->TheDecl->param_end(); AI != E; ++AI) { - (*AI)->setOwningFunction(CurBlock->TheDecl); + for (auto AI : CurBlock->TheDecl->params()) { + AI->setOwningFunction(CurBlock->TheDecl); // If this has an identifier, add it to the scope stack. - if ((*AI)->getIdentifier()) { - CheckShadow(CurBlock->TheScope, *AI); + if (AI->getIdentifier()) { + CheckShadow(CurBlock->TheScope, AI); - PushOnScopeChains(*AI, CurBlock->TheScope); + PushOnScopeChains(AI, CurBlock->TheScope); } } } @@ -10358,7 +10596,7 @@ ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, if (!BSI->ReturnType.isNull()) RetTy = BSI->ReturnType; - bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); + bool NoReturn = BSI->TheDecl->hasAttr<NoReturnAttr>(); QualType BlockTy; // Set the captured variables on the block. @@ -10390,7 +10628,7 @@ ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, // Otherwise, if we don't need to change anything about the function type, // preserve its sugar structure. - } else if (FTy->getResultType() == RetTy && + } else if (FTy->getReturnType() == RetTy && (!NoReturn || FTy->getNoReturnAttr())) { BlockTy = BSI->FunctionType; @@ -10400,7 +10638,7 @@ ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); EPI.TypeQuals = 0; // FIXME: silently? EPI.ExtInfo = Ext; - BlockTy = Context.getFunctionType(RetTy, FPT->getArgTypes(), EPI); + BlockTy = Context.getFunctionType(RetTy, FPT->getParamTypes(), EPI); } // If we don't have a function type, just build one from nothing. @@ -10416,7 +10654,6 @@ ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, // If needed, diagnose invalid gotos and switches in the block. if (getCurFunction()->NeedsScopeChecking() && - !hasAnyUnrecoverableErrorsInThisFunction() && !PP.isCodeCompletionEnabled()) DiagnoseInvalidJumps(cast<CompoundStmt>(Body)); @@ -10427,7 +10664,7 @@ ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, // to deduce an implicit return type. if (getLangOpts().CPlusPlus && RetTy->isRecordType() && !BSI->TheDecl->isDependentContext()) - computeNRVO(Body, getCurBlock()); + computeNRVO(Body, BSI); BlockExpr *Result = new (Context) BlockExpr(BSI->TheDecl, BlockTy); AnalysisBasedWarnings::Policy WP = AnalysisWarnings.getDefaultPolicy(); @@ -10442,10 +10679,8 @@ ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, // It also gets a branch-protected scope if any of the captured // variables needs destruction. - for (BlockDecl::capture_const_iterator - ci = Result->getBlockDecl()->capture_begin(), - ce = Result->getBlockDecl()->capture_end(); ci != ce; ++ci) { - const VarDecl *var = ci->getVariable(); + for (const auto &CI : Result->getBlockDecl()->captures()) { + const VarDecl *var = CI.getVariable(); if (var->getType().isDestructedType() != QualType::DK_none) { getCurFunction()->setHasBranchProtectedScope(); break; @@ -10453,7 +10688,7 @@ ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, } } - return Owned(Result); + return Result; } ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, @@ -10480,7 +10715,7 @@ ExprResult Sema::BuildVAArgExpr(SourceLocation BuiltinLoc, ExprResult Result = UsualUnaryConversions(E); if (Result.isInvalid()) return ExprError(); - E = Result.take(); + E = Result.get(); } else if (VaListType->isRecordType() && getLangOpts().CPlusPlus) { // If va_list is a record type and we are compiling in C++ mode, // check the argument using reference binding. @@ -10490,7 +10725,7 @@ ExprResult Sema::BuildVAArgExpr(SourceLocation BuiltinLoc, ExprResult Init = PerformCopyInitialization(Entity, SourceLocation(), E); if (Init.isInvalid()) return ExprError(); - E = Init.takeAs<Expr>(); + E = Init.getAs<Expr>(); } else { // Otherwise, the va_list argument must be an l-value because // it is modified by va_arg. @@ -10546,7 +10781,7 @@ ExprResult Sema::BuildVAArgExpr(SourceLocation BuiltinLoc, } QualType T = TInfo->getType().getNonLValueExprType(Context); - return Owned(new (Context) VAArgExpr(BuiltinLoc, E, TInfo, RPLoc, T)); + return new (Context) VAArgExpr(BuiltinLoc, E, TInfo, RPLoc, T); } ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { @@ -10564,41 +10799,40 @@ ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { llvm_unreachable("I don't know size of pointer!"); } - return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); + return new (Context) GNUNullExpr(Ty, TokenLoc); } -static void MakeObjCStringLiteralFixItHint(Sema& SemaRef, QualType DstType, - Expr *SrcExpr, FixItHint &Hint, - bool &IsNSString) { - if (!SemaRef.getLangOpts().ObjC1) - return; +bool +Sema::ConversionToObjCStringLiteralCheck(QualType DstType, Expr *&Exp) { + if (!getLangOpts().ObjC1) + return false; const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>(); if (!PT) - return; + return false; - // Check if the destination is of type 'id'. if (!PT->isObjCIdType()) { // Check if the destination is the 'NSString' interface. const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); if (!ID || !ID->getIdentifier()->isStr("NSString")) - return; - IsNSString = true; + return false; } - + // Ignore any parens, implicit casts (should only be // array-to-pointer decays), and not-so-opaque values. The last is // important for making this trigger for property assignments. - SrcExpr = SrcExpr->IgnoreParenImpCasts(); + Expr *SrcExpr = Exp->IgnoreParenImpCasts(); if (OpaqueValueExpr *OV = dyn_cast<OpaqueValueExpr>(SrcExpr)) if (OV->getSourceExpr()) SrcExpr = OV->getSourceExpr()->IgnoreParenImpCasts(); StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr); if (!SL || !SL->isAscii()) - return; - - Hint = FixItHint::CreateInsertion(SL->getLocStart(), "@"); + return false; + Diag(SL->getLocStart(), diag::err_missing_atsign_prefix) + << FixItHint::CreateInsertion(SL->getLocStart(), "@"); + Exp = BuildObjCStringLiteral(SL->getLocStart(), SL).get(); + return true; } bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, @@ -10617,7 +10851,8 @@ bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, ConversionFixItGenerator ConvHints; bool MayHaveConvFixit = false; bool MayHaveFunctionDiff = false; - bool IsNSString = false; + const ObjCInterfaceDecl *IFace = nullptr; + const ObjCProtocolDecl *PDecl = nullptr; switch (ConvTy) { case Compatible: @@ -10635,7 +10870,6 @@ bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, MayHaveConvFixit = true; break; case IncompatiblePointer: - MakeObjCStringLiteralFixItHint(*this, DstType, SrcExpr, Hint, IsNSString); DiagKind = (Action == AA_Passing_CFAudited ? diag::err_arc_typecheck_convert_incompatible_pointer : @@ -10649,8 +10883,6 @@ bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, SrcType = SrcType.getUnqualifiedType(); DstType = DstType.getUnqualifiedType(); } - else if (IsNSString && !Hint.isNull()) - DiagKind = diag::warn_missing_atsign_prefix; MayHaveConvFixit = true; break; case IncompatiblePointerSign: @@ -10702,11 +10934,32 @@ bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, case IncompatibleBlockPointer: DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; break; - case IncompatibleObjCQualifiedId: - // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since - // it can give a more specific diagnostic. + case IncompatibleObjCQualifiedId: { + if (SrcType->isObjCQualifiedIdType()) { + const ObjCObjectPointerType *srcOPT = + SrcType->getAs<ObjCObjectPointerType>(); + for (auto *srcProto : srcOPT->quals()) { + PDecl = srcProto; + break; + } + if (const ObjCInterfaceType *IFaceT = + DstType->getAs<ObjCObjectPointerType>()->getInterfaceType()) + IFace = IFaceT->getDecl(); + } + else if (DstType->isObjCQualifiedIdType()) { + const ObjCObjectPointerType *dstOPT = + DstType->getAs<ObjCObjectPointerType>(); + for (auto *dstProto : dstOPT->quals()) { + PDecl = dstProto; + break; + } + if (const ObjCInterfaceType *IFaceT = + SrcType->getAs<ObjCObjectPointerType>()->getInterfaceType()) + IFace = IFaceT->getDecl(); + } DiagKind = diag::warn_incompatible_qualified_id; break; + } case IncompatibleVectors: DiagKind = diag::warn_incompatible_vectors; break; @@ -10764,7 +11017,11 @@ bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, HandleFunctionTypeMismatch(FDiag, SecondType, FirstType); Diag(Loc, FDiag); - + if (DiagKind == diag::warn_incompatible_qualified_id && + PDecl && IFace && !IFace->hasDefinition()) + Diag(IFace->getLocation(), diag::not_incomplete_class_and_qualified_id) + << IFace->getName() << PDecl->getName(); + if (SecondType == Context.OverloadTy) NoteAllOverloadCandidates(OverloadExpr::find(SrcExpr).Expression, FirstType); @@ -10784,7 +11041,7 @@ ExprResult Sema::VerifyIntegerConstantExpression(Expr *E, llvm::APSInt *Result) { class SimpleICEDiagnoser : public VerifyICEDiagnoser { public: - virtual void diagnoseNotICE(Sema &S, SourceLocation Loc, SourceRange SR) { + void diagnoseNotICE(Sema &S, SourceLocation Loc, SourceRange SR) override { S.Diag(Loc, diag::err_expr_not_ice) << S.LangOpts.CPlusPlus << SR; } } Diagnoser; @@ -10803,7 +11060,7 @@ ExprResult Sema::VerifyIntegerConstantExpression(Expr *E, IDDiagnoser(unsigned DiagID) : VerifyICEDiagnoser(DiagID == 0), DiagID(DiagID) { } - virtual void diagnoseNotICE(Sema &S, SourceLocation Loc, SourceRange SR) { + void diagnoseNotICE(Sema &S, SourceLocation Loc, SourceRange SR) override { S.Diag(Loc, DiagID) << SR; } } Diagnoser(DiagID); @@ -10835,40 +11092,40 @@ Sema::VerifyIntegerConstantExpression(Expr *E, llvm::APSInt *Result, : ICEConvertDiagnoser(/*AllowScopedEnumerations*/false, Silent, true) {} - virtual SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, - QualType T) { + SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, + QualType T) override { return S.Diag(Loc, diag::err_ice_not_integral) << T; } - virtual SemaDiagnosticBuilder diagnoseIncomplete( - Sema &S, SourceLocation Loc, QualType T) { + SemaDiagnosticBuilder diagnoseIncomplete( + Sema &S, SourceLocation Loc, QualType T) override { return S.Diag(Loc, diag::err_ice_incomplete_type) << T; } - virtual SemaDiagnosticBuilder diagnoseExplicitConv( - Sema &S, SourceLocation Loc, QualType T, QualType ConvTy) { + SemaDiagnosticBuilder diagnoseExplicitConv( + Sema &S, SourceLocation Loc, QualType T, QualType ConvTy) override { return S.Diag(Loc, diag::err_ice_explicit_conversion) << T << ConvTy; } - virtual SemaDiagnosticBuilder noteExplicitConv( - Sema &S, CXXConversionDecl *Conv, QualType ConvTy) { + SemaDiagnosticBuilder noteExplicitConv( + Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override { return S.Diag(Conv->getLocation(), diag::note_ice_conversion_here) << ConvTy->isEnumeralType() << ConvTy; } - virtual SemaDiagnosticBuilder diagnoseAmbiguous( - Sema &S, SourceLocation Loc, QualType T) { + SemaDiagnosticBuilder diagnoseAmbiguous( + Sema &S, SourceLocation Loc, QualType T) override { return S.Diag(Loc, diag::err_ice_ambiguous_conversion) << T; } - virtual SemaDiagnosticBuilder noteAmbiguous( - Sema &S, CXXConversionDecl *Conv, QualType ConvTy) { + SemaDiagnosticBuilder noteAmbiguous( + Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override { return S.Diag(Conv->getLocation(), diag::note_ice_conversion_here) << ConvTy->isEnumeralType() << ConvTy; } - virtual SemaDiagnosticBuilder diagnoseConversion( - Sema &S, SourceLocation Loc, QualType T, QualType ConvTy) { + SemaDiagnosticBuilder diagnoseConversion( + Sema &S, SourceLocation Loc, QualType T, QualType ConvTy) override { llvm_unreachable("conversion functions are permitted"); } } ConvertDiagnoser(Diagnoser.Suppress); @@ -10877,7 +11134,7 @@ Sema::VerifyIntegerConstantExpression(Expr *E, llvm::APSInt *Result, ConvertDiagnoser); if (Converted.isInvalid()) return Converted; - E = Converted.take(); + E = Converted.get(); if (!E->getType()->isIntegralOrUnscopedEnumerationType()) return ExprError(); } else if (!E->getType()->isIntegralOrUnscopedEnumerationType()) { @@ -10892,7 +11149,7 @@ Sema::VerifyIntegerConstantExpression(Expr *E, llvm::APSInt *Result, if (!getLangOpts().CPlusPlus11 && E->isIntegerConstantExpr(Context)) { if (Result) *Result = E->EvaluateKnownConstInt(Context); - return Owned(E); + return E; } Expr::EvalResult EvalResult; @@ -10910,7 +11167,7 @@ Sema::VerifyIntegerConstantExpression(Expr *E, llvm::APSInt *Result, if (Folded && getLangOpts().CPlusPlus11 && Notes.empty()) { if (Result) *Result = EvalResult.Val.getInt(); - return Owned(E); + return E; } // If our only note is the usual "invalid subexpression" note, just point @@ -10938,7 +11195,7 @@ Sema::VerifyIntegerConstantExpression(Expr *E, llvm::APSInt *Result, if (Result) *Result = EvalResult.Val.getInt(); - return Owned(E); + return E; } namespace { @@ -10957,7 +11214,7 @@ namespace { // FIXME: This does the right thing, but maybe we need a more general // fix to TreeTransform? StmtResult TransformLabelStmt(LabelStmt *S) { - S->getDecl()->setStmt(0); + S->getDecl()->setStmt(nullptr); return BaseTransform::TransformLabelStmt(S); } @@ -11174,37 +11431,32 @@ void Sema::MarkFunctionReferenced(SourceLocation Loc, FunctionDecl *Func) { // Note that this declaration has been used. if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Func)) { + Constructor = cast<CXXConstructorDecl>(Constructor->getFirstDecl()); if (Constructor->isDefaulted() && !Constructor->isDeleted()) { if (Constructor->isDefaultConstructor()) { - if (Constructor->isTrivial()) + if (Constructor->isTrivial() && !Constructor->hasAttr<DLLExportAttr>()) return; - if (!Constructor->isUsed(false)) - DefineImplicitDefaultConstructor(Loc, Constructor); + DefineImplicitDefaultConstructor(Loc, Constructor); } else if (Constructor->isCopyConstructor()) { - if (!Constructor->isUsed(false)) - DefineImplicitCopyConstructor(Loc, Constructor); + DefineImplicitCopyConstructor(Loc, Constructor); } else if (Constructor->isMoveConstructor()) { - if (!Constructor->isUsed(false)) - DefineImplicitMoveConstructor(Loc, Constructor); + DefineImplicitMoveConstructor(Loc, Constructor); } } else if (Constructor->getInheritedConstructor()) { - if (!Constructor->isUsed(false)) - DefineInheritingConstructor(Loc, Constructor); + DefineInheritingConstructor(Loc, Constructor); } - - MarkVTableUsed(Loc, Constructor->getParent()); } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(Func)) { - if (Destructor->isDefaulted() && !Destructor->isDeleted() && - !Destructor->isUsed(false)) + Destructor = cast<CXXDestructorDecl>(Destructor->getFirstDecl()); + if (Destructor->isDefaulted() && !Destructor->isDeleted()) DefineImplicitDestructor(Loc, Destructor); if (Destructor->isVirtual()) MarkVTableUsed(Loc, Destructor->getParent()); } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(Func)) { - if (MethodDecl->isDefaulted() && !MethodDecl->isDeleted() && - MethodDecl->isOverloadedOperator() && + if (MethodDecl->isOverloadedOperator() && MethodDecl->getOverloadedOperator() == OO_Equal) { - if (!MethodDecl->isUsed(false)) { + MethodDecl = cast<CXXMethodDecl>(MethodDecl->getFirstDecl()); + if (MethodDecl->isDefaulted() && !MethodDecl->isDeleted()) { if (MethodDecl->isCopyAssignmentOperator()) DefineImplicitCopyAssignment(Loc, MethodDecl); else @@ -11212,7 +11464,8 @@ void Sema::MarkFunctionReferenced(SourceLocation Loc, FunctionDecl *Func) { } } else if (isa<CXXConversionDecl>(MethodDecl) && MethodDecl->getParent()->isLambda()) { - CXXConversionDecl *Conversion = cast<CXXConversionDecl>(MethodDecl); + CXXConversionDecl *Conversion = + cast<CXXConversionDecl>(MethodDecl->getFirstDecl()); if (Conversion->isLambdaToBlockPointerConversion()) DefineImplicitLambdaToBlockPointerConversion(Loc, Conversion); else @@ -11276,10 +11529,9 @@ void Sema::MarkFunctionReferenced(SourceLocation Loc, FunctionDecl *Func) { } } else { // Walk redefinitions, as some of them may be instantiable. - for (FunctionDecl::redecl_iterator i(Func->redecls_begin()), - e(Func->redecls_end()); i != e; ++i) { + for (auto i : Func->redecls()) { if (!i->isUsed(false) && i->isImplicitlyInstantiable()) - MarkFunctionReferenced(Loc, *i); + MarkFunctionReferenced(Loc, i); } } @@ -11345,8 +11597,8 @@ diagnoseUncapturableValueReference(Sema &S, SourceLocation loc, << var->getIdentifier(); } - S.Diag(var->getLocation(), diag::note_local_variable_declared_here) - << var->getIdentifier(); + S.Diag(var->getLocation(), diag::note_entity_declared_at) + << var->getIdentifier(); // FIXME: Add additional diagnostic info about class etc. which prevents // capture. @@ -11388,7 +11640,7 @@ static DeclContext *getParentOfCapturingContextOrNull(DeclContext *DC, VarDecl * if (Diagnose) diagnoseUncapturableValueReference(S, Loc, Var, DC); } - return 0; + return nullptr; } // Certain capturing entities (lambdas, blocks etc.) are not allowed to capture @@ -11414,7 +11666,7 @@ static bool isVariableCapturable(CapturingScopeInfo *CSI, VarDecl *Var, } // Prohibit variably-modified types; they're difficult to deal with. - if (Var->getType()->isVariablyModifiedType()) { + if (Var->getType()->isVariablyModifiedType() && (IsBlock || IsLambda)) { if (Diagnose) { if (IsBlock) S.Diag(Loc, diag::err_ref_vm_type); @@ -11465,7 +11717,7 @@ static bool captureInBlock(BlockScopeInfo *BSI, VarDecl *Var, QualType &DeclRefType, const bool Nested, Sema &S) { - Expr *CopyExpr = 0; + Expr *CopyExpr = nullptr; bool ByRef = false; // Blocks are not allowed to capture arrays. @@ -11523,7 +11775,7 @@ static bool captureInBlock(BlockScopeInfo *BSI, VarDecl *Var, = S.PerformCopyInitialization( InitializedEntity::InitializeBlock(Var->getLocation(), CaptureType, false), - Loc, S.Owned(DeclRef)); + Loc, DeclRef); // Build a full-expression copy expression if initialization // succeeded and used a non-trivial constructor. Recover from @@ -11532,7 +11784,7 @@ static bool captureInBlock(BlockScopeInfo *BSI, VarDecl *Var, !cast<CXXConstructExpr>(Result.get())->getConstructor() ->isTrivial()) { Result = S.MaybeCreateExprWithCleanups(Result); - CopyExpr = Result.take(); + CopyExpr = Result.get(); } } } @@ -11562,18 +11814,17 @@ static bool captureInCapturedRegion(CapturedRegionScopeInfo *RSI, bool ByRef = true; // Using an LValue reference type is consistent with Lambdas (see below). CaptureType = S.Context.getLValueReferenceType(DeclRefType); - Expr *CopyExpr = 0; + Expr *CopyExpr = nullptr; if (BuildAndDiagnose) { // The current implementation assumes that all variables are captured - // by references. Since there is no capture by copy, no expression evaluation - // will be needed. - // + // by references. Since there is no capture by copy, no expression + // evaluation will be needed. RecordDecl *RD = RSI->TheRecordDecl; FieldDecl *Field - = FieldDecl::Create(S.Context, RD, Loc, Loc, 0, CaptureType, + = FieldDecl::Create(S.Context, RD, Loc, Loc, nullptr, CaptureType, S.Context.getTrivialTypeSourceInfo(CaptureType, Loc), - 0, false, ICIS_NoInit); + nullptr, false, ICIS_NoInit); Field->setImplicit(true); Field->setAccess(AS_private); RD->addDecl(Field); @@ -11605,9 +11856,9 @@ static ExprResult addAsFieldToClosureType(Sema &S, // Build the non-static data member. FieldDecl *Field - = FieldDecl::Create(S.Context, Lambda, Loc, Loc, 0, FieldType, + = FieldDecl::Create(S.Context, Lambda, Loc, Loc, nullptr, FieldType, S.Context.getTrivialTypeSourceInfo(FieldType, Loc), - 0, false, ICIS_NoInit); + nullptr, false, ICIS_NoInit); Field->setImplicit(true); Field->setAccess(AS_private); Lambda->addDecl(Field); @@ -11645,7 +11896,7 @@ static ExprResult addAsFieldToClosureType(Sema &S, while (const ConstantArrayType *Array = S.Context.getAsConstantArrayType(BaseType)) { // Create the iteration variable for this array index. - IdentifierInfo *IterationVarName = 0; + IdentifierInfo *IterationVarName = nullptr; { SmallString<8> Str; llvm::raw_svector_ostream OS(Str); @@ -11665,20 +11916,20 @@ static ExprResult addAsFieldToClosureType(Sema &S, = S.BuildDeclRefExpr(IterationVar, SizeType, VK_LValue, Loc); assert(!IterationVarRef.isInvalid() && "Reference to invented variable cannot fail!"); - IterationVarRef = S.DefaultLvalueConversion(IterationVarRef.take()); + IterationVarRef = S.DefaultLvalueConversion(IterationVarRef.get()); assert(!IterationVarRef.isInvalid() && "Conversion of invented variable cannot fail!"); // Subscript the array with this iteration variable. ExprResult Subscript = S.CreateBuiltinArraySubscriptExpr( - Ref, Loc, IterationVarRef.take(), Loc); + Ref, Loc, IterationVarRef.get(), Loc); if (Subscript.isInvalid()) { S.CleanupVarDeclMarking(); S.DiscardCleanupsInEvaluationContext(); return ExprError(); } - Ref = Subscript.take(); + Ref = Subscript.get(); BaseType = Array->getElementType(); } @@ -11778,19 +12029,28 @@ static bool captureInLambda(LambdaScopeInfo *LSI, return false; } - if (S.RequireNonAbstractType(Loc, CaptureType, - diag::err_capture_of_abstract_type)) - return false; + // Make sure that by-copy captures are of a complete and non-abstract type. + if (BuildAndDiagnose) { + if (!CaptureType->isDependentType() && + S.RequireCompleteType(Loc, CaptureType, + diag::err_capture_of_incomplete_type, + Var->getDeclName())) + return false; + + if (S.RequireNonAbstractType(Loc, CaptureType, + diag::err_capture_of_abstract_type)) + return false; + } } // Capture this variable in the lambda. - Expr *CopyExpr = 0; + Expr *CopyExpr = nullptr; if (BuildAndDiagnose) { ExprResult Result = addAsFieldToClosureType(S, LSI, Var, CaptureType, DeclRefType, Loc, RefersToEnclosingLocal); if (!Result.isInvalid()) - CopyExpr = Result.take(); + CopyExpr = Result.get(); } // Compute the type of a reference to this captured variable. @@ -11893,8 +12153,107 @@ bool Sema::tryCaptureVariable(VarDecl *Var, SourceLocation ExprLoc, // certain types of variables (unnamed, variably modified types etc.) // so check for eligibility. if (!isVariableCapturable(CSI, Var, ExprLoc, BuildAndDiagnose, *this)) - return true; - + return true; + + // Try to capture variable-length arrays types. + if (Var->getType()->isVariablyModifiedType()) { + // We're going to walk down into the type and look for VLA + // expressions. + QualType QTy = Var->getType(); + if (ParmVarDecl *PVD = dyn_cast_or_null<ParmVarDecl>(Var)) + QTy = PVD->getOriginalType(); + do { + const Type *Ty = QTy.getTypePtr(); + switch (Ty->getTypeClass()) { +#define TYPE(Class, Base) +#define ABSTRACT_TYPE(Class, Base) +#define NON_CANONICAL_TYPE(Class, Base) +#define DEPENDENT_TYPE(Class, Base) case Type::Class: +#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) +#include "clang/AST/TypeNodes.def" + QTy = QualType(); + break; + // These types are never variably-modified. + case Type::Builtin: + case Type::Complex: + case Type::Vector: + case Type::ExtVector: + case Type::Record: + case Type::Enum: + case Type::Elaborated: + case Type::TemplateSpecialization: + case Type::ObjCObject: + case Type::ObjCInterface: + case Type::ObjCObjectPointer: + llvm_unreachable("type class is never variably-modified!"); + case Type::Adjusted: + QTy = cast<AdjustedType>(Ty)->getOriginalType(); + break; + case Type::Decayed: + QTy = cast<DecayedType>(Ty)->getPointeeType(); + break; + case Type::Pointer: + QTy = cast<PointerType>(Ty)->getPointeeType(); + break; + case Type::BlockPointer: + QTy = cast<BlockPointerType>(Ty)->getPointeeType(); + break; + case Type::LValueReference: + case Type::RValueReference: + QTy = cast<ReferenceType>(Ty)->getPointeeType(); + break; + case Type::MemberPointer: + QTy = cast<MemberPointerType>(Ty)->getPointeeType(); + break; + case Type::ConstantArray: + case Type::IncompleteArray: + // Losing element qualification here is fine. + QTy = cast<ArrayType>(Ty)->getElementType(); + break; + case Type::VariableArray: { + // Losing element qualification here is fine. + const VariableArrayType *Vat = cast<VariableArrayType>(Ty); + + // Unknown size indication requires no size computation. + // Otherwise, evaluate and record it. + if (Expr *Size = Vat->getSizeExpr()) { + MarkDeclarationsReferencedInExpr(Size); + } + QTy = Vat->getElementType(); + break; + } + case Type::FunctionProto: + case Type::FunctionNoProto: + QTy = cast<FunctionType>(Ty)->getReturnType(); + break; + case Type::Paren: + case Type::TypeOf: + case Type::UnaryTransform: + case Type::Attributed: + case Type::SubstTemplateTypeParm: + case Type::PackExpansion: + // Keep walking after single level desugaring. + QTy = QTy.getSingleStepDesugaredType(getASTContext()); + break; + case Type::Typedef: + QTy = cast<TypedefType>(Ty)->desugar(); + break; + case Type::Decltype: + QTy = cast<DecltypeType>(Ty)->desugar(); + break; + case Type::Auto: + QTy = cast<AutoType>(Ty)->getDeducedType(); + break; + case Type::TypeOfExpr: + QTy = cast<TypeOfExprType>(Ty)->getUnderlyingExpr()->getType(); + break; + case Type::Atomic: + QTy = cast<AtomicType>(Ty)->getValueType(); + break; + } + } while (!QTy.isNull() && QTy->isVariablyModifiedType()); + } + if (CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_None && !Explicit) { // No capture-default, and this is not an explicit capture // so cannot capture this variable. @@ -11964,7 +12323,7 @@ bool Sema::tryCaptureVariable(VarDecl *Var, SourceLocation Loc, QualType DeclRefType; return tryCaptureVariable(Var, Loc, Kind, EllipsisLoc, /*BuildAndDiagnose=*/true, CaptureType, - DeclRefType, 0); + DeclRefType, nullptr); } QualType Sema::getCapturedDeclRefType(VarDecl *Var, SourceLocation Loc) { @@ -11974,7 +12333,7 @@ QualType Sema::getCapturedDeclRefType(VarDecl *Var, SourceLocation Loc) { // Determine whether we can capture this variable. if (tryCaptureVariable(Var, Loc, TryCapture_Implicit, SourceLocation(), /*BuildAndDiagnose=*/false, CaptureType, - DeclRefType, 0)) + DeclRefType, nullptr)) return QualType(); return DeclRefType; @@ -11991,7 +12350,7 @@ static inline bool IsVariableNonDependentAndAConstantExpression(VarDecl *Var, if (Var->getType()->isDependentType()) return false; - const VarDecl *DefVD = 0; + const VarDecl *DefVD = nullptr; Var->getAnyInitializer(DefVD); if (!DefVD) return false; @@ -12017,7 +12376,7 @@ void Sema::UpdateMarkingForLValueToRValue(Expr *E) { // variable. if (LambdaScopeInfo *LSI = getCurLambda()) { Expr *SansParensExpr = E->IgnoreParens(); - VarDecl *Var = 0; + VarDecl *Var = nullptr; if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(SansParensExpr)) Var = dyn_cast<VarDecl>(DRE->getFoundDecl()); else if (MemberExpr *ME = dyn_cast<MemberExpr>(SansParensExpr)) @@ -12056,7 +12415,8 @@ void Sema::CleanupVarDeclMarking() { llvm_unreachable("Unexpcted expression"); } - MarkVarDeclODRUsed(Var, Loc, *this, /*MaxFunctionScopeIndex Pointer*/ 0); + MarkVarDeclODRUsed(Var, Loc, *this, + /*MaxFunctionScopeIndex Pointer*/ nullptr); } MaybeODRUseExprs.clear(); @@ -12069,42 +12429,39 @@ static void DoMarkVarDeclReferenced(Sema &SemaRef, SourceLocation Loc, "Invalid Expr argument to DoMarkVarDeclReferenced"); Var->setReferenced(); - // If the context is not PotentiallyEvaluated and not Unevaluated - // (i.e PotentiallyEvaluatedIfUsed) do not bother to consider variables - // in this context for odr-use unless we are within a lambda. - // If we don't know whether the context is potentially evaluated or not - // (for e.g., if we're in a generic lambda), we want to add a potential - // capture and eventually analyze for odr-use. - // We should also be able to analyze certain constructs in a non-generic - // lambda setting for potential odr-use and capture violation: - // template<class T> void foo(T t) { - // auto L = [](int i) { return t; }; - // } - // + // If the context is not potentially evaluated, this is not an odr-use and + // does not trigger instantiation. if (!IsPotentiallyEvaluatedContext(SemaRef)) { + if (SemaRef.isUnevaluatedContext()) + return; - if (SemaRef.isUnevaluatedContext()) return; + // If we don't yet know whether this context is going to end up being an + // evaluated context, and we're referencing a variable from an enclosing + // scope, add a potential capture. + // + // FIXME: Is this necessary? These contexts are only used for default + // arguments, where local variables can't be used. + const bool RefersToEnclosingScope = + (SemaRef.CurContext != Var->getDeclContext() && + Var->getDeclContext()->isFunctionOrMethod() && + Var->hasLocalStorage()); + if (!RefersToEnclosingScope) + return; - const bool refersToEnclosingScope = - (SemaRef.CurContext != Var->getDeclContext() && - Var->getDeclContext()->isFunctionOrMethod()); - if (!refersToEnclosingScope) return; - if (LambdaScopeInfo *const LSI = SemaRef.getCurLambda()) { // If a variable could potentially be odr-used, defer marking it so - // until we finish analyzing the full expression for any lvalue-to-rvalue + // until we finish analyzing the full expression for any lvalue-to-rvalue // or discarded value conversions that would obviate odr-use. // Add it to the list of potential captures that will be analyzed // later (ActOnFinishFullExpr) for eventual capture and odr-use marking // unless the variable is a reference that was initialized by a constant // expression (this will never need to be captured or odr-used). - const bool IsConstantExpr = IsVariableNonDependentAndAConstantExpression( - Var, SemaRef.Context); assert(E && "Capture variable should be used in an expression."); - if (!IsConstantExpr || !Var->getType()->isReferenceType()) - LSI->addPotentialCapture(E->IgnoreParens()); - } - return; + if (!Var->getType()->isReferenceType() || + !IsVariableNonDependentAndAConstantExpression(Var, SemaRef.Context)) + LSI->addPotentialCapture(E->IgnoreParens()); + } + return; } VarTemplateSpecializationDecl *VarSpec = @@ -12112,10 +12469,10 @@ static void DoMarkVarDeclReferenced(Sema &SemaRef, SourceLocation Loc, assert(!isa<VarTemplatePartialSpecializationDecl>(Var) && "Can't instantiate a partial template specialization."); - // Implicit instantiation of static data members, static data member - // templates of class templates, and variable template specializations. - // Delay instantiations of variable templates, except for those - // that could be used in a constant expression. + // Perform implicit instantiation of static data members, static data member + // templates of class templates, and variable template specializations. Delay + // instantiations of variable templates, except for those that could be used + // in a constant expression. TemplateSpecializationKind TSK = Var->getTemplateSpecializationKind(); if (isTemplateInstantiation(TSK)) { bool TryInstantiating = TSK == TSK_ImplicitInstantiation; @@ -12155,6 +12512,7 @@ static void DoMarkVarDeclReferenced(Sema &SemaRef, SourceLocation Loc, } } } + // Per C++11 [basic.def.odr], a variable is odr-used "unless it satisfies // the requirements for appearing in a constant expression (5.19) and, if // it is an object, the lvalue-to-rvalue conversion (4.1) @@ -12164,22 +12522,20 @@ static void DoMarkVarDeclReferenced(Sema &SemaRef, SourceLocation Loc, // C++03 depends on whether we get the C++03 version correct. The second // part does not apply to references, since they are not objects. if (E && IsVariableAConstantExpression(Var, SemaRef.Context)) { - // A reference initialized by a constant expression can never be + // A reference initialized by a constant expression can never be // odr-used, so simply ignore it. - // But a non-reference might get odr-used if it doesn't undergo - // an lvalue-to-rvalue or is discarded, so track it. if (!Var->getType()->isReferenceType()) SemaRef.MaybeODRUseExprs.insert(E); - } - else - MarkVarDeclODRUsed(Var, Loc, SemaRef, /*MaxFunctionScopeIndex ptr*/0); + } else + MarkVarDeclODRUsed(Var, Loc, SemaRef, + /*MaxFunctionScopeIndex ptr*/ nullptr); } /// \brief Mark a variable referenced, and check whether it is odr-used /// (C++ [basic.def.odr]p2, C99 6.9p3). Note that this should not be /// used directly for normal expressions referring to VarDecl. void Sema::MarkVariableReferenced(SourceLocation Loc, VarDecl *Var) { - DoMarkVarDeclReferenced(*this, Loc, Var, 0); + DoMarkVarDeclReferenced(*this, Loc, Var, nullptr); } static void MarkExprReferenced(Sema &SemaRef, SourceLocation Loc, @@ -12241,9 +12597,9 @@ void Sema::MarkMemberReferenced(MemberExpr *E) { } /// \brief Perform marking for a reference to an arbitrary declaration. It -/// marks the declaration referenced, and performs odr-use checking for functions -/// and variables. This method should not be used when building an normal -/// expression which refers to a variable. +/// marks the declaration referenced, and performs odr-use checking for +/// functions and variables. This method should not be used when building a +/// normal expression which refers to a variable. void Sema::MarkAnyDeclReferenced(SourceLocation Loc, Decl *D, bool OdrUse) { if (OdrUse) { if (VarDecl *VD = dyn_cast<VarDecl>(D)) { @@ -12277,7 +12633,7 @@ namespace { } bool MarkReferencedDecls::TraverseTemplateArgument( - const TemplateArgument &Arg) { + const TemplateArgument &Arg) { if (Arg.getKind() == TemplateArgument::Declaration) { if (Decl *D = Arg.getAsDecl()) S.MarkAnyDeclReferenced(Loc, D, true); @@ -12324,7 +12680,7 @@ namespace { S.MarkDeclRefReferenced(E); } - + void VisitMemberExpr(MemberExpr *E) { S.MarkMemberReferenced(E); Inherited::VisitMemberExpr(E); @@ -12447,8 +12803,8 @@ bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, public: CallReturnIncompleteDiagnoser(FunctionDecl *FD, CallExpr *CE) : FD(FD), CE(CE) { } - - virtual void diagnose(Sema &S, SourceLocation Loc, QualType T) { + + void diagnose(Sema &S, SourceLocation Loc, QualType T) override { if (!FD) { S.Diag(Loc, diag::err_call_incomplete_return) << T << CE->getSourceRange(); @@ -12457,9 +12813,8 @@ bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, S.Diag(Loc, diag::err_call_function_incomplete_return) << CE->getSourceRange() << FD->getDeclName() << T; - S.Diag(FD->getLocation(), - diag::note_function_with_incomplete_return_type_declared_here) - << FD->getDeclName(); + S.Diag(FD->getLocation(), diag::note_entity_declared_at) + << FD->getDeclName(); } } Diagnoser(FD, CE); @@ -12563,7 +12918,7 @@ ExprResult Sema::CheckBooleanCondition(Expr *E, SourceLocation Loc) { ExprResult result = CheckPlaceholderExpr(E); if (result.isInvalid()) return ExprError(); - E = result.take(); + E = result.get(); if (!E->isTypeDependent()) { if (getLangOpts().CPlusPlus) @@ -12572,7 +12927,7 @@ ExprResult Sema::CheckBooleanCondition(Expr *E, SourceLocation Loc) { ExprResult ERes = DefaultFunctionArrayLvalueConversion(E); if (ERes.isInvalid()) return ExprError(); - E = ERes.take(); + E = ERes.get(); QualType T = E->getType(); if (!T->isScalarType()) { // C99 6.8.4.1p1 @@ -12582,7 +12937,7 @@ ExprResult Sema::CheckBooleanCondition(Expr *E, SourceLocation Loc) { } } - return Owned(E); + return E; } ExprResult Sema::ActOnBooleanCondition(Scope *S, SourceLocation Loc, @@ -12619,7 +12974,7 @@ namespace { ExprResult SubResult = Visit(E->getSubExpr()); if (SubResult.isInvalid()) return ExprError(); - Expr *SubExpr = SubResult.take(); + Expr *SubExpr = SubResult.get(); E->setSubExpr(SubExpr); E->setType(SubExpr->getType()); E->setValueKind(SubExpr->getValueKind()); @@ -12639,7 +12994,7 @@ namespace { ExprResult SubResult = Visit(E->getSubExpr()); if (SubResult.isInvalid()) return ExprError(); - Expr *SubExpr = SubResult.take(); + Expr *SubExpr = SubResult.get(); E->setSubExpr(SubExpr); E->setType(S.Context.getPointerType(SubExpr->getType())); assert(E->getValueKind() == VK_RValue); @@ -12676,7 +13031,7 @@ namespace { static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *FunctionExpr) { ExprResult Result = RebuildUnknownAnyFunction(S).Visit(FunctionExpr); if (Result.isInvalid()) return ExprError(); - return S.DefaultFunctionArrayConversion(Result.take()); + return S.DefaultFunctionArrayConversion(Result.get()); } namespace { @@ -12713,7 +13068,7 @@ namespace { template <class T> ExprResult rebuildSugarExpr(T *E) { ExprResult SubResult = Visit(E->getSubExpr()); if (SubResult.isInvalid()) return ExprError(); - Expr *SubExpr = SubResult.take(); + Expr *SubExpr = SubResult.get(); E->setSubExpr(SubExpr); E->setType(SubExpr->getType()); E->setValueKind(SubExpr->getValueKind()); @@ -12744,7 +13099,7 @@ namespace { DestType = Ptr->getPointeeType(); ExprResult SubResult = Visit(E->getSubExpr()); if (SubResult.isInvalid()) return ExprError(); - E->setSubExpr(SubResult.take()); + E->setSubExpr(SubResult.get()); return E; } @@ -12825,7 +13180,7 @@ ExprResult RebuildUnknownAnyExpr::VisitCallExpr(CallExpr *E) { // This is a hack, but it is far superior to moving the // corresponding target-specific code from IR-gen to Sema/AST. - ArrayRef<QualType> ParamTypes = Proto->getArgTypes(); + ArrayRef<QualType> ParamTypes = Proto->getParamTypes(); SmallVector<QualType, 8> ArgTypes; if (ParamTypes.empty() && Proto->isVariadic()) { // the special case ArgTypes.reserve(E->getNumArgs()); @@ -12866,7 +13221,7 @@ ExprResult RebuildUnknownAnyExpr::VisitCallExpr(CallExpr *E) { // Finally, we can recurse. ExprResult CalleeResult = Visit(CalleeExpr); if (!CalleeResult.isUsable()) return ExprError(); - E->setCallee(CalleeResult.take()); + E->setCallee(CalleeResult.get()); // Bind a temporary if necessary. return S.MaybeBindToTemporary(E); @@ -12882,8 +13237,8 @@ ExprResult RebuildUnknownAnyExpr::VisitObjCMessageExpr(ObjCMessageExpr *E) { // Rewrite the method result type if available. if (ObjCMethodDecl *Method = E->getMethodDecl()) { - assert(Method->getResultType() == S.Context.UnknownAnyTy); - Method->setResultType(DestType); + assert(Method->getReturnType() == S.Context.UnknownAnyTy); + Method->setReturnType(DestType); } // Change the type of the message. @@ -12907,8 +13262,8 @@ ExprResult RebuildUnknownAnyExpr::VisitImplicitCastExpr(ImplicitCastExpr *E) { ExprResult Result = Visit(E->getSubExpr()); if (!Result.isUsable()) return ExprError(); - E->setSubExpr(Result.take()); - return S.Owned(E); + E->setSubExpr(Result.get()); + return E; } else if (E->getCastKind() == CK_LValueToRValue) { assert(E->getValueKind() == VK_RValue); assert(E->getObjectKind() == OK_Ordinary); @@ -12923,8 +13278,8 @@ ExprResult RebuildUnknownAnyExpr::VisitImplicitCastExpr(ImplicitCastExpr *E) { ExprResult Result = Visit(E->getSubExpr()); if (!Result.isUsable()) return ExprError(); - E->setSubExpr(Result.take()); - return S.Owned(E); + E->setSubExpr(Result.get()); + return E; } else { llvm_unreachable("Unhandled cast type!"); } @@ -12942,7 +13297,7 @@ ExprResult RebuildUnknownAnyExpr::resolveDecl(Expr *E, ValueDecl *VD) { DestType = Ptr->getPointeeType(); ExprResult Result = resolveDecl(E, VD); if (Result.isInvalid()) return ExprError(); - return S.ImpCastExprToType(Result.take(), Type, + return S.ImpCastExprToType(Result.get(), Type, CK_FunctionToPointerDecay, VK_RValue); } @@ -12984,7 +13339,7 @@ ExprResult RebuildUnknownAnyExpr::resolveDecl(Expr *E, ValueDecl *VD) { VD->setType(DestType); E->setType(Type); E->setValueKind(ValueKind); - return S.Owned(E); + return E; } /// Check a cast of an unknown-any type. We intentionally only @@ -12996,7 +13351,7 @@ ExprResult Sema::checkUnknownAnyCast(SourceRange TypeRange, QualType CastType, ExprResult result = RebuildUnknownAnyExpr(*this, CastType).Visit(CastExpr); if (!result.isUsable()) return ExprError(); - CastExpr = result.take(); + CastExpr = result.get(); VK = CastExpr->getValueKind(); CastKind = CK_NoOp; @@ -13027,7 +13382,7 @@ ExprResult Sema::checkUnknownAnyArg(SourceLocation callLoc, InitializedEntity entity = InitializedEntity::InitializeParameter(Context, paramType, /*consumed*/ false); - return PerformCopyInitialization(entity, callLoc, Owned(arg)); + return PerformCopyInitialization(entity, callLoc, arg); } static ExprResult diagnoseUnknownAnyExpr(Sema &S, Expr *E) { @@ -13077,7 +13432,7 @@ static ExprResult diagnoseUnknownAnyExpr(Sema &S, Expr *E) { /// Returns true if there was an error and no recovery was possible. ExprResult Sema::CheckPlaceholderExpr(Expr *E) { const BuiltinType *placeholderType = E->getType()->getAsPlaceholderType(); - if (!placeholderType) return Owned(E); + if (!placeholderType) return E; switch (placeholderType->getKind()) { @@ -13085,7 +13440,7 @@ ExprResult Sema::CheckPlaceholderExpr(Expr *E) { case BuiltinType::Overload: { // Try to resolve a single function template specialization. // This is obligatory. - ExprResult result = Owned(E); + ExprResult result = E; if (ResolveAndFixSingleFunctionTemplateSpecialization(result, false)) { return result; @@ -13099,7 +13454,7 @@ ExprResult Sema::CheckPlaceholderExpr(Expr *E) { // Bound member functions. case BuiltinType::BoundMember: { - ExprResult result = Owned(E); + ExprResult result = E; tryToRecoverWithCall(result, PDiag(diag::err_bound_member_function), /*complain*/ true); return result; @@ -13109,7 +13464,7 @@ ExprResult Sema::CheckPlaceholderExpr(Expr *E) { case BuiltinType::ARCUnbridgedCast: { Expr *realCast = stripARCUnbridgedCast(E); diagnoseARCUnbridgedCast(realCast); - return Owned(realCast); + return realCast; } // Expressions of unknown type. @@ -13120,9 +13475,22 @@ ExprResult Sema::CheckPlaceholderExpr(Expr *E) { case BuiltinType::PseudoObject: return checkPseudoObjectRValue(E); - case BuiltinType::BuiltinFn: + case BuiltinType::BuiltinFn: { + // Accept __noop without parens by implicitly converting it to a call expr. + auto *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts()); + if (DRE) { + auto *FD = cast<FunctionDecl>(DRE->getDecl()); + if (FD->getBuiltinID() == Builtin::BI__noop) { + E = ImpCastExprToType(E, Context.getPointerType(FD->getType()), + CK_BuiltinFnToFnPtr).get(); + return new (Context) CallExpr(Context, E, None, Context.IntTy, + VK_RValue, SourceLocation()); + } + } + Diag(E->getLocStart(), diag::err_builtin_fn_use); return ExprError(); + } // Everything else should be impossible. #define BUILTIN_TYPE(Id, SingletonId) \ @@ -13160,6 +13528,6 @@ Sema::ActOnObjCBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { } if (Context.getBOOLDecl()) BoolT = Context.getBOOLType(); - return Owned(new (Context) ObjCBoolLiteralExpr(Kind == tok::kw___objc_yes, - BoolT, OpLoc)); + return new (Context) + ObjCBoolLiteralExpr(Kind == tok::kw___objc_yes, BoolT, OpLoc); } |