summaryrefslogtreecommitdiff
path: root/lib/AST/ASTDumper.cpp
blob: ddc57e8453e2dc6c81effd1fe8116f76d29bc1d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
//===--- ASTDumper.cpp - Dump Fortran AST --------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//  This file dumps AST.
//
//===----------------------------------------------------------------------===//

#include "flang/AST/Expr.h"
#include "flang/AST/Stmt.h"
#include "flang/AST/ExprVisitor.h"
#include "flang/AST/StmtVisitor.h"
#include "flang/AST/DeclVisitor.h"
#include "flang/AST/Type.h"
#include "flang/AST/TypeVisitor.h"
#include "flang/AST/FormatItem.h"
#include "flang/Basic/LLVM.h"
#include "llvm/Support/raw_ostream.h"
using namespace flang;

namespace {

class ASTDumper : public ConstStmtVisitor<ASTDumper>,
  public ConstExprVisitor<ASTDumper>,
  public ConstDeclVisitor<ASTDumper>,
  public TypeVisitor<ASTDumper> {
  raw_ostream &OS;

  int indent;
public:
  ASTDumper(raw_ostream &os) : OS(os), indent(0) {}

  // utilities
  void dumpCompoundPartStart(const char *Name);
  void dumpCompoundPartEnd();
  void dumpIndent();

  // declarations
  void dumpDecl(const Decl *D);
  void dumpDeclContext(const DeclContext *Ctx);
  void VisitTranslationUnitDecl(const TranslationUnitDecl *D);
  void VisitMainProgramDecl(const MainProgramDecl *D);
  void VisitFunctionDecl(const FunctionDecl *D);
  void VisitVarDecl(const VarDecl *D);

  // types
  void dumpType(QualType T);
  void VisitBuiltinType(const BuiltinType *T, Qualifiers QS);
  void VisitCharacterType(const CharacterType *T, Qualifiers QS);
  void VisitArrayType(const ArrayType *T, Qualifiers QS);
  void VisitFunctionType(const FunctionType *T, Qualifiers QS);
  void VisitRecordType(const RecordType *T, Qualifiers QS);

  // statements
  void dumpStmt(const Stmt *S);
  void dumpSubStmt(const Stmt *S);
  void dumpConstructNamePrefix(ConstructName Name);
  void dumpConstructNameSuffix(ConstructName Name);

  void VisitConstructPartStmt(const ConstructPartStmt *S);
  void VisitDeclStmt(const DeclStmt *S);
  void VisitCompoundStmt(const CompoundStmt *S);
  void VisitProgramStmt(const ProgramStmt *S);
  void VisitParameterStmt(const ParameterStmt *S);
  void VisitImplicitStmt(const ImplicitStmt *S);
  void VisitDimensionStmt(const DimensionStmt *S);
  void VisitExternalStmt(const ExternalStmt *S);
  void VisitIntrinsicStmt(const IntrinsicStmt *S);
  void VisitSaveStmt(const SaveStmt *S);
  void VisitEquivalenceStmt(const EquivalenceStmt *S);
  void VisitDataStmt(const DataStmt *S);
  void VisitBlockStmt(const BlockStmt *S);
  void VisitAssignStmt(const AssignStmt *S);
  void VisitAssignedGotoStmt(const AssignedGotoStmt *S);
  void VisitGotoStmt(const GotoStmt *S);
  void VisitComputedGotoStmt(const ComputedGotoStmt *S);
  void VisitIfStmt(const IfStmt *S);
  void VisitDoStmt(const DoStmt *S);
  void VisitDoWhileStmt(const DoWhileStmt *S);
  void VisitCycleStmt(const CycleStmt *S);
  void VisitExitStmt(const ExitStmt *S);
  void VisitSelectCaseStmt(const SelectCaseStmt *S);
  void VisitCaseStmt(const CaseStmt *S);
  void VisitDefaultCaseStmt(const DefaultCaseStmt *S);
  void VisitWhereStmt(const WhereStmt *S);
  void VisitContinueStmt(const ContinueStmt *S);
  void VisitStopStmt(const StopStmt *S);
  void VisitReturnStmt(const ReturnStmt *S);
  void VisitCallStmt(const CallStmt *S);
  void VisitAssignmentStmt(const AssignmentStmt *S);
  void VisitPrintStmt(const PrintStmt *S);
  void VisitWriteStmt(const WriteStmt *S);
  void VisitFormatStmt(const FormatStmt *S);

  // expressions
  void dumpExpr(const Expr *E);
  void dumpExprList(ArrayRef<Expr*> List);
  void dumpExprOrNull(const Expr *E) {
    if(E) dumpExpr(E);
  }

  void VisitIntegerConstantExpr(const IntegerConstantExpr *E);
  void VisitRealConstantExpr(const RealConstantExpr *E);
  void VisitComplexConstantExpr(const ComplexConstantExpr *E);
  void VisitCharacterConstantExpr(const CharacterConstantExpr *E);
  void VisitBOZConstantExpr(const BOZConstantExpr *E);
  void VisitLogicalConstantExpr(const LogicalConstantExpr *E);
  void VisitRepeatedConstantExpr(const RepeatedConstantExpr *E);
  void VisitVarExpr(const VarExpr *E);
  void VisitUnresolvedIdentifierExpr(const UnresolvedIdentifierExpr *E);
  void VisitUnaryExpr(const UnaryExpr *E);
  void VisitDefinedUnaryOperatorExpr(const DefinedUnaryOperatorExpr *E);
  void VisitImplicitCastExpr(const ImplicitCastExpr *E);
  void VisitBinaryExpr(const BinaryExpr *E);
  void VisitDefinedBinaryOperatorExpr(const DefinedBinaryOperatorExpr *E);
  void VisitMemberExpr(const MemberExpr *E);
  void VisitSubstringExpr(const SubstringExpr *E);
  void VisitArrayElementExpr(const ArrayElementExpr *E);
  void VisitArraySectionExpr(const ArraySectionExpr *E);
  void VisitImplicitArrayPackExpr(const ImplicitArrayPackExpr *E);
  void VisitImplicitTempArrayExpr(const ImplicitTempArrayExpr *E);
  void VisitCallExpr(const CallExpr *E);
  void VisitIntrinsicCallExpr(const IntrinsicCallExpr *E);
  void VisitImpliedDoExpr(const ImpliedDoExpr *E);
  void VisitArrayConstructorExpr(const ArrayConstructorExpr *E);
  void VisitTypeConstructorExpr(const TypeConstructorExpr *E);
  void VisitRangeExpr(const RangeExpr *E);
  void VisitStridedRangeExpr(const StridedRangeExpr *E);

  // array specification
  void dumpArraySpec(const ArraySpec *S);

};

} // end anonymous namespace

