Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 90d2e084e9e714fd82f9892977dd7c1d9b024d1f (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
/*******************************************************************************
 * Copyright (c) 2009 University of Illinois at Urbana-Champaign and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    UIUC - Initial API and implementation
 *******************************************************************************/
package org.eclipse.photran.internal.core.refactoring.infrastructure;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.RefactoringStatusContext;
import org.eclipse.photran.core.IFortranAST;
import org.eclipse.photran.internal.core.FortranAST;
import org.eclipse.photran.internal.core.FortranCorePlugin;
import org.eclipse.photran.internal.core.analysis.binding.Definition;
import org.eclipse.photran.internal.core.analysis.binding.ScopingNode;
import org.eclipse.photran.internal.core.analysis.loops.ASTProperLoopConstructNode;
import org.eclipse.photran.internal.core.analysis.loops.LoopReplacer;
import org.eclipse.photran.internal.core.lexer.ASTLexerFactory;
import org.eclipse.photran.internal.core.lexer.IAccumulatingLexer;
import org.eclipse.photran.internal.core.lexer.Terminal;
import org.eclipse.photran.internal.core.lexer.Token;
import org.eclipse.photran.internal.core.lexer.Token.FakeToken;
import org.eclipse.photran.internal.core.parser.ASTAssignmentStmtNode;
import org.eclipse.photran.internal.core.parser.ASTCallStmtNode;
import org.eclipse.photran.internal.core.parser.ASTContainsStmtNode;
import org.eclipse.photran.internal.core.parser.ASTFunctionSubprogramNode;
import org.eclipse.photran.internal.core.parser.ASTImplicitStmtNode;
import org.eclipse.photran.internal.core.parser.ASTMainProgramNode;
import org.eclipse.photran.internal.core.parser.ASTModuleNode;
import org.eclipse.photran.internal.core.parser.ASTNode;
import org.eclipse.photran.internal.core.parser.ASTSubroutineSubprogramNode;
import org.eclipse.photran.internal.core.parser.ASTUseStmtNode;
import org.eclipse.photran.internal.core.parser.ASTVarOrFnRefNode;
import org.eclipse.photran.internal.core.parser.GenericASTVisitor;
import org.eclipse.photran.internal.core.parser.IASTListNode;
import org.eclipse.photran.internal.core.parser.IASTNode;
import org.eclipse.photran.internal.core.parser.IBodyConstruct;
import org.eclipse.photran.internal.core.parser.IExpr;
import org.eclipse.photran.internal.core.parser.IProgramUnit;
import org.eclipse.photran.internal.core.parser.ISpecificationPartConstruct;
import org.eclipse.photran.internal.core.parser.Parser;
import org.eclipse.photran.internal.core.util.IterableWrapper;
import org.eclipse.photran.internal.core.util.Notification;
import org.eclipse.photran.internal.core.vpg.PhotranTokenRef;
import org.eclipse.photran.internal.core.vpg.PhotranVPG;
import org.eclipse.rephraserengine.core.refactorings.IResourceRefactoring;
import org.eclipse.rephraserengine.core.util.OffsetLength;
import org.eclipse.rephraserengine.core.vpg.refactoring.VPGResourceRefactoring;

/**
 * This is a base class for all Photran refactorings that apply to multiple files
 * @author Jeff Overbey, Timofey Yuvashev
 */
public abstract class FortranResourceRefactoring
    extends VPGResourceRefactoring<IFortranAST, Token, PhotranVPG>
    implements IResourceRefactoring
{
    // TEMPORARY -- So we can continue working on fixed form refactoring while effectively disabling it in the public 6.0 release
    public static final boolean FIXED_FORM_REFACTORING_ENABLED = System.getenv("ENABLE_FIXED_FORM_REFACTORING") != null; //$NON-NLS-1$
    
    // TEMPORARY -- So we can continue working on fixed form refactoring while effectively disabling it in the public 6.0 release
    public static final boolean PREPROCESSOR_REFACTORING_ENABLED = System.getenv("ENABLE_PREPROCESSOR_REFACTORING") != null; //$NON-NLS-1$
    
    @Override
    protected final PhotranVPG getVPG()
    {
        return PhotranVPG.getInstance();
    }

    @Override
    protected final void preCheckInitialConditions(RefactoringStatus status, IProgressMonitor pm) throws PreconditionFailure
    {
        //status.addWarning("C preprocessor directives are IGNORED by the refactoring engine.  Use at your own risk.");
     
        if (FIXED_FORM_REFACTORING_ENABLED)
        {
            for (IFile file : this.selectedFiles)
            {
                if (org.eclipse.photran.internal.core.sourceform.SourceForm.isFixedForm(file))
                {
                    status.addWarning(Messages.FortranResourceRefactoring_IndentationAndLineLengthAreNotChecked);
                    return;
                }
            }
        }
    }

    @Override
    protected final String getSourceCodeFromAST(IFortranAST ast)
    {
        return SourcePrinter.getSourceCodeFromAST(ast);
    }

    protected void ensureProjectHasRefactoringEnabled(RefactoringStatus status) throws PreconditionFailure
    {
        if (FortranCorePlugin.inTestingMode()) return;

        HashSet<IFile> filesToBeRemoved = new HashSet<IFile>();

        for (IFile f : this.selectedFiles)
        {
            if (!PhotranVPG.getInstance().doesProjectHaveRefactoringEnabled(f))
            {
                if (f.getProject() == null)
                {
                    status.addWarning(Messages.bind(Messages.FortranResourceRefactoring_FileIsNotInAFortranProject, f.getName()));
                    filesToBeRemoved.add(f);
                }
                else
                {
                    status.addWarning(Messages.bind(Messages.FortranResourceRefactoring_AnalysisRefactoringNotEnabled, f.getProject().getName()));
                    filesToBeRemoved.add(f);
                }
            }
        }
        //Remove files that didn't have Refactoring enabled in their projects
        this.selectedFiles.removeAll(filesToBeRemoved);
    }

    protected void removeFixedFormFilesFrom(Collection<IFile> files, RefactoringStatus status)
    {
        if (FIXED_FORM_REFACTORING_ENABLED) return;
        
        Set<IFile> filesToRemove = new HashSet<IFile>();

        for (IFile file : files)
        {
            if (!filesToRemove.contains(file) && org.eclipse.photran.internal.core.sourceform.SourceForm.isFixedForm(file))
            {
                status.addError(Messages.bind(Messages.FortranResourceRefactoring_FixedFormFileWillNotBeRefactored, file.getName()));
                filesToRemove.add(file);
            }
        }

        files.removeAll(filesToRemove);
    }
    
    protected void removeCpreprocessedFilesFrom(Collection<IFile> files, RefactoringStatus status)
    {
        Set<IFile> filesToRemove = new HashSet<IFile>();

        for (IFile file : files)
        {
            if (!filesToRemove.contains(file) && org.eclipse.photran.internal.core.sourceform.SourceForm.isCPreprocessed(file))
            {
                status.addError(Messages.bind(Messages.FortranResourceRefactoring_CPreprocessedFileWillNotBeRefactored, file.getName()));
                filesToRemove.add(file);
            }
        }

        files.removeAll(filesToRemove);
    }

    ///////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////
    //
    // U T I L I T Y   M E T H O D S
    //
    ///////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////

    // SOURCE FORMATTING //////////////////////////////////////////////////////

    /**
     * @return the whitespace preceding the given token.  Note that {@link Token#getWhiteBefore()}
     *         may return comments and line continuations, while this method only returns everything
     *         <i>following</i> the last <tt>\n</tt> (if any), so those will be omitted.
     */
    protected String extractWhitespacePreceding(Token token)
    {
        String result = token.getWhiteBefore();
        return result.substring(result.lastIndexOf('\n')+1);
    }

    // REFACTORING STATUS /////////////////////////////////////////////////////

    protected RefactoringStatusContext createContext(Token token)
    {
        return createContext(token.getTokenRef());
    }

    // CODE EXTRACTION ////////////////////////////////////////////////////////



    /**
     * Parses the given Fortran statement.
     * <p>
     * Internally, <code>string</code> is embedded into the following program
     * <pre>
     * program p
     *   (string is placed here)
     * end program
     * </pre>
     * which is parsed and its body extracted and returned,
     * so <code>string</code> must "make sense" (syntactically) in this context.
     * No semantic analysis is done; it is only necessary that the
     * program be syntactically correct.
     */
    protected static IBodyConstruct parseLiteralStatement(String string)
    {
        return parseLiteralStatementSequence(string).get(0);
    }

    /**
     * Parses the given Fortran statement, or returns <code>null</code> if the
     * statement cannot be parsed.
     *
     * @see #parseLiteralStatement(String)
     */
    protected static IBodyConstruct parseLiteralStatementNoFail(String string)
    {
        try
        {
            return parseLiteralStatement(string);
        }
        catch (Throwable e)
        {
            return null;
        }
    }

    /**
     * Parses the given Fortran expression.
     * <p>
     * Internally, <code>string</code> is embedded into the following program
     * <pre>
     * program p
     *   x = (string is placed here)
     * end program
     * </pre>
     * which is parsed and the resulting expression extracted and returned,
     * so <code>string</code> must "make sense" (syntactically) in this context.
     * No semantic analysis is done; it is only necessary that the
     * program be syntactically correct.
     */
    protected static IExpr parseLiteralExpression(String string)
    {
        return ((ASTAssignmentStmtNode)parseLiteralStatement("x = " + string)).getRhs(); //$NON-NLS-1$
    }
    
    /**
     * Parses the given list of Fortran statements.
     * <p>
     * @see parseLiteralStatement
     */
    protected static IASTListNode<IBodyConstruct> parseLiteralStatementSequence(String string)
    {
        string = "program p\n" + string + "\nend program"; //$NON-NLS-1$ //$NON-NLS-2$
        return ((ASTMainProgramNode)parseLiteralProgramUnit(string)).getBody();
    }

    /**
     * Parses the given DO-loop as a {@link ASTProperLoopConstructNode}.
     * <p>
     * @see parseLiteralStatement
     */
    protected static ASTProperLoopConstructNode parseLiteralDoLoop(String string)
    {
        string = "program p\n" + string + "\nend program"; //$NON-NLS-1$ //$NON-NLS-2$
        ASTMainProgramNode prog = (ASTMainProgramNode)parseLiteralProgramUnit(string);
        LoopReplacer.replaceAllLoopsIn(prog);
        return (ASTProperLoopConstructNode)prog.getBody().get(0);
    }

    /** @return a CONTAINS statement */
    protected static ASTContainsStmtNode createContainsStmt()
    {
        String string = "program p\ncontains\nsubroutine s\nend subroutine\nend program"; //$NON-NLS-1$
        return ((ASTMainProgramNode)parseLiteralProgramUnit(string)).getContainsStmt();
    }

    /**
     * Parses the given Fortran program unit.
     * <p>
     * No semantic analysis is done; it is only necessary that the
     * program unit be syntactically correct.
     */
    protected static IProgramUnit parseLiteralProgramUnit(String string)
    {
        try
        {
            IAccumulatingLexer lexer = new ASTLexerFactory().createLexer(
                new StringReader(string), null, "(none)"); //$NON-NLS-1$
            Parser parser = new Parser();

            FortranAST ast = new FortranAST(null, parser.parse(lexer), lexer.getTokenList());
            return ast.getRoot().getProgramUnitList().get(0);
        }
        catch (Exception e)
        {
            throw new Error(e);
        }
    }

    // USER INTERACTION ///////////////////////////////////////////////////////

    protected static String describeToken(Token token)
    {
        return "\"" + token.getText() + "\" " + describeTokenPos(token); //$NON-NLS-1$ //$NON-NLS-2$
    }

    protected static String describeTokenPos(Token token)
    {
        return Messages.bind(Messages.FortranResourceRefactoring_LineColumn, token.getLine(), token.getCol());
    }

    // TEXT<->TREE MAPPING ////////////////////////////////////////////////////

    protected static Definition findUnambiguousDeclaration(Token t)
    {
        if(t == null)
            return null;

        List<Definition> defs = t.resolveBinding();
        if(defs.size() <= 0 || defs.size() > 1)
            return null;
        return defs.get(0);
    }

    protected static Token findEnclosingToken(IFortranAST ast, final ITextSelection selection)
    {
        Token prevToken = null;
        for (Token token : new IterableWrapper<Token>(ast))
        {
            if (OffsetLength.contains(token.getFileOffset(), token.getLength(),
                                      selection.getOffset(), selection.getLength()))
            {
                String tokenText = token.getText();
                //If we get whitespace, that means we want the previous token (cursor was put AFTER
                // the identifier we want to rename
                if(tokenText.length() == 1 && Character.isWhitespace(tokenText.charAt(0)))
                {
                    return prevToken;
                }
                else
                    return token;
            }
            prevToken = token;
        }
        return null;
    }

    protected static IASTNode findEnclosingNode(IFortranAST ast, ITextSelection selection)
    {
        Token firstToken = findFirstTokenAfter(ast, selection.getOffset());
        Token lastToken = findLastTokenBefore(ast, OffsetLength.getPositionPastEnd(selection.getOffset(), selection.getLength()));
        if (firstToken == null || lastToken == null) return null;

        for (IASTNode parent = lastToken.getParent(); parent != null; parent = parent.getParent())
            if (contains(parent, firstToken))
                return parent;

        return null;
    }

    @SuppressWarnings("unchecked")
    protected static <T extends IASTNode> T findEnclosingNode(IFortranAST ast, ITextSelection selection, Class<T> type)
    {
        IASTNode node = findEnclosingNode(ast, selection);
        if (node == null) return null;
        
        if (type.isAssignableFrom(node.getClass()))
            return (T)node;
        else
            return node.findNearestAncestor(type);
    }

    protected static boolean nodeExactlyEnclosesRegion(IASTNode parent, Token firstToken, Token lastToken)
    {
        return parent.findFirstToken() == firstToken && parent.findLastToken() == lastToken;
    }

    protected static boolean nodeExactlyEnclosesRegion(IASTNode node, IFortranAST ast, ITextSelection selection)
    {
        Token firstInNode = node.findFirstToken();
        Token lastInNode = node.findLastToken();

        Token firstInSel = findFirstTokenAfter(ast, selection.getOffset());
        Token lastInSel = findLastTokenBefore(ast, OffsetLength.getPositionPastEnd(selection.getOffset(), selection.getLength()));

        return firstInNode != null
            && lastInNode != null
            && firstInSel != null
            && lastInSel != null
            && firstInNode == firstInSel
            && lastInNode == lastInSel;
    }

//    protected IASTNode findEnclosingNode(IFortranAST ast, ITextSelection selection, Nonterminal nodeType, boolean allowNesting)
//    {
//        IASTNode smallestEnclosure = findEnclosingNode(ast, selection);
//        if (smallestEnclosure == null) return null;
//
//        for (IASTNode n = smallestEnclosure; n != null; n = n.getParent())
//        {
//            if (n.getNonterminal() == nodeType)
//            {
//                if (allowNesting)
//                    return n;
//                else if (n.getParent() == null)
//                    return null;
//                else if (n.getParent().getNonterminal() != nodeType)
//                    return n;
//                else if (n.getParent().getNonterminal() == nodeType)
//                    continue;
//            }
//        }
//
//        return null;
//    }

    private static boolean contains(IASTNode target, Token token)
    {
        for (IASTNode node = token.getParent(); node != null; node = node.getParent())
            if (node == target)
                return true;
        return false;
    }

    private static Token findFirstTokenAfter(IFortranAST ast, final int targetFileOffset)
    {
        for (Token token : new IterableWrapper<Token>(ast))
            if (token.isOnOrAfterFileOffset(targetFileOffset))
                return token;
        return null;
    }

    private static Token findLastTokenBefore(IFortranAST ast, final int targetFileOffset)
    {
        Token previousToken = null;
        for (Token token : new IterableWrapper<Token>(ast))
        {
            if (token.isOnOrAfterFileOffset(targetFileOffset))
                return previousToken;
            else
                previousToken = token;
        }
        return null;
    }

    protected static class StatementSequence
    {
        public final ScopingNode enclosingScope;
        public final IASTListNode<? extends IASTNode> listContainingStmts;
        public final int startIndex;
        public final int endIndex;
        public final List<IASTNode> selectedStmts;

        private StatementSequence(ScopingNode enclosingScope, IASTListNode<? extends IASTNode> body, int startIndex, int endIndex)
        {
            this.enclosingScope = enclosingScope;
            this.listContainingStmts = body;
            this.startIndex = startIndex;
            this.endIndex = endIndex;

//            this.precedingStmts = new ArrayList<IASTNode>();
//            for (int i = 1; i < startIndex; i++)
//                this.precedingStmts.add(body.get(i));

            this.selectedStmts = new ArrayList<IASTNode>();
            for (int i = startIndex; i <= endIndex; i++)
                if (body.get(i) != null)
                    this.selectedStmts.add(body.get(i));

//            this.followingStmts = new ArrayList<IASTNode>();
//            for (int i = endIndex + 1; i < body.size(); i++)
//                this.followingStmts.add(body.get(i));
        }

        public IASTNode firstStmt()
        {
            return selectedStmts.get(0);
        }

        public Token firstToken()
        {
            return firstStmt().findFirstToken();
        }

        public IASTNode lastStmt()
        {
            return selectedStmts.get(selectedStmts.size()-1);
        }

        public Token lastToken()
        {
            return lastStmt().findLastToken();
        }
    }

    protected static ASTProperLoopConstructNode getLoopNode(IFortranAST ast, ITextSelection selection)
    {
        /*Token firstToken = this.findFirstTokenAfter(ast, selection.getOffset());
        Token lastToken = this.findLastTokenBefore(ast, selection.getOffset()+selection.getLength());
        if (firstToken == null || lastToken == null)
            return null;

        return getLoopNode(firstToken, lastToken);*/
        return (ASTProperLoopConstructNode)getNode(ast, selection, ASTProperLoopConstructNode.class);
    }
    /**start test code */
    protected static ASTProperLoopConstructNode getLoopNodeAtIndx(IFortranAST ast, ITextSelection selection, int off)
    {
        return (ASTProperLoopConstructNode)getNodeAtIndx(ast, selection, ASTProperLoopConstructNode.class, off);
    }
    
    protected static ASTNode getNodeAtIndx(IFortranAST ast, ITextSelection selection, Class<? extends ASTNode> node, int off)
    {
        Token firstToken = findFirstTokenAfter(ast, selection.getOffset()+off);
        Token lastToken = findLastTokenBefore(ast, selection.getOffset()+selection.getLength());
        if (firstToken == null || lastToken == null)
            return null;
        return getNode(firstToken, lastToken, node);
    }
    /**end test code*/
    protected static <T extends IASTNode> T getNode(IFortranAST ast, ITextSelection selection, Class<T> node)
    {
        Token firstToken = findFirstTokenAfter(ast, selection.getOffset());
        Token lastToken = findLastTokenBefore(ast, selection.getOffset()+selection.getLength());
        if (firstToken == null || lastToken == null)
            return null;
        return getNode(firstToken, lastToken, node);
    }

    protected static <T extends IASTNode> T getNode(Token firstToken, Token lastToken, Class<T> node)
    {
        assert(firstToken != null);
        assert(lastToken  != null);
        T firstTokenNode = firstToken.findNearestAncestor(node);
        T lastTokenNode = lastToken.findNearestAncestor(node);
        if (firstTokenNode == null || lastTokenNode == null || firstTokenNode != lastTokenNode)
            return null;
        return firstTokenNode;
        //return null;
    }

    protected static ASTProperLoopConstructNode getLoopNode(Token firstToken, Token lastToken)
    {
        /*assert(firstToken != null);
        assert(lastToken  != null);
        ASTProperLoopConstructNode loopContainingFirstToken = firstToken.findNearestAncestor(ASTProperLoopConstructNode.class);
        ASTProperLoopConstructNode loopContainingLastToken = lastToken.findNearestAncestor(ASTProperLoopConstructNode.class);
        if (loopContainingFirstToken == null || loopContainingLastToken == null || loopContainingFirstToken != loopContainingLastToken)
            return null;

        return loopContainingFirstToken;*/
        return (ASTProperLoopConstructNode)getNode(firstToken, lastToken, ASTProperLoopConstructNode.class);
    }

    @SuppressWarnings("unchecked")
    protected static StatementSequence findEnclosingStatementSequence(IFortranAST ast, ITextSelection selection)
    {
        Token firstToken = findFirstTokenAfter(ast, selection.getOffset());
        Token lastToken = findLastTokenBefore(ast, selection.getOffset()+selection.getLength());
        if (firstToken == null || lastToken == null) return null;

        IASTListNode<? extends IASTNode> listContainingFirstToken = firstToken.findNearestAncestor(IASTListNode.class);
        IASTListNode<? extends IASTNode> listContainingLastToken = lastToken.findNearestAncestor(IASTListNode.class);
        if (listContainingFirstToken == null || listContainingLastToken == null || listContainingFirstToken != listContainingLastToken) return null;

        IASTListNode<? extends IASTNode> listContainingStmts = listContainingFirstToken;
        int startIndex = -1;
        int endIndex = -1;
        for (int i = 0; i < listContainingStmts.size(); i++)
        {
            IASTNode node = listContainingStmts.get(i);
            if (contains(node, firstToken))
                startIndex = i;
            if (contains(node, lastToken))
                endIndex = i;
        }
        if (startIndex < 0 || endIndex < 0 || endIndex < startIndex)
            throw new Error("INTERNAL ERROR: Unable to locate selected statements in IASTListNode"); //$NON-NLS-1$

        return new StatementSequence(
            listContainingStmts.findNearestAncestor(ScopingNode.class),
            listContainingStmts,
            startIndex,
            endIndex);
    }

//    private IASTListNode<? extends IASTNode> findEnclosingBodyNode(Token token)
//    {
//        ScopingNode scope = token.findNearestAncestor(ScopingNode.class);
//        return scope == null ? null : scope.getBody();
//    }
//
//    private boolean isBodyNode(IASTNode currentNode)
//    {
//        return currentNode instanceof ASTBodyNode;
//    }
//
//    private boolean isNestedBodyNode(IASTNode currentNode)
//    {
//        return isBodyNode(currentNode)
//               && currentNode.getParent() != null
//               && currentNode.getParent() instanceof ASTBodyNode;
//    }

    protected static int findIndexToInsertTypeDeclaration(IASTListNode<? extends IASTNode> body)
    {
        IASTNode node = null;
        Iterator<? extends IASTNode> iterator = body.iterator();
        while(iterator.hasNext())
        {
            node = iterator.next();
            if (!(node instanceof ASTUseStmtNode) && !(node instanceof ASTImplicitStmtNode))
            {
                break;
            }
        }
        //If there are no other nodes besides use statements and implicit none, then increment the index
        if (node instanceof ASTUseStmtNode || node instanceof ASTImplicitStmtNode)
        {
            return body.indexOf(node) + 1;
        }else
        {
            return body.indexOf(node);
        }
    }

    protected static int findIndexToInsertStatement(IASTListNode<? extends IASTNode> body)
    {
        IASTNode node = null;
        Iterator<? extends IASTNode> iterator = body.iterator();
        while(iterator.hasNext())
        {
            node = iterator.next();
            if (!(node instanceof ISpecificationPartConstruct))
            {
                break;
            }
        }
        //If there are no other nodes besides those that implement ISpecificationPartConstruct,
        //then increment the index
        if (node instanceof ISpecificationPartConstruct)
        {
            return body.indexOf(node) + 1;
        }else
        {
            return body.indexOf(node);
        }
    }

    protected ASTImplicitStmtNode findExistingImplicitStatement(final ScopingNode scope)
    {
        try
        {
            scope.accept(new GenericASTVisitor()
            {
                @Override
                public void visitASTImplicitStmtNode(ASTImplicitStmtNode node)
                {
                    if (node.getImplicitToken().getEnclosingScope() == scope)
                        throw new Notification(node);
                }
            });
        }
        catch (Notification n)
        {
            return (ASTImplicitStmtNode)n.getResult();
        }
        return null;
    }

    protected int findIndexOfLastUseStmtIn(IASTListNode<? extends IASTNode> body)
    {
        int result = -1;
    
        for (int i = 0; i < body.size(); i++)
        {
            if (body.get(i) instanceof ASTUseStmtNode)
                result = i;
            else
                break; // USE statements precede all other statements, so we can stop here
        }
    
        return result;
    }

    ///////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////
    //
    // P R E C O N D I T I O N S
    //
    ///////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////

    protected static boolean isIdentifier(Token token)
    {
        return token != null && token.getTerminal() == Terminal.T_IDENT;
    }

    protected static boolean isPreprocessed(Token token)
    {
        return token.getPreprocessorDirective() != null;
    }

    protected static boolean isValidIdentifier(String name)
    {
        return Pattern.matches("[A-Za-z$][A-Za-z0-9$_]*", name); //$NON-NLS-1$
    }

    protected static boolean isBoundIdentifier(Token t)
    {
        return isIdentifier(t) && !t.resolveBinding().isEmpty();
    }

    protected static boolean isUniquelyDefinedIdentifer(Token t)
    {
        return isBoundIdentifier(t) && t.resolveBinding().size() == 1;
    }

    ///////////////////////////////////////////////////////////////////////////
    // Check for conflicting bindings
    ///////////////////////////////////////////////////////////////////////////

    public static final class Conflict
    {
        public final String name;
        public final PhotranTokenRef tokenRef;

        public Conflict(String name, PhotranTokenRef tokenRef)
        {
            this.name = name;
            this.tokenRef = tokenRef;
        }
    }


    public static interface IConflictingBindingCallback
    {
        void addConflictError(List<Conflict> conflictingDef);
        void addConflictWarning(List<Conflict> conflictingDef);
        void addReferenceWillChangeError(String newName, Token reference);
    }

    /**
     * Given a {@link Definition} and a list of references to that Definition
     * (see {@link Definition#findAllReferences(boolean)}), checks if any of
     * the <code>newNames</code> will conflict in the scope of any of the given
     * references; if so, the given callback is invoked to record an error or
     * warning.
     * <p>
     * This is the fundamental precondition check for Photran's Rename refactoring.
     */
    protected static void checkForConflictingBindings(
        IProgressMonitor pm,
        IConflictingBindingCallback callback,
        Definition definitionToCheck,
        Collection<PhotranTokenRef> allReferences,
        String... newNames)
    {
        checkForConflictingBindings(pm, callback, definitionToCheck, allReferences, Arrays.asList(newNames));
    }

    /**
     * Given a {@link Definition} and a list of references to that Definition
     * (see {@link Definition#findAllReferences(boolean)}), checks if any of
     * the <code>newNames</code> will conflict in the scope of any of the given
     * references; if so, the given callback is invoked to record an error or
     * warning.
     * <p>
     * This is the fundamental precondition check for Photran's Rename refactoring.
     */
    protected static void checkForConflictingBindings(
        IProgressMonitor pm,
        IConflictingBindingCallback callback,
        Definition definitionToCheck,
        Collection<PhotranTokenRef> allReferences,
        Collection<String> newNames)
    {
        new CheckForConflictBindings(definitionToCheck, allReferences, newNames).check(pm, callback);
    }

    /**
     * Determines whether a declaration with the given <code>name</code> can be added to the given scope.
     */
    protected static boolean checkIfDeclarationCanBeAddedToScope(
        String name,
        ScopingNode scope,
        IProgressMonitor pm)
    {
        try
        {
            IConflictingBindingCallback callback = new IConflictingBindingCallback()
            {
                public void addConflictError(List<Conflict> conflictingDef)
                {
                    throw new Notification(Boolean.FALSE);
                }

                public void addConflictWarning(List<Conflict> conflictingDef)
                {
                    throw new Notification(Boolean.FALSE);
                }

                public void addReferenceWillChangeError(String newName, Token reference)
                {
                    throw new Notification(Boolean.FALSE);
                }
            };

            new CheckForConflictBindings(scope, Collections.singleton(name)).check(pm, callback);
        }
        catch (Notification n)
        {
            return (Boolean)n.getResult();
        }

        return true;
    }

    private static final class CheckForConflictBindings
    {
        private IProgressMonitor pm = null;
        private Definition definitionToCheck = null;
        private ScopingNode scopeOfDefinitionToCheck = null;
        private Collection<String> newNames = null;
        private Collection<PhotranTokenRef> allReferences = null;

        public CheckForConflictBindings(Definition definitionToCheck,
                                        Collection<PhotranTokenRef> allReferences,
                                        Collection<String> newNames)
        {
            this.definitionToCheck = definitionToCheck;
            this.scopeOfDefinitionToCheck = definitionToCheck.getTokenRef().findToken().getEnclosingScope();
            this.allReferences = allReferences;
            this.newNames = newNames;
        }

        public CheckForConflictBindings(ScopingNode checkInScope,
                                        Collection<String> newNames)
        {
            this.definitionToCheck = null;
            this.scopeOfDefinitionToCheck = checkInScope;
            this.allReferences = Collections.emptySet();
            this.newNames = newNames;
        }

        public void check(IProgressMonitor pm, IConflictingBindingCallback callback)
        {
            this.pm = pm;

            checkForConflictingDefinitionOrShadowing(callback);

            for (PhotranTokenRef ref : findReferencesToShadowedDefinitions())
                checkIfReferenceBindingWillChange(callback, ref, false);

            for (PhotranTokenRef ref : allReferences)
                checkIfReferenceBindingWillChange(callback, ref, true);
        }

        /** Check whether the new definition will either conflict with or shadow an existing definition */
        private void checkForConflictingDefinitionOrShadowing(IConflictingBindingCallback callback)
        {
            List<Conflict> conflictingDef = findAllPotentiallyConflictingDefinitions();
            if (!conflictingDef.isEmpty())
                callback.addConflictError(conflictingDef);

            conflictingDef = findAllPotentiallyConflictingUnboundSubprogramCalls();
            if (!conflictingDef.isEmpty())
                callback.addConflictWarning(conflictingDef);
        }

        private List<Conflict> findAllPotentiallyConflictingDefinitions()
        {
            List<Conflict> conflicts = new ArrayList<Conflict>();

            if (definitionToCheck != null)
            {
                // Cannot call a main program (or function, etc.) X if it has an internal subprogram named X,
                // even if that subprogram is never used (in which case it wouldn't be caught below)
                if (definitionToCheck.isMainProgram()
                    || definitionToCheck.isSubprogram()
                    || definitionToCheck.isModule())
                {
                    findAllPotentiallyConflictingDefinitionsInScope(
                        conflicts,
                        definitionToCheck.getTokenRef().findToken().findNearestAncestor(ScopingNode.class),
                        false);
                }
                for (String newName : newNames)
                {
                    if (definitionToCheck.isInternalSubprogramDefinition()
                        && scopeContainingInternalSubprogram().isNamed(newName))
                    {
                        conflicts.add(
                            new Conflict(
                                newName,
                                scopeContainingInternalSubprogram().getNameToken().getTokenRef()));
                    }
                }
            }

            for (ScopingNode importingScope : scopeItselfAndAllScopesThatImport(scopeOfDefinitionToCheck))
            {
                pm.subTask(Messages.bind(Messages.FortranResourceRefactoring_CheckingForConflictingDefinitionsIn, importingScope.describe()));
                findAllPotentiallyConflictingDefinitionsInScope(conflicts, importingScope, true);
            }

            return conflicts;
        }

        private ScopingNode scopeContainingInternalSubprogram()
        {
            return definitionToCheck.getTokenRef().findToken().getEnclosingScope();
        }

        /**
         * Cannot call a function X if it is defined in or imported into a scope with a function X already defined
         * <p>
         * The third parameter indicates whether or not we should check that the definition to check is actually imported
         * into the target scope (it may not be if there is a USE statement with a Rename or ONLY list).
         */
        private void findAllPotentiallyConflictingDefinitionsInScope(List<Conflict> conflicts, ScopingNode importingScope, boolean shouldCheckIfDefinitionImportedIntoScope)
        {
            for (String newName : newNames)
            {
                List<PhotranTokenRef> definitionsLocalToScope = collectLocalDefinitions(importingScope);

                if (isProgramOrSubprogramOrModuleScope(importingScope) && shouldCheckIfDefinitionImportedIntoScope)
                {
                    // Cannot call a variable X inside a function named X
                    if (importingScope.isNamed(newName))
                    {
                        if (definitionToCheck == null || definitionsLocalToScope.contains(definitionToCheck.getTokenRef()))
                        {
                            conflicts.add(new Conflict(newName, importingScope.getNameToken().getTokenRef()));
                        }
                    }
                    // Cannot call a variable X inside a function named Y inside a module named X
                    else
                    {
                        ScopingNode parent = importingScope.findNearestAncestor(ScopingNode.class);
                        if (parent != null && parent.isNamed(newName))
                        {
                            List<PhotranTokenRef> definitionsLocalToParent = collectLocalDefinitions(parent);
                            if (definitionToCheck == null || definitionsLocalToParent.contains(definitionToCheck.getTokenRef()))
                            {
                                conflicts.add(new Conflict(newName, parent.getNameToken().getTokenRef()));
                            }
                        }
                    }
                }

                // Cannot call a function X if it is defined in or imported into a scope with a function X already defined
                Token newNameToken = definitionToCheck == null ? new FakeToken(scopeOfDefinitionToCheck, newName) : new FakeToken(definitionToCheck.getTokenRef().findToken(), newName);
                for (PhotranTokenRef conflict : importingScope.manuallyResolveInLocalScope(newNameToken))
                {
                    if (definitionsLocalToScope.contains(conflict))
                    {
                        if (shouldCheckIfDefinitionImportedIntoScope)
                        {
                            if (definitionToCheck == null
                                || definitionsLocalToScope.contains(definitionToCheck.getTokenRef()))
                            {
                                conflicts.add(new Conflict(newName, conflict));
                            }
                        }
                        else
                        {
                            conflicts.add(new Conflict(newName, conflict));
                        }
                    }
                }
            }
        }

        /** Check whether the new definition will either conflict with or shadow an existing definition */
        private List<PhotranTokenRef> findReferencesToShadowedDefinitions()
        {
            List<PhotranTokenRef> referencesToShadowedDefinitions = new LinkedList<PhotranTokenRef>();

            for (String newName : newNames)
            {
                Token token = definitionToCheck == null ? new FakeToken(scopeOfDefinitionToCheck, newName) : new FakeToken(definitionToCheck.getTokenRef().findToken(), newName);

                List<PhotranTokenRef> shadowedDefinitions = scopeOfDefinitionToCheck.manuallyResolve(token);
                // TODO: Does not consider rename or only lists (need to tell if this SPECIFIC definition will be imported)
                for (ScopingNode importingScope : scopeOfDefinitionToCheck.findImportingScopes())
                {
                    pm.subTask(Messages.bind(Messages.FortranResourceRefactoring_CheckingForReferencesTo, newName, importingScope.describe()));
                    shadowedDefinitions.addAll(importingScope.manuallyResolve(token));
                }

                for (PhotranTokenRef def : shadowedDefinitions)
                {
                    Definition definition = PhotranVPG.getInstance().getDefinitionFor(def);
                    if (definition != null)
                        referencesToShadowedDefinitions.addAll(definition.findAllReferences(false));
                }
            }

            return referencesToShadowedDefinitions;
        }

        private void checkIfReferenceBindingWillChange(IConflictingBindingCallback callback, PhotranTokenRef ref, boolean shouldReferenceRenamedDefinition)
        {
            pm.subTask(Messages.bind(Messages.FortranResourceRefactoring_CheckingForBindingConflictsIn, PhotranVPG.lastSegmentOfFilename(ref.getFilename())));

            Token reference = ref.findToken();

            if (definitionToCheck != null)
            {
                ScopingNode scopeOfDefinitionToRename = reference.findScopeDeclaringOrImporting(definitionToCheck);
                if (scopeOfDefinitionToRename == null) return;

                for (String newName : newNames)
                {
                    for (PhotranTokenRef existingBinding : new FakeToken(reference, newName).manuallyResolveBinding())
                    {
                        ScopingNode scopeOfExistingBinding = existingBinding.findToken().getEnclosingScope();

                        boolean willReferenceRenamedDefinition = scopeOfExistingBinding.isParentScopeOf(scopeOfDefinitionToRename);
                        if (shouldReferenceRenamedDefinition != willReferenceRenamedDefinition)
                            callback.addReferenceWillChangeError(newName, reference);
                    }
                }
            }
            else
            {
                if (scopeOfDefinitionToCheck == reference.getLocalScope()
                    || scopeOfDefinitionToCheck.isParentScopeOf(reference.getLocalScope()))
                {
                    for (String newName : newNames)
                    {
                        for (PhotranTokenRef existingBinding : new FakeToken(reference, newName).manuallyResolveBinding())
                        {
                            ScopingNode scopeOfExistingBinding = existingBinding.findToken().getEnclosingScope();

                            boolean willReferenceRenamedDefinition = scopeOfExistingBinding.isParentScopeOf(scopeOfDefinitionToCheck);
                            if (shouldReferenceRenamedDefinition != willReferenceRenamedDefinition)
                                callback.addReferenceWillChangeError(newName, reference);
                        }
                    }
                }
            }
        }

        private List<Conflict> findAllPotentiallyConflictingUnboundSubprogramCalls()
        {
            final List<Conflict> conflictingDef = new ArrayList<Conflict>();

            for (ScopingNode importingScope : scopeItselfAndAllScopesThatImport(scopeOfDefinitionToCheck))
            {
                pm.subTask(Messages.bind(Messages.FortranResourceRefactoring_CheckingForSubprogramBindingConflictsIn, importingScope.describe()));

                importingScope.accept(new GenericASTVisitor()
                {
                    @Override public void visitASTVarOrFnRefNode(ASTVarOrFnRefNode node)
                    {
                        if (node.getName() != null && node.getName().getName() != null)
                            checkForConflict(node.getName().getName());
                    }

                    @Override public void visitASTCallStmtNode(ASTCallStmtNode node)
                    {
                        if (node.getSubroutineName() != null)
                            checkForConflict(node.getSubroutineName());
                    }

                    private void checkForConflict(Token name)
                    {
                        if (name.getLogicalFile() != null)
                            for (String newName : newNames)
                                if (name != null && name.getText().equals(newName) && name.resolveBinding().isEmpty())
                                    conflictingDef.add(new Conflict(newName, name.getTokenRef()));
                    }
                });
            }

            return conflictingDef;
        }

        private Iterable<ScopingNode> scopeItselfAndAllScopesThatImport(final ScopingNode scope)
        {
            if (scope == null) return Collections.emptySet();

            return new Iterable<ScopingNode>()
            {
                public Iterator<ScopingNode> iterator()
                {
                    return new Iterator<ScopingNode>()
                    {
                        private ScopingNode first = scope;
                        private Iterator<ScopingNode> rest = scope.findImportingScopes().iterator();

                        public boolean hasNext()
                        {
                            if (first != null)
                                return true;
                            else
                                return rest.hasNext();
                        }

                        public ScopingNode next()
                        {
                            if (first != null)
                            {
                                ScopingNode result = first;
                                first = null;
                                return result;
                            }
                            else return rest.next();
                        }

                        public void remove() { throw new UnsupportedOperationException(); }
                    };
                }
            };
        }

        private List<PhotranTokenRef> collectLocalDefinitions(ScopingNode importingScope)
        {
            List<PhotranTokenRef> definitionsLocalToScope = new ArrayList<PhotranTokenRef>();
            for (Definition def : importingScope.getAllDefinitions())
                if (def != null && !def.isIntrinsic())
                    definitionsLocalToScope.add(def.getTokenRef());
            return definitionsLocalToScope;
        }

        private boolean isProgramOrSubprogramOrModuleScope(ScopingNode scope)
        {
            return scope instanceof ASTMainProgramNode
                || scope instanceof ASTFunctionSubprogramNode
                || scope instanceof ASTSubroutineSubprogramNode
                || scope instanceof ASTModuleNode;
        }
    }
}

Back to the top