xref: /llvm-project/flang/lib/Semantics/resolve-labels.cpp (revision f75033a0588a05b0077ad8fedb1dad6f466558c0)
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 {
45   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 
57 bool HasScope(ProxyForScope scope) { return scope != ProxyForScope{0u}; }
58 
59 // F18:R1131
60 template <typename 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 
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 
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>
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>
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 
156 static unsigned SayLabel(parser::Label label) {
157   return static_cast<unsigned>(label);
158 }
159 
160 struct UnitAnalysis {
161   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>
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;
201   ParseTreeAnalyzer(SemanticsContext &context) : context_{context} {}
202 
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   }
220   template <typename A> constexpr void Post(const A &) {}
221 
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
258   bool Pre(const parser::ProgramUnit &) { return InitializeNewScopeContext(); }
259   bool Pre(const parser::InternalSubprogram &) {
260     return InitializeNewScopeContext();
261   }
262   bool Pre(const parser::ModuleSubprogram &) {
263     return InitializeNewScopeContext();
264   }
265   bool Pre(const parser::AssociateConstruct &associateConstruct) {
266     return PushConstructName(associateConstruct);
267   }
268   bool Pre(const parser::BlockConstruct &blockConstruct) {
269     return PushConstructName(blockConstruct);
270   }
271   bool Pre(const parser::ChangeTeamConstruct &changeTeamConstruct) {
272     return PushConstructName(changeTeamConstruct);
273   }
274   bool Pre(const parser::CriticalConstruct &criticalConstruct) {
275     return PushConstructName(criticalConstruct);
276   }
277   bool Pre(const parser::DoConstruct &doConstruct) {
278     return PushConstructName(doConstruct);
279   }
280   bool Pre(const parser::IfConstruct &ifConstruct) {
281     return PushConstructName(ifConstruct);
282   }
283   bool Pre(const parser::IfConstruct::ElseIfBlock &) {
284     return SwitchToNewScope();
285   }
286   bool Pre(const parser::IfConstruct::ElseBlock &) {
287     return SwitchToNewScope();
288   }
289   bool Pre(const parser::CaseConstruct &caseConstruct) {
290     return PushConstructName(caseConstruct);
291   }
292   void Post(const parser::SelectCaseStmt &) { PushScope(); }
293   bool Pre(const parser::CaseConstruct::Case &) { return SwitchToNewScope(); }
294   bool Pre(const parser::SelectRankConstruct &selectRankConstruct) {
295     return PushConstructName(selectRankConstruct);
296   }
297   void Post(const parser::SelectRankStmt &) { PushScope(); }
298   bool Pre(const parser::SelectRankConstruct::RankCase &) {
299     return SwitchToNewScope();
300   }
301   bool Pre(const parser::SelectTypeConstruct &selectTypeConstruct) {
302     return PushConstructName(selectTypeConstruct);
303   }
304   void Post(const parser::SelectTypeStmt &) { PushScope(); }
305   bool Pre(const parser::SelectTypeConstruct::TypeCase &) {
306     return SwitchToNewScope();
307   }
308   void Post(const parser::EndSelectStmt &) { PopScope(); }
309   bool Pre(const parser::WhereConstruct &whereConstruct) {
310     return PushConstructName(whereConstruct);
311   }
312   bool Pre(const parser::ForallConstruct &forallConstruct) {
313     return PushConstructName(forallConstruct);
314   }
315 
316   void Post(const parser::AssociateConstruct &associateConstruct) {
317     PopConstructName(associateConstruct);
318   }
319   void Post(const parser::BlockConstruct &blockConstruct) {
320     PopConstructName(blockConstruct);
321   }
322   void Post(const parser::ChangeTeamConstruct &changeTeamConstruct) {
323     PopConstructName(changeTeamConstruct);
324   }
325   void Post(const parser::CriticalConstruct &criticalConstruct) {
326     PopConstructName(criticalConstruct);
327   }
328   void Post(const parser::DoConstruct &doConstruct) {
329     PopConstructName(doConstruct);
330   }
331   void Post(const parser::IfConstruct &ifConstruct) {
332     PopConstructName(ifConstruct);
333   }
334   void Post(const parser::CaseConstruct &caseConstruct) {
335     PopConstructName(caseConstruct);
336   }
337   void Post(const parser::SelectRankConstruct &selectRankConstruct) {
338     PopConstructName(selectRankConstruct);
339   }
340   void Post(const parser::SelectTypeConstruct &selectTypeConstruct) {
341     PopConstructName(selectTypeConstruct);
342   }
343   void Post(const parser::WhereConstruct &whereConstruct) {
344     PopConstructName(whereConstruct);
345   }
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>
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
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   bool Pre(const parser::InterfaceBody &) {
378     PushDisposableMap();
379     return true;
380   }
381   void Post(const parser::InterfaceBody &) { PopDisposableMap(); }
382 
383   // C1564
384   void Post(const parser::InterfaceBody::Function &func) {
385     CheckOptionalName<parser::FunctionStmt>("FUNCTION", func,
386         std::get<parser::Statement<parser::EndFunctionStmt>>(func.t));
387   }
388 
389   // C1564
390   void Post(const parser::FunctionSubprogram &functionSubprogram) {
391     CheckOptionalName<parser::FunctionStmt>("FUNCTION", functionSubprogram,
392         std::get<parser::Statement<parser::EndFunctionStmt>>(
393             functionSubprogram.t));
394   }
395 
396   // C1502
397   void Post(const parser::InterfaceBlock &interfaceBlock) {
398     if (const auto &endGenericSpec{
399             std::get<parser::Statement<parser::EndInterfaceStmt>>(
400                 interfaceBlock.t)
401                 .statement.v}) {
402       const auto &interfaceStmt{
403           std::get<parser::Statement<parser::InterfaceStmt>>(interfaceBlock.t)};
404       if (std::holds_alternative<parser::Abstract>(interfaceStmt.statement.u)) {
405         context_
406             .Say(endGenericSpec->source,
407                 "END INTERFACE generic name (%s) may not appear for ABSTRACT INTERFACE"_err_en_US,
408                 endGenericSpec->source)
409             .Attach(
410                 interfaceStmt.source, "corresponding ABSTRACT INTERFACE"_en_US);
411       } else if (const auto &genericSpec{
412                      std::get<std::optional<parser::GenericSpec>>(
413                          interfaceStmt.statement.u)}) {
414         bool ok{genericSpec->source == endGenericSpec->source};
415         if (!ok) {
416           // Accept variant spellings of .LT. &c.
417           const auto *endOp{
418               std::get_if<parser::DefinedOperator>(&endGenericSpec->u)};
419           const auto *op{std::get_if<parser::DefinedOperator>(&genericSpec->u)};
420           if (endOp && op) {
421             const auto *endIntrin{
422                 std::get_if<parser::DefinedOperator::IntrinsicOperator>(
423                     &endOp->u)};
424             const auto *intrin{
425                 std::get_if<parser::DefinedOperator::IntrinsicOperator>(
426                     &op->u)};
427             ok = endIntrin && intrin && *endIntrin == *intrin;
428           }
429         }
430         if (!ok) {
431           context_
432               .Say(endGenericSpec->source,
433                   "END INTERFACE generic name (%s) does not match generic INTERFACE (%s)"_err_en_US,
434                   endGenericSpec->source, genericSpec->source)
435               .Attach(genericSpec->source, "corresponding INTERFACE"_en_US);
436         }
437       } else {
438         context_
439             .Say(endGenericSpec->source,
440                 "END INTERFACE generic name (%s) may not appear for non-generic INTERFACE"_err_en_US,
441                 endGenericSpec->source)
442             .Attach(interfaceStmt.source, "corresponding INTERFACE"_en_US);
443       }
444     }
445   }
446 
447   // C1402
448   void Post(const parser::Module &module) {
449     CheckOptionalName<parser::ModuleStmt>("MODULE", module,
450         std::get<parser::Statement<parser::EndModuleStmt>>(module.t));
451   }
452 
453   // C1569
454   void Post(const parser::SeparateModuleSubprogram &separateModuleSubprogram) {
455     CheckOptionalName<parser::MpSubprogramStmt>("MODULE PROCEDURE",
456         separateModuleSubprogram,
457         std::get<parser::Statement<parser::EndMpSubprogramStmt>>(
458             separateModuleSubprogram.t));
459   }
460 
461   // C1401
462   void Post(const parser::MainProgram &mainProgram) {
463     if (const parser::CharBlock *
464         endName{GetStmtName(std::get<parser::Statement<parser::EndProgramStmt>>(
465             mainProgram.t))}) {
466       if (const auto &program{
467               std::get<std::optional<parser::Statement<parser::ProgramStmt>>>(
468                   mainProgram.t)}) {
469         if (*endName != program->statement.v.source) {
470           context_.Say(*endName, "END PROGRAM name mismatch"_err_en_US)
471               .Attach(program->statement.v.source, "should be"_en_US);
472         }
473       } else {
474         context_.Say(*endName,
475             "END PROGRAM has name without PROGRAM statement"_err_en_US);
476       }
477     }
478   }
479 
480   // C1413
481   void Post(const parser::Submodule &submodule) {
482     CheckOptionalName<parser::SubmoduleStmt>("SUBMODULE", submodule,
483         std::get<parser::Statement<parser::EndSubmoduleStmt>>(submodule.t));
484   }
485 
486   // C1567
487   void Post(const parser::InterfaceBody::Subroutine &sub) {
488     CheckOptionalName<parser::SubroutineStmt>("SUBROUTINE", sub,
489         std::get<parser::Statement<parser::EndSubroutineStmt>>(sub.t));
490   }
491 
492   // C1567
493   void Post(const parser::SubroutineSubprogram &subroutineSubprogram) {
494     CheckOptionalName<parser::SubroutineStmt>("SUBROUTINE",
495         subroutineSubprogram,
496         std::get<parser::Statement<parser::EndSubroutineStmt>>(
497             subroutineSubprogram.t));
498   }
499 
500   // C739
501   bool Pre(const parser::DerivedTypeDef &) {
502     PushDisposableMap();
503     return true;
504   }
505   void Post(const parser::DerivedTypeDef &derivedTypeDef) {
506     CheckOptionalName<parser::DerivedTypeStmt>("derived type definition",
507         derivedTypeDef,
508         std::get<parser::Statement<parser::EndTypeStmt>>(derivedTypeDef.t));
509     PopDisposableMap();
510   }
511 
512   void Post(const parser::LabelDoStmt &labelDoStmt) {
513     AddLabelReferenceFromDoStmt(std::get<parser::Label>(labelDoStmt.t));
514   }
515   void Post(const parser::GotoStmt &gotoStmt) { AddLabelReference(gotoStmt.v); }
516   void Post(const parser::ComputedGotoStmt &computedGotoStmt) {
517     AddLabelReference(std::get<std::list<parser::Label>>(computedGotoStmt.t));
518   }
519   void Post(const parser::ArithmeticIfStmt &arithmeticIfStmt) {
520     AddLabelReference(std::get<1>(arithmeticIfStmt.t));
521     AddLabelReference(std::get<2>(arithmeticIfStmt.t));
522     AddLabelReference(std::get<3>(arithmeticIfStmt.t));
523   }
524   void Post(const parser::AssignStmt &assignStmt) {
525     AddLabelReferenceFromAssignStmt(std::get<parser::Label>(assignStmt.t));
526   }
527   void Post(const parser::AssignedGotoStmt &assignedGotoStmt) {
528     AddLabelReference(std::get<std::list<parser::Label>>(assignedGotoStmt.t));
529   }
530   void Post(const parser::AltReturnSpec &altReturnSpec) {
531     AddLabelReference(altReturnSpec.v);
532   }
533 
534   void Post(const parser::ErrLabel &errLabel) { AddLabelReference(errLabel.v); }
535   void Post(const parser::EndLabel &endLabel) { AddLabelReference(endLabel.v); }
536   void Post(const parser::EorLabel &eorLabel) { AddLabelReference(eorLabel.v); }
537   void Post(const parser::Format &format) {
538     if (const auto *labelPointer{std::get_if<parser::Label>(&format.u)}) {
539       AddLabelReferenceToFormatStmt(*labelPointer);
540     }
541   }
542   void Post(const parser::CycleStmt &cycleStmt) {
543     if (cycleStmt.v) {
544       CheckLabelContext("CYCLE", cycleStmt.v->source);
545     }
546   }
547   void Post(const parser::ExitStmt &exitStmt) {
548     if (exitStmt.v) {
549       CheckLabelContext("EXIT", exitStmt.v->source);
550     }
551   }
552 
553   const std::vector<UnitAnalysis> &ProgramUnits() const {
554     return programUnits_;
555   }
556   SemanticsContext &ErrorHandler() { return context_; }
557 
558 private:
559   ScopeInfo &PushScope() {
560     auto &model{programUnits_.back().scopeModel};
561     int newDepth{model.empty() ? 1 : model[currentScope_].depth + 1};
562     ScopeInfo &result{model.emplace_back()};
563     result.parent = currentScope_;
564     result.depth = newDepth;
565     currentScope_ = model.size() - 1;
566     return result;
567   }
568   bool InitializeNewScopeContext() {
569     programUnits_.emplace_back(UnitAnalysis{});
570     currentScope_ = 0u;
571     PushScope();
572     return true;
573   }
574   ScopeInfo &PopScope() {
575     ScopeInfo &result{programUnits_.back().scopeModel[currentScope_]};
576     currentScope_ = result.parent;
577     return result;
578   }
579   ProxyForScope ParentScope() {
580     return programUnits_.back().scopeModel[currentScope_].parent;
581   }
582   bool SwitchToNewScope() {
583     ScopeInfo &oldScope{PopScope()};
584     bool isExteriorGotoFatal{oldScope.isExteriorGotoFatal};
585     PushScope().isExteriorGotoFatal = isExteriorGotoFatal;
586     return true;
587   }
588 
589   template <typename A> bool PushConstructName(const A &a) {
590     const auto &optionalName{std::get<0>(std::get<0>(a.t).statement.t)};
591     if (optionalName) {
592       constructNames_.emplace_back(optionalName->ToString());
593     }
594     // Gotos into this construct from outside it are diagnosed, and
595     // are fatal unless the construct is a DO, IF, or SELECT CASE.
596     PushScope().isExteriorGotoFatal =
597         !(std::is_same_v<A, parser::DoConstruct> ||
598             std::is_same_v<A, parser::IfConstruct> ||
599             std::is_same_v<A, parser::CaseConstruct>);
600     return true;
601   }
602   bool PushConstructName(const parser::BlockConstruct &blockConstruct) {
603     const auto &optionalName{
604         std::get<parser::Statement<parser::BlockStmt>>(blockConstruct.t)
605             .statement.v};
606     if (optionalName) {
607       constructNames_.emplace_back(optionalName->ToString());
608     }
609     PushScope().isExteriorGotoFatal = true;
610     return true;
611   }
612   template <typename A> void PopConstructNameIfPresent(const A &a) {
613     const auto &optionalName{std::get<0>(std::get<0>(a.t).statement.t)};
614     if (optionalName) {
615       constructNames_.pop_back();
616     }
617   }
618   void PopConstructNameIfPresent(const parser::BlockConstruct &blockConstruct) {
619     const auto &optionalName{
620         std::get<parser::Statement<parser::BlockStmt>>(blockConstruct.t)
621             .statement.v};
622     if (optionalName) {
623       constructNames_.pop_back();
624     }
625   }
626 
627   template <typename A> void PopConstructName(const A &a) {
628     CheckName(a);
629     PopScope();
630     PopConstructNameIfPresent(a);
631   }
632 
633   template <typename FIRST, typename CASEBLOCK, typename CASE,
634       typename CONSTRUCT>
635   void CheckSelectNames(const char *tag, const CONSTRUCT &construct) {
636     CheckEndName<FIRST, parser::EndSelectStmt>(tag, construct);
637     for (const auto &inner : std::get<std::list<CASEBLOCK>>(construct.t)) {
638       CheckOptionalName<FIRST>(
639           tag, construct, std::get<parser::Statement<CASE>>(inner.t));
640     }
641   }
642 
643   // C1144
644   void PopConstructName(const parser::CaseConstruct &caseConstruct) {
645     CheckSelectNames<parser::SelectCaseStmt, parser::CaseConstruct::Case,
646         parser::CaseStmt>("SELECT CASE", caseConstruct);
647     PopScope();
648     PopConstructNameIfPresent(caseConstruct);
649   }
650 
651   // C1154, C1156
652   void PopConstructName(
653       const parser::SelectRankConstruct &selectRankConstruct) {
654     CheckSelectNames<parser::SelectRankStmt,
655         parser::SelectRankConstruct::RankCase, parser::SelectRankCaseStmt>(
656         "SELECT RANK", selectRankConstruct);
657     PopScope();
658     PopConstructNameIfPresent(selectRankConstruct);
659   }
660 
661   // C1165
662   void PopConstructName(
663       const parser::SelectTypeConstruct &selectTypeConstruct) {
664     CheckSelectNames<parser::SelectTypeStmt,
665         parser::SelectTypeConstruct::TypeCase, parser::TypeGuardStmt>(
666         "SELECT TYPE", selectTypeConstruct);
667     PopScope();
668     PopConstructNameIfPresent(selectTypeConstruct);
669   }
670 
671   // Checks for missing or mismatching names on various constructs (e.g., BLOCK)
672   // and their END statements.  Both names must be present if either one is.
673   template <typename FIRST, typename END, typename CONSTRUCT>
674   void CheckEndName(const char *constructTag, const CONSTRUCT &a) {
675     const auto &constructStmt{std::get<parser::Statement<FIRST>>(a.t)};
676     const auto &endStmt{std::get<parser::Statement<END>>(a.t)};
677     const parser::CharBlock *endName{GetStmtName(endStmt)};
678     if (const parser::CharBlock * constructName{GetStmtName(constructStmt)}) {
679       if (endName) {
680         if (*constructName != *endName) {
681           context_
682               .Say(*endName, "%s construct name mismatch"_err_en_US,
683                   constructTag)
684               .Attach(*constructName, "should be"_en_US);
685         }
686       } else {
687         context_
688             .Say(endStmt.source,
689                 "%s construct name required but missing"_err_en_US,
690                 constructTag)
691             .Attach(*constructName, "should be"_en_US);
692       }
693     } else if (endName) {
694       context_
695           .Say(*endName, "%s construct name unexpected"_err_en_US, constructTag)
696           .Attach(
697               constructStmt.source, "unnamed %s statement"_en_US, constructTag);
698     }
699   }
700 
701   // C1106
702   void CheckName(const parser::AssociateConstruct &associateConstruct) {
703     CheckEndName<parser::AssociateStmt, parser::EndAssociateStmt>(
704         "ASSOCIATE", associateConstruct);
705   }
706   // C1117
707   void CheckName(const parser::CriticalConstruct &criticalConstruct) {
708     CheckEndName<parser::CriticalStmt, parser::EndCriticalStmt>(
709         "CRITICAL", criticalConstruct);
710   }
711   // C1131
712   void CheckName(const parser::DoConstruct &doConstruct) {
713     CheckEndName<parser::NonLabelDoStmt, parser::EndDoStmt>("DO", doConstruct);
714   }
715   // C1035
716   void CheckName(const parser::ForallConstruct &forallConstruct) {
717     CheckEndName<parser::ForallConstructStmt, parser::EndForallStmt>(
718         "FORALL", forallConstruct);
719   }
720 
721   // C1109
722   void CheckName(const parser::BlockConstruct &blockConstruct) {
723     CheckEndName<parser::BlockStmt, parser::EndBlockStmt>(
724         "BLOCK", blockConstruct);
725   }
726   // C1112
727   void CheckName(const parser::ChangeTeamConstruct &changeTeamConstruct) {
728     CheckEndName<parser::ChangeTeamStmt, parser::EndChangeTeamStmt>(
729         "CHANGE TEAM", changeTeamConstruct);
730   }
731 
732   // C1142
733   void CheckName(const parser::IfConstruct &ifConstruct) {
734     CheckEndName<parser::IfThenStmt, parser::EndIfStmt>("IF", ifConstruct);
735     for (const auto &elseIfBlock :
736         std::get<std::list<parser::IfConstruct::ElseIfBlock>>(ifConstruct.t)) {
737       CheckOptionalName<parser::IfThenStmt>("IF construct", ifConstruct,
738           std::get<parser::Statement<parser::ElseIfStmt>>(elseIfBlock.t));
739     }
740     if (const auto &elseBlock{
741             std::get<std::optional<parser::IfConstruct::ElseBlock>>(
742                 ifConstruct.t)}) {
743       CheckOptionalName<parser::IfThenStmt>("IF construct", ifConstruct,
744           std::get<parser::Statement<parser::ElseStmt>>(elseBlock->t));
745     }
746   }
747 
748   // C1033
749   void CheckName(const parser::WhereConstruct &whereConstruct) {
750     CheckEndName<parser::WhereConstructStmt, parser::EndWhereStmt>(
751         "WHERE", whereConstruct);
752     for (const auto &maskedElsewhere :
753         std::get<std::list<parser::WhereConstruct::MaskedElsewhere>>(
754             whereConstruct.t)) {
755       CheckOptionalName<parser::WhereConstructStmt>("WHERE construct",
756           whereConstruct,
757           std::get<parser::Statement<parser::MaskedElsewhereStmt>>(
758               maskedElsewhere.t));
759     }
760     if (const auto &elsewhere{
761             std::get<std::optional<parser::WhereConstruct::Elsewhere>>(
762                 whereConstruct.t)}) {
763       CheckOptionalName<parser::WhereConstructStmt>("WHERE construct",
764           whereConstruct,
765           std::get<parser::Statement<parser::ElsewhereStmt>>(elsewhere->t));
766     }
767   }
768 
769   // C1134, C1166
770   void CheckLabelContext(
771       const char *const stmtString, const parser::CharBlock &constructName) {
772     const auto iter{std::find(constructNames_.crbegin(),
773         constructNames_.crend(), constructName.ToString())};
774     if (iter == constructNames_.crend()) {
775       context_.Say(constructName, "%s construct-name is not in scope"_err_en_US,
776           stmtString);
777     }
778   }
779 
780   // 6.2.5, paragraph 2
781   void CheckLabelInRange(parser::Label label) {
782     if (label < 1 || label > 99999) {
783       context_.Say(currentPosition_, "Label '%u' is out of range"_err_en_US,
784           SayLabel(label));
785     }
786   }
787 
788   // 6.2.5., paragraph 2
789   void AddTargetLabelDefinition(parser::Label label,
790       LabeledStmtClassificationSet labeledStmtClassificationSet,
791       ProxyForScope scope, bool isExecutableConstructEndStmt = false) {
792     CheckLabelInRange(label);
793     TargetStmtMap &targetStmtMap{disposableMaps_.empty()
794             ? programUnits_.back().targetStmts
795             : disposableMaps_.back()};
796     const auto pair{targetStmtMap.emplace(label,
797         LabeledStatementInfoTuplePOD{scope, currentPosition_,
798             labeledStmtClassificationSet, isExecutableConstructEndStmt})};
799     if (!pair.second) {
800       context_.Say(currentPosition_, "Label '%u' is not distinct"_err_en_US,
801           SayLabel(label));
802     }
803   }
804 
805   void AddLabelReferenceFromDoStmt(parser::Label label) {
806     CheckLabelInRange(label);
807     programUnits_.back().doStmtSources.emplace_back(
808         label, currentScope_, currentPosition_);
809   }
810 
811   void AddLabelReferenceToFormatStmt(parser::Label label) {
812     CheckLabelInRange(label);
813     programUnits_.back().formatStmtSources.emplace_back(
814         label, currentScope_, currentPosition_);
815   }
816 
817   void AddLabelReferenceFromAssignStmt(parser::Label label) {
818     CheckLabelInRange(label);
819     programUnits_.back().assignStmtSources.emplace_back(
820         label, currentScope_, currentPosition_);
821   }
822 
823   void AddLabelReference(parser::Label label) {
824     CheckLabelInRange(label);
825     programUnits_.back().otherStmtSources.emplace_back(
826         label, currentScope_, currentPosition_);
827   }
828 
829   void AddLabelReference(const std::list<parser::Label> &labels) {
830     for (const parser::Label &label : labels) {
831       AddLabelReference(label);
832     }
833   }
834 
835   void PushDisposableMap() { disposableMaps_.emplace_back(); }
836   void PopDisposableMap() { disposableMaps_.pop_back(); }
837 
838   std::vector<UnitAnalysis> programUnits_;
839   SemanticsContext &context_;
840   parser::CharBlock currentPosition_;
841   ProxyForScope currentScope_;
842   std::vector<std::string> constructNames_;
843   // For labels in derived type definitions and procedure
844   // interfaces, which are their own inclusive scopes.  None
845   // of these labels can be used as a branch target, but they
846   // should be pairwise distinct.
847   std::vector<TargetStmtMap> disposableMaps_;
848 };
849 
850 bool InInclusiveScope(const std::vector<ScopeInfo> &scopes, ProxyForScope tail,
851     ProxyForScope head) {
852   for (; tail != head; tail = scopes[tail].parent) {
853     if (!HasScope(tail)) {
854       return false;
855     }
856   }
857   return true;
858 }
859 
860 ParseTreeAnalyzer LabelAnalysis(
861     SemanticsContext &context, const parser::Program &program) {
862   ParseTreeAnalyzer analysis{context};
863   Walk(program, analysis);
864   return analysis;
865 }
866 
867 bool InBody(const parser::CharBlock &position,
868     const std::pair<parser::CharBlock, parser::CharBlock> &pair) {
869   if (position.begin() >= pair.first.begin()) {
870     if (position.begin() < pair.second.end()) {
871       return true;
872     }
873   }
874   return false;
875 }
876 
877 LabeledStatementInfoTuplePOD GetLabel(
878     const TargetStmtMap &labels, const parser::Label &label) {
879   const auto iter{labels.find(label)};
880   if (iter == labels.cend()) {
881     return {0u, nullptr, LabeledStmtClassificationSet{}, false};
882   } else {
883     return iter->second;
884   }
885 }
886 
887 // 11.1.7.3
888 void CheckBranchesIntoDoBody(const SourceStmtList &branches,
889     const TargetStmtMap &labels, const IndexList &loopBodies,
890     SemanticsContext &context) {
891   for (const auto &branch : branches) {
892     const auto &label{branch.parserLabel};
893     auto branchTarget{GetLabel(labels, label)};
894     if (HasScope(branchTarget.proxyForScope)) {
895       const auto &fromPosition{branch.parserCharBlock};
896       const auto &toPosition{branchTarget.parserCharBlock};
897       for (const auto &body : loopBodies) {
898         if (!InBody(fromPosition, body) && InBody(toPosition, body)) {
899           context
900               .Say(
901                   fromPosition, "branch into loop body from outside"_warn_en_US)
902               .Attach(body.first, "the loop branched into"_en_US);
903         }
904       }
905     }
906   }
907 }
908 
909 void CheckDoNesting(const IndexList &loopBodies, SemanticsContext &context) {
910   for (auto i1{loopBodies.cbegin()}; i1 != loopBodies.cend(); ++i1) {
911     const auto &v1{*i1};
912     for (auto i2{i1 + 1}; i2 != loopBodies.cend(); ++i2) {
913       const auto &v2{*i2};
914       if (v2.first.begin() < v1.second.end() &&
915           v1.second.begin() < v2.second.begin()) {
916         context.Say(v1.first, "DO loop doesn't properly nest"_err_en_US)
917             .Attach(v2.first, "DO loop conflicts"_en_US);
918       }
919     }
920   }
921 }
922 
923 parser::CharBlock SkipLabel(const parser::CharBlock &position) {
924   const std::size_t maxPosition{position.size()};
925   if (maxPosition && parser::IsDecimalDigit(position[0])) {
926     std::size_t i{1l};
927     for (; (i < maxPosition) && parser::IsDecimalDigit(position[i]); ++i) {
928     }
929     for (; (i < maxPosition) && std::isspace(position[i]); ++i) {
930     }
931     return parser::CharBlock{position.begin() + i, position.end()};
932   }
933   return position;
934 }
935 
936 ProxyForScope ParentScope(
937     const std::vector<ScopeInfo> &scopes, ProxyForScope scope) {
938   return scopes[scope].parent;
939 }
940 
941 void CheckLabelDoConstraints(const SourceStmtList &dos,
942     const SourceStmtList &branches, const TargetStmtMap &labels,
943     const std::vector<ScopeInfo> &scopes, SemanticsContext &context) {
944   IndexList loopBodies;
945   for (const auto &stmt : dos) {
946     const auto &label{stmt.parserLabel};
947     const auto &scope{stmt.proxyForScope};
948     const auto &position{stmt.parserCharBlock};
949     auto doTarget{GetLabel(labels, label)};
950     if (!HasScope(doTarget.proxyForScope)) {
951       // C1133
952       context.Say(
953           position, "Label '%u' cannot be found"_err_en_US, SayLabel(label));
954     } else if (doTarget.parserCharBlock.begin() < position.begin()) {
955       // R1119
956       context.Say(position,
957           "Label '%u' doesn't lexically follow DO stmt"_err_en_US,
958           SayLabel(label));
959 
960     } else if ((InInclusiveScope(scopes, scope, doTarget.proxyForScope) &&
961                    doTarget.labeledStmtClassificationSet.test(
962                        TargetStatementEnum::CompatibleDo)) ||
963         (doTarget.isExecutableConstructEndStmt &&
964             ParentScope(scopes, doTarget.proxyForScope) == scope)) {
965       if (context.warnOnNonstandardUsage() ||
966           context.ShouldWarn(
967               common::LanguageFeature::OldLabelDoEndStatements)) {
968         context
969             .Say(position,
970                 "A DO loop should terminate with an END DO or CONTINUE"_port_en_US)
971             .Attach(doTarget.parserCharBlock,
972                 "DO loop currently ends at statement:"_en_US);
973       }
974     } else if (!InInclusiveScope(scopes, scope, doTarget.proxyForScope)) {
975       context.Say(position, "Label '%u' is not in DO loop scope"_err_en_US,
976           SayLabel(label));
977     } else if (!doTarget.labeledStmtClassificationSet.test(
978                    TargetStatementEnum::Do)) {
979       context.Say(doTarget.parserCharBlock,
980           "A DO loop should terminate with an END DO or CONTINUE"_err_en_US);
981     } else {
982       loopBodies.emplace_back(SkipLabel(position), doTarget.parserCharBlock);
983     }
984   }
985 
986   CheckBranchesIntoDoBody(branches, labels, loopBodies, context);
987   CheckDoNesting(loopBodies, context);
988 }
989 
990 // 6.2.5
991 void CheckScopeConstraints(const SourceStmtList &stmts,
992     const TargetStmtMap &labels, const std::vector<ScopeInfo> &scopes,
993     SemanticsContext &context) {
994   for (const auto &stmt : stmts) {
995     const auto &label{stmt.parserLabel};
996     const auto &scope{stmt.proxyForScope};
997     const auto &position{stmt.parserCharBlock};
998     auto target{GetLabel(labels, label)};
999     if (!HasScope(target.proxyForScope)) {
1000       context.Say(
1001           position, "Label '%u' was not found"_err_en_US, SayLabel(label));
1002     } else if (!InInclusiveScope(scopes, scope, target.proxyForScope)) {
1003       // Clause 11.1.2.1 prohibits transfer of control to the interior of a
1004       // block from outside the block, but this does not apply to formats.
1005       // C1038 and C1034 forbid statements in FORALL and WHERE constructs
1006       // (resp.) from being branch targets.
1007       if (target.labeledStmtClassificationSet.test(
1008               TargetStatementEnum::Format)) {
1009         continue;
1010       }
1011       bool isFatal{false};
1012       ProxyForScope fromScope{scope};
1013       for (ProxyForScope toScope{target.proxyForScope}; fromScope != toScope;
1014            toScope = scopes[toScope].parent) {
1015         if (scopes[toScope].isExteriorGotoFatal) {
1016           isFatal = true;
1017           break;
1018         }
1019         if (scopes[toScope].depth == scopes[fromScope].depth) {
1020           fromScope = scopes[fromScope].parent;
1021         }
1022       }
1023       context.Say(position,
1024           isFatal
1025               ? "Label '%u' is in a construct that prevents its use as a branch target here"_err_en_US
1026               : "Label '%u' is in a construct that should not be used as a branch target here"_warn_en_US,
1027           SayLabel(label));
1028     }
1029   }
1030 }
1031 
1032 void CheckBranchTargetConstraints(const SourceStmtList &stmts,
1033     const TargetStmtMap &labels, SemanticsContext &context) {
1034   for (const auto &stmt : stmts) {
1035     const auto &label{stmt.parserLabel};
1036     auto branchTarget{GetLabel(labels, label)};
1037     if (HasScope(branchTarget.proxyForScope)) {
1038       if (!branchTarget.labeledStmtClassificationSet.test(
1039               TargetStatementEnum::Branch) &&
1040           !branchTarget.labeledStmtClassificationSet.test(
1041               TargetStatementEnum::CompatibleBranch)) { // error
1042         context
1043             .Say(branchTarget.parserCharBlock,
1044                 "Label '%u' is not a branch target"_err_en_US, SayLabel(label))
1045             .Attach(stmt.parserCharBlock, "Control flow use of '%u'"_en_US,
1046                 SayLabel(label));
1047       } else if (!branchTarget.labeledStmtClassificationSet.test(
1048                      TargetStatementEnum::Branch)) { // warning
1049         context
1050             .Say(branchTarget.parserCharBlock,
1051                 "Label '%u' is not a branch target"_warn_en_US, SayLabel(label))
1052             .Attach(stmt.parserCharBlock, "Control flow use of '%u'"_en_US,
1053                 SayLabel(label));
1054       }
1055     }
1056   }
1057 }
1058 
1059 void CheckBranchConstraints(const SourceStmtList &branches,
1060     const TargetStmtMap &labels, const std::vector<ScopeInfo> &scopes,
1061     SemanticsContext &context) {
1062   CheckScopeConstraints(branches, labels, scopes, context);
1063   CheckBranchTargetConstraints(branches, labels, context);
1064 }
1065 
1066 void CheckDataXferTargetConstraints(const SourceStmtList &stmts,
1067     const TargetStmtMap &labels, SemanticsContext &context) {
1068   for (const auto &stmt : stmts) {
1069     const auto &label{stmt.parserLabel};
1070     auto ioTarget{GetLabel(labels, label)};
1071     if (HasScope(ioTarget.proxyForScope)) {
1072       if (!ioTarget.labeledStmtClassificationSet.test(
1073               TargetStatementEnum::Format)) {
1074         context
1075             .Say(ioTarget.parserCharBlock, "'%u' not a FORMAT"_err_en_US,
1076                 SayLabel(label))
1077             .Attach(stmt.parserCharBlock, "data transfer use of '%u'"_en_US,
1078                 SayLabel(label));
1079       }
1080     }
1081   }
1082 }
1083 
1084 void CheckDataTransferConstraints(const SourceStmtList &dataTransfers,
1085     const TargetStmtMap &labels, const std::vector<ScopeInfo> &scopes,
1086     SemanticsContext &context) {
1087   CheckScopeConstraints(dataTransfers, labels, scopes, context);
1088   CheckDataXferTargetConstraints(dataTransfers, labels, context);
1089 }
1090 
1091 void CheckAssignTargetConstraints(const SourceStmtList &stmts,
1092     const TargetStmtMap &labels, SemanticsContext &context) {
1093   for (const auto &stmt : stmts) {
1094     const auto &label{stmt.parserLabel};
1095     auto target{GetLabel(labels, label)};
1096     if (HasScope(target.proxyForScope) &&
1097         !target.labeledStmtClassificationSet.test(
1098             TargetStatementEnum::Branch) &&
1099         !target.labeledStmtClassificationSet.test(
1100             TargetStatementEnum::Format)) {
1101       context
1102           .Say(target.parserCharBlock,
1103               target.labeledStmtClassificationSet.test(
1104                   TargetStatementEnum::CompatibleBranch)
1105                   ? "Label '%u' is not a branch target or FORMAT"_warn_en_US
1106                   : "Label '%u' is not a branch target or FORMAT"_err_en_US,
1107               SayLabel(label))
1108           .Attach(stmt.parserCharBlock, "ASSIGN statement use of '%u'"_en_US,
1109               SayLabel(label));
1110     }
1111   }
1112 }
1113 
1114 void CheckAssignConstraints(const SourceStmtList &assigns,
1115     const TargetStmtMap &labels, const std::vector<ScopeInfo> &scopes,
1116     SemanticsContext &context) {
1117   CheckScopeConstraints(assigns, labels, scopes, context);
1118   CheckAssignTargetConstraints(assigns, labels, context);
1119 }
1120 
1121 bool CheckConstraints(ParseTreeAnalyzer &&parseTreeAnalysis) {
1122   auto &context{parseTreeAnalysis.ErrorHandler()};
1123   for (const auto &programUnit : parseTreeAnalysis.ProgramUnits()) {
1124     const auto &dos{programUnit.doStmtSources};
1125     const auto &branches{programUnit.otherStmtSources};
1126     const auto &labels{programUnit.targetStmts};
1127     const auto &scopes{programUnit.scopeModel};
1128     CheckLabelDoConstraints(dos, branches, labels, scopes, context);
1129     CheckBranchConstraints(branches, labels, scopes, context);
1130     const auto &dataTransfers{programUnit.formatStmtSources};
1131     CheckDataTransferConstraints(dataTransfers, labels, scopes, context);
1132     const auto &assigns{programUnit.assignStmtSources};
1133     CheckAssignConstraints(assigns, labels, scopes, context);
1134   }
1135   return !context.AnyFatalError();
1136 }
1137 
1138 bool ValidateLabels(SemanticsContext &context, const parser::Program &program) {
1139   return CheckConstraints(LabelAnalysis(context, program));
1140 }
1141 } // namespace Fortran::semantics
1142