// utilities


void ASTDumper::dumpCompoundPartStart(const char *Name) {
  OS << Name << ' ';
}

void ASTDumper::dumpCompoundPartEnd() {
  OS << "\n";
}

void ASTDumper::dumpIndent() {
  for(int i = 0; i < indent; ++i)
    OS << "  ";
}

// declarations

void ASTDumper::dumpDecl(const Decl *D) {
  dumpIndent();
  ConstDeclVisitor<ASTDumper>::Visit(D);
}

void ASTDumper::dumpDeclContext(const DeclContext *Ctx) {
  auto I = Ctx->decls_begin();
  for(auto E = Ctx->decls_end(); I!=E; ++I) {
    if((*I)->getDeclContext() == Ctx)
      dumpDecl(*I);
  }
}

void ASTDumper::VisitTranslationUnitDecl(const TranslationUnitDecl *D) {
  dumpDeclContext(D);
}

void ASTDumper::VisitMainProgramDecl(const MainProgramDecl *D) {
  OS << "program " << D->getName() << "\n";
  indent++;
  dumpDeclContext(D);
  indent--;
  if(D->getBody())
    dumpSubStmt(D->getBody());
}

void ASTDumper::VisitFunctionDecl(const FunctionDecl *D) {
  if(!D->getType().isNull()) {
    D->getType().print(OS);
    OS << ' ';
  }
  OS << (D->isNormalFunction()? "function " : (D->isStatementFunction()? "stmt function " : "subroutine "))
     << D->getName() << "(";
  auto Args = D->getArguments();
  for(size_t I = 0; I < Args.size(); ++I) {
    if(I) OS << ", ";
    OS << cast<VarDecl>(Args[I])->getName();
  }

  if(D->isStatementFunction()) {
    OS << ") = ";
    if(D->getBodyExpr())
      dumpExpr(D->getBodyExpr());
    OS << "\n";
  } else {
    OS << ")\n";
    indent++;
    dumpDeclContext(D);
    indent--;
    if(D->getBody())
      dumpSubStmt(D->getBody());
  }
}

