Skip to content

Commit

Permalink
[flang] Retrieve the correct scope when lowering SELECT TYPE
Browse files Browse the repository at this point in the history
Scope to retrieve the associating entity is needed to map the
symbol to the IR value. The scope can be found with a source
information. For the type case in SELECT TYPE construct, the source
information is on the Statement<TypeCase>. This patch updates
the lowering so the scopes for each type guards is retrieved
before the processing.

Reviewed By: PeteSteinfeld, vdonaldson

Differential Revision: https://reviews.llvm.org/D144133
  • Loading branch information
clementval committed Feb 16, 2023
1 parent 86c8ea9 commit b0de872
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
17 changes: 14 additions & 3 deletions flang/lib/Lower/Bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2179,6 +2179,19 @@ class FirConverter : public Fortran::lower::AbstractConverter {
unsigned typeGuardIdx = 0;
std::size_t defaultAttrPos = std::numeric_limits<size_t>::max();
bool hasLocalScope = false;
llvm::SmallVector<const Fortran::semantics::Scope *> typeCaseScopes;

const auto &typeCaseList =
std::get<std::list<Fortran::parser::SelectTypeConstruct::TypeCase>>(
selectTypeConstruct.t);
for (const auto &typeCase : typeCaseList) {
const auto &stmt =
std::get<Fortran::parser::Statement<Fortran::parser::TypeGuardStmt>>(
typeCase.t);
const Fortran::semantics::Scope &scope =
bridge.getSemanticsContext().FindScope(stmt.source);
typeCaseScopes.push_back(&scope);
}

for (Fortran::lower::pft::Evaluation &eval :
getEval().getNestedEvaluations()) {
Expand Down Expand Up @@ -2275,13 +2288,11 @@ class FirConverter : public Fortran::lower::AbstractConverter {
"TypeGuard attribute missing");
mlir::Attribute typeGuardAttr = attrList[typeGuardIdx];
mlir::Block *typeGuardBlock = blockList[typeGuardIdx];
const Fortran::semantics::Scope &guardScope =
bridge.getSemanticsContext().FindScope(eval.position);
mlir::OpBuilder::InsertPoint crtInsPt = builder->saveInsertionPoint();
builder->setInsertionPointToStart(typeGuardBlock);

auto addAssocEntitySymbol = [&](fir::ExtendedValue exv) {
for (auto &symbol : guardScope.GetSymbols()) {
for (auto &symbol : typeCaseScopes[typeGuardIdx]->GetSymbols()) {
if (symbol->GetUltimate()
.detailsIf<Fortran::semantics::AssocEntityDetails>()) {
addSymbol(symbol, exv);
Expand Down
17 changes: 17 additions & 0 deletions flang/test/Lower/select-type.f90
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,23 @@ subroutine select_type13(a)
! CHECK: ^bb6:
! CHECK: ^bb7:

subroutine select_type14(a, b)
class(p1) :: a, b

select type(a)
type is (p2)
select type (b)
type is (p2)
print*,a%c,b%C
end select
class default
print*,a%a
end select
end subroutine

! Just makes sure the example can be lowered.
! CHECK-LABEL: func.func @_QMselect_type_lower_testPselect_type14

end module

program test_select_type
Expand Down

0 comments on commit b0de872

Please sign in to comment.