Skip to content

[flang] Handle substring in data statement constant #120130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions flang/include/flang/Parser/parse-tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -1481,9 +1481,10 @@ struct DataStmtConstant {
UNION_CLASS_BOILERPLATE(DataStmtConstant);
CharBlock source;
mutable TypedExpr typedExpr;
std::variant<LiteralConstant, SignedIntLiteralConstant,
SignedRealLiteralConstant, SignedComplexLiteralConstant, NullInit,
common::Indirection<Designator>, StructureConstructor>
std::variant<common::Indirection<CharLiteralConstantSubstring>,
LiteralConstant, SignedIntLiteralConstant, SignedRealLiteralConstant,
SignedComplexLiteralConstant, NullInit, common::Indirection<Designator>,
StructureConstructor>
u;
};

Expand Down
7 changes: 5 additions & 2 deletions flang/lib/Parser/Fortran-parsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -929,8 +929,11 @@ TYPE_PARSER(construct<DataStmtRepeat>(intLiteralConstant) ||
// components can be ambiguous with a scalar-constant-subobject.
// So we parse literal constants, designator, null-init, and
// structure-constructor, so that semantics can figure things out later
// with the symbol table.
TYPE_PARSER(sourced(first(construct<DataStmtConstant>(literalConstant),
// with the symbol table. A literal constant substring must be attempted
// first to avoid a partial match with a literal constant.
TYPE_PARSER(sourced(first(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps the comment above (starting on line 921) could be updated to include literal constant substrings?

construct<DataStmtConstant>(indirect(charLiteralConstantSubstring)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: indirect here is just to ensure that charLiteralConstantSubstring is not null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it allows the type CharLiteralConstantSubstring to be forward-referenced before it can be defined.

construct<DataStmtConstant>(literalConstant),
construct<DataStmtConstant>(signedRealLiteralConstant),
construct<DataStmtConstant>(signedIntLiteralConstant),
extension<LanguageFeature::SignedComplexLiteral>(
Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Parser/expr-parsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ TYPE_PARSER(construct<AcImpliedDoControl>(
// type-param-inquiry is parsed as a structure component, except for
// substring%KIND/LEN
constexpr auto primary{instrumented("primary"_en_US,
first(construct<Expr>(indirect(Parser<CharLiteralConstantSubstring>{})),
first(construct<Expr>(indirect(charLiteralConstantSubstring)),
construct<Expr>(literalConstant),
construct<Expr>(construct<Expr::Parentheses>("(" >>
expr / !","_tok / recovery(")"_tok, SkipPastNested<'(', ')'>{}))),
Expand Down
1 change: 1 addition & 0 deletions flang/lib/Parser/type-parsers.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ constexpr Parser<KindParam> kindParam; // R709
constexpr Parser<RealLiteralConstant> realLiteralConstant; // R714
constexpr Parser<CharLength> charLength; // R723
constexpr Parser<CharLiteralConstant> charLiteralConstant; // R724
constexpr Parser<CharLiteralConstantSubstring> charLiteralConstantSubstring;
constexpr Parser<Initialization> initialization; // R743 & R805
constexpr Parser<DerivedTypeSpec> derivedTypeSpec; // R754
constexpr Parser<TypeDeclarationStmt> typeDeclarationStmt; // R801
Expand Down
7 changes: 7 additions & 0 deletions flang/test/Parser/lit-substr-data.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
!RUN: %flang_fc1 -fdebug-unparse %s 2>&1 | FileCheck %s
!Regression test for bug #119005
character*2 :: ary4
!CHECK: DATA ary4/"cd"/
data ary4/"abcdef"(3:4)/
end

Loading