void ASTDumper::VisitVarDecl(const VarDecl *D) {
  if(!D->getType().isNull()) {
    D->getType().print(OS);
    OS << ' ';
  }
  OS << D->getName();
  if(D->hasInit()) {
    OS << " = ";
    dumpExpr(D->getInit());
  }
  OS << "\n";
}

// types

void ASTDumper::dumpType(QualType T) {
  TypeVisitor::Visit(T);

  /*
   * FIXME: Print out declarations.
#define PRINT_QUAL(Q, QNAME) \
  do {                                                      \
    if (Quals.hasAttributeSpec(Qualifiers::Q)) {            \
      if (Comma) OS << ", "; Comma = true;                  \
      OS << QNAME;                                          \
    }                                                       \
  } while (0)

  Qualifiers Quals = EQ->getQualifiers();
  PRINT_QUAL(AS_allocatable,  "ALLOCATABLE");
  PRINT_QUAL(AS_asynchronous, "ASYNCHRONOUS");
  PRINT_QUAL(AS_codimension,  "CODIMENSION");
  PRINT_QUAL(AS_contiguous,   "CONTIGUOUS");
  PRINT_QUAL(AS_external,     "EXTERNAL");
  PRINT_QUAL(AS_intrinsic,    "INTRINSIC");
  PRINT_QUAL(AS_optional,     "OPTIONAL");
  PRINT_QUAL(AS_parameter,    "PARAMETER");
  PRINT_QUAL(AS_pointer,      "POINTER");
  PRINT_QUAL(AS_protected,    "PROTECTED");
  PRINT_QUAL(AS_save,         "SAVE");
  PRINT_QUAL(AS_target,       "TARGET");
  PRINT_QUAL(AS_value,        "VALUE");
  PRINT_QUAL(AS_volatile,     "VOLATILE");

  if (Quals.hasIntentAttr()) {
    if (Comma) OS << ", "; Comma = true;
    OS << "INTENT(";
    switch (Quals.getIntentAttr()) {
    default: assert(false && "Invalid intent attribute"); break;
    case Qualifiers::IS_in:    OS << "IN"; break;
    case Qualifiers::IS_out:   OS << "OUT"; break;
    case Qualifiers::IS_inout: OS << "INOUT"; break;
    }
    OS << ")";
  }

  if (Quals.hasAccessAttr()) {
    if (Comma) OS << ", "; Comma = true;
    switch (Quals.getAccessAttr()) {
    default: assert(false && "Invalid access attribute"); break;
    case Qualifiers::AC_public:  OS << "PUBLIC";  break;
    case Qualifiers::AC_private: OS << "PRIVATE"; break;
    }
    OS << ")";
  } */
}

void ASTDumper::VisitBuiltinType(const BuiltinType *T, Qualifiers QS) {
  if(T->isDoublePrecisionKindSpecified()) {
    if(T->isRealType())
      OS << "double precision";
    else OS << "double complex";
  } else if(T->isByteKindSpecified()) {
    OS << "byte";
  } else {
    switch (T->getTypeSpec()) {
    default: assert(false && "Invalid built-in type!");
    case BuiltinType::Integer:
      OS << "integer";
      break;
    case BuiltinType::Real:
      OS << "real";
      break;
    case BuiltinType::Complex:
      OS << "complex";
      break;
    case BuiltinType::Logical:
      OS << "logical";
      break;
    }
  }

  if(T->isKindExplicitlySpecified()) {
    OS << " (Kind=" << BuiltinType::getTypeKindString(T->getBuiltinTypeKind());
    OS << ")";
  }
}

void ASTDumper::VisitCharacterType(const CharacterType *T, Qualifiers QS) {
  OS << "character";
  if(T->hasLength() && T->getLength() > 1)
    OS << " (Len=" << T->getLength() << ")";
}

