Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2010 Stephen Downs, Robert Samblanet, Kevin Schilling, Jon |
3 |
* Woolwine, and Chad Zamzow |
4 |
* All rights reserved. This program and the accompanying materials |
5 |
* are made available under the terms of the Eclipse Public License v1.0 |
6 |
* which accompanies this distribution, and is available at |
7 |
* http://www.eclipse.org/legal/epl-v10.html |
8 |
* |
9 |
* Contributors: |
10 |
* UIUC - Initial API and implementation |
11 |
*******************************************************************************/ |
12 |
package org.eclipse.photran.internal.core.refactoring; |
13 |
|
14 |
import java.util.HashSet; |
15 |
import java.util.Iterator; |
16 |
import java.util.TreeSet; |
17 |
|
18 |
import org.eclipse.core.internal.resources.SavedState; |
19 |
import org.eclipse.core.resources.IFile; |
20 |
import org.eclipse.core.runtime.CoreException; |
21 |
import org.eclipse.core.runtime.IProgressMonitor; |
22 |
import org.eclipse.core.runtime.OperationCanceledException; |
23 |
import org.eclipse.ltk.core.refactoring.RefactoringStatus; |
24 |
import org.eclipse.photran.core.IFortranAST; |
25 |
import org.eclipse.photran.internal.core.analysis.binding.ScopingNode; |
26 |
import org.eclipse.photran.internal.core.lexer.Terminal; |
27 |
import org.eclipse.photran.internal.core.lexer.Token; |
28 |
import org.eclipse.photran.internal.core.parser.ASTAssignmentStmtNode; |
29 |
import org.eclipse.photran.internal.core.parser.ASTAttrSpecNode; |
30 |
import org.eclipse.photran.internal.core.parser.ASTAttrSpecSeqNode; |
31 |
import org.eclipse.photran.internal.core.parser.ASTDatalistNode; |
32 |
import org.eclipse.photran.internal.core.parser.ASTDataStmtNode; |
33 |
import org.eclipse.photran.internal.core.parser.ASTEntityDeclNode; |
34 |
import org.eclipse.photran.internal.core.parser.ASTListNode; |
35 |
import org.eclipse.photran.internal.core.parser.ASTSaveStmtNode; |
36 |
import org.eclipse.photran.internal.core.parser.ASTSavedEntityNode; |
37 |
import org.eclipse.photran.internal.core.parser.ASTSeparatedListNode; |
38 |
import org.eclipse.photran.internal.core.parser.ASTTypeDeclarationStmtNode; |
39 |
import org.eclipse.photran.internal.core.parser.ASTVariableNode; |
40 |
import org.eclipse.photran.internal.core.parser.ASTVisitor; |
41 |
import org.eclipse.photran.internal.core.parser.IASTListNode; |
42 |
import org.eclipse.photran.internal.core.parser.IASTNode; |
43 |
import org.eclipse.photran.internal.core.refactoring.infrastructure.FortranResourceRefactoring; |
44 |
import org.eclipse.photran.internal.core.refactoring.infrastructure.SourcePrinter; |
45 |
import org.eclipse.photran.internal.core.reindenter.Reindenter; |
46 |
|
47 |
/** |
48 |
* Makes all implicitly saved variables explicitly saved. |
49 |
* |
50 |
* @author Stephen Downs |
51 |
* @author Robert Samblanet |
52 |
* @author Kevin Schilling |
53 |
* @author Jon Woolwine |
54 |
* @author Chad Zamzow |
55 |
*/ |
56 |
@SuppressWarnings("all") |
57 |
public class MakeSaveExplicitRefactoring extends FortranResourceRefactoring |
58 |
{ |
59 |
/////////////////////////////////////////////////////////////////////////// |
60 |
// Fields |
61 |
/////////////////////////////////////////////////////////////////////////// |
62 |
|
63 |
@Override |
64 |
public String getName() |
65 |
{ |
66 |
return Messages.MakeSaveExplicitRefactoring_Name; |
67 |
} |
68 |
|
69 |
IFortranAST currAST = null; |
70 |
|
71 |
/////////////////////////////////////////////////////////////////////////// |
72 |
// Initial Preconditions |
73 |
/////////////////////////////////////////////////////////////////////////// |
74 |
|
75 |
/** |
76 |
* @see MoveSavedToCommonBlockRefactoring |
77 |
*/ |
78 |
@Override |
79 |
protected void doCheckInitialConditions(RefactoringStatus status, IProgressMonitor pm) |
80 |
throws PreconditionFailure |
81 |
{ |
82 |
ensureProjectHasRefactoringEnabled(status); |
83 |
} |
84 |
|
85 |
/////////////////////////////////////////////////////////////////////////// |
86 |
// Final Preconditions |
87 |
/////////////////////////////////////////////////////////////////////////// |
88 |
|
89 |
@Override |
90 |
protected void doCheckFinalConditions(RefactoringStatus status, IProgressMonitor pm) throws PreconditionFailure{ |
91 |
try |
92 |
{ |
93 |
for (IFile file : selectedFiles) |
94 |
{ |
95 |
IFortranAST ast = vpg.acquirePermanentAST(file); |
96 |
if(ast == null) |
97 |
{ |
98 |
status.addError(Messages.bind(Messages.MakeSaveExplicitRefactoring_SelectedFileCannotBeParsed, file.getName())); |
99 |
} |
100 |
else |
101 |
{ |
102 |
currAST = ast; |
103 |
makeChangesTo(file, ast, status, pm); |
104 |
vpg.releaseAST(file); |
105 |
} |
106 |
} |
107 |
} |
108 |
finally |
109 |
{ |
110 |
vpg.releaseAllASTs(); |
111 |
} |
112 |
} |
113 |
|
114 |
/////////////////////////////////////////////////////////////////////////// |
115 |
// Change |
116 |
/////////////////////////////////////////////////////////////////////////// |
117 |
|
118 |
@Override |
119 |
protected void doCreateChange(IProgressMonitor pm) throws CoreException, OperationCanceledException |
120 |
{ |
121 |
|
122 |
} |
123 |
|
124 |
/** |
125 |
* Given an AST, makes the refactoring changes by calling the makeAllSaveAttributesExplicit function |
126 |
* @param file |
127 |
* @param ast |
128 |
* @param status |
129 |
* @param pm |
130 |
* @throws PreconditionFailure |
131 |
*/ |
132 |
private void makeChangesTo(IFile file, IFortranAST ast, RefactoringStatus status, IProgressMonitor pm) throws PreconditionFailure |
133 |
{ |
134 |
for (ScopingNode scope : ast.getRoot().getAllContainedScopes()) |
135 |
{ |
136 |
SavedVariableVisitor savedVariableVisitor = new SavedVariableVisitor(); |
137 |
scope.accept(savedVariableVisitor); |
138 |
if(!savedVariableVisitor.hasGlobalSaveStmt() && !scope.isMainProgram()) |
139 |
{ |
140 |
HashSet<String> explicitlySavedVariables = savedVariableVisitor.getExplicitlySavedVariables(); |
141 |
TreeSet<String> dataVariables = savedVariableVisitor.getDataBlockVariables(); |
142 |
makeAllSaveAttributesExplicit(scope, explicitlySavedVariables, dataVariables); |
143 |
} |
144 |
} |
145 |
addChangeFromModifiedAST(file, pm); |
146 |
} |
147 |
|
148 |
/** |
149 |
* Makes all implicit saves explicit in the given scope |
150 |
* @param scope |
151 |
* @param explicitlySavedVariables |
152 |
* @param dataEntities |
153 |
* @throws PreconditionFailure |
154 |
*/ |
155 |
private void makeAllSaveAttributesExplicit(ScopingNode scope, HashSet<String> explicitlySavedVariables, TreeSet<String> dataEntities) |
156 |
throws PreconditionFailure |
157 |
{ |
158 |
if(scope.getBody() == null) |
159 |
{ |
160 |
return; |
161 |
} |
162 |
|
163 |
for (IASTNode node : scope.getBody().getChildren()) |
164 |
{ |
165 |
if (node instanceof ASTTypeDeclarationStmtNode) |
166 |
{ |
167 |
ASTTypeDeclarationStmtNode declarationNode = (ASTTypeDeclarationStmtNode)node; |
168 |
makeImplicitlySavedVariablesExplicitlySaved(scope, declarationNode, dataEntities, explicitlySavedVariables); |
169 |
} |
170 |
} |
171 |
|
172 |
//add all implicitly saved data block variables to a save statement |
173 |
for(String variable : dataEntities) |
174 |
{ |
175 |
if(!explicitlySavedVariables.contains(variable.toLowerCase())) |
176 |
{ |
177 |
addVariableToSaveStmt(scope, variable); |
178 |
explicitlySavedVariables.add(variable.toLowerCase()); |
179 |
} |
180 |
} |
181 |
} |
182 |
|
183 |
/** |
184 |
* Helper function that adds SAVE to a type declaration |
185 |
* @param scope |
186 |
* @param typeDeclaration |
187 |
* @param dataEntities |
188 |
* @param savedEntities |
189 |
*/ |
190 |
private void makeImplicitlySavedVariablesExplicitlySaved(ScopingNode scope, ASTTypeDeclarationStmtNode typeDeclaration, TreeSet<String> dataEntities, HashSet<String> savedEntities) |
191 |
{ |
192 |
IASTListNode<ASTEntityDeclNode> entityDeclList = typeDeclaration.getEntityDeclList(); |
193 |
Iterator<ASTEntityDeclNode> declIterator = entityDeclList.iterator(); |
194 |
boolean declContainsSavedAndUnsavedVariables = containsUnsavedAndSavedVariables(typeDeclaration, dataEntities); |
195 |
while(declIterator.hasNext()) |
196 |
{ |
197 |
ASTEntityDeclNode variableDeclaration = declIterator.next(); |
198 |
if (isImplicitlySaved(scope, variableDeclaration, dataEntities) && |
199 |
!savedEntities.contains(declarationVariableName(variableDeclaration).toLowerCase())) |
200 |
{ |
201 |
if(!declContainsSavedAndUnsavedVariables) |
202 |
{ |
203 |
String declString = SourcePrinter.getSourceCodeFromASTNode(typeDeclaration); |
204 |
ASTAttrSpecSeqNode attrSpecSeqNode = createSaveAttrSpecSeqNode(!declString.contains("::")); |
205 |
|
206 |
// if there is no attrSpecSeq, create a new one and add it to the typeDeclaration |
207 |
if( typeDeclaration.getAttrSpecSeq() == null ) |
208 |
{ |
209 |
IASTListNode<ASTAttrSpecSeqNode> attrSpecSeq = new ASTListNode<ASTAttrSpecSeqNode>( 1 ); |
210 |
typeDeclaration.setAttrSpecSeq(attrSpecSeq); |
211 |
} |
212 |
|
213 |
// add Save attribute to attrSpecSeq |
214 |
typeDeclaration.getAttrSpecSeq().add(attrSpecSeqNode); |
215 |
|
216 |
for(ASTEntityDeclNode decl : typeDeclaration.getEntityDeclList()) |
217 |
{ |
218 |
savedEntities.add(declarationVariableName(decl).toLowerCase()); |
219 |
} |
220 |
|
221 |
return; |
222 |
} |
223 |
else |
224 |
{ |
225 |
String variableName = declarationVariableName(variableDeclaration); |
226 |
savedEntities.add(variableName.toLowerCase()); |
227 |
addVariableToSaveStmt(scope, variableName); |
228 |
} |
229 |
} |
230 |
} |
231 |
} |
232 |
|
233 |
/** |
234 |
* Helper function that adds the given variable name to a global save statement in the given scope |
235 |
* @param scope |
236 |
* @param variableName |
237 |
*/ |
238 |
private void addVariableToSaveStmt(ScopingNode scope, String variableName) |
239 |
{ |
240 |
ASTSavedEntityNode savedEntity = new ASTSavedEntityNode(); |
241 |
Token savedEntityToken = new Token(Terminal.T_IDENT, variableName); |
242 |
savedEntity.setVariableName(savedEntityToken); |
243 |
for (IASTNode node : scope.getBody().getChildren()) |
244 |
{ |
245 |
if(node instanceof ASTSaveStmtNode) |
246 |
{ |
247 |
IASTListNode<ASTSavedEntityNode> variableList = ((ASTSaveStmtNode)node).getVariableList(); |
248 |
ASTSeparatedListNode<ASTSavedEntityNode> astSeparatedListNode = (ASTSeparatedListNode<ASTSavedEntityNode>)variableList; |
249 |
astSeparatedListNode.add(new Token(null, ", "), savedEntity); |
250 |
return; |
251 |
} |
252 |
} |
253 |
ASTSaveStmtNode newSaveStmt = (ASTSaveStmtNode)parseLiteralStatement("SAVE " + variableName); |
254 |
IASTListNode body = scope.getBody(); |
255 |
body.add(0, newSaveStmt); |
256 |
Reindenter.reindent(newSaveStmt, currAST); |
257 |
} |
258 |
|
259 |
/** |
260 |
* Helper function that checks declaration lists to see if it contains both saved and unsaved variables |
261 |
* @param typeDeclaration |
262 |
* @param dataEntities |
263 |
* @return |
264 |
*/ |
265 |
private boolean containsUnsavedAndSavedVariables(ASTTypeDeclarationStmtNode typeDeclaration, TreeSet<String> dataEntities) |
266 |
{ |
267 |
if(typeDeclaration.getEntityDeclList() == null) return false; |
268 |
boolean containsSaved = false, containsUnSaved = false; |
269 |
|
270 |
for(ASTEntityDeclNode decl : typeDeclaration.getEntityDeclList()) |
271 |
{ |
272 |
if (decl.getInitialization() == null |
273 |
&& !dataEntities.contains(declarationVariableName(decl).toLowerCase())) |
274 |
{ |
275 |
containsUnSaved = true; |
276 |
} |
277 |
else |
278 |
{ |
279 |
containsSaved = true; |
280 |
} |
281 |
} |
282 |
return containsSaved && containsUnSaved; |
283 |
} |
284 |
|
285 |
/** |
286 |
* Helper function that extracts the declaration variable name from a declaration node |
287 |
* @param decl |
288 |
* @return |
289 |
*/ |
290 |
private String declarationVariableName(ASTEntityDeclNode decl) |
291 |
{ |
292 |
return decl.getObjectName().getObjectName().getText(); |
293 |
} |
294 |
|
295 |
/** |
296 |
* Helper function that checks to see if a given variable is implicitly saved |
297 |
* @param scope |
298 |
* @param variableDeclaration |
299 |
* @param dataEntities |
300 |
* @return |
301 |
*/ |
302 |
private boolean isImplicitlySaved(ScopingNode scope, ASTEntityDeclNode variableDeclaration, TreeSet<String> dataEntities) |
303 |
{ |
304 |
return (variableDeclaration.getInitialization() != null || |
305 |
dataEntities.contains(declarationVariableName(variableDeclaration).toLowerCase())); |
306 |
} |
307 |
|
308 |
/** |
309 |
* Creates a new AttrSpecSeqNode with SAVE attribute |
310 |
* @return new AttrSpecSeqNode with SAVE attribute |
311 |
*/ |
312 |
private ASTAttrSpecSeqNode createSaveAttrSpecSeqNode(boolean addDblColon) |
313 |
{ |
314 |
ASTAttrSpecSeqNode attrSpecSeqNode = new ASTAttrSpecSeqNode(); |
315 |
ASTAttrSpecNode attrSpecNode = new ASTAttrSpecNode(); |
316 |
|
317 |
Token token; |
318 |
if(addDblColon) |
319 |
{ |
320 |
token = new Token(null, ", SAVE ::" ); |
321 |
} |
322 |
else |
323 |
{ |
324 |
token = new Token(null, ", SAVE" ); |
325 |
} |
326 |
|
327 |
attrSpecNode.setIsSave( token ); |
328 |
attrSpecSeqNode.setAttrSpec(attrSpecNode); |
329 |
return attrSpecSeqNode; |
330 |
} |
331 |
|
332 |
/** |
333 |
* Saved Variable Visitor class |
334 |
* |
335 |
* Iterates through all nodes in a scope. While doing this, it checks three different cases: |
336 |
* 1) If the node is a save statement, it checks to see if it is a global save statement. |
337 |
* If it isn't, it adds all the saved variables to the explicitlySavedVariables list. |
338 |
* 2) If the node is a declaration, it checks to see if it has been explicitly saved. If it |
339 |
* has, it adds the variable to the explicitlySavedVariables list. |
340 |
* 3) If the node is a data statement, it adds all variables in the data block to the |
341 |
* dataBlockVariables list. |
342 |
* |
343 |
* @author ShinSheep |
344 |
*/ |
345 |
private class SavedVariableVisitor extends ASTVisitor |
346 |
{ |
347 |
private boolean hasGlobalSaveStmt; |
348 |
private HashSet<String> explicitlySavedVariables; |
349 |
private TreeSet<String> dataBlockVariables; |
350 |
private ASTSaveStmtNode saveStmt; |
351 |
|
352 |
public TreeSet<String> getDataBlockVariables() |
353 |
{ |
354 |
return dataBlockVariables; |
355 |
} |
356 |
|
357 |
public HashSet<String> getExplicitlySavedVariables() |
358 |
{ |
359 |
return explicitlySavedVariables; |
360 |
} |
361 |
|
362 |
public boolean hasGlobalSaveStmt() |
363 |
{ |
364 |
return hasGlobalSaveStmt; |
365 |
} |
366 |
|
367 |
public SavedVariableVisitor() |
368 |
{ |
369 |
super(); |
370 |
this.hasGlobalSaveStmt = false; |
371 |
explicitlySavedVariables = new HashSet<String>(); |
372 |
dataBlockVariables = new TreeSet<String>(); |
373 |
saveStmt = null; |
374 |
} |
375 |
|
376 |
@Override |
377 |
public void visitASTSaveStmtNode(ASTSaveStmtNode node) |
378 |
{ |
379 |
this.saveStmt = node; |
380 |
if (node.getVariableList() == null) |
381 |
{ |
382 |
hasGlobalSaveStmt = true; |
383 |
} |
384 |
else |
385 |
{ |
386 |
for(ASTSavedEntityNode variable : node.getVariableList()) |
387 |
{ |
388 |
explicitlySavedVariables.add(variable.getVariableName().getText().toLowerCase()); |
389 |
} |
390 |
} |
391 |
} |
392 |
|
393 |
@Override |
394 |
public void visitASTTypeDeclarationStmtNode(ASTTypeDeclarationStmtNode node) |
395 |
{ |
396 |
IASTListNode<ASTAttrSpecSeqNode> attrSpecSeq = node.getAttrSpecSeq(); |
397 |
if(attrSpecSeq != null) |
398 |
{ |
399 |
Iterator<ASTAttrSpecSeqNode> specIterator = attrSpecSeq.iterator(); |
400 |
while(specIterator.hasNext()) |
401 |
{ |
402 |
ASTAttrSpecSeqNode attrSpecSeqNode = specIterator.next(); |
403 |
ASTAttrSpecNode attrSpecNode = attrSpecSeqNode.getAttrSpec(); |
404 |
if(attrSpecNode != null && attrSpecNode.isSave()) |
405 |
{ |
406 |
for(ASTEntityDeclNode variable : node.getEntityDeclList()) |
407 |
{ |
408 |
explicitlySavedVariables.add(variable.getObjectName().getObjectName().getText().toLowerCase()); |
409 |
} |
410 |
} |
411 |
} |
412 |
} |
413 |
} |
414 |
|
415 |
@Override |
416 |
public void visitASTDataStmtNode(ASTDataStmtNode node) |
417 |
{ |
418 |
IASTListNode<ASTDatalistNode> dataList = ((ASTDataStmtNode)node).getDatalist(); |
419 |
for (IASTNode dataEntity : dataList.getChildren()) |
420 |
{ |
421 |
ASTDatalistNode dataListNode = (ASTDatalistNode)dataEntity; |
422 |
for( IASTNode variableNameNode : dataListNode.getDataStmtSet().getDataStmtObjectList().getChildren()) |
423 |
{ |
424 |
if(variableNameNode instanceof ASTVariableNode) |
425 |
{ |
426 |
ASTVariableNode variableNode = (ASTVariableNode)variableNameNode; |
427 |
String variableName = variableNode.getDataRef().get(0).getName().getText(); |
428 |
|
429 |
dataBlockVariables.add(variableName.toLowerCase()); |
430 |
} |
431 |
} |
432 |
} |
433 |
} |
434 |
} |
435 |
} |