Skip to content

Commit b7ef804

Browse files
Revert "[flang] Improve constant folding for type parameter inquiries"
This reverts commit 8c7bf2f.
1 parent f1313b3 commit b7ef804

File tree

6 files changed

+16
-100
lines changed

6 files changed

+16
-100
lines changed

flang/lib/Evaluate/fold-integer.cpp

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -658,43 +658,24 @@ Expr<Type<TypeCategory::Integer, KIND>> FoldIntrinsicFunction(
658658
// Substitute a bare type parameter reference with its value if it has one now
659659
Expr<TypeParamInquiry::Result> FoldOperation(
660660
FoldingContext &context, TypeParamInquiry &&inquiry) {
661-
std::optional<NamedEntity> base{inquiry.base()};
662-
parser::CharBlock parameterName{inquiry.parameter().name()};
663-
if (base) {
664-
// Handling "designator%typeParam". Get the value of the type parameter
665-
// from the instantiation of the base
666-
if (const semantics::DeclTypeSpec *
667-
declType{base->GetLastSymbol().GetType()}) {
668-
const semantics::DerivedTypeSpec dType{declType->derivedTypeSpec()};
669-
if (const semantics::ParamValue *
670-
paramValue{dType.FindParameter(parameterName)}) {
671-
const semantics::MaybeIntExpr &paramExpr{paramValue->GetExplicit()};
672-
if (paramExpr && IsConstantExpr(*paramExpr)) {
673-
Expr<SomeInteger> intExpr{*paramExpr};
674-
return Fold(context,
675-
ConvertToType<TypeParamInquiry::Result>(std::move(intExpr)));
676-
}
677-
}
678-
}
679-
} else {
661+
if (!inquiry.base()) {
680662
// A "bare" type parameter: replace with its value, if that's now known.
681663
if (const auto *pdt{context.pdtInstance()}) {
682664
if (const semantics::Scope * scope{context.pdtInstance()->scope()}) {
683-
auto iter{scope->find(parameterName)};
665+
auto iter{scope->find(inquiry.parameter().name())};
684666
if (iter != scope->end()) {
685667
const Symbol &symbol{*iter->second};
686668
const auto *details{symbol.detailsIf<semantics::TypeParamDetails>()};
687-
if (details) {
688-
const semantics::MaybeIntExpr &initExpr{details->init()};
689-
if (initExpr && IsConstantExpr(*initExpr)) {
690-
Expr<SomeInteger> expr{*initExpr};
691-
return Fold(context,
692-
ConvertToType<TypeParamInquiry::Result>(std::move(expr)));
693-
}
669+
if (details && details->init() &&
670+
(details->attr() == common::TypeParamAttr::Kind ||
671+
IsConstantExpr(*details->init()))) {
672+
Expr<SomeInteger> expr{*details->init()};
673+
return Fold(context,
674+
ConvertToType<TypeParamInquiry::Result>(std::move(expr)));
694675
}
695676
}
696677
}
697-
if (const auto *value{pdt->FindParameter(parameterName)}) {
678+
if (const auto *value{pdt->FindParameter(inquiry.parameter().name())}) {
698679
if (value->isExplicit()) {
699680
return Fold(context,
700681
AsExpr(ConvertToType<TypeParamInquiry::Result>(

flang/lib/Evaluate/formatting.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ llvm::raw_ostream &BaseObject::AsFortran(llvm::raw_ostream &o) const {
614614

615615
llvm::raw_ostream &TypeParamInquiry::AsFortran(llvm::raw_ostream &o) const {
616616
if (base_) {
617-
base_.value().AsFortran(o) << '%';
617+
return base_->AsFortran(o) << '%';
618618
}
619619
return EmitVar(o, parameter_);
620620
}

flang/lib/Semantics/type.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,7 @@ class InstantiateHelper {
205205
}
206206
void InstantiateComponent(const Symbol &);
207207
const DeclTypeSpec *InstantiateType(const Symbol &);
208-
const DeclTypeSpec &InstantiateIntrinsicType(
209-
SourceName, const DeclTypeSpec &);
208+
const DeclTypeSpec &InstantiateIntrinsicType(const DeclTypeSpec &);
210209
DerivedTypeSpec CreateDerivedTypeSpec(const DerivedTypeSpec &, bool);
211210

212211
SemanticsContext &context_;
@@ -365,7 +364,7 @@ const DeclTypeSpec *InstantiateHelper::InstantiateType(const Symbol &symbol) {
365364
CreateDerivedTypeSpec(*spec, symbol.test(Symbol::Flag::ParentComp)),
366365
context_, type->category());
367366
} else if (type->AsIntrinsic()) {
368-
return &InstantiateIntrinsicType(symbol.name(), *type);
367+
return &InstantiateIntrinsicType(*type);
369368
} else if (type->category() == DeclTypeSpec::ClassStar) {
370369
return type;
371370
} else {
@@ -375,7 +374,7 @@ const DeclTypeSpec *InstantiateHelper::InstantiateType(const Symbol &symbol) {
375374

376375
// Apply type parameter values to an intrinsic type spec.
377376
const DeclTypeSpec &InstantiateHelper::InstantiateIntrinsicType(
378-
SourceName symbolName, const DeclTypeSpec &spec) {
377+
const DeclTypeSpec &spec) {
379378
const IntrinsicTypeSpec &intrinsic{DEREF(spec.AsIntrinsic())};
380379
if (evaluate::ToInt64(intrinsic.kind())) {
381380
return spec; // KIND is already a known constant
@@ -388,7 +387,7 @@ const DeclTypeSpec &InstantiateHelper::InstantiateIntrinsicType(
388387
if (evaluate::IsValidKindOfIntrinsicType(intrinsic.category(), *value)) {
389388
kind = *value;
390389
} else {
391-
foldingContext().messages().Say(symbolName,
390+
foldingContext().messages().Say(
392391
"KIND parameter value (%jd) of intrinsic type %s "
393392
"did not resolve to a supported value"_err_en_US,
394393
*value,

flang/test/Semantics/assign04.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ subroutine s1
88
type(t(1, 2)) :: x
99
!ERROR: Assignment to constant 'x%k' is not allowed
1010
x%k = 4
11-
!ERROR: Assignment to constant 'x%l' is not allowed
11+
!ERROR: Left-hand side of assignment is not modifiable
1212
x%l = 3
1313
end
1414

flang/test/Semantics/resolve104.f90

Lines changed: 0 additions & 64 deletions
This file was deleted.

flang/test/Semantics/resolve89.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ subroutine inner (derivedArg)
107107
type localDerivedType
108108
! OK because the specification inquiry is a constant
109109
integer, dimension(localDerived%kindParam) :: goodField
110-
! OK because the value of lenParam is constant in this context
110+
!ERROR: Invalid specification expression: non-constant reference to a type parameter inquiry not allowed for derived type components or type parameter values
111111
integer, dimension(derivedArg%lenParam) :: badField
112112
end type localDerivedType
113113

0 commit comments

Comments
 (0)