void ASTDumper::VisitArrayType(const ArrayType *T, Qualifiers QS) {
  dumpType(T->getElementType());
  OS << " array";
}

void ASTDumper::VisitFunctionType(const FunctionType *T, Qualifiers QS) {
  OS << "procedure (";
  if(T->hasPrototype())
    OS << T->getPrototype()->getName();
  OS << ")";
}

void ASTDumper::VisitRecordType(const RecordType *T, Qualifiers QS) {
  auto Record = T->getDecl();
  OS << "type " << Record->getName();
}

// statements

void ASTDumper::dumpStmt(const Stmt *S) {
  dumpIndent();
  ConstStmtVisitor<ASTDumper>::Visit(S);
}

void ASTDumper::dumpSubStmt(const Stmt *S) {
  if(isa<BlockStmt>(S))
    dumpStmt(S);
  else {
    ++indent;
    dumpStmt(S);
    --indent;
  }
}

void ASTDumper::dumpConstructNamePrefix(ConstructName Name) {
  if(Name.isUsable())
    OS << Name.IDInfo->getName() << ": ";
}

void ASTDumper::dumpConstructNameSuffix(ConstructName Name) {
  if(Name.isUsable())
    OS << " " << Name.IDInfo->getName();
}

void ASTDumper::VisitConstructPartStmt(const ConstructPartStmt *S) {
  switch(S->getConstructStmtClass()) {
  case ConstructPartStmt::EndStmtClass: OS << "end"; break;
  case ConstructPartStmt::EndProgramStmtClass: OS << "end program"; break;
  case ConstructPartStmt::EndFunctionStmtClass: OS << "end function"; break;
  case ConstructPartStmt::EndSubroutineStmtClass: OS << "end subroutine"; break;
  case ConstructPartStmt::ElseStmtClass: OS << "else"; break;
  case ConstructPartStmt::EndIfStmtClass: OS << "end if"; break;
  case ConstructPartStmt::EndDoStmtClass: OS << "end do"; break;
  case ConstructPartStmt::EndSelectStmtClass: OS << "end select"; break;
  case ConstructPartStmt::ElseWhereStmtClass: OS << "else where"; break;
  case ConstructPartStmt::EndWhereStmtClass: OS << "end where"; break;
  default: break;
  }
  dumpConstructNameSuffix(S->getName());
  OS << "\n";
}

void ASTDumper::VisitDeclStmt(const DeclStmt *S) {
  dumpDecl(S->getDeclaration());
  OS << "\n";
}

void ASTDumper::VisitCompoundStmt(const CompoundStmt *S) {
  auto Body = S->getBody();
  for(size_t I = 0; I < Body.size(); ++I) {
    if(I) OS<<"&";
    dumpStmt(Body[I]);
  }
}

void ASTDumper::VisitProgramStmt(const ProgramStmt *S) {
  const IdentifierInfo *Name = S->getProgramName();
  OS << "program";
  if (Name) OS << ":  '" << Name->getName() << "'";
  OS << "\n";
}

void ASTDumper::VisitParameterStmt(const ParameterStmt *S) {
  dumpCompoundPartStart("parameter");
  OS << S->getIdentifier()->getName() << " = ";
  dumpExpr(S->getValue());
  dumpCompoundPartEnd();
}

void ASTDumper::VisitImplicitStmt(const ImplicitStmt *S) {
  OS << "implicit";
  if (S->isNone()) {
    OS << " none\n";
    return;
  }
  OS << " ";
  S->getType().print(OS);
  OS << " :: ";

  auto Spec = S->getLetterSpec();
  OS << " (" << Spec.first->getName();
  if (Spec.second)
    OS << "-" << Spec.second->getName();
  OS << ")\n";
}

void ASTDumper::VisitDimensionStmt(const DimensionStmt *S) {
  OS << "dimension " << S->getVariableName()->getNameStart()
     << "\n";
}

void ASTDumper::VisitExternalStmt(const ExternalStmt *S) {
  dumpCompoundPartStart("external");
  OS << S->getIdentifier()->getName();
  dumpCompoundPartEnd();
}

