Skip to content

Commit 9fd22cb

Browse files
authored
[flang][NFC] Move new code to right place (#144551)
Some new code was added to flang/Semantics that only depends on facilities in flang/Evaluate. Move it into Evaluate and clean up some minor stylistic problems.
1 parent 03692aa commit 9fd22cb

File tree

7 files changed

+467
-482
lines changed

7 files changed

+467
-482
lines changed

flang/include/flang/Evaluate/tools.h

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,154 @@ inline bool HasCUDAImplicitTransfer(const Expr<SomeType> &expr) {
13891389
return (hasConstant || (hostSymbols > 0)) && deviceSymbols > 0;
13901390
}
13911391

1392+
// Checks whether the symbol on the LHS is present in the RHS expression.
1393+
bool CheckForSymbolMatch(const Expr<SomeType> *lhs, const Expr<SomeType> *rhs);
1394+
1395+
namespace operation {
1396+
1397+
enum class Operator {
1398+
Unknown,
1399+
Add,
1400+
And,
1401+
Associated,
1402+
Call,
1403+
Constant,
1404+
Convert,
1405+
Div,
1406+
Eq,
1407+
Eqv,
1408+
False,
1409+
Ge,
1410+
Gt,
1411+
Identity,
1412+
Intrinsic,
1413+
Le,
1414+
Lt,
1415+
Max,
1416+
Min,
1417+
Mul,
1418+
Ne,
1419+
Neqv,
1420+
Not,
1421+
Or,
1422+
Pow,
1423+
Resize, // Convert within the same TypeCategory
1424+
Sub,
1425+
True,
1426+
};
1427+
1428+
std::string ToString(Operator op);
1429+
1430+
template <typename... Ts, int Kind>
1431+
Operator OperationCode(
1432+
const evaluate::Operation<evaluate::LogicalOperation<Kind>, Ts...> &op) {
1433+
switch (op.derived().logicalOperator) {
1434+
case common::LogicalOperator::And:
1435+
return Operator::And;
1436+
case common::LogicalOperator::Or:
1437+
return Operator::Or;
1438+
case common::LogicalOperator::Eqv:
1439+
return Operator::Eqv;
1440+
case common::LogicalOperator::Neqv:
1441+
return Operator::Neqv;
1442+
case common::LogicalOperator::Not:
1443+
return Operator::Not;
1444+
}
1445+
return Operator::Unknown;
1446+
}
1447+
1448+
template <typename T, typename... Ts>
1449+
Operator OperationCode(
1450+
const evaluate::Operation<evaluate::Relational<T>, Ts...> &op) {
1451+
switch (op.derived().opr) {
1452+
case common::RelationalOperator::LT:
1453+
return Operator::Lt;
1454+
case common::RelationalOperator::LE:
1455+
return Operator::Le;
1456+
case common::RelationalOperator::EQ:
1457+
return Operator::Eq;
1458+
case common::RelationalOperator::NE:
1459+
return Operator::Ne;
1460+
case common::RelationalOperator::GE:
1461+
return Operator::Ge;
1462+
case common::RelationalOperator::GT:
1463+
return Operator::Gt;
1464+
}
1465+
return Operator::Unknown;
1466+
}
1467+
1468+
template <typename T, typename... Ts>
1469+
Operator OperationCode(const evaluate::Operation<evaluate::Add<T>, Ts...> &op) {
1470+
return Operator::Add;
1471+
}
1472+
1473+
template <typename T, typename... Ts>
1474+
Operator OperationCode(
1475+
const evaluate::Operation<evaluate::Subtract<T>, Ts...> &op) {
1476+
return Operator::Sub;
1477+
}
1478+
1479+
template <typename T, typename... Ts>
1480+
Operator OperationCode(
1481+
const evaluate::Operation<evaluate::Multiply<T>, Ts...> &op) {
1482+
return Operator::Mul;
1483+
}
1484+
1485+
template <typename T, typename... Ts>
1486+
Operator OperationCode(
1487+
const evaluate::Operation<evaluate::Divide<T>, Ts...> &op) {
1488+
return Operator::Div;
1489+
}
1490+
1491+
template <typename T, typename... Ts>
1492+
Operator OperationCode(
1493+
const evaluate::Operation<evaluate::Power<T>, Ts...> &op) {
1494+
return Operator::Pow;
1495+
}
1496+
1497+
template <typename T, typename... Ts>
1498+
Operator OperationCode(
1499+
const evaluate::Operation<evaluate::RealToIntPower<T>, Ts...> &op) {
1500+
return Operator::Pow;
1501+
}
1502+
1503+
template <typename T, common::TypeCategory C, typename... Ts>
1504+
Operator OperationCode(
1505+
const evaluate::Operation<evaluate::Convert<T, C>, Ts...> &op) {
1506+
if constexpr (C == T::category) {
1507+
return Operator::Resize;
1508+
} else {
1509+
return Operator::Convert;
1510+
}
1511+
}
1512+
1513+
template <typename T> Operator OperationCode(const evaluate::Constant<T> &x) {
1514+
return Operator::Constant;
1515+
}
1516+
1517+
template <typename T> Operator OperationCode(const T &) {
1518+
return Operator::Unknown;
1519+
}
1520+
1521+
Operator OperationCode(const evaluate::ProcedureDesignator &proc);
1522+
1523+
} // namespace operation
1524+
1525+
// Return information about the top-level operation (ignoring parentheses):
1526+
// the operation code and the list of arguments.
1527+
std::pair<operation::Operator, std::vector<Expr<SomeType>>>
1528+
GetTopLevelOperation(const Expr<SomeType> &expr);
1529+
1530+
// Check if expr is same as x, or a sequence of Convert operations on x.
1531+
bool IsSameOrConvertOf(const Expr<SomeType> &expr, const Expr<SomeType> &x);
1532+
1533+
// Strip away any top-level Convert operations (if any exist) and return
1534+
// the input value. A ComplexConstructor(x, 0) is also considered as a
1535+
// convert operation.
1536+
// If the input is not Operation, Designator, FunctionRef or Constant,
1537+
// it returns std::nullopt.
1538+
std::optional<Expr<SomeType>> GetConvertInput(const Expr<SomeType> &x);
1539+
13921540
} // namespace Fortran::evaluate
13931541

