1 //===-- lib/Semantics/resolve-labels.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 "resolve-labels.h"
10 #include "flang/Common/enum-set.h"
11 #include "flang/Common/template.h"
12 #include "flang/Parser/parse-tree-visitor.h"
13 #include "flang/Semantics/semantics.h"
14 #include <cctype>
15 #include <cstdarg>
16 #include <type_traits>
17 
18 namespace Fortran::semantics {
19 
20 using namespace parser::literals;
21 
22 ENUM_CLASS(
23     TargetStatementEnum, Do, Branch, Format, CompatibleDo, CompatibleBranch)
24 using LabeledStmtClassificationSet =
25     common::EnumSet<TargetStatementEnum, TargetStatementEnum_enumSize>;
26 
27 using IndexList = std::vector<std::pair<parser::CharBlock, parser::CharBlock>>;
28 // A ProxyForScope is an integral proxy for a Fortran scope. This is required
29 // because the parse tree does not actually have the scopes required.
30 using ProxyForScope = unsigned;
31 // Minimal scope information
32 struct ScopeInfo {
33   ProxyForScope parent{};
34   bool isExteriorGotoFatal{false};
35   int depth{0};
36 };
37 struct LabeledStatementInfoTuplePOD {
38   ProxyForScope proxyForScope;
39   parser::CharBlock parserCharBlock;
40   LabeledStmtClassificationSet labeledStmtClassificationSet;
41   bool isExecutableConstructEndStmt;
42 };
43 using TargetStmtMap = std::map<parser::Label, LabeledStatementInfoTuplePOD>;
44 struct SourceStatementInfoTuplePOD {
SourceStatementInfoTuplePODFortran::semantics::SourceStatementInfoTuplePOD45   SourceStatementInfoTuplePOD(const parser::Label &parserLabel,
46       const ProxyForScope &proxyForScope,
47       const parser::CharBlock &parserCharBlock)
48       : parserLabel{parserLabel}, proxyForScope{proxyForScope},
49         parserCharBlock{parserCharBlock} {}
50   parser::Label parserLabel;
51   ProxyForScope proxyForScope;
52   parser::CharBlock parserCharBlock;
53 };
54 using SourceStmtList = std::vector<SourceStatementInfoTuplePOD>;
55 enum class Legality { never, always, formerly };
56 
HasScope(ProxyForScope scope)57 bool HasScope(ProxyForScope scope) { return scope != ProxyForScope{0u}; }
58 
59 // F18:R1131
60 template <typename A>
IsLegalDoTerm(const parser::Statement<A> &)61 constexpr Legality IsLegalDoTerm(const parser::Statement<A> &) {
62   if (std::is_same_v<A, common::Indirection<parser::EndDoStmt>> ||
63       std::is_same_v<A, parser::EndDoStmt>) {
64     return Legality::always;
65   } else if (std::is_same_v<A, parser::EndForallStmt> ||
66       std::is_same_v<A, parser::EndWhereStmt>) {
67     // Executable construct end statements are also supported as
68     // an extension but they need special care because the associated
69     // construct create their own scope.
70     return Legality::formerly;
71   } else {
72     return Legality::never;
73   }
74 }
75 
IsLegalDoTerm(const parser::Statement<parser::ActionStmt> & actionStmt)76 constexpr Legality IsLegalDoTerm(
77     const parser::Statement<parser::ActionStmt> &actionStmt) {
78   if (std::holds_alternative<parser::ContinueStmt>(actionStmt.statement.u)) {
79     // See F08:C816
80     return Legality::always;
81   } else if (!(std::holds_alternative<
82                    common::Indirection<parser::ArithmeticIfStmt>>(
83                    actionStmt.statement.u) ||
84                  std::holds_alternative<common::Indirection<parser::CycleStmt>>(
85                      actionStmt.statement.u) ||
86                  std::holds_alternative<common::Indirection<parser::ExitStmt>>(
87                      actionStmt.statement.u) ||
88                  std::holds_alternative<common::Indirection<parser::StopStmt>>(
89                      actionStmt.statement.u) ||
90                  std::holds_alternative<common::Indirection<parser::GotoStmt>>(
91                      actionStmt.statement.u) ||
92                  std::holds_alternative<
93                      common::Indirection<parser::ReturnStmt>>(
94                      actionStmt.statement.u))) {
95     return Legality::formerly;
96   } else {
97     return Legality::never;
98   }
99 }
100 
IsFormat(const parser::Statement<A> &)101 template <typename A> constexpr bool IsFormat(const parser::Statement<A> &) {
102   return std::is_same_v<A, common::Indirection<parser::FormatStmt>>;
103 }
104 
105 template <typename A>
IsLegalBranchTarget(const parser::Statement<A> &)106 constexpr Legality IsLegalBranchTarget(const parser::Statement<A> &) {
107   if (std::is_same_v<A, parser::ActionStmt> ||
108       std::is_same_v<A, parser::AssociateStmt> ||
109       std::is_same_v<A, parser::EndAssociateStmt> ||
110       std::is_same_v<A, parser::IfThenStmt> ||
111       std::is_same_v<A, parser::EndIfStmt> ||
112       std::is_same_v<A, parser::SelectCaseStmt> ||
113       std::is_same_v<A, parser::EndSelectStmt> ||
114       std::is_same_v<A, parser::SelectRankStmt> ||
115       std::is_same_v<A, parser::SelectTypeStmt> ||
116       std::is_same_v<A, common::Indirection<parser::LabelDoStmt>> ||
117       std::is_same_v<A, parser::NonLabelDoStmt> ||
118       std::is_same_v<A, parser::EndDoStmt> ||
119       std::is_same_v<A, common::Indirection<parser::EndDoStmt>> ||
120       std::is_same_v<A, parser::BlockStmt> ||
121       std::is_same_v<A, parser::EndBlockStmt> ||
122       std::is_same_v<A, parser::CriticalStmt> ||
123       std::is_same_v<A, parser::EndCriticalStmt> ||
124       std::is_same_v<A, parser::ForallConstructStmt> ||
125       std::is_same_v<A, parser::WhereConstructStmt> ||
126       std::is_same_v<A, parser::EndFunctionStmt> ||
127       std::is_same_v<A, parser::EndMpSubprogramStmt> ||
128       std::is_same_v<A, parser::EndProgramStmt> ||
129       std::is_same_v<A, parser::EndSubroutineStmt>) {
130     return Legality::always;
131   } else {
132     return Legality::never;
133   }
134 }
135 
136 template <typename A>
ConstructBranchTargetFlags(const parser::Statement<A> & statement)137 constexpr LabeledStmtClassificationSet ConstructBranchTargetFlags(
138     const parser::Statement<A> &statement) {
139   LabeledStmtClassificationSet labeledStmtClassificationSet{};
140   if (IsLegalDoTerm(statement) == Legality::always) {
141     labeledStmtClassificationSet.set(TargetStatementEnum::Do);
142   } else if (IsLegalDoTerm(statement) == Legality::formerly) {
143     labeledStmtClassificationSet.set(TargetStatementEnum::CompatibleDo);
144   }
145   if (IsLegalBranchTarget(statement) == Legality::always) {
146     labeledStmtClassificationSet.set(TargetStatementEnum::Branch);
147   } else if (IsLegalBranchTarget(statement) == Legality::formerly) {
148     labeledStmtClassificationSet.set(TargetStatementEnum::CompatibleBranch);
149   }
150   if (IsFormat(statement)) {
151     labeledStmtClassificationSet.set(TargetStatementEnum::Format);
152   }
153   return labeledStmtClassificationSet;
154 }
155 
SayLabel(parser::Label label)156 static unsigned SayLabel(parser::Label label) {
157   return static_cast<unsigned>(label);
158 }
159 
160 struct UnitAnalysis {
UnitAnalysisFortran::semantics::UnitAnalysis161   UnitAnalysis() { scopeModel.emplace_back(); }
162 
163   SourceStmtList doStmtSources;
164   SourceStmtList formatStmtSources;
165   SourceStmtList otherStmtSources;
166   SourceStmtList assignStmtSources;
167   TargetStmtMap targetStmts;
168   std::vector<ScopeInfo> scopeModel;
169 };
170 
171 // Some parse tree record for statements simply wrap construct names;
172 // others include them as tuple components.  Given a statement,
173 // return a pointer to its name if it has one.
174 template <typename A>
GetStmtName(const parser::Statement<A> & stmt)175 const parser::CharBlock *GetStmtName(const parser::Statement<A> &stmt) {
176   const std::optional<parser::Name> *name{nullptr};
177   if constexpr (WrapperTrait<A>) {
178     if constexpr (std::is_same_v<decltype(A::v), parser::Name>) {
179       return &stmt.statement.v.source;
180     } else {
181       name = &stmt.statement.v;
182     }
183   } else if constexpr (std::is_same_v<A, parser::SelectRankStmt> ||
184       std::is_same_v<A, parser::SelectTypeStmt>) {
185     name = &std::get<0>(stmt.statement.t);
186   } else if constexpr (common::HasMember<parser::Name,
187                            decltype(stmt.statement.t)>) {
188     return &std::get<parser::Name>(stmt.statement.t).source;
189   } else {
190     name = &std::get<std::optional<parser::Name>>(stmt.statement.t);
191   }
192   if (name && *name) {
193     return &(*name)->source;
194   }
195   return nullptr;
196 }
197 
198 class ParseTreeAnalyzer {
199 public:
200   ParseTreeAnalyzer(ParseTreeAnalyzer &&that) = default;
ParseTreeAnalyzer(SemanticsContext & context)201   ParseTreeAnalyzer(SemanticsContext &context) : context_{context} {}
202 
Pre(const A & x)203   template <typename A> constexpr bool Pre(const A &x) {
204     using LabeledProgramUnitStmts =
205         std::tuple<parser::MainProgram, parser::FunctionSubprogram,
206             parser::SubroutineSubprogram, parser::SeparateModuleSubprogram>;
207     if constexpr (common::HasMember<A, LabeledProgramUnitStmts>) {
208       const auto &endStmt{std::get<std::tuple_size_v<decltype(x.t)> - 1>(x.t)};
209       if (endStmt.label) {
210         // The END statement for a subprogram appears after any internal
211         // subprograms.  Visit that statement in advance so that results
212         // are placed in the correct programUnits_ slot.
213         auto targetFlags{ConstructBranchTargetFlags(endStmt)};
214         AddTargetLabelDefinition(
215             endStmt.label.value(), targetFlags, currentScope_);
216       }
217     }
218     return true;
219   }
Post(const A &)220   template <typename A> constexpr void Post(const A &) {}
221 
Pre(const parser::Statement<A> & statement)222   template <typename A> bool Pre(const parser::Statement<A> &statement) {
223     currentPosition_ = statement.source;
224     const auto &label = statement.label;
225     if (!label) {
226       return true;
227     }
228     using LabeledConstructStmts = std::tuple<parser::AssociateStmt,
229         parser::BlockStmt, parser::ChangeTeamStmt, parser::CriticalStmt,
230         parser::IfThenStmt, parser::NonLabelDoStmt, parser::SelectCaseStmt,
231         parser::SelectRankStmt, parser::SelectTypeStmt,
232         parser::ForallConstructStmt, parser::WhereConstructStmt>;
233     using LabeledConstructEndStmts = std::tuple<parser::EndAssociateStmt,
234         parser::EndBlockStmt, parser::EndChangeTeamStmt,
235         parser::EndCriticalStmt, parser::EndDoStmt, parser::EndForallStmt,
236         parser::EndIfStmt, parser::EndWhereStmt>;
237     using LabeledProgramUnitEndStmts =
238         std::tuple<parser::EndFunctionStmt, parser::EndMpSubprogramStmt,
239             parser::EndProgramStmt, parser::EndSubroutineStmt>;
240     auto targetFlags{ConstructBranchTargetFlags(statement)};
241     if constexpr (common::HasMember<A, LabeledConstructStmts>) {
242       AddTargetLabelDefinition(label.value(), targetFlags, ParentScope());
243     } else if constexpr (std::is_same_v<A, parser::EndSelectStmt>) {
244       // the label on an END SELECT is not in the last case
245       AddTargetLabelDefinition(label.value(), targetFlags, ParentScope(), true);
246     } else if constexpr (common::HasMember<A, LabeledConstructEndStmts>) {
247       constexpr bool isExecutableConstructEndStmt{true};
248       AddTargetLabelDefinition(label.value(), targetFlags, currentScope_,
249           isExecutableConstructEndStmt);
250     } else if constexpr (!common::HasMember<A, LabeledProgramUnitEndStmts>) {
251       // Program unit END statements have already been processed.
252       AddTargetLabelDefinition(label.value(), targetFlags, currentScope_);
253     }
254     return true;
255   }
256 
257   // see 11.1.1
Pre(const parser::ProgramUnit &)258   bool Pre(const parser::ProgramUnit &) { return InitializeNewScopeContext(); }
Pre(const parser::InternalSubprogram &)259   bool Pre(const parser::InternalSubprogram &) {
260     return InitializeNewScopeContext();
261   }
Pre(const parser::ModuleSubprogram &)262   bool Pre(const parser::ModuleSubprogram &) {
263     return InitializeNewScopeContext();
264   }
Pre(const parser::AssociateConstruct & associateConstruct)265   bool Pre(const parser::AssociateConstruct &associateConstruct) {
266     return PushConstructName(associateConstruct);
267   }
Pre(const parser::BlockConstruct & blockConstruct)268   bool Pre(const parser::BlockConstruct &blockConstruct) {
269     return PushConstructName(blockConstruct);
270   }
Pre(const parser::ChangeTeamConstruct & changeTeamConstruct)271   bool Pre(const parser::ChangeTeamConstruct &changeTeamConstruct) {
272     return PushConstructName(changeTeamConstruct);
273   }
Pre(const parser::CriticalConstruct & criticalConstruct)274   bool Pre(const parser::CriticalConstruct &criticalConstruct) {
275     return PushConstructName(criticalConstruct);
276   }
Pre(const parser::DoConstruct & doConstruct)277   bool Pre(const parser::DoConstruct &doConstruct) {
278     return PushConstructName(doConstruct);
279   }
Pre(const parser::IfConstruct & ifConstruct)280   bool Pre(const parser::IfConstruct &ifConstruct) {
281     return PushConstructName(ifConstruct);
282   }
Pre(const parser::IfConstruct::ElseIfBlock &)283   bool Pre(const parser::IfConstruct::ElseIfBlock &) {
284     return SwitchToNewScope();
285   }
Pre(const parser::IfConstruct::ElseBlock &)286   bool Pre(const parser::IfConstruct::ElseBlock &) {
287     return SwitchToNewScope();
288   }
Pre(const parser::CaseConstruct & caseConstruct)289   bool Pre(const parser::CaseConstruct &caseConstruct) {
290     return PushConstructName(caseConstruct);
291   }
Post(const parser::SelectCaseStmt &)292   void Post(const parser::SelectCaseStmt &) { PushScope(); }
Pre(const parser::CaseConstruct::Case &)293   bool Pre(const parser::CaseConstruct::Case &) { return SwitchToNewScope(); }
Pre(const parser::SelectRankConstruct & selectRankConstruct)294   bool Pre(const parser::SelectRankConstruct &selectRankConstruct) {
295     return PushConstructName(selectRankConstruct);
296   }
Post(const parser::SelectRankStmt &)297   void Post(const parser::SelectRankStmt &) { PushScope(); }
Pre(const parser::SelectRankConstruct::RankCase &)298   bool Pre(const parser::SelectRankConstruct::RankCase &) {
299     return SwitchToNewScope();
300   }
Pre(const parser::SelectTypeConstruct & selectTypeConstruct)301   bool Pre(const parser::SelectTypeConstruct &selectTypeConstruct) {
302     return PushConstructName(selectTypeConstruct);
303   }
Post(const parser::SelectTypeStmt &)304   void Post(const parser::SelectTypeStmt &) { PushScope(); }
Pre(const parser::SelectTypeConstruct::TypeCase &)305   bool Pre(const parser::SelectTypeConstruct::TypeCase &) {
306     return SwitchToNewScope();
307   }
Post(const parser::EndSelectStmt &)308   void Post(const parser::EndSelectStmt &) { PopScope(); }
Pre(const parser::WhereConstruct & whereConstruct)309   bool Pre(const parser::WhereConstruct &whereConstruct) {
310     return PushConstructName(whereConstruct);
311   }
Pre(const parser::ForallConstruct & forallConstruct)312   bool Pre(const parser::ForallConstruct &forallConstruct) {
313     return PushConstructName(forallConstruct);
314   }
315 
Post(const parser::AssociateConstruct & associateConstruct)316   void Post(const parser::AssociateConstruct &associateConstruct) {
317     PopConstructName(associateConstruct);
318   }
Post(const parser::BlockConstruct & blockConstruct)319   void Post(const parser::BlockConstruct &blockConstruct) {
320     PopConstructName(blockConstruct);
321   }
Post(const parser::ChangeTeamConstruct & changeTeamConstruct)322   void Post(const parser::ChangeTeamConstruct &changeTeamConstruct) {
323     PopConstructName(changeTeamConstruct);
324   }
Post(const parser::CriticalConstruct & criticalConstruct)325   void Post(const parser::CriticalConstruct &criticalConstruct) {
326     PopConstructName(criticalConstruct);
327   }
Post(const parser::DoConstruct & doConstruct)328   void Post(const parser::DoConstruct &doConstruct) {
329     PopConstructName(doConstruct);
330   }
Post(const parser::IfConstruct & ifConstruct)331   void Post(const parser::IfConstruct &ifConstruct) {
332     PopConstructName(ifConstruct);
333   }
Post(const parser::CaseConstruct & caseConstruct)334   void Post(const parser::CaseConstruct &caseConstruct) {
335     PopConstructName(caseConstruct);
336   }
Post(const parser::SelectRankConstruct & selectRankConstruct)337   void Post(const parser::SelectRankConstruct &selectRankConstruct) {
338     PopConstructName(selectRankConstruct);
339   }
Post(const parser::SelectTypeConstruct & selectTypeConstruct)340   void Post(const parser::SelectTypeConstruct &selectTypeConstruct) {
341     PopConstructName(selectTypeConstruct);
342   }
Post(const parser::WhereConstruct & whereConstruct)343   void Post(const parser::WhereConstruct &whereConstruct) {
344     PopConstructName(whereConstruct);
345   }
Post(const parser::ForallConstruct & forallConstruct)346   void Post(const parser::ForallConstruct &forallConstruct) {
347     PopConstructName(forallConstruct);
348   }
349 
350   // Checks for missing or mismatching names on various constructs (e.g., IF)
351   // and their intermediate or terminal statements that allow optional
352   // construct names(e.g., ELSE).  When an optional construct name is present,
353   // the construct as a whole must have a name that matches.
354   template <typename FIRST, typename CONSTRUCT, typename STMT>
CheckOptionalName(const char * constructTag,const CONSTRUCT & a,const parser::Statement<STMT> & stmt)355   void CheckOptionalName(const char *constructTag, const CONSTRUCT &a,
356       const parser::Statement<STMT> &stmt) {
357     if (const parser::CharBlock * name{GetStmtName(stmt)}) {
358       const auto &firstStmt{std::get<parser::Statement<FIRST>>(a.t)};
359       if (const parser::CharBlock * firstName{GetStmtName(firstStmt)}) {
360         if (*firstName != *name) {
361           context_.Say(*name, "%s name mismatch"_err_en_US, constructTag)
362               .Attach(*firstName, "should be"_en_US);
363         }
364       } else {
365         context_.Say(*name, "%s name not allowed"_err_en_US, constructTag)
366             .Attach(firstStmt.source, "in unnamed %s"_en_US, constructTag);
367       }
368     }
369   }
370 
371   // C1414
Post(const parser::BlockData & blockData)372   void Post(const parser::BlockData &blockData) {
373     CheckOptionalName<parser::BlockDataStmt>("BLOCK DATA subprogram", blockData,
374         std::get<parser::Statement<parser::EndBlockDataStmt>>(blockData.t));
375   }
376 
377   // C1564
Post(const parser::InterfaceBody::Function & func)378   void Post(const parser::InterfaceBody::Function &func) {
379     CheckOptionalName<parser::FunctionStmt>("FUNCTION", func,
380         std::get<parser::Statement<parser::EndFunctionStmt>>(func.t));
381   }
382 
383   // C1564
Post(const parser::FunctionSubprogram & functionSubprogram)384   void Post(const parser::FunctionSubprogram &functionSubprogram) {
385     CheckOptionalName<parser::FunctionStmt>("FUNCTION", functionSubprogram,
386         std::get<parser::Statement<parser::EndFunctionStmt>>(
387             functionSubprogram.t));
388   }
389 
390   // C1502
Post(const parser::InterfaceBlock & interfaceBlock)391   void Post(const parser::InterfaceBlock &interfaceBlock) {
392     if (const auto &endGenericSpec{
393             std::get<parser::Statement<parser::EndInterfaceStmt>>(
394                 interfaceBlock.t)
395                 .statement.v}) {
396       const auto &interfaceStmt{
397           std::get<parser::Statement<parser::InterfaceStmt>>(interfaceBlock.t)};
398       if (std::holds_alternative<parser::Abstract>(interfaceStmt.statement.u)) {
399         context_
400             .Say(endGenericSpec->source,
401                 "END INTERFACE generic name (%s) may not appear for ABSTRACT INTERFACE"_err_en_US,
402                 endGenericSpec->source)
403             .Attach(
404                 interfaceStmt.source, "corresponding ABSTRACT INTERFACE"_en_US);
405       } else if (const auto &genericSpec{
406                      std::get<std::optional<parser::GenericSpec>>(
407                          interfaceStmt.statement.u)}) {
408         bool ok{genericSpec->source == endGenericSpec->source};
409         if (!ok) {
410           // Accept variant spellings of .LT. &c.
411           const auto *endOp{
412               std::get_if<parser::DefinedOperator>(&endGenericSpec->u)};
413           const auto *op{std::get_if<parser::DefinedOperator>(&genericSpec->u)};
414           if (endOp && op) {
415             const auto *endIntrin{
416                 std::get_if<parser::DefinedOperator::IntrinsicOperator>(
417                     &endOp->u)};
418             const auto *intrin{
419                 std::get_if<parser::DefinedOperator::IntrinsicOperator>(
420                     &op->u)};
421             ok = endIntrin && intrin && *endIntrin == *intrin;
422           }
423         }
424         if (!ok) {
425           context_
426               .Say(endGenericSpec->source,
427                   "END INTERFACE generic name (%s) does not match generic INTERFACE (%s)"_err_en_US,
428                   endGenericSpec->source, genericSpec->source)
429               .Attach(genericSpec->source, "corresponding INTERFACE"_en_US);
430         }
431       } else {
432         context_
433             .Say(endGenericSpec->source,
434                 "END INTERFACE generic name (%s) may not appear for non-generic INTERFACE"_err_en_US,
435                 endGenericSpec->source)
436             .Attach(interfaceStmt.source, "corresponding INTERFACE"_en_US);
437       }
438     }
439   }
440 
441   // C1402
Post(const parser::Module & module)442   void Post(const parser::Module &module) {
443     CheckOptionalName<parser::ModuleStmt>("MODULE", module,
444         std::get<parser::Statement<parser::EndModuleStmt>>(module.t));
445   }
446 
447   // C1569
Post(const parser::SeparateModuleSubprogram & separateModuleSubprogram)448   void Post(const parser::SeparateModuleSubprogram &separateModuleSubprogram) {
449     CheckOptionalName<parser::MpSubprogramStmt>("MODULE PROCEDURE",
450         separateModuleSubprogram,
451         std::get<parser::Statement<parser::EndMpSubprogramStmt>>(
452             separateModuleSubprogram.t));
453   }
454 
455   // C1401
Post(const parser::MainProgram & mainProgram)456   void Post(const parser::MainProgram &mainProgram) {
457     if (const parser::CharBlock *
458         endName{GetStmtName(std::get<parser::Statement<parser::EndProgramStmt>>(
459             mainProgram.t))}) {
460       if (const auto &program{
461               std::get<std::optional<parser::Statement<parser::ProgramStmt>>>(
462                   mainProgram.t)}) {
463         if (*endName != program->statement.v.source) {
464           context_.Say(*endName, "END PROGRAM name mismatch"_err_en_US)
465               .Attach(program->statement.v.source, "should be"_en_US);
466         }
467       } else {
468         context_.Say(*endName,
469             "END PROGRAM has name without PROGRAM statement"_err_en_US);
470       }
471     }
472   }
473 
474   // C1413
Post(const parser::Submodule & submodule)475   void Post(const parser::Submodule &submodule) {
476     CheckOptionalName<parser::SubmoduleStmt>("SUBMODULE", submodule,
477         std::get<parser::Statement<parser::EndSubmoduleStmt>>(submodule.t));
478   }
479 
480   // C1567
Post(const parser::InterfaceBody::Subroutine & sub)481   void Post(const parser::InterfaceBody::Subroutine &sub) {
482     CheckOptionalName<parser::SubroutineStmt>("SUBROUTINE", sub,
483         std::get<parser::Statement<parser::EndSubroutineStmt>>(sub.t));
484   }
485 
486   // C1567
Post(const parser::SubroutineSubprogram & subroutineSubprogram)487   void Post(const parser::SubroutineSubprogram &subroutineSubprogram) {
488     CheckOptionalName<parser::SubroutineStmt>("SUBROUTINE",
489         subroutineSubprogram,
490         std::get<parser::Statement<parser::EndSubroutineStmt>>(
491             subroutineSubprogram.t));
492   }
493 
494   // C739
Post(const parser::DerivedTypeDef & derivedTypeDef)495   void Post(const parser::DerivedTypeDef &derivedTypeDef) {
496     CheckOptionalName<parser::DerivedTypeStmt>("derived type definition",
497         derivedTypeDef,
498         std::get<parser::Statement<parser::EndTypeStmt>>(derivedTypeDef.t));
499   }
500 
Post(const parser::LabelDoStmt & labelDoStmt)501   void Post(const parser::LabelDoStmt &labelDoStmt) {
502     AddLabelReferenceFromDoStmt(std::get<parser::Label>(labelDoStmt.t));
503   }
Post(const parser::GotoStmt & gotoStmt)504   void Post(const parser::GotoStmt &gotoStmt) { AddLabelReference(gotoStmt.v); }
Post(const parser::ComputedGotoStmt & computedGotoStmt)505   void Post(const parser::ComputedGotoStmt &computedGotoStmt) {
506     AddLabelReference(std::get<std::list<parser::Label>>(computedGotoStmt.t));
507   }
Post(const parser::ArithmeticIfStmt & arithmeticIfStmt)508   void Post(const parser::ArithmeticIfStmt &arithmeticIfStmt) {
509     AddLabelReference(std::get<1>(arithmeticIfStmt.t));
510     AddLabelReference(std::get<2>(arithmeticIfStmt.t));
511     AddLabelReference(std::get<3>(arithmeticIfStmt.t));
512   }
Post(const parser::AssignStmt & assignStmt)513   void Post(const parser::AssignStmt &assignStmt) {
514     AddLabelReferenceFromAssignStmt(std::get<parser::Label>(assignStmt.t));
515   }
Post(const parser::AssignedGotoStmt & assignedGotoStmt)516   void Post(const parser::AssignedGotoStmt &assignedGotoStmt) {
517     AddLabelReference(std::get<std::list<parser::Label>>(assignedGotoStmt.t));
518   }
Post(const parser::AltReturnSpec & altReturnSpec)519   void Post(const parser::AltReturnSpec &altReturnSpec) {
520     AddLabelReference(altReturnSpec.v);
521   }
522 
Post(const parser::ErrLabel & errLabel)523   void Post(const parser::ErrLabel &errLabel) { AddLabelReference(errLabel.v); }
Post(const parser::EndLabel & endLabel)524   void Post(const parser::EndLabel &endLabel) { AddLabelReference(endLabel.v); }
Post(const parser::EorLabel & eorLabel)525   void Post(const parser::EorLabel &eorLabel) { AddLabelReference(eorLabel.v); }
Post(const parser::Format & format)526   void Post(const parser::Format &format) {
527     if (const auto *labelPointer{std::get_if<parser::Label>(&format.u)}) {
528       AddLabelReferenceToFormatStmt(*labelPointer);
529     }
530   }
Post(const parser::CycleStmt & cycleStmt)531   void Post(const parser::CycleStmt &cycleStmt) {
532     if (cycleStmt.v) {
533       CheckLabelContext("CYCLE", cycleStmt.v->source);
534     }
535   }
Post(const parser::ExitStmt & exitStmt)536   void Post(const parser::ExitStmt &exitStmt) {
537     if (exitStmt.v) {
538       CheckLabelContext("EXIT", exitStmt.v->source);
539     }
540   }
541 
ProgramUnits() const542   const std::vector<UnitAnalysis> &ProgramUnits() const {
543     return programUnits_;
544   }
ErrorHandler()545   SemanticsContext &ErrorHandler() { return context_; }
546 
547 private:
PushScope()548   ScopeInfo &PushScope() {
549     auto &model{programUnits_.back().scopeModel};
550     int newDepth{model.empty() ? 1 : model[currentScope_].depth + 1};
551     ScopeInfo &result{model.emplace_back()};
552     result.parent = currentScope_;
553     result.depth = newDepth;
554     currentScope_ = model.size() - 1;
555     return result;
556   }
InitializeNewScopeContext()557   bool InitializeNewScopeContext() {
558     programUnits_.emplace_back(UnitAnalysis{});
559     currentScope_ = 0u;
560     PushScope();
561     return true;
562   }
PopScope()563   ScopeInfo &PopScope() {
564     ScopeInfo &result{programUnits_.back().scopeModel[currentScope_]};
565     currentScope_ = result.parent;
566     return result;
567   }
ParentScope()568   ProxyForScope ParentScope() {
569     return programUnits_.back().scopeModel[currentScope_].parent;
570   }
SwitchToNewScope()571   bool SwitchToNewScope() {
572     ScopeInfo &oldScope{PopScope()};
573     bool isExteriorGotoFatal{oldScope.isExteriorGotoFatal};
574     PushScope().isExteriorGotoFatal = isExteriorGotoFatal;
575     return true;
576   }
577 
PushConstructName(const A & a)578   template <typename A> bool PushConstructName(const A &a) {
579     const auto &optionalName{std::get<0>(std::get<0>(a.t).statement.t)};
580     if (optionalName) {
581       constructNames_.emplace_back(optionalName->ToString());
582     }
583     // Gotos into this construct from outside it are diagnosed, and
584     // are fatal unless the construct is a DO, IF, or SELECT CASE.
585     PushScope().isExteriorGotoFatal =
586         !(std::is_same_v<A, parser::DoConstruct> ||
587             std::is_same_v<A, parser::IfConstruct> ||
588             std::is_same_v<A, parser::CaseConstruct>);
589     return true;
590   }
PushConstructName(const parser::BlockConstruct & blockConstruct)591   bool PushConstructName(const parser::BlockConstruct &blockConstruct) {
592     const auto &optionalName{
593         std::get<parser::Statement<parser::BlockStmt>>(blockConstruct.t)
594             .statement.v};
595     if (optionalName) {
596       constructNames_.emplace_back(optionalName->ToString());
597     }
598     PushScope().isExteriorGotoFatal = true;
599     return true;
600   }
PopConstructNameIfPresent(const A & a)601   template <typename A> void PopConstructNameIfPresent(const A &a) {
602     const auto &optionalName{std::get<0>(std::get<0>(a.t).statement.t)};
603     if (optionalName) {
604       constructNames_.pop_back();
605     }
606   }
PopConstructNameIfPresent(const parser::BlockConstruct & blockConstruct)607   void PopConstructNameIfPresent(const parser::BlockConstruct &blockConstruct) {
608     const auto &optionalName{
609         std::get<parser::Statement<parser::BlockStmt>>(blockConstruct.t)
610             .statement.v};
611     if (optionalName) {
612       constructNames_.pop_back();
613     }
614   }
615 
PopConstructName(const A & a)616   template <typename A> void PopConstructName(const A &a) {
617     CheckName(a);
618     PopScope();
619     PopConstructNameIfPresent(a);
620   }
621 
622   template <typename FIRST, typename CASEBLOCK, typename CASE,
623       typename CONSTRUCT>
CheckSelectNames(const char * tag,const CONSTRUCT & construct)624   void CheckSelectNames(const char *tag, const CONSTRUCT &construct) {
625     CheckEndName<FIRST, parser::EndSelectStmt>(tag, construct);
626     for (const auto &inner : std::get<std::list<CASEBLOCK>>(construct.t)) {
627       CheckOptionalName<FIRST>(
628           tag, construct, std::get<parser::Statement<CASE>>(inner.t));
629     }
630   }
631 
632   // C1144
PopConstructName(const parser::CaseConstruct & caseConstruct)633   void PopConstructName(const parser::CaseConstruct &caseConstruct) {
634     CheckSelectNames<parser::SelectCaseStmt, parser::CaseConstruct::Case,
635         parser::CaseStmt>("SELECT CASE", caseConstruct);
636     PopScope();
637     PopConstructNameIfPresent(caseConstruct);
638   }
639 
640   // C1154, C1156
PopConstructName(const parser::SelectRankConstruct & selectRankConstruct)641   void PopConstructName(
642       const parser::SelectRankConstruct &selectRankConstruct) {
643     CheckSelectNames<parser::SelectRankStmt,
644         parser::SelectRankConstruct::RankCase, parser::SelectRankCaseStmt>(
645         "SELECT RANK", selectRankConstruct);
646     PopScope();
647     PopConstructNameIfPresent(selectRankConstruct);
648   }
649 
650   // C1165
PopConstructName(const parser::SelectTypeConstruct & selectTypeConstruct)651   void PopConstructName(
652       const parser::SelectTypeConstruct &selectTypeConstruct) {
653     CheckSelectNames<parser::SelectTypeStmt,
654         parser::SelectTypeConstruct::TypeCase, parser::TypeGuardStmt>(
655         "SELECT TYPE", selectTypeConstruct);
656     PopScope();
657     PopConstructNameIfPresent(selectTypeConstruct);
658   }
659 
660   // Checks for missing or mismatching names on various constructs (e.g., BLOCK)
661   // and their END statements.  Both names must be present if either one is.
662   template <typename FIRST, typename END, typename CONSTRUCT>
CheckEndName(const char * constructTag,const CONSTRUCT & a)663   void CheckEndName(const char *constructTag, const CONSTRUCT &a) {
664     const auto &constructStmt{std::get<parser::Statement<FIRST>>(a.t)};
665     const auto &endStmt{std::get<parser::Statement<END>>(a.t)};
666     const parser::CharBlock *endName{GetStmtName(endStmt)};
667     if (const parser::CharBlock * constructName{GetStmtName(constructStmt)}) {
668       if (endName) {
669         if (*constructName != *endName) {
670           context_
671               .Say(*endName, "%s construct name mismatch"_err_en_US,
672                   constructTag)
673               .Attach(*constructName, "should be"_en_US);
674         }
675       } else {
676         context_
677             .Say(endStmt.source,
678                 "%s construct name required but missing"_err_en_US,
679                 constructTag)
680             .Attach(*constructName, "should be"_en_US);
681       }
682     } else if (endName) {
683       context_
684           .Say(*endName, "%s construct name unexpected"_err_en_US, constructTag)
685           .Attach(
686               constructStmt.source, "unnamed %s statement"_en_US, constructTag);
687     }
688   }
689 
690   // C1106
CheckName(const parser::AssociateConstruct & associateConstruct)691   void CheckName(const parser::AssociateConstruct &associateConstruct) {
692     CheckEndName<parser::AssociateStmt, parser::EndAssociateStmt>(
693         "ASSOCIATE", associateConstruct);
694   }
695   // C1117
CheckName(const parser::CriticalConstruct & criticalConstruct)696   void CheckName(const parser::CriticalConstruct &criticalConstruct) {
697     CheckEndName<parser::CriticalStmt, parser::EndCriticalStmt>(
698         "CRITICAL", criticalConstruct);
699   }
700   // C1131
CheckName(const parser::DoConstruct & doConstruct)701   void CheckName(const parser::DoConstruct &doConstruct) {
702     CheckEndName<parser::NonLabelDoStmt, parser::EndDoStmt>("DO", doConstruct);
703   }
704   // C1035
CheckName(const parser::ForallConstruct & forallConstruct)705   void CheckName(const parser::ForallConstruct &forallConstruct) {
706     CheckEndName<parser::ForallConstructStmt, parser::EndForallStmt>(
707         "FORALL", forallConstruct);
708   }
709 
710   // C1109
CheckName(const parser::BlockConstruct & blockConstruct)711   void CheckName(const parser::BlockConstruct &blockConstruct) {
712     CheckEndName<parser::BlockStmt, parser::EndBlockStmt>(
713         "BLOCK", blockConstruct);
714   }
715   // C1112
CheckName(const parser::ChangeTeamConstruct & changeTeamConstruct)716   void CheckName(const parser::ChangeTeamConstruct &changeTeamConstruct) {
717     CheckEndName<parser::ChangeTeamStmt, parser::EndChangeTeamStmt>(
718         "CHANGE TEAM", changeTeamConstruct);
719   }
720 
721   // C1142
CheckName(const parser::IfConstruct & ifConstruct)722   void CheckName(const parser::IfConstruct &ifConstruct) {
723     CheckEndName<parser::IfThenStmt, parser::EndIfStmt>("IF", ifConstruct);
724     for (const auto &elseIfBlock :
725         std::get<std::list<parser::IfConstruct::ElseIfBlock>>(ifConstruct.t)) {
726       CheckOptionalName<parser::IfThenStmt>("IF construct", ifConstruct,
727           std::get<parser::Statement<parser::ElseIfStmt>>(elseIfBlock.t));
728     }
729     if (const auto &elseBlock{
730             std::get<std::optional<parser::IfConstruct::ElseBlock>>(
731                 ifConstruct.t)}) {
732       CheckOptionalName<parser::IfThenStmt>("IF construct", ifConstruct,
733           std::get<parser::Statement<parser::ElseStmt>>(elseBlock->t));
734     }
735   }
736 
737   // C1033
CheckName(const parser::WhereConstruct & whereConstruct)738   void CheckName(const parser::WhereConstruct &whereConstruct) {
739     CheckEndName<parser::WhereConstructStmt, parser::EndWhereStmt>(
740         "WHERE", whereConstruct);
741     for (const auto &maskedElsewhere :
742         std::get<std::list<parser::WhereConstruct::MaskedElsewhere>>(
743             whereConstruct.t)) {
744       CheckOptionalName<parser::WhereConstructStmt>("WHERE construct",
745           whereConstruct,
746           std::get<parser::Statement<parser::MaskedElsewhereStmt>>(
747               maskedElsewhere.t));
748     }
749     if (const auto &elsewhere{
750             std::get<std::optional<parser::WhereConstruct::Elsewhere>>(
751                 whereConstruct.t)}) {
752       CheckOptionalName<parser::WhereConstructStmt>("WHERE construct",
753           whereConstruct,
754           std::get<parser::Statement<parser::ElsewhereStmt>>(elsewhere->t));
755     }
756   }
757 
758   // C1134, C1166
CheckLabelContext(const char * const stmtString,const parser::CharBlock & constructName)759   void CheckLabelContext(
760       const char *const stmtString, const parser::CharBlock &constructName) {
761     const auto iter{std::find(constructNames_.crbegin(),
762         constructNames_.crend(), constructName.ToString())};
763     if (iter == constructNames_.crend()) {
764       context_.Say(constructName, "%s construct-name is not in scope"_err_en_US,
765           stmtString);
766     }
767   }
768 
769   // 6.2.5, paragraph 2
CheckLabelInRange(parser::Label label)770   void CheckLabelInRange(parser::Label label) {
771     if (label < 1 || label > 99999) {
772       context_.Say(currentPosition_, "Label '%u' is out of range"_err_en_US,
773           SayLabel(label));
774     }
775   }
776 
777   // 6.2.5., paragraph 2
AddTargetLabelDefinition(parser::Label label,LabeledStmtClassificationSet labeledStmtClassificationSet,ProxyForScope scope,bool isExecutableConstructEndStmt=false)778   void AddTargetLabelDefinition(parser::Label label,
779       LabeledStmtClassificationSet labeledStmtClassificationSet,
780       ProxyForScope scope, bool isExecutableConstructEndStmt = false) {
781     CheckLabelInRange(label);
782     const auto pair{programUnits_.back().targetStmts.emplace(label,
783         LabeledStatementInfoTuplePOD{scope, currentPosition_,
784             labeledStmtClassificationSet, isExecutableConstructEndStmt})};
785     if (!pair.second) {
786       context_.Say(currentPosition_, "Label '%u' is not distinct"_err_en_US,
787           SayLabel(label));
788     }
789   }
790 
AddLabelReferenceFromDoStmt(parser::Label label)791   void AddLabelReferenceFromDoStmt(parser::Label label) {
792     CheckLabelInRange(label);
793     programUnits_.back().doStmtSources.emplace_back(
794         label, currentScope_, currentPosition_);
795   }
796 
AddLabelReferenceToFormatStmt(parser::Label label)797   void AddLabelReferenceToFormatStmt(parser::Label label) {
798     CheckLabelInRange(label);
799     programUnits_.back().formatStmtSources.emplace_back(
800         label, currentScope_, currentPosition_);
801   }
802 
AddLabelReferenceFromAssignStmt(parser::Label label)803   void AddLabelReferenceFromAssignStmt(parser::Label label) {
804     CheckLabelInRange(label);
805     programUnits_.back().assignStmtSources.emplace_back(
806         label, currentScope_, currentPosition_);
807   }
808 
AddLabelReference(parser::Label label)809   void AddLabelReference(parser::Label label) {
810     CheckLabelInRange(label);
811     programUnits_.back().otherStmtSources.emplace_back(
812         label, currentScope_, currentPosition_);
813   }
814 
AddLabelReference(const std::list<parser::Label> & labels)815   void AddLabelReference(const std::list<parser::Label> &labels) {
816     for (const parser::Label &label : labels) {
817       AddLabelReference(label);
818     }
819   }
820 
821   std::vector<UnitAnalysis> programUnits_;
822   SemanticsContext &context_;
823   parser::CharBlock currentPosition_;
824   ProxyForScope currentScope_;
825   std::vector<std::string> constructNames_;
826 };
827 
InInclusiveScope(const std::vector<ScopeInfo> & scopes,ProxyForScope tail,ProxyForScope head)828 bool InInclusiveScope(const std::vector<ScopeInfo> &scopes, ProxyForScope tail,
829     ProxyForScope head) {
830   for (; tail != head; tail = scopes[tail].parent) {
831     if (!HasScope(tail)) {
832       return false;
833     }
834   }
835   return true;
836 }
837 
LabelAnalysis(SemanticsContext & context,const parser::Program & program)838 ParseTreeAnalyzer LabelAnalysis(
839     SemanticsContext &context, const parser::Program &program) {
840   ParseTreeAnalyzer analysis{context};
841   Walk(program, analysis);
842   return analysis;
843 }
844 
InBody(const parser::CharBlock & position,const std::pair<parser::CharBlock,parser::CharBlock> & pair)845 bool InBody(const parser::CharBlock &position,
846     const std::pair<parser::CharBlock, parser::CharBlock> &pair) {
847   if (position.begin() >= pair.first.begin()) {
848     if (position.begin() < pair.second.end()) {
849       return true;
850     }
851   }
852   return false;
853 }
854 
GetLabel(const TargetStmtMap & labels,const parser::Label & label)855 LabeledStatementInfoTuplePOD GetLabel(
856     const TargetStmtMap &labels, const parser::Label &label) {
857   const auto iter{labels.find(label)};
858   if (iter == labels.cend()) {
859     return {0u, nullptr, LabeledStmtClassificationSet{}, false};
860   } else {
861     return iter->second;
862   }
863 }
864 
865 // 11.1.7.3
CheckBranchesIntoDoBody(const SourceStmtList & branches,const TargetStmtMap & labels,const IndexList & loopBodies,SemanticsContext & context)866 void CheckBranchesIntoDoBody(const SourceStmtList &branches,
867     const TargetStmtMap &labels, const IndexList &loopBodies,
868     SemanticsContext &context) {
869   for (const auto &branch : branches) {
870     const auto &label{branch.parserLabel};
871     auto branchTarget{GetLabel(labels, label)};
872     if (HasScope(branchTarget.proxyForScope)) {
873       const auto &fromPosition{branch.parserCharBlock};
874       const auto &toPosition{branchTarget.parserCharBlock};
875       for (const auto &body : loopBodies) {
876         if (!InBody(fromPosition, body) && InBody(toPosition, body)) {
877           context
878               .Say(
879                   fromPosition, "branch into loop body from outside"_warn_en_US)
880               .Attach(body.first, "the loop branched into"_en_US);
881         }
882       }
883     }
884   }
885 }
886 
CheckDoNesting(const IndexList & loopBodies,SemanticsContext & context)887 void CheckDoNesting(const IndexList &loopBodies, SemanticsContext &context) {
888   for (auto i1{loopBodies.cbegin()}; i1 != loopBodies.cend(); ++i1) {
889     const auto &v1{*i1};
890     for (auto i2{i1 + 1}; i2 != loopBodies.cend(); ++i2) {
891       const auto &v2{*i2};
892       if (v2.first.begin() < v1.second.end() &&
893           v1.second.begin() < v2.second.begin()) {
894         context.Say(v1.first, "DO loop doesn't properly nest"_err_en_US)
895             .Attach(v2.first, "DO loop conflicts"_en_US);
896       }
897     }
898   }
899 }
900 
SkipLabel(const parser::CharBlock & position)901 parser::CharBlock SkipLabel(const parser::CharBlock &position) {
902   const std::size_t maxPosition{position.size()};
903   if (maxPosition && parser::IsDecimalDigit(position[0])) {
904     std::size_t i{1l};
905     for (; (i < maxPosition) && parser::IsDecimalDigit(position[i]); ++i) {
906     }
907     for (; (i < maxPosition) && std::isspace(position[i]); ++i) {
908     }
909     return parser::CharBlock{position.begin() + i, position.end()};
910   }
911   return position;
912 }
913 
ParentScope(const std::vector<ScopeInfo> & scopes,ProxyForScope scope)914 ProxyForScope ParentScope(
915     const std::vector<ScopeInfo> &scopes, ProxyForScope scope) {
916   return scopes[scope].parent;
917 }
918 
CheckLabelDoConstraints(const SourceStmtList & dos,const SourceStmtList & branches,const TargetStmtMap & labels,const std::vector<ScopeInfo> & scopes,SemanticsContext & context)919 void CheckLabelDoConstraints(const SourceStmtList &dos,
920     const SourceStmtList &branches, const TargetStmtMap &labels,
921     const std::vector<ScopeInfo> &scopes, SemanticsContext &context) {
922   IndexList loopBodies;
923   for (const auto &stmt : dos) {
924     const auto &label{stmt.parserLabel};
925     const auto &scope{stmt.proxyForScope};
926     const auto &position{stmt.parserCharBlock};
927     auto doTarget{GetLabel(labels, label)};
928     if (!HasScope(doTarget.proxyForScope)) {
929       // C1133
930       context.Say(
931           position, "Label '%u' cannot be found"_err_en_US, SayLabel(label));
932     } else if (doTarget.parserCharBlock.begin() < position.begin()) {
933       // R1119
934       context.Say(position,
935           "Label '%u' doesn't lexically follow DO stmt"_err_en_US,
936           SayLabel(label));
937 
938     } else if ((InInclusiveScope(scopes, scope, doTarget.proxyForScope) &&
939                    doTarget.labeledStmtClassificationSet.test(
940                        TargetStatementEnum::CompatibleDo)) ||
941         (doTarget.isExecutableConstructEndStmt &&
942             ParentScope(scopes, doTarget.proxyForScope) == scope)) {
943       if (context.warnOnNonstandardUsage() ||
944           context.ShouldWarn(
945               common::LanguageFeature::OldLabelDoEndStatements)) {
946         context
947             .Say(position,
948                 "A DO loop should terminate with an END DO or CONTINUE"_port_en_US)
949             .Attach(doTarget.parserCharBlock,
950                 "DO loop currently ends at statement:"_en_US);
951       }
952     } else if (!InInclusiveScope(scopes, scope, doTarget.proxyForScope)) {
953       context.Say(position, "Label '%u' is not in DO loop scope"_err_en_US,
954           SayLabel(label));
955     } else if (!doTarget.labeledStmtClassificationSet.test(
956                    TargetStatementEnum::Do)) {
957       context.Say(doTarget.parserCharBlock,
958           "A DO loop should terminate with an END DO or CONTINUE"_err_en_US);
959     } else {
960       loopBodies.emplace_back(SkipLabel(position), doTarget.parserCharBlock);
961     }
962   }
963 
964   CheckBranchesIntoDoBody(branches, labels, loopBodies, context);
965   CheckDoNesting(loopBodies, context);
966 }
967 
968 // 6.2.5
CheckScopeConstraints(const SourceStmtList & stmts,const TargetStmtMap & labels,const std::vector<ScopeInfo> & scopes,SemanticsContext & context)969 void CheckScopeConstraints(const SourceStmtList &stmts,
970     const TargetStmtMap &labels, const std::vector<ScopeInfo> &scopes,
971     SemanticsContext &context) {
972   for (const auto &stmt : stmts) {
973     const auto &label{stmt.parserLabel};
974     const auto &scope{stmt.proxyForScope};
975     const auto &position{stmt.parserCharBlock};
976     auto target{GetLabel(labels, label)};
977     if (!HasScope(target.proxyForScope)) {
978       context.Say(
979           position, "Label '%u' was not found"_err_en_US, SayLabel(label));
980     } else if (!InInclusiveScope(scopes, scope, target.proxyForScope)) {
981       // Clause 11.1.2.1 prohibits transfer of control to the interior of a
982       // block from outside the block, but this does not apply to formats.
983       // C1038 and C1034 forbid statements in FORALL and WHERE constructs
984       // (resp.) from being branch targets.
985       if (target.labeledStmtClassificationSet.test(
986               TargetStatementEnum::Format)) {
987         continue;
988       }
989       bool isFatal{false};
990       ProxyForScope fromScope{scope};
991       for (ProxyForScope toScope{target.proxyForScope}; fromScope != toScope;
992            toScope = scopes[toScope].parent) {
993         if (scopes[toScope].isExteriorGotoFatal) {
994           isFatal = true;
995           break;
996         }
997         if (scopes[toScope].depth == scopes[fromScope].depth) {
998           fromScope = scopes[fromScope].parent;
999         }
1000       }
1001       context.Say(position,
1002           isFatal
1003               ? "Label '%u' is in a construct that prevents its use as a branch target here"_err_en_US
1004               : "Label '%u' is in a construct that should not be used as a branch target here"_warn_en_US,
1005           SayLabel(label));
1006     }
1007   }
1008 }
1009 
CheckBranchTargetConstraints(const SourceStmtList & stmts,const TargetStmtMap & labels,SemanticsContext & context)1010 void CheckBranchTargetConstraints(const SourceStmtList &stmts,
1011     const TargetStmtMap &labels, SemanticsContext &context) {
1012   for (const auto &stmt : stmts) {
1013     const auto &label{stmt.parserLabel};
1014     auto branchTarget{GetLabel(labels, label)};
1015     if (HasScope(branchTarget.proxyForScope)) {
1016       if (!branchTarget.labeledStmtClassificationSet.test(
1017               TargetStatementEnum::Branch) &&
1018           !branchTarget.labeledStmtClassificationSet.test(
1019               TargetStatementEnum::CompatibleBranch)) { // error
1020         context
1021             .Say(branchTarget.parserCharBlock,
1022                 "Label '%u' is not a branch target"_err_en_US, SayLabel(label))
1023             .Attach(stmt.parserCharBlock, "Control flow use of '%u'"_en_US,
1024                 SayLabel(label));
1025       } else if (!branchTarget.labeledStmtClassificationSet.test(
1026                      TargetStatementEnum::Branch)) { // warning
1027         context
1028             .Say(branchTarget.parserCharBlock,
1029                 "Label '%u' is not a branch target"_warn_en_US, SayLabel(label))
1030             .Attach(stmt.parserCharBlock, "Control flow use of '%u'"_en_US,
1031                 SayLabel(label));
1032       }
1033     }
1034   }
1035 }
1036 
CheckBranchConstraints(const SourceStmtList & branches,const TargetStmtMap & labels,const std::vector<ScopeInfo> & scopes,SemanticsContext & context)1037 void CheckBranchConstraints(const SourceStmtList &branches,
1038     const TargetStmtMap &labels, const std::vector<ScopeInfo> &scopes,
1039     SemanticsContext &context) {
1040   CheckScopeConstraints(branches, labels, scopes, context);
1041   CheckBranchTargetConstraints(branches, labels, context);
1042 }
1043 
CheckDataXferTargetConstraints(const SourceStmtList & stmts,const TargetStmtMap & labels,SemanticsContext & context)1044 void CheckDataXferTargetConstraints(const SourceStmtList &stmts,
1045     const TargetStmtMap &labels, SemanticsContext &context) {
1046   for (const auto &stmt : stmts) {
1047     const auto &label{stmt.parserLabel};
1048     auto ioTarget{GetLabel(labels, label)};
1049     if (HasScope(ioTarget.proxyForScope)) {
1050       if (!ioTarget.labeledStmtClassificationSet.test(
1051               TargetStatementEnum::Format)) {
1052         context
1053             .Say(ioTarget.parserCharBlock, "'%u' not a FORMAT"_err_en_US,
1054                 SayLabel(label))
1055             .Attach(stmt.parserCharBlock, "data transfer use of '%u'"_en_US,
1056                 SayLabel(label));
1057       }
1058     }
1059   }
1060 }
1061 
CheckDataTransferConstraints(const SourceStmtList & dataTransfers,const TargetStmtMap & labels,const std::vector<ScopeInfo> & scopes,SemanticsContext & context)1062 void CheckDataTransferConstraints(const SourceStmtList &dataTransfers,
1063     const TargetStmtMap &labels, const std::vector<ScopeInfo> &scopes,
1064     SemanticsContext &context) {
1065   CheckScopeConstraints(dataTransfers, labels, scopes, context);
1066   CheckDataXferTargetConstraints(dataTransfers, labels, context);
1067 }
1068 
CheckAssignTargetConstraints(const SourceStmtList & stmts,const TargetStmtMap & labels,SemanticsContext & context)1069 void CheckAssignTargetConstraints(const SourceStmtList &stmts,
1070     const TargetStmtMap &labels, SemanticsContext &context) {
1071   for (const auto &stmt : stmts) {
1072     const auto &label{stmt.parserLabel};
1073     auto target{GetLabel(labels, label)};
1074     if (HasScope(target.proxyForScope) &&
1075         !target.labeledStmtClassificationSet.test(
1076             TargetStatementEnum::Branch) &&
1077         !target.labeledStmtClassificationSet.test(
1078             TargetStatementEnum::Format)) {
1079       context
1080           .Say(target.parserCharBlock,
1081               target.labeledStmtClassificationSet.test(
1082                   TargetStatementEnum::CompatibleBranch)
1083                   ? "Label '%u' is not a branch target or FORMAT"_warn_en_US
1084                   : "Label '%u' is not a branch target or FORMAT"_err_en_US,
1085               SayLabel(label))
1086           .Attach(stmt.parserCharBlock, "ASSIGN statement use of '%u'"_en_US,
1087               SayLabel(label));
1088     }
1089   }
1090 }
1091 
CheckAssignConstraints(const SourceStmtList & assigns,const TargetStmtMap & labels,const std::vector<ScopeInfo> & scopes,SemanticsContext & context)1092 void CheckAssignConstraints(const SourceStmtList &assigns,
1093     const TargetStmtMap &labels, const std::vector<ScopeInfo> &scopes,
1094     SemanticsContext &context) {
1095   CheckScopeConstraints(assigns, labels, scopes, context);
1096   CheckAssignTargetConstraints(assigns, labels, context);
1097 }
1098 
CheckConstraints(ParseTreeAnalyzer && parseTreeAnalysis)1099 bool CheckConstraints(ParseTreeAnalyzer &&parseTreeAnalysis) {
1100   auto &context{parseTreeAnalysis.ErrorHandler()};
1101   for (const auto &programUnit : parseTreeAnalysis.ProgramUnits()) {
1102     const auto &dos{programUnit.doStmtSources};
1103     const auto &branches{programUnit.otherStmtSources};
1104     const auto &labels{programUnit.targetStmts};
1105     const auto &scopes{programUnit.scopeModel};
1106     CheckLabelDoConstraints(dos, branches, labels, scopes, context);
1107     CheckBranchConstraints(branches, labels, scopes, context);
1108     const auto &dataTransfers{programUnit.formatStmtSources};
1109     CheckDataTransferConstraints(dataTransfers, labels, scopes, context);
1110     const auto &assigns{programUnit.assignStmtSources};
1111     CheckAssignConstraints(assigns, labels, scopes, context);
1112   }
1113   return !context.AnyFatalError();
1114 }
1115 
ValidateLabels(SemanticsContext & context,const parser::Program & program)1116 bool ValidateLabels(SemanticsContext &context, const parser::Program &program) {
1117   return CheckConstraints(LabelAnalysis(context, program));
1118 }
1119 } // namespace Fortran::semantics
1120