void ASTDumper::VisitIntrinsicStmt(const IntrinsicStmt *S) {
  dumpCompoundPartStart("intrinsic");
  OS << S->getIdentifier()->getName();
  dumpCompoundPartEnd();
}

void ASTDumper::VisitSaveStmt(const SaveStmt *S) {
  dumpCompoundPartStart("save");
  if(S->getIdentifier())
    OS << S->getIdentifier()->getName();
  dumpCompoundPartEnd();
}

void ASTDumper::VisitEquivalenceStmt(const EquivalenceStmt *S) {
  dumpCompoundPartStart("equivalence");
  OS << "(";
  dumpExprList(S->getObjects());
  OS << ") ";
  dumpCompoundPartEnd();
}

void ASTDumper::VisitDataStmt(const DataStmt *S) {
  OS << "data ";
  dumpExprList(S->getObjects());
  OS << " / ";
  dumpExprList(S->getValues());
  OS << " / \n";
}

void ASTDumper::VisitBlockStmt(const BlockStmt *S) {
  indent++;
  auto Body = S->getStatements();
  for(size_t I = 0; I < Body.size(); ++I) {
    auto S = Body[I];
    if(isa<ConstructPartStmt>(S) && I == Body.size()-1){
      indent--;
      dumpStmt(S);
      return;
    }
    dumpStmt(S);
  }
  indent--;
}

void ASTDumper::VisitAssignStmt(const AssignStmt *S) {
  OS << "assign ";
  if(S->getAddress().Statement)
    dumpExpr(S->getAddress().Statement->getStmtLabel());
  OS << " to ";
  dumpExpr(S->getDestination());
  OS << "\n";
}

void ASTDumper::VisitAssignedGotoStmt(const AssignedGotoStmt *S) {
  OS << "goto ";
  dumpExpr(S->getDestination());
  OS << "\n";
}

void ASTDumper::VisitGotoStmt(const GotoStmt *S) {
  OS << "goto ";
  if(S->getDestination().Statement)
    dumpExpr(S->getDestination().Statement->getStmtLabel());
  OS << "\n";
}

void ASTDumper::VisitComputedGotoStmt(const ComputedGotoStmt *S) {
  OS << "goto (";
  auto Targets = S->getTargets();
  for(size_t I = 0; I < Targets.size(); ++I) {
    if(I) OS << ", ";
    if(Targets[I].Statement)
      dumpExpr(Targets[I].Statement->getStmtLabel());
  }
  OS << ") ";
  if(S->getExpression())
    dumpExpr(S->getExpression());
  OS << "\n";
}

void ASTDumper::VisitIfStmt(const IfStmt* S) {
  dumpConstructNamePrefix(S->getName());
  OS << "if ";
  dumpExpr(S->getCondition());
  OS << "\n";

  if(S->getThenStmt())
    dumpSubStmt(S->getThenStmt());
  if(S->getElseStmt())
    dumpSubStmt(S->getElseStmt());
}

void ASTDumper::VisitDoStmt(const DoStmt *S) {
  dumpConstructNamePrefix(S->getName());
  OS<<"do ";
  if(S->getTerminatingStmt().Statement) {
    dumpExpr(S->getTerminatingStmt().Statement->getStmtLabel());
    OS << " ";
  }
  dumpExpr(S->getDoVar());
  OS << " = ";
  dumpExpr(S->getInitialParameter());
  OS << ", ";
  dumpExpr(S->getTerminalParameter());
  if(S->getIncrementationParameter()) {
    OS << ", ";
    dumpExpr(S->getIncrementationParameter());
  }
  OS << "\n";
  if(S->getBody())
    dumpSubStmt(S->getBody());
}

void ASTDumper::VisitDoWhileStmt(const DoWhileStmt *S) {
  dumpConstructNamePrefix(S->getName());
  OS << "do while(";
  dumpExpr(S->getCondition());
  OS << ")\n";
  if(S->getBody())
    dumpSubStmt(S->getBody());
}

void ASTDumper::VisitCycleStmt(const CycleStmt *S) {
  OS << "cycle";
  if(S->getLoopName().isUsable())
    OS << ' ' << S->getLoopName().IDInfo->getName();
  OS << "\n";
}

