From 7024515c7d94598d8464d7478ac3a58d9443df76 Mon Sep 17 00:00:00 2001 From: Steve Scalpone Date: Wed, 27 Mar 2019 15:44:17 -0700 Subject: [PATCH] [flang] Implement semantics for computed GOTO. Note that a PGI extension will implicitly convert a float to integer (and issue a warning). This commit does not implement that extension. Original-commit: flang-compiler/f18@b85755f26fef767850572ef5d3deea87258d87a7 Reviewed-on: https://github.com/flang-compiler/f18/pull/360 Tree-same-pre-rewrite: false --- flang/lib/semantics/CMakeLists.txt | 1 + flang/lib/semantics/check-computed-goto.cc | 44 ++++++++++++++++++++++++++++++ flang/lib/semantics/check-computed-goto.h | 35 ++++++++++++++++++++++++ flang/lib/semantics/semantics.cc | 4 ++- flang/test/semantics/CMakeLists.txt | 2 ++ flang/test/semantics/computed-goto01.f90 | 37 +++++++++++++++++++++++++ flang/test/semantics/computed-goto02.f90 | 36 ++++++++++++++++++++++++ 7 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 flang/lib/semantics/check-computed-goto.cc create mode 100644 flang/lib/semantics/check-computed-goto.h create mode 100644 flang/test/semantics/computed-goto01.f90 create mode 100644 flang/test/semantics/computed-goto02.f90 diff --git a/flang/lib/semantics/CMakeLists.txt b/flang/lib/semantics/CMakeLists.txt index 4b380ce..39d3d63 100644 --- a/flang/lib/semantics/CMakeLists.txt +++ b/flang/lib/semantics/CMakeLists.txt @@ -17,6 +17,7 @@ add_library(FortranSemantics attr.cc canonicalize-do.cc check-arithmeticif.cc + check-computed-goto.cc check-do-concurrent.cc check-if-construct.cc check-if-stmt.cc diff --git a/flang/lib/semantics/check-computed-goto.cc b/flang/lib/semantics/check-computed-goto.cc new file mode 100644 index 0000000..3ae95c2 --- /dev/null +++ b/flang/lib/semantics/check-computed-goto.cc @@ -0,0 +1,44 @@ +// Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "check-computed-goto.h" +#include "attr.h" +#include "scope.h" +#include "semantics.h" +#include "symbol.h" +#include "tools.h" +#include "type.h" +#include "../evaluate/traversal.h" +#include "../parser/message.h" +#include "../parser/parse-tree.h" + +namespace Fortran::semantics { + +void ComputedGotoStmtChecker::Leave( + const parser::ComputedGotoStmt &computedGotoStmt) { + // C1169 Labels will have already been check + // R1158 Check for scalar-int-expr + auto &expr{ + std::get(computedGotoStmt.t).thing.thing.value()}; + if (expr.typedExpr->v.Rank() > 0) { + context_.messages().Say(expr.source, + "Computed GOTO expression must be a scalar expression"_err_en_US); + } else if (!ExprHasTypeCategory( + *expr.typedExpr, common::TypeCategory::Integer)) { + context_.messages().Say(expr.source, + "Computed GOTO expression must be an integer expression"_err_en_US); + } +} + +} // namespace Fortran::semantics diff --git a/flang/lib/semantics/check-computed-goto.h b/flang/lib/semantics/check-computed-goto.h new file mode 100644 index 0000000..f439e2f --- /dev/null +++ b/flang/lib/semantics/check-computed-goto.h @@ -0,0 +1,35 @@ +// Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef FORTRAN_SEMANTICS_CHECK_COMPUTED_GOTO_STMT_H_ +#define FORTRAN_SEMANTICS_CHECK_COMPUTED_GOTO_STMT_H_ + +#include "semantics.h" + +namespace Fortran::parser { +struct ComputedGotoStmt; +} + +namespace Fortran::semantics { +class ComputedGotoStmtChecker : public virtual BaseChecker { +public: + inline ComputedGotoStmtChecker(SemanticsContext &context) + : context_{context} {} + void Leave(const parser::ComputedGotoStmt &); + +private: + SemanticsContext &context_; +}; +} +#endif // FORTRAN_SEMANTICS_CHECK_COMPUTED_GOTO_STMT_H_ diff --git a/flang/lib/semantics/semantics.cc b/flang/lib/semantics/semantics.cc index 803d698..66af295 100644 --- a/flang/lib/semantics/semantics.cc +++ b/flang/lib/semantics/semantics.cc @@ -16,6 +16,7 @@ #include "assignment.h" #include "canonicalize-do.h" #include "check-arithmeticif.h" +#include "check-computed-goto.h" #include "check-do-concurrent.h" #include "check-if-construct.h" #include "check-if-stmt.h" @@ -64,7 +65,8 @@ private: using StatementSemanticsPass1 = SemanticsVisitor; using StatementSemanticsPass2 = SemanticsVisitor; + AssignmentChecker, ComputedGotoStmtChecker, DoConcurrentChecker, + IfConstructChecker, IfStmtChecker>; SemanticsContext::SemanticsContext( const common::IntrinsicTypeDefaultKinds &defaultKinds, diff --git a/flang/test/semantics/CMakeLists.txt b/flang/test/semantics/CMakeLists.txt index 670c951..c32e5a2 100644 --- a/flang/test/semantics/CMakeLists.txt +++ b/flang/test/semantics/CMakeLists.txt @@ -84,6 +84,8 @@ set(ERROR_TESTS if_construct02.f90 if_stmt02.f90 if_stmt03.f90 + computed-goto01.f90 + computed-goto02.f90 ) # These test files have expected symbols in the source diff --git a/flang/test/semantics/computed-goto01.f90 b/flang/test/semantics/computed-goto01.f90 new file mode 100644 index 0000000..8440938 --- /dev/null +++ b/flang/test/semantics/computed-goto01.f90 @@ -0,0 +1,37 @@ +! Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. +! +! Licensed under the Apache License, Version 2.0 (the "License"); +! you may not use this file except in compliance with the License. +! You may obtain a copy of the License at +! +! http://www.apache.org/licenses/LICENSE-2.0 +! +! Unless required by applicable law or agreed to in writing, software +! distributed under the License is distributed on an "AS IS" BASIS, +! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +! See the License for the specific language governing permissions and +! limitations under the License. + +! Check that a basic computed goto compiles + +INTEGER, DIMENSION (2) :: B + +GOTO (100) 1 +GOTO (100) I +GOTO (100) I+J +GOTO (100) B(1) + +GOTO (100, 200) 1 +GOTO (100, 200) I +GOTO (100, 200) I+J +GOTO (100, 200) B(1) + +GOTO (100, 200, 300) 1 +GOTO (100, 200, 300) I +GOTO (100, 200, 300) I+J +GOTO (100, 200, 300) B(1) + +100 CONTINUE +200 CONTINUE +300 CONTINUE +END diff --git a/flang/test/semantics/computed-goto02.f90 b/flang/test/semantics/computed-goto02.f90 new file mode 100644 index 0000000..45efc75 --- /dev/null +++ b/flang/test/semantics/computed-goto02.f90 @@ -0,0 +1,36 @@ +! Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. +! +! Licensed under the Apache License, Version 2.0 (the "License"); +! you may not use this file except in compliance with the License. +! You may obtain a copy of the License at +! +! http://www.apache.org/licenses/LICENSE-2.0 +! +! Unless required by applicable law or agreed to in writing, software +! distributed under the License is distributed on an "AS IS" BASIS, +! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +! See the License for the specific language governing permissions and +! limitations under the License. + +! Check that computed goto express must be a scalar integer expression +! TODO: PGI, for example, accepts a float & converts the value to int. + +REAL R +COMPLEX Z +LOGICAL L +INTEGER, DIMENSION (2) :: B + +!ERROR: Computed GOTO expression must be an integer expression +GOTO (100) 1.5 +!ERROR: Computed GOTO expression must be an integer expression +GOTO (100) .TRUE. +!ERROR: Computed GOTO expression must be an integer expression +GOTO (100) R +!ERROR: Computed GOTO expression must be an integer expression +GOTO (100) Z +!ERROR: Computed GOTO expression must be a scalar expression +GOTO (100) B + +100 CONTINUE + +END -- 2.7.4