Debian Codegraph @

flang/lib/Semantics/canonicalize-directives.cpp

raw
1 //===-- lib/Semantics/canonicalize-directives.cpp -------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "canonicalize-directives.h"
10 #include "flang/Parser/parse-tree-visitor.h"
11
12 namespace Fortran::semantics {
13
14 using namespace parser::literals;
15
16 // Check that directives are associated with the correct constructs.
17 // Directives that need to be associated with other constructs in the execution
18 // part are moved to the execution part so they can be checked there.
19 class CanonicalizationOfDirectives {
20 public:
21 CanonicalizationOfDirectives(parser::Messages &messages)
22 : messages_{messages} {}
23
24 template <typename T> bool Pre(T &) { return true; }
25 template <typename T> void Post(T &) {}
26
27 // Move directives that must appear in the Execution part out of the
28 // Specification part.
29 void Post(parser::SpecificationPart &spec);
30 bool Pre(parser::ExecutionPart &x);
31
32 // Ensure that directives associated with constructs appear accompanying the
33 // construct.
34 void Post(parser::Block &block);
35
36 private:
37 // Ensure that loop directives appear immediately before a loop.
38 void CheckLoopDirective(parser::CompilerDirective &dir, parser::Block &block,
39 std::list<parser::ExecutionPartConstruct>::iterator it);
40
41 parser::Messages &messages_;
42
43 // Directives to be moved to the Execution part from the Specification part.
44 std::list<common::Indirection<parser::CompilerDirective>>
45 directivesToConvert_;
46 };
47
48 bool CanonicalizeDirectives(
49 parser::Messages &messages, parser::Program &program) {
50 CanonicalizationOfDirectives dirs{messages};
51 Walk(program, dirs);
52 return !messages.AnyFatalError();
53 }
54
55 static bool IsExecutionDirective(const parser::CompilerDirective &dir) {
56 return std::holds_alternative<parser::CompilerDirective::VectorAlways>(dir.u);
57 }
58
59 void CanonicalizationOfDirectives::Post(parser::SpecificationPart &spec) {
60 auto &list{
61 std::get<std::list<common::Indirection<parser::CompilerDirective>>>(
62 spec.t)};
63 for (auto it{list.begin()}; it != list.end();) {
64 if (IsExecutionDirective(it->value())) {
65 directivesToConvert_.emplace_back(std::move(*it));
66 it = list.erase(it);
67 } else {
68 ++it;
69 }
70 }
71 }
72
73 bool CanonicalizationOfDirectives::Pre(parser::ExecutionPart &x) {
74 auto origFirst{x.v.begin()};
75 for (auto &dir : directivesToConvert_) {
76 x.v.insert(origFirst,
77 parser::ExecutionPartConstruct{
78 parser::ExecutableConstruct{std::move(dir)}});
79 }
80
81 directivesToConvert_.clear();
82 return true;
83 }
84
85 template <typename T> T *GetConstructIf(parser::ExecutionPartConstruct &x) {
86 if (auto *y{std::get_if<parser::ExecutableConstruct>(&x.u)}) {
87 if (auto *z{std::get_if<common::Indirection<T>>(&y->u)}) {
88 return &z->value();
89 }
90 }
91 return nullptr;
92 }
93
94 void CanonicalizationOfDirectives::CheckLoopDirective(
95 parser::CompilerDirective &dir, parser::Block &block,
96 std::list<parser::ExecutionPartConstruct>::iterator it) {
97
98 // Skip over this and other compiler directives
99 while (GetConstructIf<parser::CompilerDirective>(*it)) {
100 ++it;
101 }
102
103 if (it == block.end() || !GetConstructIf<parser::DoConstruct>(*it)) {
104 std::string s{parser::ToUpperCaseLetters(dir.source.ToString())};
105 s.pop_back(); // Remove trailing newline from source string
106 messages_.Say(
107 dir.source, "A DO loop must follow the %s directive"_warn_en_US, s);
108 }
109 }
110
111 void CanonicalizationOfDirectives::Post(parser::Block &block) {
112 for (auto it{block.begin()}; it != block.end(); ++it) {
113 if (auto *dir{GetConstructIf<parser::CompilerDirective>(*it)}) {
114 std::visit(
115 common::visitors{[&](parser::CompilerDirective::VectorAlways &) {
116 CheckLoopDirective(*dir, block, it);
117 },
118 [&](auto &) {}},
119 dir->u);
120 }
121 }
122 }
123
124 } // namespace Fortran::semantics