void ASTDumper::VisitExitStmt(const ExitStmt *S) {
  OS << "exit";
  if(S->getLoopName().isUsable())
    OS << ' ' << S->getLoopName().IDInfo->getName();
  OS << "\n";
}

void ASTDumper::VisitSelectCaseStmt(const SelectCaseStmt *S) {
  dumpConstructNamePrefix(S->getName());
  OS << "select case(";
  dumpExprOrNull(S->getOperand());
  OS << ")\n";
  if(S->getBody())
    dumpSubStmt(S->getBody());
}

void ASTDumper::VisitCaseStmt(const CaseStmt *S) {
  OS << "case (";
  dumpExprList(S->getValues());
  OS << ")";
  dumpConstructNameSuffix(S->getName());
  OS << "\n";
  if(S->getBody())
    dumpSubStmt(S->getBody());
}

void ASTDumper::VisitDefaultCaseStmt(const DefaultCaseStmt *S) {
  OS << "case default";
  dumpConstructNameSuffix(S->getName());
  OS << "\n";
  if(S->getBody())
    dumpSubStmt(S->getBody());
}

void ASTDumper::VisitWhereStmt(const WhereStmt *S) {
  OS << "where (";
  dumpExprOrNull(S->getMask());
  OS << ")\n";
  if(S->getThenStmt())
    dumpSubStmt(S->getThenStmt());
  if(S->getElseStmt())
    dumpSubStmt(S->getElseStmt());
}

void ASTDumper::VisitContinueStmt(const ContinueStmt *S) {
  OS << "continue\n";
}

void ASTDumper::VisitStopStmt(const StopStmt *S) {
  OS << "stop ";
  dumpExprOrNull(S->getStopCode());
  OS << "\n";
}

void ASTDumper::VisitReturnStmt(const ReturnStmt *S) {
  OS << "return ";
  dumpExprOrNull(S->getE());
  OS << "\n";
}

void ASTDumper::VisitCallStmt(const CallStmt *S) {
  OS << "call " << S->getFunction()->getName() << '(';
  dumpExprList(S->getArguments());
  OS << ")\n";
}

void ASTDumper::VisitAssignmentStmt(const AssignmentStmt *S) {
  dumpExprOrNull(S->getLHS());
  OS << " = ";
  dumpExprOrNull(S->getRHS());
  OS << "\n";
}

void ASTDumper::VisitPrintStmt(const PrintStmt *S) {
  OS << "print ";
  dumpExprList(S->getOutputList());
  OS << "\n";
}

void ASTDumper::VisitWriteStmt(const WriteStmt *S) {
  OS << "write ";
  dumpExprList(S->getOutputList());
  OS << "\n";
}

void ASTDumper::VisitFormatStmt(const FormatStmt *S) {
  OS << "format ";
  S->getItemList()->print(OS);
  if(S->getUnlimitedItemList())
    S->getUnlimitedItemList()->print(OS);
  OS << "\n";
}

// expressions

void ASTDumper::dumpExpr(const Expr *E) {
  ConstExprVisitor<ASTDumper>::Visit(E);
}

void ASTDumper::dumpExprList(ArrayRef<Expr*> List) {
  for(size_t I = 0; I < List.size(); ++I) {
    if(I) OS << ", ";
    if(List[I])
      dumpExpr(List[I]);
  }
}

void ASTDumper::VisitIntegerConstantExpr(const IntegerConstantExpr *E) {
  OS << E->getValue();
}

void ASTDumper::VisitRealConstantExpr(const RealConstantExpr *E)  {
  llvm::SmallVector<char, 32> Str;
  E->getValue().toString(Str);
  Str.push_back('\0');
  OS << Str.begin();
}

void ASTDumper::VisitComplexConstantExpr(const ComplexConstantExpr *E)  {
  OS << '(';
  dumpExpr(E->getRealPart());
  OS << ',';
  dumpExpr(E->getImPart());
  OS << ')';
}

void ASTDumper::VisitCharacterConstantExpr(const CharacterConstantExpr *E)  {
  OS << "'" << E->getValue() << "'";
}

void ASTDumper::VisitBOZConstantExpr(const BOZConstantExpr *E) {

}

void ASTDumper::VisitLogicalConstantExpr(const LogicalConstantExpr *E)  {
  OS << (E->isTrue()? "true" : "false");
}

