• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- include/flang/Semantics/tools.h -------------------------*- C++ -*-===//
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 #ifndef FORTRAN_SEMANTICS_TOOLS_H_
10 #define FORTRAN_SEMANTICS_TOOLS_H_
11 
12 // Simple predicates and look-up functions that are best defined
13 // canonically for use in semantic checking.
14 
15 #include "flang/Common/Fortran.h"
16 #include "flang/Evaluate/expression.h"
17 #include "flang/Evaluate/type.h"
18 #include "flang/Evaluate/variable.h"
19 #include "flang/Parser/message.h"
20 #include "flang/Parser/parse-tree.h"
21 #include "flang/Semantics/attr.h"
22 #include "flang/Semantics/expression.h"
23 #include "flang/Semantics/semantics.h"
24 #include <functional>
25 
26 namespace Fortran::semantics {
27 
28 class DeclTypeSpec;
29 class DerivedTypeSpec;
30 class Scope;
31 class Symbol;
32 
33 // Note: Here ProgramUnit includes internal subprograms while TopLevelUnit
34 // does not. "program-unit" in the Fortran standard matches TopLevelUnit.
35 const Scope &GetTopLevelUnitContaining(const Scope &);
36 const Scope &GetTopLevelUnitContaining(const Symbol &);
37 const Scope &GetProgramUnitContaining(const Scope &);
38 const Scope &GetProgramUnitContaining(const Symbol &);
39 
40 const Scope *FindModuleContaining(const Scope &);
41 const Scope *FindPureProcedureContaining(const Scope &);
42 const Scope *FindPureProcedureContaining(const Symbol &);
43 const Symbol *FindPointerComponent(const Scope &);
44 const Symbol *FindPointerComponent(const DerivedTypeSpec &);
45 const Symbol *FindPointerComponent(const DeclTypeSpec &);
46 const Symbol *FindPointerComponent(const Symbol &);
47 const Symbol *FindInterface(const Symbol &);
48 const Symbol *FindSubprogram(const Symbol &);
49 const Symbol *FindFunctionResult(const Symbol &);
50 const Symbol *FindOverriddenBinding(const Symbol &);
51 
52 const DeclTypeSpec *FindParentTypeSpec(const DerivedTypeSpec &);
53 const DeclTypeSpec *FindParentTypeSpec(const DeclTypeSpec &);
54 const DeclTypeSpec *FindParentTypeSpec(const Scope &);
55 const DeclTypeSpec *FindParentTypeSpec(const Symbol &);
56 
57 enum class Tristate { No, Yes, Maybe };
ToTristate(bool x)58 inline Tristate ToTristate(bool x) { return x ? Tristate::Yes : Tristate::No; }
59 
60 // Is this a user-defined assignment? If both sides are the same derived type
61 // (and the ranks are okay) the answer is Maybe.
62 Tristate IsDefinedAssignment(
63     const std::optional<evaluate::DynamicType> &lhsType, int lhsRank,
64     const std::optional<evaluate::DynamicType> &rhsType, int rhsRank);
65 // Test for intrinsic unary and binary operators based on types and ranks
66 bool IsIntrinsicRelational(common::RelationalOperator,
67     const evaluate::DynamicType &, int, const evaluate::DynamicType &, int);
68 bool IsIntrinsicNumeric(const evaluate::DynamicType &);
69 bool IsIntrinsicNumeric(
70     const evaluate::DynamicType &, int, const evaluate::DynamicType &, int);
71 bool IsIntrinsicLogical(const evaluate::DynamicType &);
72 bool IsIntrinsicLogical(
73     const evaluate::DynamicType &, int, const evaluate::DynamicType &, int);
74 bool IsIntrinsicConcat(
75     const evaluate::DynamicType &, int, const evaluate::DynamicType &, int);
76 
77 bool IsGenericDefinedOp(const Symbol &);
78 bool IsDefinedOperator(SourceName);
79 std::string MakeOpName(SourceName);
80 bool DoesScopeContain(const Scope *maybeAncestor, const Scope &maybeDescendent);
81 bool DoesScopeContain(const Scope *, const Symbol &);
82 bool IsUseAssociated(const Symbol &, const Scope &);
83 bool IsHostAssociated(const Symbol &, const Scope &);
IsStmtFunction(const Symbol & symbol)84 inline bool IsStmtFunction(const Symbol &symbol) {
85   const auto *subprogram{symbol.detailsIf<SubprogramDetails>()};
86   return subprogram && subprogram->stmtFunction();
87 }
88 bool IsInStmtFunction(const Symbol &);
89 bool IsStmtFunctionDummy(const Symbol &);
90 bool IsStmtFunctionResult(const Symbol &);
91 bool IsPointerDummy(const Symbol &);
92 bool IsBindCProcedure(const Symbol &);
93 bool IsBindCProcedure(const Scope &);
94 bool IsProcName(const Symbol &); // proc-name
95 bool IsFunctionResultWithSameNameAsFunction(const Symbol &);
96 bool IsExtensibleType(const DerivedTypeSpec *);
97 bool IsBuiltinDerivedType(const DerivedTypeSpec *derived, const char *name);
98 // Is this derived type TEAM_TYPE from module ISO_FORTRAN_ENV
99 bool IsTeamType(const DerivedTypeSpec *);
100 // Is this derived type either C_PTR or C_FUNPTR from module ISO_C_BINDING
101 bool IsIsoCType(const DerivedTypeSpec *);
102 bool IsEventTypeOrLockType(const DerivedTypeSpec *);
103 bool IsOrContainsEventOrLockComponent(const Symbol &);
104 bool CanBeTypeBoundProc(const Symbol *);
105 bool IsInitialized(const Symbol &, bool ignoreDATAstatements = false,
106     const Symbol *derivedType = nullptr);
107 bool HasIntrinsicTypeName(const Symbol &);
108 bool IsSeparateModuleProcedureInterface(const Symbol *);
109 bool IsAutomatic(const Symbol &);
110 bool HasAlternateReturns(const Symbol &);
111 bool InCommonBlock(const Symbol &);
112 
113 // Return an ultimate component of type that matches predicate, or nullptr.
114 const Symbol *FindUltimateComponent(const DerivedTypeSpec &type,
115     const std::function<bool(const Symbol &)> &predicate);
116 const Symbol *FindUltimateComponent(
117     const Symbol &symbol, const std::function<bool(const Symbol &)> &predicate);
118 
119 // Returns an immediate component of type that matches predicate, or nullptr.
120 // An immediate component of a type is one declared for that type or is an
121 // immediate component of the type that it extends.
122 const Symbol *FindImmediateComponent(
123     const DerivedTypeSpec &, const std::function<bool(const Symbol &)> &);
124 
IsPointer(const Symbol & symbol)125 inline bool IsPointer(const Symbol &symbol) {
126   return symbol.attrs().test(Attr::POINTER);
127 }
IsAllocatable(const Symbol & symbol)128 inline bool IsAllocatable(const Symbol &symbol) {
129   return symbol.attrs().test(Attr::ALLOCATABLE);
130 }
IsAllocatableOrPointer(const Symbol & symbol)131 inline bool IsAllocatableOrPointer(const Symbol &symbol) {
132   return IsPointer(symbol) || IsAllocatable(symbol);
133 }
IsNamedConstant(const Symbol & symbol)134 inline bool IsNamedConstant(const Symbol &symbol) {
135   return symbol.attrs().test(Attr::PARAMETER);
136 }
IsOptional(const Symbol & symbol)137 inline bool IsOptional(const Symbol &symbol) {
138   return symbol.attrs().test(Attr::OPTIONAL);
139 }
IsIntentIn(const Symbol & symbol)140 inline bool IsIntentIn(const Symbol &symbol) {
141   return symbol.attrs().test(Attr::INTENT_IN);
142 }
IsIntentInOut(const Symbol & symbol)143 inline bool IsIntentInOut(const Symbol &symbol) {
144   return symbol.attrs().test(Attr::INTENT_INOUT);
145 }
IsIntentOut(const Symbol & symbol)146 inline bool IsIntentOut(const Symbol &symbol) {
147   return symbol.attrs().test(Attr::INTENT_OUT);
148 }
IsProtected(const Symbol & symbol)149 inline bool IsProtected(const Symbol &symbol) {
150   return symbol.attrs().test(Attr::PROTECTED);
151 }
IsImpliedDoIndex(const Symbol & symbol)152 inline bool IsImpliedDoIndex(const Symbol &symbol) {
153   return symbol.owner().kind() == Scope::Kind::ImpliedDos;
154 }
155 bool IsFinalizable(const Symbol &);
156 bool IsFinalizable(const DerivedTypeSpec &);
157 bool HasImpureFinal(const DerivedTypeSpec &);
158 bool IsCoarray(const Symbol &);
159 bool IsInBlankCommon(const Symbol &);
160 bool IsAutomaticObject(const Symbol &);
IsAssumedSizeArray(const Symbol & symbol)161 inline bool IsAssumedSizeArray(const Symbol &symbol) {
162   const auto *details{symbol.detailsIf<ObjectEntityDetails>()};
163   return details && details->IsAssumedSize();
164 }
IsAssumedRankArray(const Symbol & symbol)165 inline bool IsAssumedRankArray(const Symbol &symbol) {
166   const auto *details{symbol.detailsIf<ObjectEntityDetails>()};
167   return details && details->IsAssumedRank();
168 }
169 bool IsAssumedLengthCharacter(const Symbol &);
170 bool IsExternal(const Symbol &);
171 bool IsModuleProcedure(const Symbol &);
172 // Is the symbol modifiable in this scope
173 std::optional<parser::MessageFixedText> WhyNotModifiable(
174     const Symbol &, const Scope &);
175 std::optional<parser::Message> WhyNotModifiable(SourceName, const SomeExpr &,
176     const Scope &, bool vectorSubscriptIsOk = false);
177 const Symbol *IsExternalInPureContext(const Symbol &, const Scope &);
178 bool HasCoarray(const parser::Expr &);
179 bool IsPolymorphicAllocatable(const Symbol &);
180 // Return an error if component symbol is not accessible from scope (7.5.4.8(2))
181 std::optional<parser::MessageFormattedText> CheckAccessibleComponent(
182     const semantics::Scope &, const Symbol &);
183 
184 // Analysis of image control statements
185 bool IsImageControlStmt(const parser::ExecutableConstruct &);
186 // Get the location of the image control statement in this ExecutableConstruct
187 parser::CharBlock GetImageControlStmtLocation(
188     const parser::ExecutableConstruct &);
189 // Image control statements that reference coarrays need an extra message
190 // to clarify why they're image control statements.  This function returns
191 // std::nullopt for ExecutableConstructs that do not require an extra message.
192 std::optional<parser::MessageFixedText> GetImageControlStmtCoarrayMsg(
193     const parser::ExecutableConstruct &);
194 
195 // Returns the complete list of derived type parameter symbols in
196 // the order in which their declarations appear in the derived type
197 // definitions (parents first).
198 SymbolVector OrderParameterDeclarations(const Symbol &);
199 // Returns the complete list of derived type parameter names in the
200 // order defined by 7.5.3.2.
201 std::list<SourceName> OrderParameterNames(const Symbol &);
202 
203 // Return an existing or new derived type instance
204 const DeclTypeSpec &FindOrInstantiateDerivedType(Scope &, DerivedTypeSpec &&,
205     SemanticsContext &, DeclTypeSpec::Category = DeclTypeSpec::TypeDerived);
206 
207 // When a subprogram defined in a submodule defines a separate module
208 // procedure whose interface is defined in an ancestor (sub)module,
209 // returns a pointer to that interface, else null.
210 const Symbol *FindSeparateModuleSubprogramInterface(const Symbol *);
211 
212 // Determines whether an object might be visible outside a
213 // pure function (C1594); returns a non-null Symbol pointer for
214 // diagnostic purposes if so.
215 const Symbol *FindExternallyVisibleObject(const Symbol &, const Scope &);
216 
217 template <typename A>
FindExternallyVisibleObject(const A &,const Scope &)218 const Symbol *FindExternallyVisibleObject(const A &, const Scope &) {
219   return nullptr; // default base case
220 }
221 
222 template <typename T>
FindExternallyVisibleObject(const evaluate::Designator<T> & designator,const Scope & scope)223 const Symbol *FindExternallyVisibleObject(
224     const evaluate::Designator<T> &designator, const Scope &scope) {
225   if (const Symbol * symbol{designator.GetBaseObject().symbol()}) {
226     return FindExternallyVisibleObject(*symbol, scope);
227   } else if (std::holds_alternative<evaluate::CoarrayRef>(designator.u)) {
228     // Coindexed values are visible even if their image-local objects are not.
229     return designator.GetBaseObject().symbol();
230   } else {
231     return nullptr;
232   }
233 }
234 
235 template <typename T>
FindExternallyVisibleObject(const evaluate::Expr<T> & expr,const Scope & scope)236 const Symbol *FindExternallyVisibleObject(
237     const evaluate::Expr<T> &expr, const Scope &scope) {
238   return std::visit(
239       [&](const auto &x) { return FindExternallyVisibleObject(x, scope); },
240       expr.u);
241 }
242 
243 using SomeExpr = evaluate::Expr<evaluate::SomeType>;
244 
245 bool ExprHasTypeCategory(
246     const SomeExpr &expr, const common::TypeCategory &type);
247 bool ExprTypeKindIsDefault(
248     const SomeExpr &expr, const SemanticsContext &context);
249 
250 struct GetExprHelper {
251   static const SomeExpr *Get(const parser::Expr &);
252   static const SomeExpr *Get(const parser::Variable &);
253   static const SomeExpr *Get(const parser::DataStmtConstant &);
254   template <typename T>
GetGetExprHelper255   static const SomeExpr *Get(const common::Indirection<T> &x) {
256     return Get(x.value());
257   }
GetGetExprHelper258   template <typename T> static const SomeExpr *Get(const std::optional<T> &x) {
259     return x ? Get(*x) : nullptr;
260   }
GetGetExprHelper261   template <typename T> static const SomeExpr *Get(const T &x) {
262     if constexpr (ConstraintTrait<T>) {
263       return Get(x.thing);
264     } else if constexpr (WrapperTrait<T>) {
265       return Get(x.v);
266     } else {
267       return nullptr;
268     }
269   }
270 };
271 
GetExpr(const T & x)272 template <typename T> const SomeExpr *GetExpr(const T &x) {
273   return GetExprHelper{}.Get(x);
274 }
275 
276 const evaluate::Assignment *GetAssignment(const parser::AssignmentStmt &);
277 const evaluate::Assignment *GetAssignment(
278     const parser::PointerAssignmentStmt &);
279 
GetIntValue(const T & x)280 template <typename T> std::optional<std::int64_t> GetIntValue(const T &x) {
281   if (const auto *expr{GetExpr(x)}) {
282     return evaluate::ToInt64(*expr);
283   } else {
284     return std::nullopt;
285   }
286 }
287 
IsZero(const T & expr)288 template <typename T> bool IsZero(const T &expr) {
289   auto value{GetIntValue(expr)};
290   return value && *value == 0;
291 }
292 
293 // 15.2.2
294 enum class ProcedureDefinitionClass {
295   None,
296   Intrinsic,
297   External,
298   Internal,
299   Module,
300   Dummy,
301   Pointer,
302   StatementFunction
303 };
304 
305 ProcedureDefinitionClass ClassifyProcedure(const Symbol &);
306 
307 // Derived type component iterator that provides a C++ LegacyForwardIterator
308 // iterator over the Ordered, Direct, Ultimate or Potential components of a
309 // DerivedTypeSpec. These iterators can be used with STL algorithms
310 // accepting LegacyForwardIterator.
311 // The kind of component is a template argument of the iterator factory
312 // ComponentIterator.
313 //
314 // - Ordered components are the components from the component order defined
315 // in 7.5.4.7, except that the parent component IS added between the parent
316 // component order and the components in order of declaration.
317 // This "deviation" is important for structure-constructor analysis.
318 // For this kind of iterator, the component tree is recursively visited in the
319 // following order:
320 //  - first, the Ordered components of the parent type (if relevant)
321 //  - then, the parent component (if relevant, different from 7.5.4.7!)
322 //  - then, the components in declaration order (without visiting subcomponents)
323 //
324 // - Ultimate, Direct and Potential components are as defined in 7.5.1.
325 //   - Ultimate components of a derived type are the closure of its components
326 //     of intrinsic type, its ALLOCATABLE or POINTER components, and the
327 //     ultimate components of its non-ALLOCATABLE non-POINTER derived type
328 //     components.  (No ultimate component has a derived type unless it is
329 //     ALLOCATABLE or POINTER.)
330 //   - Direct components of a derived type are all of its components, and all
331 //     of the direct components of its non-ALLOCATABLE non-POINTER derived type
332 //     components.  (Direct components are always present.)
333 //   - Potential subobject components of a derived type are the closure of
334 //     its non-POINTER components and the potential subobject components of
335 //     its non-POINTER derived type components.  (The lifetime of each
336 //     potential subobject component is that of the entire instance.)
337 // Parent and procedure components are considered against these definitions.
338 // For this kind of iterator, the component tree is recursively visited in the
339 // following order:
340 //  - the parent component first (if relevant)
341 //  - then, the components of the parent type (if relevant)
342 //      + visiting the component and then, if it is derived type data component,
343 //        visiting the subcomponents before visiting the next
344 //        component in declaration order.
345 //  - then, components in declaration order, similarly to components of parent
346 //    type.
347 //  Here, the parent component is visited first so that search for a component
348 //  verifying a property will never descend into a component that already
349 //  verifies the property (this helps giving clearer feedback).
350 //
351 // ComponentIterator::const_iterator remain valid during the whole lifetime of
352 // the DerivedTypeSpec passed by reference to the ComponentIterator factory.
353 // Their validity is independent of the ComponentIterator factory lifetime.
354 //
355 // For safety and simplicity, the iterators are read only and can only be
356 // incremented. This could be changed if desired.
357 //
358 // Note that iterators are made in such a way that one can easily test and build
359 // info message in the following way:
360 //    ComponentIterator<ComponentKind::...> comp{derived}
361 //    if (auto it{std::find_if(comp.begin(), comp.end(), predicate)}) {
362 //       msg = it.BuildResultDesignatorName() + " verifies predicates";
363 //       const Symbol *component{*it};
364 //       ....
365 //    }
366 
ENUM_CLASS(ComponentKind,Ordered,Direct,Ultimate,Potential,Scope)367 ENUM_CLASS(ComponentKind, Ordered, Direct, Ultimate, Potential, Scope)
368 
369 template <ComponentKind componentKind> class ComponentIterator {
370 public:
371   ComponentIterator(const DerivedTypeSpec &derived) : derived_{derived} {}
372   class const_iterator {
373   public:
374     using iterator_category = std::forward_iterator_tag;
375     using value_type = SymbolRef;
376     using difference_type = void;
377     using pointer = const Symbol *;
378     using reference = const Symbol &;
379 
380     static const_iterator Create(const DerivedTypeSpec &);
381 
382     const_iterator &operator++() {
383       Increment();
384       return *this;
385     }
386     const_iterator operator++(int) {
387       const_iterator tmp(*this);
388       Increment();
389       return tmp;
390     }
391     reference operator*() const {
392       CHECK(!componentPath_.empty());
393       return DEREF(componentPath_.back().component());
394     }
395     pointer operator->() const { return &**this; }
396 
397     bool operator==(const const_iterator &other) const {
398       return componentPath_ == other.componentPath_;
399     }
400     bool operator!=(const const_iterator &other) const {
401       return !(*this == other);
402     }
403 
404     // bool() operator indicates if the iterator can be dereferenced without
405     // having to check against an end() iterator.
406     explicit operator bool() const { return !componentPath_.empty(); }
407 
408     // Builds a designator name of the referenced component for messages.
409     // The designator helps when the component referred to by the iterator
410     // may be "buried" into other components. This gives the full
411     // path inside the iterated derived type: e.g "%a%b%c%ultimate"
412     // when it->name() only gives "ultimate". Parent components are
413     // part of the path for clarity, even though they could be
414     // skipped.
415     std::string BuildResultDesignatorName() const;
416 
417   private:
418     using name_iterator =
419         std::conditional_t<componentKind == ComponentKind::Scope,
420             typename Scope::const_iterator,
421             typename std::list<SourceName>::const_iterator>;
422 
423     class ComponentPathNode {
424     public:
425       explicit ComponentPathNode(const DerivedTypeSpec &derived)
426           : derived_{derived} {
427         if constexpr (componentKind == ComponentKind::Scope) {
428           const Scope &scope{DEREF(derived.scope())};
429           nameIterator_ = scope.cbegin();
430           nameEnd_ = scope.cend();
431         } else {
432           const std::list<SourceName> &nameList{
433               derived.typeSymbol().get<DerivedTypeDetails>().componentNames()};
434           nameIterator_ = nameList.cbegin();
435           nameEnd_ = nameList.cend();
436         }
437       }
438       const Symbol *component() const { return component_; }
439       void set_component(const Symbol &component) { component_ = &component; }
440       bool visited() const { return visited_; }
441       void set_visited(bool yes) { visited_ = yes; }
442       bool descended() const { return descended_; }
443       void set_descended(bool yes) { descended_ = yes; }
444       name_iterator &nameIterator() { return nameIterator_; }
445       name_iterator nameEnd() { return nameEnd_; }
446       const Symbol &GetTypeSymbol() const { return derived_->typeSymbol(); }
447       const Scope &GetScope() const { return DEREF(derived_->scope()); }
448       bool operator==(const ComponentPathNode &that) const {
449         return &*derived_ == &*that.derived_ &&
450             nameIterator_ == that.nameIterator_ &&
451             component_ == that.component_;
452       }
453 
454     private:
455       common::Reference<const DerivedTypeSpec> derived_;
456       name_iterator nameEnd_;
457       name_iterator nameIterator_;
458       const Symbol *component_{nullptr}; // until Increment()
459       bool visited_{false};
460       bool descended_{false};
461     };
462 
463     const DerivedTypeSpec *PlanComponentTraversal(
464         const Symbol &component) const;
465     // Advances to the next relevant symbol, if any.  Afterwards, the
466     // iterator will either be at its end or contain no null component().
467     void Increment();
468 
469     std::vector<ComponentPathNode> componentPath_;
470   };
471 
472   const_iterator begin() { return cbegin(); }
473   const_iterator end() { return cend(); }
474   const_iterator cbegin() { return const_iterator::Create(derived_); }
475   const_iterator cend() { return const_iterator{}; }
476 
477 private:
478   const DerivedTypeSpec &derived_;
479 };
480 
481 extern template class ComponentIterator<ComponentKind::Ordered>;
482 extern template class ComponentIterator<ComponentKind::Direct>;
483 extern template class ComponentIterator<ComponentKind::Ultimate>;
484 extern template class ComponentIterator<ComponentKind::Potential>;
485 extern template class ComponentIterator<ComponentKind::Scope>;
486 using OrderedComponentIterator = ComponentIterator<ComponentKind::Ordered>;
487 using DirectComponentIterator = ComponentIterator<ComponentKind::Direct>;
488 using UltimateComponentIterator = ComponentIterator<ComponentKind::Ultimate>;
489 using PotentialComponentIterator = ComponentIterator<ComponentKind::Potential>;
490 using ScopeComponentIterator = ComponentIterator<ComponentKind::Scope>;
491 
492 // Common component searches, the iterator returned is referring to the first
493 // component, according to the order defined for the related ComponentIterator,
494 // that verifies the property from the name.
495 // If no component verifies the property, an end iterator (casting to false)
496 // is returned. Otherwise, the returned iterator casts to true and can be
497 // dereferenced.
498 PotentialComponentIterator::const_iterator FindEventOrLockPotentialComponent(
499     const DerivedTypeSpec &);
500 UltimateComponentIterator::const_iterator FindCoarrayUltimateComponent(
501     const DerivedTypeSpec &);
502 UltimateComponentIterator::const_iterator FindPointerUltimateComponent(
503     const DerivedTypeSpec &);
504 UltimateComponentIterator::const_iterator FindAllocatableUltimateComponent(
505     const DerivedTypeSpec &);
506 UltimateComponentIterator::const_iterator
507 FindPolymorphicAllocatableUltimateComponent(const DerivedTypeSpec &);
508 UltimateComponentIterator::const_iterator
509 FindPolymorphicAllocatableNonCoarrayUltimateComponent(const DerivedTypeSpec &);
510 
511 // The LabelEnforce class (given a set of labels) provides an error message if
512 // there is a branch to a label which is not in the given set.
513 class LabelEnforce {
514 public:
LabelEnforce(SemanticsContext & context,std::set<parser::Label> && labels,parser::CharBlock constructSourcePosition,const char * construct)515   LabelEnforce(SemanticsContext &context, std::set<parser::Label> &&labels,
516       parser::CharBlock constructSourcePosition, const char *construct)
517       : context_{context}, labels_{labels},
518         constructSourcePosition_{constructSourcePosition}, construct_{
519                                                                construct} {}
Pre(const T &)520   template <typename T> bool Pre(const T &) { return true; }
Pre(const parser::Statement<T> & statement)521   template <typename T> bool Pre(const parser::Statement<T> &statement) {
522     currentStatementSourcePosition_ = statement.source;
523     return true;
524   }
525 
Post(const T &)526   template <typename T> void Post(const T &) {}
527 
528   void Post(const parser::GotoStmt &gotoStmt);
529   void Post(const parser::ComputedGotoStmt &computedGotoStmt);
530   void Post(const parser::ArithmeticIfStmt &arithmeticIfStmt);
531   void Post(const parser::AssignStmt &assignStmt);
532   void Post(const parser::AssignedGotoStmt &assignedGotoStmt);
533   void Post(const parser::AltReturnSpec &altReturnSpec);
534   void Post(const parser::ErrLabel &errLabel);
535   void Post(const parser::EndLabel &endLabel);
536   void Post(const parser::EorLabel &eorLabel);
537   void checkLabelUse(const parser::Label &labelUsed);
538 
539 private:
540   SemanticsContext &context_;
541   std::set<parser::Label> labels_;
542   parser::CharBlock currentStatementSourcePosition_{nullptr};
543   parser::CharBlock constructSourcePosition_{nullptr};
544   const char *construct_{nullptr};
545 
546   parser::MessageFormattedText GetEnclosingConstructMsg();
547   void SayWithConstruct(SemanticsContext &context,
548       parser::CharBlock stmtLocation, parser::MessageFormattedText &&message,
549       parser::CharBlock constructLocation);
550 };
551 // Return the (possibly null) name of the ConstructNode
552 const std::optional<parser::Name> &MaybeGetNodeName(
553     const ConstructNode &construct);
554 } // namespace Fortran::semantics
555 #endif // FORTRAN_SEMANTICS_TOOLS_H_
556