13941542
namespace Fortran::semantics {

flang/include/flang/Semantics/tools.h

Lines changed: 0 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -756,154 +756,5 @@ std::string GetCommonBlockObjectName(const Symbol &, bool underscoring);
756756
// Check for ambiguous USE associations
757757
bool HadUseError(SemanticsContext &, SourceName at, const Symbol *);
758758

759-
// Checks whether the symbol on the LHS is present in the RHS expression.
760-
bool CheckForSymbolMatch(const SomeExpr *lhs, const SomeExpr *rhs);
761-
762-
namespace operation {
763-
764-
enum class Operator {
765-
Unknown,
766-
Add,
767-
And,
768-
Associated,
769-
Call,
770-
Constant,
771-
Convert,
772-
Div,
773-
Eq,
774-
Eqv,
775-
False,
776-
Ge,
777-
Gt,
778-
Identity,
779-
Intrinsic,
780-
Le,
781-
Lt,
782-
Max,
783-
Min,
784-
Mul,
785-
Ne,
786-
Neqv,
787-
Not,
788-
Or,
789-
Pow,
790-
Resize, // Convert within the same TypeCategory
791-
Sub,
792-
True,
793-
};
794-
795-
std::string ToString(Operator op);
796-
797-
template <typename... Ts, int Kind>
798-
Operator OperationCode(
799-
const evaluate::Operation<evaluate::LogicalOperation<Kind>, Ts...> &op) {
800-
switch (op.derived().logicalOperator) {
801-
case common::LogicalOperator::And:
802-
return Operator::And;
803-
case common::LogicalOperator::Or:
804-
return Operator::Or;
805-
case common::LogicalOperator::Eqv:
806-
return Operator::Eqv;
807-
case common::LogicalOperator::Neqv:
808-
return Operator::Neqv;
809-
case common::LogicalOperator::Not:
810-
return Operator::Not;
811-
}
812-
return Operator::Unknown;
813-
}
814-
815-
template <typename T, typename... Ts>
816-
Operator OperationCode(
817-
const evaluate::Operation<evaluate::Relational<T>, Ts...> &op) {
818-
switch (op.derived().opr) {
819-
case common::RelationalOperator::LT:
820-
return Operator::Lt;
821-
case common::RelationalOperator::LE:
822-
return Operator::Le;
823-
case common::RelationalOperator::EQ:
824-
return Operator::Eq;
825-
case common::RelationalOperator::NE:
826-
return Operator::Ne;
827-
case common::RelationalOperator::GE:
828-
return Operator::Ge;
829-
case common::RelationalOperator::GT:
830-
return Operator::Gt;
831-
}
832-
return Operator::Unknown;
833-
}
834-
835-
template <typename T, typename... Ts>
836-
Operator OperationCode(const evaluate::Operation<evaluate::Add<T>, Ts...> &op) {
837-
return Operator::Add;
838-
}
839-
840-
template <typename T, typename... Ts>
841-
Operator OperationCode(
842-
const evaluate::Operation<evaluate::Subtract<T>, Ts...> &op) {
843-
return Operator::Sub;
844-
}
845-
846-
template <typename T, typename... Ts>
847-
Operator OperationCode(
848-
const evaluate::Operation<evaluate::Multiply<T>, Ts...> &op) {
849-
return Operator::Mul;
850-
}
851-
852-
template <typename T, typename... Ts>
853-
Operator OperationCode(
854-
const evaluate::Operation<evaluate::Divide<T>, Ts...> &op) {
855-
return Operator::Div;
856-
}
857-
858-
template <typename T, typename... Ts>
859-
Operator OperationCode(
860-
const evaluate::Operation<evaluate::Power<T>, Ts...> &op) {
861-
return Operator::Pow;
862-
}
863-
864-
template <typename T, typename... Ts>
865-
Operator OperationCode(
866-
const evaluate::Operation<evaluate::RealToIntPower<T>, Ts...> &op) {
867-
return Operator::Pow;
868-
}
869-
870-
template <typename T, common::TypeCategory C, typename... Ts>
871-
Operator OperationCode(
872-
const evaluate::Operation<evaluate::Convert<T, C>, Ts...> &op) {
873-
if constexpr (C == T::category) {
874-
return Operator::Resize;
875-
} else {
876-
return Operator::Convert;
877-
}
878-
}
879-
880-
template <typename T> //
881-
Operator OperationCode(const evaluate::Constant<T> &x) {
882-
return Operator::Constant;
883-
}
884-
885-
template <typename T> //
886-
Operator OperationCode(const T &) {
887-
return Operator::Unknown;
888-
}
889-
890-
Operator OperationCode(const evaluate::ProcedureDesignator &proc);
891-
892-
} // namespace operation
893-
894-
/// Return information about the top-level operation (ignoring parentheses):
895-
/// the operation code and the list of arguments.
896-
std::pair<operation::Operator, std::vector<SomeExpr>> GetTopLevelOperation(
897-
const SomeExpr &expr);
898-
899-
/// Check if expr is same as x, or a sequence of Convert operations on x.
900-
bool IsSameOrConvertOf(const SomeExpr &expr, const SomeExpr &x);
901-
902-
/// Strip away any top-level Convert operations (if any exist) and return
903-
/// the input value. A ComplexConstructor(x, 0) is also considered as a
904-
/// convert operation.
905-
/// If the input is not Operation, Designator, FunctionRef or Constant,
906-
/// it returns std::nullopt.
907-
MaybeExpr GetConvertInput(const SomeExpr &x);
908759
} // namespace Fortran::semantics
909760
#endif // FORTRAN_SEMANTICS_TOOLS_H_

0 commit comments

Comments
 (0)