void ASTDumper::VisitRepeatedConstantExpr(const RepeatedConstantExpr *E)  {
  OS << E->getRepeatCount() << "*";
  dumpExpr(E->getExpression());
}

void ASTDumper::VisitVarExpr(const VarExpr *E) {
  OS << E->getVarDecl()->getName();
}

void ASTDumper::VisitUnresolvedIdentifierExpr(const UnresolvedIdentifierExpr *E) {
  OS << E->getIdentifier()->getName();
}

void ASTDumper::VisitUnaryExpr(const UnaryExpr *E) {
  OS << '(';
  const char *op = "";
  switch (E->getOperator()) {
  default: break;
  case UnaryExpr::Not:   op = ".NOT."; break;
  case UnaryExpr::Plus:  op = "+";     break;
  case UnaryExpr::Minus: op = "-";     break;
  }
  OS << op;
  dumpExpr(E->getExpression());
  OS << ')';
}

void ASTDumper::VisitDefinedUnaryOperatorExpr(const DefinedUnaryOperatorExpr *E) {
  OS << '(' << E->getIdentifierInfo()->getName();
  dumpExpr(E->getExpression());
  OS << ')';
}

void ASTDumper::VisitImplicitCastExpr(const ImplicitCastExpr *E) {
  auto Type = E->getType().getSelfOrArrayElementType();
  if(Type->isIntegerType())
    OS << "int(";
  else if(Type->isRealType())
    OS << "real(";
  else if(Type->isComplexType())
    OS << "cmplx(";
  else if(Type->isLogicalType())
    OS << "logical(";
  else {
    dumpType(Type);
    OS << "(";
  }
  dumpExpr(E->getExpression());
  if(auto BTy = Type->asBuiltinType()) {
    if(BTy->isKindExplicitlySpecified() || BTy->isDoublePrecisionKindSpecified())
       OS << ",Kind=" << BuiltinType::getTypeKindString(BTy->getBuiltinTypeKind());
  }
  OS << ')';
}

void ASTDumper::VisitBinaryExpr(const BinaryExpr *E) {
  OS << '(';
  dumpExpr(E->getLHS());
  const char *op = 0;
  switch (E->getOperator()) {
  default: break;
  case BinaryExpr::Eqv:              op = ".EQV.";  break;
  case BinaryExpr::Neqv:             op = ".NEQV."; break;
  case BinaryExpr::Or:               op = ".OR.";   break;
  case BinaryExpr::And:              op = ".AND.";  break;
  case BinaryExpr::Equal:            op = "==";     break;
  case BinaryExpr::NotEqual:         op = "/=";     break;
  case BinaryExpr::LessThan:         op = "<";      break;
  case BinaryExpr::LessThanEqual:    op = "<=";     break;
  case BinaryExpr::GreaterThan:      op = ">";      break;
  case BinaryExpr::GreaterThanEqual: op = ">=";     break;
  case BinaryExpr::Concat:           op = "//";     break;
  case BinaryExpr::Plus:             op = "+";      break;
  case BinaryExpr::Minus:            op = "-";      break;
  case BinaryExpr::Multiply:         op = "*";      break;
  case BinaryExpr::Divide:           op = "/";      break;
  case BinaryExpr::Power:            op = "**";     break;
  }
  OS << op;
  dumpExpr(E->getRHS());
  OS << ')';
}

void ASTDumper::VisitDefinedBinaryOperatorExpr(const DefinedBinaryOperatorExpr *E) {
  OS << '(';
  dumpExpr(E->getLHS());
  OS << E->getIdentifierInfo()->getName();
  dumpExpr(E->getRHS());
  OS << ')';
}

void ASTDumper::VisitMemberExpr(const MemberExpr *E) {
  dumpExpr(E->getTarget());
  OS << " % " << E->getField()->getName();
}

void ASTDumper::VisitSubstringExpr(const SubstringExpr *E) {
  dumpExpr(E->getTarget());
  OS << '(';
  dumpExprOrNull(E->getStartingPoint());
  OS << ':';
  dumpExprOrNull(E->getEndPoint());
  OS << ')';
}

