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