D.getDeclSpec().getUnalignedSpecLoc());
}
-static void CopyTypeConstraintFromAutoType(Sema &SemaRef, const AutoType *Auto,
- AutoTypeLoc AutoLoc,
- TemplateTypeParmDecl *TP,
- SourceLocation EllipsisLoc) {
-
- TemplateArgumentListInfo TAL(AutoLoc.getLAngleLoc(), AutoLoc.getRAngleLoc());
- for (unsigned Idx = 0; Idx < AutoLoc.getNumArgs(); ++Idx)
- TAL.addArgument(AutoLoc.getArgLoc(Idx));
-
- SemaRef.AttachTypeConstraint(
- AutoLoc.getNestedNameSpecifierLoc(), AutoLoc.getConceptNameInfo(),
- AutoLoc.getNamedConcept(),
- AutoLoc.hasExplicitTemplateArgs() ? &TAL : nullptr, TP, EllipsisLoc);
-}
-
-static QualType InventTemplateParameter(
- TypeProcessingState &state, QualType T, TypeSourceInfo *TSI, AutoType *Auto,
- InventedTemplateParameterInfo &Info) {
+static std::pair<QualType, TypeSourceInfo *>
+InventTemplateParameter(TypeProcessingState &state, QualType T,
+ TypeSourceInfo *TrailingTSI, AutoType *Auto,
+ InventedTemplateParameterInfo &Info) {
Sema &S = state.getSema();
Declarator &D = state.getDeclarator();
IsParameterPack, /*HasTypeConstraint=*/Auto->isConstrained());
InventedTemplateParam->setImplicit();
Info.TemplateParams.push_back(InventedTemplateParam);
- // Attach type constraints
+
+ // Attach type constraints to the new parameter.
if (Auto->isConstrained()) {
- if (TSI) {
- CopyTypeConstraintFromAutoType(
- S, Auto, TSI->getTypeLoc().getContainedAutoTypeLoc(),
- InventedTemplateParam, D.getEllipsisLoc());
+ if (TrailingTSI) {
+ // The 'auto' appears in a trailing return type we've already built;
+ // extract its type constraints to attach to the template parameter.
+ AutoTypeLoc AutoLoc = TrailingTSI->getTypeLoc().getContainedAutoTypeLoc();
+ TemplateArgumentListInfo TAL(AutoLoc.getLAngleLoc(), AutoLoc.getRAngleLoc());
+ for (unsigned Idx = 0; Idx < AutoLoc.getNumArgs(); ++Idx)
+ TAL.addArgument(AutoLoc.getArgLoc(Idx));
+
+ S.AttachTypeConstraint(AutoLoc.getNestedNameSpecifierLoc(),
+ AutoLoc.getConceptNameInfo(),
+ AutoLoc.getNamedConcept(),
+ AutoLoc.hasExplicitTemplateArgs() ? &TAL : nullptr,
+ InventedTemplateParam, D.getEllipsisLoc());
} else {
+ // The 'auto' appears in the decl-specifiers; we've not finished forming
+ // TypeSourceInfo for it yet.
TemplateIdAnnotation *TemplateId = D.getDeclSpec().getRepAsTemplateId();
TemplateArgumentListInfo TemplateArgsInfo;
if (TemplateId->LAngleLoc.isValid()) {
}
}
- // If TSI is nullptr, this is a constrained declspec auto and the type
- // constraint will be attached later in TypeSpecLocFiller
-
// Replace the 'auto' in the function parameter with this invented
// template type parameter.
// FIXME: Retain some type sugar to indicate that this was written
// as 'auto'?
- return state.ReplaceAutoType(
- T, QualType(InventedTemplateParam->getTypeForDecl(), 0));
+ QualType Replacement(InventedTemplateParam->getTypeForDecl(), 0);
+ QualType NewT = state.ReplaceAutoType(T, Replacement);
+ TypeSourceInfo *NewTSI =
+ TrailingTSI ? S.ReplaceAutoTypeSourceInfo(TrailingTSI, Replacement)
+ : nullptr;
+ return {NewT, NewTSI};
}
static TypeSourceInfo *
if (!D.getAttributes().empty())
distributeTypeAttrsFromDeclarator(state, T);
+ // Find the deduced type in this type. Look in the trailing return type if we
+ // have one, otherwise in the DeclSpec type.
+ // FIXME: The standard wording doesn't currently describe this.
+ DeducedType *Deduced = T->getContainedDeducedType();
+ bool DeducedIsTrailingReturnType = false;
+ if (Deduced && isa<AutoType>(Deduced) && D.hasTrailingReturnType()) {
+ QualType T = SemaRef.GetTypeFromParser(D.getTrailingReturnType());
+ Deduced = T.isNull() ? nullptr : T->getContainedDeducedType();
+ DeducedIsTrailingReturnType = true;
+ }
+
// C++11 [dcl.spec.auto]p5: reject 'auto' if it is not in an allowed context.
- if (DeducedType *Deduced = T->getContainedDeducedType()) {
+ if (Deduced) {
AutoType *Auto = dyn_cast<AutoType>(Deduced);
int Error = -1;
} else if (!SemaRef.getCurScope()->isFunctionDeclarationScope()) {
Error = 21;
break;
- } else if (D.hasTrailingReturnType()) {
- // This might be OK, but we'll need to convert the trailing return
- // type later.
- break;
}
Info = &SemaRef.InventedParameterInfos.back();
Info = SemaRef.getCurLambda();
assert(Info && "No LambdaScopeInfo on the stack!");
}
- T = InventTemplateParameter(state, T, nullptr, Auto, *Info);
+
+ // We'll deal with inventing template parameters for 'auto' in trailing
+ // return types when we pick up the trailing return type when processing
+ // the function chunk.
+ if (!DeducedIsTrailingReturnType)
+ T = InventTemplateParameter(state, T, nullptr, Auto, *Info).first;
break;
}
case DeclaratorContext::MemberContext: {
(!SemaRef.getLangOpts().CPlusPlus11 || !IsCXXAutoType))
Error = 13;
- bool HaveTrailing = false;
-
- // C++11 [dcl.spec.auto]p2: 'auto' is always fine if the declarator
- // contains a trailing return type. That is only legal at the outermost
- // level. Check all declarator chunks (outermost first) anyway, to give
- // better diagnostics.
- // We don't support '__auto_type' with trailing return types.
- // FIXME: Should we only do this for 'auto' and not 'decltype(auto)'?
- if (SemaRef.getLangOpts().CPlusPlus11 && IsCXXAutoType &&
- D.hasTrailingReturnType()) {
- HaveTrailing = true;
- Error = -1;
- }
-
SourceRange AutoRange = D.getDeclSpec().getTypeSpecTypeLoc();
if (D.getName().getKind() == UnqualifiedIdKind::IK_ConversionFunctionId)
AutoRange = D.getName().getSourceRange();
T = SemaRef.Context.IntTy;
D.setInvalidType(true);
- } else if (Auto && !HaveTrailing &&
- D.getContext() != DeclaratorContext::LambdaExprContext) {
+ } else if (Auto && D.getContext() != DeclaratorContext::LambdaExprContext) {
// If there was a trailing return type, we already got
// warn_cxx98_compat_trailing_return_type in the parser.
SemaRef.Diag(AutoRange.getBegin(),
// An error occurred parsing the trailing return type.
T = Context.IntTy;
D.setInvalidType(true);
- } else if (S.getLangOpts().CPlusPlus20)
- // Handle cases like: `auto f() -> auto` or `auto f() -> C auto`.
- if (AutoType *Auto = T->getContainedAutoType())
- if (S.getCurScope()->isFunctionDeclarationScope())
- T = InventTemplateParameter(state, T, TInfo, Auto,
- S.InventedParameterInfos.back());
+ } else if (AutoType *Auto = T->getContainedAutoType()) {
+ // If the trailing return type contains an `auto`, we may need to
+ // invent a template parameter for it, for cases like
+ // `auto f() -> C auto` or `[](auto (*p) -> auto) {}`.
+ InventedTemplateParameterInfo *InventedParamInfo = nullptr;
+ if (D.getContext() == DeclaratorContext::PrototypeContext)
+ InventedParamInfo = &S.InventedParameterInfos.back();
+ else if (D.getContext() ==
+ DeclaratorContext::LambdaExprParameterContext)
+ InventedParamInfo = S.getCurLambda();
+ if (InventedParamInfo) {
+ std::tie(T, TInfo) = InventTemplateParameter(
+ state, T, TInfo, Auto, *InventedParamInfo);
+ }
+ }
} else {
// This function type is not the type of the entity being declared,
// so checking the 'auto' is not the responsibility of this chunk.