void ASTDumper::VisitArrayElementExpr(const ArrayElementExpr *E) {
  dumpExpr(E->getTarget());
  OS << '(';
  dumpExprList(E->getArguments());
  OS << ')';
}

void ASTDumper::VisitArraySectionExpr(const ArraySectionExpr *E) {
  dumpExpr(E->getTarget());
  OS << '(';
  dumpExprList(E->getArguments());
  OS << ')';
}

void ASTDumper::VisitImplicitArrayPackExpr(const ImplicitArrayPackExpr *E) {
  OS << "ImplicitArrayPackExpr(";
  dumpExpr(E->getExpression());
  OS << ")";
}

void ASTDumper::VisitImplicitTempArrayExpr(const ImplicitTempArrayExpr *E) {
  OS << "ImplicitTempArrayExpr(";
  dumpExpr(E->getExpression());
  OS << ")";
}

void ASTDumper::VisitCallExpr(const CallExpr *E) {
  OS << E->getFunction()->getName() << '(';
  dumpExprList(E->getArguments());
  OS << ')';
}

void ASTDumper::VisitIntrinsicCallExpr(const IntrinsicCallExpr *E) {
  OS << intrinsic::getFunctionName(E->getIntrinsicFunction()) << '(';
  dumpExprList(E->getArguments());
  OS << ')';
}

void ASTDumper::VisitImpliedDoExpr(const ImpliedDoExpr *E) {
  OS << '(';
  dumpExprList(E->getBody());
  OS << ", " << E->getVarDecl()->getIdentifier()->getName();
  OS << " = ";
  dumpExpr(E->getInitialParameter());
  OS << ", ";
  dumpExpr(E->getTerminalParameter());
  if(E->getIncrementationParameter()) {
     OS << ", ";
     dumpExpr(E->getIncrementationParameter());
  }
  OS << ')';
}

void ASTDumper::VisitArrayConstructorExpr(const ArrayConstructorExpr *E) {
  OS << "(/";
  dumpExprList(E->getItems());
  OS << " /)";
}

void ASTDumper::VisitTypeConstructorExpr(const TypeConstructorExpr *E) {
  OS << E->getRecord()->getName() << "(";
  dumpExprList(E->getArguments());
  OS << ")";
}

void ASTDumper::VisitRangeExpr(const RangeExpr *E) {
  dumpExprOrNull(E->getFirstExpr());
  OS << ":";
  dumpExprOrNull(E->getSecondExpr());
}

void ASTDumper::VisitStridedRangeExpr(const StridedRangeExpr *E) {
  VisitRangeExpr(E);
  OS << ":";
  dumpExprOrNull(E->getStride());
}

// array specification
void ASTDumper::dumpArraySpec(const ArraySpec *S) {
  if(auto Explicit = dyn_cast<ExplicitShapeSpec>(S)) {
    if(Explicit->getLowerBound()) {
      dumpExpr(Explicit->getLowerBound());
      OS << ':';
    }
    dumpExpr(Explicit->getUpperBound());
  } else if(auto Implied = dyn_cast<ImpliedShapeSpec>(S)) {
    if(Implied->getLowerBound()) {
      dumpExpr(Implied ->getLowerBound());
      OS << ':';
    }
    OS << '*';
  } else OS << "<unknown array spec>";
}

namespace flang {

void Decl::dump() const {
  dump(llvm::errs());
}
void Decl::dump(llvm::raw_ostream &OS) const {
  ASTDumper SV(OS);
  SV.dumpDecl(this);
}

void QualType::dump() const {
  print(llvm::errs());
}

void QualType::print(raw_ostream &OS) const {
  ASTDumper SV(OS);
  SV.dumpType(*this);
}

void Stmt::dump() const {
  dump(llvm::errs());
}
void Stmt::dump(llvm::raw_ostream &OS) const {
  ASTDumper SV(OS);
  SV.dumpStmt(this);
}

void Expr::dump() const {
  dump(llvm::errs());
}
void Expr::dump(llvm::raw_ostream &OS) const {
  ASTDumper SV(OS);
  SV.dumpExpr(this);
}

void ArraySpec::dump() const {
  dump(llvm::errs());
}
void ArraySpec::dump(llvm::raw_ostream &OS) const {
  ASTDumper SV(OS);
  SV.dumpArraySpec(this);
}

}