-
Notifications
You must be signed in to change notification settings - Fork 1
/
unparseFortran_statements.C
executable file
·6169 lines (5136 loc) · 239 KB
/
unparseFortran_statements.C
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
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* unparseFortran_statements.C
*
* Code to unparse Sage/Fortran statement nodes.
*
*/
#include "sage3basic.h"
#include "unparser.h"
#include <limits>
// DQ (10/14/2010): This should only be included by source files that require it.
// This fixed a reported bug which caused conflicts with autoconf macros (e.g. PACKAGE_BUGREPORT).
// Interestingly it must be at the top of the list of include files.
#include "rose_config.h"
#ifdef _MSC_VER
#define strncasecmp _strnicmp
#endif
using namespace std;
using namespace Rose;
inline bool
namesMatch ( const string &x, const string &y )
{
// This function checks a case insensitive match of x against y.
// This is required because Fortran is case insensitive.
size_t x_length = x.length();
size_t y_length = y.length();
ROSE_ASSERT(x_length > 0 && y_length > 0);
return (x_length == y_length) ? strncasecmp(x.c_str(),y.c_str(),x_length) == 0 : false;
}
FortranCodeGeneration_locatedNode::FortranCodeGeneration_locatedNode(Unparser* unp, std::string fname)
: UnparseLanguageIndependentConstructs(unp,fname)
{
// Nothing to do here!
}
FortranCodeGeneration_locatedNode::~FortranCodeGeneration_locatedNode()
{
// Nothing to do here!
}
// void FortranCodeGeneration_locatedNode::unparseStatementNumbersSupport ( int numeric_label )
// void FortranCodeGeneration_locatedNode::unparseStatementNumbersSupport ( SgLabelSymbol* numeric_label_symbol )
// void FortranCodeGeneration_locatedNode::unparseStatementNumbersSupport ( SgLabelSymbol* numeric_label_symbol, SgUnparse_Info& info )
void
FortranCodeGeneration_locatedNode::unparseStatementNumbersSupport ( SgLabelRefExp* numeric_label_exp, SgUnparse_Info& info )
{
// This is a supporting function for the unparseStatementNumbers, but can be called directly for statments
// in the IR that can have botha starting yntax and an ending syntax, both of which can be labeled.
// See test2007_01.f90 for an example of the SgProgramHeaderStatement used this way.
// In fixed format all labels must appear within columns 1-5 (where column 1 is the first column)
// and the 6th column is for the line continuation character (any character, I think).
const int NumericLabelIndentation = 6;
if (info.SkipFormatting() == true)
{
return;
}
// Let the default be fixed format for now (just for fun)
bool fixedFormat = (unp->currentFile==NULL) ||
(unp->currentFile->get_outputFormat() == SgFile::e_unknown_output_format) ||
(unp->currentFile->get_outputFormat() == SgFile::e_fixed_form_output_format);
// if (numeric_label_symbol != NULL)
if (numeric_label_exp != NULL)
{
// ASSERT_not_null(numeric_label_exp);
SgLabelSymbol* numeric_label_symbol = numeric_label_exp->get_symbol();
int numeric_label = numeric_label_symbol->get_numeric_label_value();
// printf ("In unparseStatementNumbers: numeric_label = %d \n",numeric_label);
ROSE_ASSERT(numeric_label >= -1);
// DQ (12/24/2007): I think that this value is an error in all versions of Fortran
ROSE_ASSERT(numeric_label != 0);
// If it is greater than zero then output the value converted to a string.
if (numeric_label >= 0)
{
// A label exists in the source code
string numeric_label_string = StringUtility::numberToString(numeric_label);
// append an extra blank to seperate the lable from other code (if fixedFormat == true
// then this puts a blank into column 6 as required for this to be a code statement).
numeric_label_string += " ";
if (fixedFormat == true)
{
// Now indent the statement so that it will appear uniform (just for fun!)
int spacing = numeric_label_string.size();
while (spacing < NumericLabelIndentation)
{
// prepend the extra blanks to right justify the numeric labels
// (we have to fill the space anyway and this makes them look nice).
numeric_label_string = " " + numeric_label_string;
spacing++;
}
}
// printf ("In unparseStatementNumbers: numeric_label_string = %s \n",numeric_label_string.c_str());
curprint( numeric_label_string );
}
else
{
if (fixedFormat == true)
{
// if fixed format then output 6 blanks
curprint(" ");
}
}
}
else
{
if (fixedFormat == true)
{
// if fixed format then output 6 blanks
curprint(" ");
}
}
}
void
FortranCodeGeneration_locatedNode::unparseStatementNumbers ( SgStatement* stmt, SgUnparse_Info& info )
{
// This is a virtual function (called by the UnparseLanguageIndependentConstructs::unparseStatement() member function).
// printf ("In unparseStatementNumbers(): stmt = %p = %s \n",stmt,stmt->class_name().c_str());
// This is a Fortran specific case (different from use of SgLabelStatement in C/C++).
// unparseStatementNumbersSupport(stmt->get_numeric_label(),info);
// DQ (11/29/2008): If this is a CPP directive then don't output statement
// number or the white space for then in fixed format mode.
if (isSgC_PreprocessorDirectiveStatement(stmt) != NULL)
{
printf ("This is a CPP directive, skip leading white space in unparsing. \n");
return;
}
// This fixes a formatting problem, an aspect fo which was reported by Liao 12/28/2007).
if ( isSgGlobal(stmt) != NULL || isSgBasicBlock(stmt) != NULL )
{
// Skip any formatting since these don't result in statements that are output!
}
else
{
SgProgramHeaderStatement* program_header = isSgProgramHeaderStatement(stmt);
if (program_header != NULL)
{
if (program_header->get_name() != ROSE_IMPLICIT_FORTRAN_PROGRAM_NAME)
{
// If this is a program name that will be output then format the start
// of the output (in case there is a label or this is fixed format).
unparseStatementNumbersSupport(stmt->get_numeric_label(),info);
}
}
else
{
// This is a Fortran specific case (different from use of SgLabelStatement in C/C++).
unparseStatementNumbersSupport(stmt->get_numeric_label(),info);
}
}
// The default value is -1 and any non-negative value is allowed as a label
// ROSE_ASSERT(stmt->get_numeric_label() >= -1);
}
void
FortranCodeGeneration_locatedNode::unparseLanguageSpecificStatement(SgStatement* stmt, SgUnparse_Info& info)
{
// This function unparses the language specific parse not handled by the base class unparseStatement() member function
ASSERT_not_null(stmt);
#if 0
printf ("In FortranCodeGeneration_locatedNode::unparseLanguageSpecificStatement ( stmt = %p = %s ) language = %s \n",stmt,stmt->class_name().c_str(),languageName().c_str());
#endif
// DQ (11/17/2007): Add numeric lables where they apply, this is called in UnparseLanguageIndependentConstructs::unparseStatement().
// unparseStatementNumbers(stmt);
switch (stmt->variantT())
{
// program units
// case V_SgModuleStatement: unparseModuleStmt(stmt, info); break;
case V_SgProgramHeaderStatement: unparseProgHdrStmt(stmt, info); break;
case V_SgProcedureHeaderStatement: unparseProcHdrStmt(stmt, info); break;
// declarations
case V_SgInterfaceStatement: unparseInterfaceStmt(stmt, info); break;
case V_SgCommonBlock: unparseCommonBlock(stmt, info); break;
case V_SgVariableDeclaration: unparseVarDeclStmt(stmt, info); break;
case V_SgVariableDefinition: unparseVarDefnStmt(stmt, info); break;
case V_SgParameterStatement: unparseParamDeclStmt(stmt, info); break;
case V_SgUseStatement: unparseUseStmt(stmt, info); break;
// DQ (8/25/2007): Added to support Fortran derived types
case V_SgDerivedTypeStatement: unparseClassDeclStmt_derivedType(stmt, info); break;
case V_SgModuleStatement: unparseClassDeclStmt_module(stmt, info); break;
case V_SgClassDefinition: unparseClassDefnStmt(stmt, info); break;
// executable statements, control flow
case V_SgBasicBlock: unparseBasicBlockStmt(stmt, info); break;
case V_SgIfStmt: unparseIfStmt(stmt, info); break;
case V_SgFortranDo: unparseDoStmt(stmt, info); break;
case V_SgSwitchStatement: unparseSwitchStmt(stmt, info); break;
case V_SgCaseOptionStmt: unparseCaseStmt(stmt, info); break;
case V_SgDefaultOptionStmt: unparseDefaultStmt(stmt, info); break;
case V_SgProcessControlStatement: unparseProcessControlStmt(stmt, info); break;
// executable statements, IO
// case V_SgIOStatement: unparseIOStmt(stmt, info); break;
// DQ (11/25/2007): These are derived from SgIOStatement
case V_SgPrintStatement: unparsePrintStatement(stmt, info); break;
case V_SgReadStatement: unparseReadStatement(stmt, info); break;
case V_SgWriteStatement: unparseWriteStatement(stmt, info); break;
case V_SgOpenStatement: unparseOpenStatement(stmt, info); break;
case V_SgCloseStatement: unparseCloseStatement(stmt, info); break;
case V_SgInquireStatement: unparseInquireStatement(stmt, info); break;
case V_SgFlushStatement: unparseFlushStatement(stmt, info); break;
case V_SgRewindStatement: unparseRewindStatement(stmt, info); break;
case V_SgBackspaceStatement: unparseBackspaceStatement(stmt, info); break;
case V_SgEndfileStatement: unparseEndfileStatement(stmt, info); break;
case V_SgWaitStatement: unparseWaitStatement(stmt, info); break;
// Rasmussen (9/21/2018): These are derived from SgImageControlStatement
case V_SgSyncAllStatement: unparseSyncAllStatement(stmt, info); break;
case V_SgSyncImagesStatement: unparseSyncImagesStatement(stmt, info); break;
case V_SgSyncMemoryStatement: unparseSyncMemoryStatement(stmt, info); break;
case V_SgSyncTeamStatement: unparseSyncTeamStatement(stmt, info); break;
case V_SgLockStatement: unparseLockStatement(stmt, info); break;
case V_SgUnlockStatement: unparseUnlockStatement(stmt, info); break;
// DQ (11/30/2007): Added support for associate statement (F2003)
case V_SgAssociateStatement: unparseAssociateStatement(stmt, info); break;
// DQ (11/25/2007): This has now been eliminated
// case V_SgIOControlStatement: unparse_IO_ControlStatement(stmt, info); break;
// case V_SgIOFileControlStmt: unparseIOFileControlStatement(stmt, info);break;
// DQ (8/22/2007): We have made unparsing of a SgFunctionDeclaration C/C++ specific, and
// defined derived classes for SgProgramHeaderStatement and SgProcedureHeaderStatement objects.
// case V_SgFunctionDeclaration: unparseFuncDeclStmt(stmt, info); break;
case V_SgFunctionDefinition: unparseFuncDefnStmt(stmt, info); break;
case V_SgExprStatement: unparseExprStmt(stmt, info); break;
// DQ (8/22/2007): New statements
case V_SgImplicitStatement: unparseImplicitStmt(stmt, info); break;
case V_SgBlockDataStatement: unparseBlockDataStmt(stmt, info); break;
case V_SgStatementFunctionStatement: unparseStatementFunctionStmt(stmt, info); break;
case V_SgWhereStatement: unparseWhereStmt(stmt, info); break;
case V_SgElseWhereStatement: unparseElseWhereStmt(stmt, info); break;
case V_SgNullifyStatement: unparseNullifyStmt(stmt, info); break;
case V_SgEquivalenceStatement: unparseEquivalenceStmt(stmt, info); break;
case V_SgArithmeticIfStatement: unparseArithmeticIfStmt(stmt, info); break;
case V_SgAssignStatement: unparseAssignStmt(stmt, info); break;
case V_SgComputedGotoStatement: unparseComputedGotoStmt(stmt, info); break;
case V_SgAssignedGotoStatement: unparseAssignedGotoStmt(stmt, info); break;
// DQ (11/16/2007): This is unparsed as a CONTINUE statement
case V_SgLabelStatement: unparseLabelStmt(stmt, info); break;
// DQ (11/16/2007): This is a "DO WHILE" statement
case V_SgWhileStmt: unparseWhileStmt(stmt, info); break;
// DQ (11/17/2007): This is unparsed as a Fortran EXIT statement
case V_SgBreakStmt: unparseBreakStmt(stmt, info); break;
// DQ (11/17/2007): This is unparsed as a Fortran CYCLE statement
case V_SgContinueStmt: unparseContinueStmt(stmt, info); break;
// DQ (11/17/2007): Added support for Fortran attribute statements.
case V_SgAttributeSpecificationStatement: unparseAttributeSpecificationStatement(stmt, info); break;
// DQ (11/19/2007): Added support for Fortran namelist statement.
case V_SgNamelistStatement: unparseNamelistStatement(stmt, info); break;
// DQ (11/21/2007): Added support for Fortran return statement
case V_SgReturnStmt: unparseReturnStmt(stmt, info); break;
// DQ (11/21/2007): Added support for Fortran return statement
case V_SgImportStatement: unparseImportStatement(stmt, info); break;
// DQ (12/18/2007): Added support for format statement
case V_SgFormatStatement: unparseFormatStatement(stmt, info); break;
case V_SgGotoStatement: unparseGotoStmt(stmt, info); break;
// Rasmussen (10/02/2018): This is temporary fix (actual ForAllStatements aren't created)
case V_SgForAllStatement: unparseForAllStatement(stmt, info); break;
case V_SgContainsStatement: unparseContainsStatement(stmt, info); break;
case V_SgEntryStatement: unparseEntryStatement(stmt, info); break;
case V_SgFortranIncludeLine: unparseFortranIncludeLine(stmt, info); break;
case V_SgAllocateStatement: unparseAllocateStatement(stmt, info); break;
case V_SgDeallocateStatement: unparseDeallocateStatement(stmt, info); break;
case V_SgCAFWithTeamStatement: unparseWithTeamStatement(stmt, info); break;
// Language independent code generation (placed in base class)
// scope
// case V_SgGlobal: unparseGlobalStmt(stmt, info); break;
// case V_SgScopeStatement: unparseScopeStmt(stmt, info); break;
// case V_SgWhileStmt: unparseWhileStmt(stmt, info); break;
// case V_SgLabelStatement: unparseLabelStmt(stmt, info); break;
// case V_SgGotoStatement: unparseGotoStmt(stmt, info); break;
// executable statements, other
// case V_SgExprStatement: unparseExprStmt(stmt, info); break;
// Liao 10/18/2010, I turn on the pragma unparsing here to help debugging OpenMP programs
// , where OpenMP directive comments are used to generate C/C++-like pragmas internally.
// Those pragmas later are used to reuse large portion of OpenMP AST construction of C/C++
// pragmas
case V_SgPragmaDeclaration: unparsePragmaDeclStmt(stmt, info); break;
// Liao 10/21/2010, Fortran-only OpenMP handling
case V_SgOmpDoStatement: unparseOmpDoStatement(stmt, info); break;
#if 0
// Optional support for unparsing Fortran from C
case V_SgFunctionDeclaration: unparseProcHdrStmt(stmt, info); break;
#endif
default:
{
printf("FortranCodeGeneration_locatedNode::unparseLanguageSpecificStatement: Error: No unparse function for %s (variant: %d)\n",stmt->sage_class_name(), stmt->variantT());
ROSE_ABORT();
}
}
}
void
FortranCodeGeneration_locatedNode::unparseFortranIncludeLine (SgStatement* stmt, SgUnparse_Info& info)
{
// This is support for the language specific include mechanism.
if (info.outputFortranModFile()) // rmod file expands the include file but does not contain the include statement
return;
SgFortranIncludeLine* includeLine = isSgFortranIncludeLine(stmt);
curprint("include ");
// DQ (10/3/2008): Added special case code generation to support an inconsistant
// behavior between gfortran 4.2 and previous versions in the Fortran include mechanism.
string includeFileName = includeLine->get_filename();
#if USE_GFORTRAN_IN_ROSE
bool usingGfortran = false;
#ifdef USE_CMAKE
#ifdef CMAKE_COMPILER_IS_GNUG77
usingGfortran = true;
#endif
#else
string fortranCompilerName = BACKEND_FORTRAN_COMPILER_NAME_WITH_PATH;
usingGfortran = (fortranCompilerName == "gfortran");
#endif
if (usingGfortran)
{
// DQ (3/17/2017): Fixed this to support GNU 5.1.
if ( (BACKEND_FORTRAN_COMPILER_MAJOR_VERSION_NUMBER == 3) ||
( (BACKEND_FORTRAN_COMPILER_MAJOR_VERSION_NUMBER == 4) && (BACKEND_FORTRAN_COMPILER_MINOR_VERSION_NUMBER <= 1) ) )
{
// gfortran versions before 4.2 can not handle absolute path names in the Fortran specific include mechanism.
// Note that this fix would mistakenly strip all specified include files to their basename, even include files
// specified as "../sys/math.h" would become "math.h" and this could cause an error.
printf ("Warning: gfortran versions before 4.2 can not handle absolute path names in the Fortran specific include mechanism (using basename)... \n");
includeFileName = StringUtility::stripPathFromFileName(includeLine->get_filename());
}
}
else
{
// What is this compiler
printf ("Default compiler behavior ... in code generation (Fortran include uses absolute paths) \n");
// ROSE_ASSERT(false);
}
#endif
// printf ("Unparsing Fortran include using includeFileName = %s \n",includeFileName.c_str());
curprint("\"");
curprint(includeFileName);
curprint("\"");
unp->cur.insert_newline(1);
}
void
FortranCodeGeneration_locatedNode::unparseEntryStatement (SgStatement* stmt, SgUnparse_Info& info)
{
// This is much like a function declaration inside of an existing function
SgEntryStatement* entryStatement = isSgEntryStatement(stmt);
curprint("entry ");
curprint(entryStatement->get_name());
curprint("(");
unparseFunctionArgs(entryStatement,info);
curprint(")");
// Unparse the result(<name>) suffix if present
if (entryStatement->get_result_name() != NULL)
{
curprint(" result(");
curprint(entryStatement->get_result_name()->get_name());
curprint(")");
}
unp->cur.insert_newline(1);
}
void
FortranCodeGeneration_locatedNode::unparseContainsStatement (SgStatement* stmt, SgUnparse_Info& info)
{
curprint("CONTAINS");
unp->cur.insert_newline(1);
}
// DQ (11/19/2007): support for type attributes when used as statements.
void
FortranCodeGeneration_locatedNode::unparseNamelistStatement (SgStatement* stmt, SgUnparse_Info& info)
{
SgNamelistStatement* namelistStatement = isSgNamelistStatement(stmt);
curprint("namelist ");
SgNameGroupPtrList & groupList = namelistStatement->get_group_list();
SgNameGroupPtrList::iterator i = groupList.begin();
while (i != groupList.end())
{
SgNameGroup* nameGroup = *i;
curprint ("/" + nameGroup->get_group_name() + "/ ");
SgStringList & nameList = nameGroup->get_name_list();
SgStringList::iterator j = nameList.begin();
while (j != nameList.end())
{
curprint (*j);
j++;
if (j != nameList.end())
{
curprint (",");
}
}
i++;
// Put a little space before the next group name (it there are multiple groups specified)
if (i != groupList.end())
{
curprint(" ");
}
}
unp->cur.insert_newline(1);
}
// DQ (12/18/2007): support for format statement
void
FortranCodeGeneration_locatedNode::unparseFormatItemList (SgFormatItemList* formatItemList, SgUnparse_Info& info)
{
SgFormatItemPtrList & formatList = formatItemList->get_format_item_list();
SgFormatItemPtrList::iterator i = formatList.begin();
while (i != formatList.end())
{
bool skip_comma = false;
SgFormatItem* formatItem = *i;
// The default value is "-1" so zero should be an invalid value
int repeat_specification = formatItem->get_repeat_specification();
ROSE_ASSERT(repeat_specification != 0);
// Valid values are > 0
if (repeat_specification > 0)
{
string stringValue = StringUtility::numberToString(repeat_specification);
curprint(stringValue);
curprint(" ");
}
// ASSERT_not_null(formatItem->get_data());
if (formatItem->get_data() != NULL)
{
SgStringVal* stringValue = isSgStringVal(formatItem->get_data());
ASSERT_not_null(stringValue);
// The string is stored without quotes, and we put them back on as required in code generation
string str;
if (stringValue->get_usesSingleQuotes() == true)
{
str = string("\'") + stringValue->get_value() + string("\'");
}
else
{
if (stringValue->get_usesDoubleQuotes() == true)
{
str = string("\"") + stringValue->get_value() + string("\"");
}
else
{
// Normally if usesSingleQuotes == false we use double quotes, but that would be
// a mistake since this is not a string literal used in the format statement.
// At some point we want to classify this, since it is a specific kind of edit
// descriptor (see R1005, R1011, R1013, R1015, R1016, R1017, R1018).
str = stringValue->get_value();
}
}
curprint(str);
}
else
{
if (formatItem->get_format_item_list() != NULL)
{
curprint("(");
unparseFormatItemList(formatItem->get_format_item_list(),info);
curprint(")");
}
else
{
// This is the case of "format (10/)" which processes "10" and "/" seperately (I think this is a bug, see test2007_241.f).
printf ("Error: both get_data() and get_format_item_list() are NULL \n");
// ROSE_ASSERT(false);
// In this case we want to avoid "10,/" to be output!
skip_comma = true;
}
}
i++;
if (i != formatList.end() && skip_comma == false )
{
curprint (",");
}
}
}
void
FortranCodeGeneration_locatedNode::unparseFormatStatement (SgStatement* stmt, SgUnparse_Info& info)
{
// Note that we use a SgStringVal in the SgFormatItem to hold a string which is not really
// interpreted as a literal in the Fortram grammar (I think).
SgFormatStatement* formatStatement = isSgFormatStatement(stmt);
curprint("format ( ");
#if 0
SgFormatItemPtrList & formatList = formatStatement->get_format_item_list();
SgFormatItemPtrList::iterator i = formatList.begin();
while (i != formatList.end())
{
SgFormatItem* formatItem = *i;
ASSERT_not_null(formatItem->get_data());
SgStringVal* stringValue = isSgStringVal(formatItem->get_data());
ASSERT_not_null(stringValue);
// The string is stored without quotes, and we put them back on as required in code generation
string str;
if (stringValue->get_usesSingleQuotes() == true)
{
str = string("\'") + stringValue->get_value() + string("\'");
}
else
{
// Noremally if usesSingleQuotes == false we use double quotes, but that would be
// a mistake since this is not a string literal used in the format statement.
str = stringValue->get_value();
}
curprint(str);
i++;
if (i != formatList.end())
{
curprint (",");
}
}
#else
#if 1
SgFormatItemList* formatItemList = formatStatement->get_format_item_list();
unparseFormatItemList(formatItemList,info);
#else
SgFormatItemPtrList & formatList = formatStatement->get_format_item_list()->get_format_item_list();
SgFormatItemPtrList::iterator i = formatList.begin();
while (i != formatList.end())
{
SgFormatItem* formatItem = *i;
// The default value is "-1" so zero should be an invalid value
ROSE_ASSERT(formatItem->get_repeat_specifier() != 0);
// Valid values are > 0
if (formatItem->get_repeat_specifier() > 0)
{
string stringValue = StringUtility::numberToString(formatItem->get_repeat_specifier());
curprint(stringValue);
curprint(" ");
}
// ASSERT_not_null(formatItem->get_data());
if (formatItem->get_data() != NULL)
{
SgStringVal* stringValue = isSgStringVal(formatItem->get_data());
ASSERT_not_null(stringValue);
// The string is stored without quotes, and we put them back on as required in code generation
string str;
if (stringValue->get_usesSingleQuotes() == true)
{
str = string("\'") + stringValue->get_value() + string("\'");
}
else
{
if (stringValue->get_usesDoubleQuotes() == true)
{
str = string("\"") + stringValue->get_value() + string("\"");
}
else
{
// Normally if usesSingleQuotes == false we use double quotes, but that would be
// a mistake since this is not a string literal used in the format statement.
// At some point we want to classify this, since it is a specific kind of edit
// descriptor (see R1005, R1011, R1013, R1015, R1016, R1017, R1018).
str = stringValue->get_value();
}
}
curprint(str);
i++;
if (i != formatList.end())
{
curprint (",");
}
}
else
{
if (formatItem->get_format_item_list() != NULL)
{
format_item_list
}
else
{
printf ("Error: both get_data() and get_format_item_list() are NULL \n");
ROSE_ABORT();
}
}
}
#endif
#endif
curprint(" )");
unp->cur.insert_newline(1);
}
// DQ (11/19/2007): support for type attributes when used as statements.
void
FortranCodeGeneration_locatedNode::unparseImportStatement (SgStatement* stmt, SgUnparse_Info& info)
{
SgImportStatement* importStatement = isSgImportStatement(stmt);
SgExpressionPtrList & importList = importStatement->get_import_list();
SgExpressionPtrList::iterator i = importList.begin();
curprint("import ");
if (importList.size() > 0) curprint(":: ");
while (i != importList.end())
{
unparseExpression(*i,info);
i++;
// Put a little space before the next name (it there are multiple names specified)
if (i != importList.end())
{
curprint(", ");
}
}
unp->cur.insert_newline(1);
}
bool
unparseDimensionStatementForArrayVariable( SgPntrArrRefExp* arrayReference )
{
// If an array variable has an explicit variable declaration (in the code) then the dimension
// information will be output there. If not then we have to output the dimension statement
// and an entry for this variable.
ASSERT_not_null(arrayReference);
SgVarRefExp* variableReference = isSgVarRefExp(arrayReference->get_lhs_operand());
ASSERT_not_null(variableReference);
SgVariableSymbol* variableSymbol = variableReference->get_symbol();
ASSERT_not_null(variableSymbol);
SgInitializedName* variableName = variableSymbol->get_declaration();
ASSERT_not_null(variableName);
// printf ("variableName = %p = %s \n",variableName,variableName->get_name().str());
// variableName->get_file_info()->display("variableName: unparseDimensionStatementForArrayVariable");
SgVariableDeclaration* variableDeclaration = isSgVariableDeclaration(variableName->get_parent());
// If there is a SgVariableDeclaration then it is simpler to look for it in the scope,
// else we have to look at each variable declaration for the SgInitializedName (which
// is only more expensive).
bool foundArrayVariableDeclaration = false;
if (variableDeclaration != NULL)
{
SgScopeStatement* variableScope = variableDeclaration->get_scope();
switch(variableScope->variantT())
{
case V_SgBasicBlock:
{
SgBasicBlock* basicBlock = isSgBasicBlock(variableScope);
SgStatementPtrList statementList = basicBlock->get_statements();
SgStatementPtrList::iterator i = find(statementList.begin(),statementList.end(),variableDeclaration);
foundArrayVariableDeclaration = (i != statementList.end());
break;
}
default:
{
printf ("Default reached, variableScope = %p = %s \n",variableScope,variableScope->class_name().c_str());
ROSE_ABORT();
}
}
}
else
{
// There was no variable declaration found though the symbol, so we have to look
// for the SgInitializedName in each SgVariableDeclaration. However there will be
// at least two (and hopefully no more) SgInitializedName objects for a function
// parameter if it also has an explicit declaration in a SgVariableDeclaration.
// It would be cleaner to have one, and it could be consistant with old Style K&R C,
// however it is not clear if this would be a problem for where we would visit the
// IR node twice in the traversals. Need to look at the implementation of the old
// style C function parameter handling.
// printf ("There was no variable declaration found though the symbol, so we have to look for the SgInitializedName in each SgVariableDeclaration \n");
SgScopeStatement* variableScope = variableName->get_scope();
SgFunctionDefinition* functionDefinition = isSgFunctionDefinition (variableScope);
ASSERT_not_null(functionDefinition);
// SgFunctionDeclaration* functionDeclaration = functionDefinition->get_declaration();
SgBasicBlock* basicBlock = functionDefinition->get_body();
ASSERT_not_null(basicBlock);
SgStatementPtrList statementList = basicBlock->get_statements();
SgStatementPtrList::iterator i = statementList.begin();
while (i != statementList.end())
{
SgVariableDeclaration* variableDeclaration = isSgVariableDeclaration(*i);
if (variableDeclaration != NULL)
{
SgInitializedNamePtrList & variableList = variableDeclaration->get_variables();
SgInitializedNamePtrList::iterator i = find(variableList.begin(),variableList.end(),variableName);
foundArrayVariableDeclaration = (i != variableList.end());
}
i++;
}
#if 0
printf ("Exiting as a test! \n");
ROSE_ABORT();
#endif
}
// printf ("foundArrayVariableDeclaration = %s \n",foundArrayVariableDeclaration ? "true" : "false");
// variableDeclaration->get_file_info()->display("variableDeclaration: unparseDimensionStatementForArrayVariable");
// return variableDeclaration->get_file_info()->isCompilerGenerated();
// If we found the variation declaration then we do NOT need to output the dimension
// statement (since the array will be dimensioned in the variable declaration).
return (foundArrayVariableDeclaration == false);
}
bool
unparseDimensionStatement(SgStatement* stmt)
{
// DQ (12/9/2007): If the dimension statement is what declares a variable (array) then we need it,
// else it is redundant (and an error) when used with the dimensioning specification in the variable
// declaration (which will be built from the type information in the variable declaration.
SgAttributeSpecificationStatement* attributeSpecificationStatement = isSgAttributeSpecificationStatement(stmt);
bool unparseDimensionStatementResult = false;
ROSE_ASSERT(attributeSpecificationStatement->get_attribute_kind() == SgAttributeSpecificationStatement::e_dimensionStatement);
#if 0
SgDimensionObjectPtrList & dimensionObjectList = attributeSpecificationStatement->get_dimension_object_list();
// printf ("dimensionObjectList.size() = %" PRIuPTR " \n",dimensionObjectList.size());
SgDimensionObjectPtrList::iterator i_object = dimensionObjectList.begin();
while (i_object != dimensionObjectList.end())
{
// Output the array name
// printf ("case e_dimensionStatement: Array name = %s \n",(*i_object)->get_array()->get_name().str());
// The SgDimensionObject should store a variable reference instead of a stringafied name!
printf ("The SgDimensionObject should store a variable reference instead of a stringified name! \n");
SgName name = (*i_object)->get_array()->get_name();
SgScopeStatement* currentScope = attributeSpecificationStatement->get_scope();
ASSERT_not_null(currentScope);
SgVariableSymbol* variableSymbol = currentScope->lookup_variable_symbol(name);
if (variableSymbol == NULL)
{
// This is a function parameter, so get the function scope and look for the symbol there
// attributeSpecificationStatement->get_file_info()->display("Error: variableSymbol == NULL");
SgScopeStatement* functionScope = TransformationSupport::getFunctionDefinition(currentScope);
ASSERT_not_null(functionScope);
variableSymbol = functionScope->lookup_variable_symbol(name);
// If this was a function parameter then unparse the dimension statement
unparseDimensionStatementResult = true;
}
ASSERT_not_null(variableSymbol);
SgInitializedName* initializedName = variableSymbol->get_declaration();
ASSERT_not_null(initializedName);
SgNode* parentNode = initializedName->get_parent();
// printf ("unparsing dimension statement: parentNode = %s \n",parentNode->class_name().c_str());
SgVariableDeclaration* variableDeclaration = isSgVariableDeclaration(parentNode);
if (variableDeclaration != NULL)
{
variableDeclaration->get_startOfConstruct()->display("Is this compiler generated");
// Iterate over all the variables.
for (unsigned long i=0; i < variableDeclaration->get_variables().size(); i++)
{
// if (variableDeclaration->get_startOfConstruct()->isSourcePositionUnavailableInFrontend() == true)
if (variableDeclaration->get_variables()[i]->get_startOfConstruct()->isSourcePositionUnavailableInFrontend() == true)
{
// This was not a part of the original source code so the dimension statement must be put out!
unparseDimensionStatementResult = true;
}
}
}
i_object++;
}
#else
ASSERT_not_null(attributeSpecificationStatement->get_parameter_list());
SgExpressionPtrList & parameterList = attributeSpecificationStatement->get_parameter_list()->get_expressions();
SgExpressionPtrList::iterator i = parameterList.begin();
// Loop over the array variables and see if there is an explicit declaration for it.
// If so then the dimension information will be output in the associated SgVariableDeclaration.
while (i != parameterList.end())
{
SgPntrArrRefExp* arrayReference = isSgPntrArrRefExp(*i);
ASSERT_not_null(arrayReference);
bool unparseForArrayVariable = unparseDimensionStatementForArrayVariable(arrayReference);
// printf ("unparseForArrayVariable = %s \n",unparseForArrayVariable ? "true" : "false");
if (unparseForArrayVariable == true)
unparseDimensionStatementResult = true;
i++;
}
// unparseDimensionStatementResult = true;
#endif
// printf ("unparseDimensionStatementResult = %s \n",unparseDimensionStatementResult ? "true" : "false");
return unparseDimensionStatementResult;
}
void
FortranCodeGeneration_locatedNode::unparseAttributeSpecificationStatement(SgStatement* stmt, SgUnparse_Info& info)
{
SgAttributeSpecificationStatement* attributeSpecificationStatement = isSgAttributeSpecificationStatement(stmt);
if (attributeSpecificationStatement->get_attribute_kind() == SgAttributeSpecificationStatement::e_dimensionStatement)
{
// The dimension statement will have changed the type and the original declaration will have been
// output with the dimension computed as part of the type. The only exception is that there may
// have been no explicit declaration (only an implicit declaration from the dimension statement).
// DQ (12/9/2007):
// This test checks if we will need a dimension statement, we still might not want all entries in
// the dimension statement to be unparsed (because some, but not all, might have appeared in an
// explicit declaration previously. I hate this part of Fortran!
// printf ("This is a dimension statement \n");
if (unparseDimensionStatement(stmt) == false)
{
// Output the new line so that we leave a hole where the dimension statement was and don't
// screwup the formatting of the labels (in columns 1-6)
// curprint("! Skipping output of dimension statement (handled in declaration)");
unp->cur.insert_newline(1);
return;
}
else
{
// printf ("Unparsing the dimension statement \n");
}
}
string name;
switch(attributeSpecificationStatement->get_attribute_kind())
{
case SgAttributeSpecificationStatement::e_unknown_attribute_spec: name = "unknown_attribute"; break;
case SgAttributeSpecificationStatement::e_accessStatement_private:name = "private"; break;
case SgAttributeSpecificationStatement::e_accessStatement_public: name = "public"; break;
case SgAttributeSpecificationStatement::e_allocatableStatement: name = "allocatable"; break;
case SgAttributeSpecificationStatement::e_asynchronousStatement: name = "asynchronous"; break;
case SgAttributeSpecificationStatement::e_bindStatement: name = "bind"; break;
case SgAttributeSpecificationStatement::e_dataStatement: name = "data"; break;
case SgAttributeSpecificationStatement::e_dimensionStatement: name = "dimension"; break;
case SgAttributeSpecificationStatement::e_externalStatement: name = "external"; break;
case SgAttributeSpecificationStatement::e_intentStatement: name = "intent"; break;
case SgAttributeSpecificationStatement::e_intrinsicStatement: name = "intrinsic"; break;
case SgAttributeSpecificationStatement::e_optionalStatement: name = "optional"; break;
case SgAttributeSpecificationStatement::e_parameterStatement: name = "parameter"; break;
case SgAttributeSpecificationStatement::e_pointerStatement: name = "pointer"; break;
case SgAttributeSpecificationStatement::e_protectedStatement: name = "protected"; break;
case SgAttributeSpecificationStatement::e_saveStatement: name = "save"; break;
case SgAttributeSpecificationStatement::e_targetStatement: name = "target"; break;
case SgAttributeSpecificationStatement::e_valueStatement: name = "value"; break;
case SgAttributeSpecificationStatement::e_volatileStatement: name = "volatile"; break;
case SgAttributeSpecificationStatement::e_last_attribute_spec: name = "last_attribute"; break;
default:
{
printf ("Error: default reached %d \n",attributeSpecificationStatement->get_attribute_kind());
ROSE_ABORT();
}
}
curprint(name);
if (attributeSpecificationStatement->get_attribute_kind() == SgAttributeSpecificationStatement::e_intentStatement)
{
// This define is copied from OFP actionEnum.h This needs to be better handled later (using a proper enum type).
#define IntentSpecBase 600
#ifndef _MSC_VER
// tps (02/02/2010) : error C2513: 'const int' : no variable declared before '='
const int IN = IntentSpecBase+0;
const int OUT = IntentSpecBase+1;
const int INOUT = IntentSpecBase+2;
string intentString;
switch(attributeSpecificationStatement->get_intent())
{
case IN: intentString = "in"; break;
case OUT: intentString = "out"; break;
case INOUT: intentString = "inout"; break;
default:
{
printf ("Error: default reached attributeSpecificationStatement->get_intent() = %d \n",attributeSpecificationStatement->get_intent());
ROSE_ABORT();
}
}
curprint("(" + intentString + ")");
#endif
}
// The parameter statement is a bit different from the other attribute statements (perhaps enough for it to be it's own IR node.
if (attributeSpecificationStatement->get_attribute_kind() == SgAttributeSpecificationStatement::e_parameterStatement)