The StaDyn Programming Language
Source code documentation of StaDyn, a hybrid static and dynamic typing language.
VisitorSSA.cs
Go to the documentation of this file.
1 // -------------------------------------------------------------------------- //
3 // Project rROTOR //
4 // -------------------------------------------------------------------------- //
5 // File: VisitorSSA.cs //
6 // Authors: Cristina Gonzalez Muņoz - cristi.gm@gmail.com //
7 // Francisco Ortin - francisco.ortin@gmail.com //
8 // Description: //
9 // This class makes the static single assignment algorithm in which every //
10 // variable is assigned exactly once. //
11 // It also collects the set of references that are used in the if //
12 // and switch statements to be used later (VisitorTypeInference) //
13 // as theta function parameters //
14 // Inheritance: VisitorAdapter. //
15 // Implements Visitor pattern [Concrete Visitor]. //
16 // -------------------------------------------------------------------------- //
17 // Create date: 06-04-2007 //
18 // Modification date: 07-06-2007 //
20 
21 using System;
22 using System.Collections.Generic;
23 using System.Text;
24 
25 using AST;
26 using Tools;
27 using ErrorManagement;
28 
29 namespace Semantic.SSAAlgorithm {
39  #region Fields
40 
44  private SSAMap map;
45 
49  private Dictionary<string, FieldDeclaration> fieldsInfo;
50 
55  IList<SingleIdentifierExpression> referencesUsedInIfElseBody;
56 
61  IList<SingleIdentifierExpression> referencesUsedInCaseBody;
62 
63  #endregion
64 
65  #region Constructor
66 
70  public VisitorSSA() {
71  this.map = new SSAMap();
72  this.fieldsInfo = new Dictionary<string, FieldDeclaration>();
73  }
74 
75  #endregion
76 
77  #region addAssignmentStatements()
78 
84  private void addAssignmentStatements(Dictionary<string, SSAElement> vars, Block node) {
88  ThisExpression th = new ThisExpression(node.Location);
89 
90  foreach (KeyValuePair<string, SSAElement> pair in vars) {
91  if (pair.Value.IndexSSA > 0) {
92  eop1 = new SingleIdentifierExpression(pair.Key.Substring(6, pair.Key.Length - 6), node.Location);
93  eop1.IndexOfSSA = -1;
94  op1 = new FieldAccessExpression(th, eop1, node.Location);
95  op2 = new SingleIdentifierExpression(pair.Key, node.Location);
96  op2.IndexOfSSA = pair.Value.IndexSSA;
97  node.AddStatement(new AssignmentExpression(op1, op2, AssignmentOperator.Assign, node.Location));
98  }
99  }
100  }
101 
102  #endregion
103 
104  #region addLocalVariable()
105 
111  private void addLocalVariable(Dictionary<string, SSAElement> vars, Statement node) {
112  string type;
113  Block statements;
114  if (node is Block) {
115  statements = (Block)node;
116  foreach (KeyValuePair<string, SSAElement> pair in vars) {
117  for (int i = 0; i < pair.Value.IndexSSA; i++) {
118  type = TypeSystem.TypeTable.ObtainNewType(pair.Value.TypeId);
119  statements.AddStatementToTheBeginning(new IdDeclaration(new SingleIdentifierExpression(pair.Key, pair.Value.Location), i + 1, type, pair.Value.Location));
120  }
121  }
122  }
123  }
124 
125  #endregion
126 
127  // Declarations
128 
129  #region Visit(ClassDefinition node, Object obj)
130 
131  public override Object Visit(ClassDefinition node, Object obj) {
132  //this.map.SetScope();
133 
134  // first step
135  // we store field declarations in the dictionary fieldsInfo
136  this.fieldsInfo.Clear();
137  for (int i = 0; i < node.MemberCount; i++) {
138  if (node.GetMemberElement(i) is FieldDeclaration)
139  this.fieldsInfo.Add(((FieldDeclaration)node.GetMemberElement(i)).Identifier, (FieldDeclaration)node.GetMemberElement(i));
140  }
141 
142  // second step
143  for (int i = 0; i < node.MemberCount; i++) {
144  node.GetMemberElement(i).Accept(this, obj);
145  }
146 
147  //this.addField(this.map.ResetScope(), node);
148  return null;
149  }
150 
151  #endregion
152 
153  #region Visit(IdDeclaration node, Object obj)
154 
155  public override Object Visit(IdDeclaration node, Object obj) {
156  if (!node.FullName.EndsWith("[]"))
157  this.map.AddNewVariable(node.Identifier, node.FullName, node.Location);
158  else
159  node.IdentifierExp.IndexOfSSA = -1;
160  return null;
161  }
162 
163  #endregion
164 
165  #region Visit(Definition node, Object obj)
166 
167  public override Object Visit(Definition node, Object obj) {
168  if (!node.FullName.EndsWith("[]"))
169  this.map.AddNewVariable(node.Identifier, node.FullName, node.Location);
170  else
171  node.IdentifierExp.IndexOfSSA = -1;
172  node.Init.Accept(this, false);
173  return null;
174  }
175 
176  #endregion
177 
178  #region Visit(ConstantDefinition node, Object obj)
179 
180  public override Object Visit(ConstantDefinition node, Object obj) {
181  if (!node.FullName.EndsWith("[]"))
182  this.map.AddNewVariable(node.Identifier, node.FullName, node.Location);
183  else
184  node.IdentifierExp.IndexOfSSA = -1;
185  node.Init.Accept(this, false);
186  return null;
187  }
188 
189  #endregion
190 
191  #region Visit(MethodDefinition node, Object obj)
192 
193  public override Object Visit(MethodDefinition node, Object obj) {
194  // Scope to store the local variable created for each field
195  this.map.SetScope();
196  //Dictionary<string, FieldDeclaration>.KeyCollection keys = this.fieldsInfo.Keys;
197  //foreach (string tmpName in keys)
198  //{
199  // if (!fieldsInfo[tmpName].TypeInfo.EndsWith("[]"))
200  // this.map.AddNewField("this__" + tmpName, fieldsInfo[tmpName].TypeInfo, node.FileName, fieldsInfo[tmpName].Line, fieldsInfo[tmpName].Column);
201  //}
202 
203  this.map.SetScope();
204  for (int i = 0; i < node.ParametersInfo.Count; i++) {
205  if (!node.ParametersInfo[i].ParamType.EndsWith("[]"))
206  this.map.AddNewVariable(node.ParametersInfo[i].Identifier, node.ParametersInfo[i].ParamType, node.Location);
207  }
208 
209  bool returnFound = (bool)node.Body.Accept(this, obj);
210  this.addLocalVariable(this.map.ResetScope(), node.Body);
211 
212  // Add local variable for each field in use.
213  //this.addLocalVariable(this.map.GetFieldScope(), node.Body);
214 
215  // If the last sentence is not a return statement adds new assignment statements for each field in use.
216  //if (!returnFound)
217  // this.addAssignmentStatements(this.map.ResetScope(), node.Body);
218  //else
219  this.map.ResetScope(); // Resets the scope
220  return null;
221  }
222 
223  #endregion
224 
225  #region Visit(ConstructorDefinition node, Object obj)
226 
227  public override Object Visit(ConstructorDefinition node, Object obj) {
228  // Scope to store the auxiliar local variables for each field
229  this.map.SetScope();
230  //Dictionary<string, FieldDeclaration>.KeyCollection keys = this.fieldsInfo.Keys;
231  //foreach (string tmpName in keys)
232  //{
233  // if (!fieldsInfo[tmpName].TypeInfo.EndsWith("[]"))
234  // this.map.AddNewField("this__" + tmpName, fieldsInfo[tmpName].TypeInfo, node.FileName, fieldsInfo[tmpName].Line, fieldsInfo[tmpName].Column);
235  //}
236 
237  this.map.SetScope();
238  for (int i = 0; i < node.ParametersInfo.Count; i++) {
239 
240  if (!node.ParametersInfo[i].ParamType.EndsWith("[]"))
241  this.map.AddNewVariable(node.ParametersInfo[i].Identifier, node.ParametersInfo[i].ParamType, node.Location);
242  }
243 
244  if (node.Initialization != null)
245  node.Initialization.Accept(this, obj);
246 
247  node.Body.Accept(this, obj);
248  this.addLocalVariable(this.map.ResetScope(), node.Body);
249  // Add local variable for each field in use.
250  //this.addLocalVariable(this.map.GetFieldScope(), node.Body);
251  this.map.ResetScope(); //this.addAssignmentStatements(this.map.ResetScope(), node.Body);
252  return null;
253  }
254 
255  #endregion
256 
257  // Expressions
258 
259  #region Visit(AssignmentExpression node, Object obj)
260 
261  public override Object Visit(AssignmentExpression node, Object obj) {
262  Object aux = null;
263  node.SecondOperand.LeftExpression = false;
264  if ((aux = node.SecondOperand.Accept(this, false)) is SingleIdentifierExpression)
265  node.SecondOperand = (SingleIdentifierExpression)aux;
266  node.FirstOperand.LeftExpression = true;
267  if ((aux = node.FirstOperand.Accept(this, false)) is SingleIdentifierExpression)
268  node.FirstOperand = (SingleIdentifierExpression)aux;
269  // * If the left expression is an SingleID expression, we do not add it to the used
270  // references because the theta function sets its type as a union type
271  SingleIdentifierExpression singleId=node.FirstOperand as SingleIdentifierExpression;
272  if (singleId!=null) {
273  if (this.referencesUsedInIfElseBody!=null)
274  this.referencesUsedInIfElseBody.Remove(singleId);
275  if (this.referencesUsedInCaseBody!=null)
276  this.referencesUsedInCaseBody.Remove(singleId);
277  }
278  return null;
279  }
280 
281  #endregion
282 
283  #region Visit(SingleIdentifierExpression node, Object obj)
284  public override Object Visit(SingleIdentifierExpression node, Object obj) {
288  // * Is our father a field access
289  bool isMember = true;
290  if (obj != null)
291  isMember = getInheritedAttributes<Boolean>(obj);
292  // * If we are in a if statement, we add the reference to the list of used references (node IfElseStatement of the AST)
293  if (!isMember) {
294  if (this.referencesUsedInIfElseBody != null && !this.referencesUsedInIfElseBody.Contains(node))
295  this.referencesUsedInIfElseBody.Add(node);
296  if (this.referencesUsedInCaseBody != null && !this.referencesUsedInCaseBody.Contains(node))
297  this.referencesUsedInCaseBody.Add(node);
298  }
299 
300  int iValue;
301  if (node.LeftExpression) {
302  this.map.Increment(node.Identifier);
303  }
304 
305  if ((iValue = this.map.Search(node.Identifier)) != -1) {
306  node.IndexOfSSA = iValue;
307  }
308 
309  return null;
310  }
311 
312  #endregion
313 
314  #region Visit(FieldAccessExpression node, Object obj)
315 
316  public override Object Visit(FieldAccessExpression node, Object obj) {
317  Object aux = null;
318  if ((aux = node.Expression.Accept(this, false)) is SingleIdentifierExpression)
319  node.Expression = (SingleIdentifierExpression)aux;
320  node.FieldName.Accept(this, true);
321  return null;
322  }
323 
324  #endregion
325 
326  // ---
327 
328  #region Visit(ArgumentExpression node, Object obj)
329 
330  public override Object Visit(ArgumentExpression node, Object obj) {
331  Object aux = null;
332  node.Argument.LeftExpression = node.LeftExpression;
333  if ((aux = node.Argument.Accept(this, false)) is SingleIdentifierExpression)
334  node.Argument = (SingleIdentifierExpression)aux;
335  return null;
336  }
337 
338  #endregion
339 
340  #region Visit(ArithmeticExpression node, Object obj)
341 
342  public override Object Visit(ArithmeticExpression node, Object obj) {
343  Object aux = null;
344  node.FirstOperand.LeftExpression = node.LeftExpression;
345  node.SecondOperand.LeftExpression = node.LeftExpression;
346  if ((aux = node.FirstOperand.Accept(this, false)) is SingleIdentifierExpression)
347  node.FirstOperand = (SingleIdentifierExpression)aux;
348  if ((aux = node.SecondOperand.Accept(this, false)) is SingleIdentifierExpression)
349  node.SecondOperand = (SingleIdentifierExpression)aux;
350  return null;
351  }
352 
353  #endregion
354 
355  #region Visit(ArrayAccessExpression node, Object obj)
356 
357  public override Object Visit(ArrayAccessExpression node, Object obj) {
358  Object aux = null;
359  //node.FirstOperand.LeftExpression = node.LeftExpression;
360  node.FirstOperand.LeftExpression = false;
361  node.SecondOperand.LeftExpression = false;
362  if ((aux = node.FirstOperand.Accept(this, false)) is SingleIdentifierExpression)
363  node.FirstOperand = (SingleIdentifierExpression)aux;
364  if ((aux = node.SecondOperand.Accept(this, false)) is SingleIdentifierExpression)
365  node.SecondOperand = (SingleIdentifierExpression)aux;
366  node.FirstOperand.LeftExpression = node.LeftExpression;
367  return null;
368  }
369 
370  #endregion
371 
372  #region Visit(BaseCallExpression node, Object obj)
373 
374  public override Object Visit(BaseCallExpression node, Object obj) {
375  node.Arguments.LeftExpression = false;
376  node.Arguments.Accept(this, false);
377  return null;
378  }
379 
380  #endregion
381 
382  #region Visit(BinaryExpression node, Object obj)
383 
384  public override Object Visit(BinaryExpression node, Object obj) {
385  Object aux = null;
386  node.FirstOperand.LeftExpression = node.LeftExpression;
387  node.SecondOperand.LeftExpression = node.LeftExpression;
388  if ((aux = node.FirstOperand.Accept(this, false)) is SingleIdentifierExpression)
389  node.FirstOperand = (SingleIdentifierExpression)aux;
390  if ((aux = node.SecondOperand.Accept(this, false)) is SingleIdentifierExpression)
391  node.SecondOperand = (SingleIdentifierExpression)aux;
392  return null;
393  }
394 
395  #endregion
396 
397  #region Visit(CastExpression node, Object obj)
398 
399  public override Object Visit(CastExpression node, Object obj) {
400  Object aux = null;
401  node.Expression.LeftExpression = node.LeftExpression;
402  if ((aux = node.Expression.Accept(this, false)) is SingleIdentifierExpression)
403  node.Expression = (SingleIdentifierExpression)aux;
404  return null;
405  }
406 
407  #endregion
408 
409  #region Visit(CompoundExpression node, Object obj)
410 
411  public override Object Visit(CompoundExpression node, Object obj) {
412  Object aux = null;
413  for (int i = 0; i < node.ExpressionCount; i++) {
414  node.GetExpressionElement(i).LeftExpression = node.LeftExpression;
415  if ((aux = node.GetExpressionElement(i).Accept(this, false)) is SingleIdentifierExpression)
417  }
418  return null;
419  }
420 
421  #endregion
422 
423  #region Visit(InvocationExpression node, Object obj)
424 
425  public override Object Visit(InvocationExpression node, Object obj) {
426  Object aux = null;
427  node.Identifier.LeftExpression = node.LeftExpression;
428  node.Arguments.LeftExpression = false;
429  if ((aux = node.Identifier.Accept(this, true)) is SingleIdentifierExpression)
430  node.Identifier = (SingleIdentifierExpression)aux;
431  node.Arguments.Accept(this, false);
432  return null;
433  }
434 
435  #endregion
436 
437  #region Visit(IsExpression node, Object obj)
438 
439  public override Object Visit(IsExpression node, Object obj) {
440  Object aux = null;
441  node.Expression.LeftExpression = node.LeftExpression;
442  if ((aux = node.Expression.Accept(this, false)) is SingleIdentifierExpression)
443  node.Expression = (SingleIdentifierExpression)aux;
444  return null;
445  }
446 
447  #endregion
448 
449  #region Visit(LogicalExpression node, Object obj)
450 
451  public override Object Visit(LogicalExpression node, Object obj) {
452  Object aux = null;
453  node.FirstOperand.LeftExpression = node.LeftExpression;
454  node.SecondOperand.LeftExpression = node.LeftExpression;
455  if ((aux = node.FirstOperand.Accept(this, false)) is SingleIdentifierExpression)
456  node.FirstOperand = (SingleIdentifierExpression)aux;
457  if ((aux = node.SecondOperand.Accept(this, false)) is SingleIdentifierExpression)
458  node.SecondOperand = (SingleIdentifierExpression)aux;
459  return null;
460  }
461 
462  #endregion
463 
464  #region Visit(NewArrayExpression node, Object obj)
465 
466  public override Object Visit(NewArrayExpression node, Object obj) {
467  Object aux = null;
468  if (node.Size != null) {
469  node.Size.LeftExpression = node.LeftExpression;
470  if ((aux = node.Size.Accept(this, false)) is SingleIdentifierExpression)
471  node.Size = (SingleIdentifierExpression)aux;
472  }
473  if (node.Init != null) {
474  node.Init.LeftExpression = node.LeftExpression;
475  node.Init.Accept(this, false);
476  }
477  return null;
478  }
479 
480  #endregion
481 
482  #region Visit(NewExpression node, Object obj)
483 
484  public override Object Visit(NewExpression node, Object obj) {
485  node.Arguments.LeftExpression = false;
486  node.Arguments.Accept(this, false);
487  return null;
488  }
489 
490  #endregion
491 
492  #region Visit(QualifiedIdentifierExpression node, Object obj)
493 
494  public override Object Visit(QualifiedIdentifierExpression node, Object obj) {
495  node.IdName.LeftExpression = node.LeftExpression;
496  node.IdExpression.LeftExpression = node.LeftExpression;
497  node.IdName.Accept(this, false);
498  node.IdExpression.Accept(this, true);
499  return null;
500  }
501 
502  #endregion
503 
504  #region Visit(RelationalExpression node, Object obj)
505 
506  public override Object Visit(RelationalExpression node, Object obj) {
507  Object aux = null;
508  node.FirstOperand.LeftExpression = node.LeftExpression;
509  node.SecondOperand.LeftExpression = node.LeftExpression;
510  if ((aux = node.FirstOperand.Accept(this, false)) is SingleIdentifierExpression)
511  node.FirstOperand = (SingleIdentifierExpression)aux;
512  if ((aux = node.SecondOperand.Accept(this, false)) is SingleIdentifierExpression)
513  node.SecondOperand = (SingleIdentifierExpression)aux;
514  return null;
515  }
516 
517  #endregion
518 
519  #region Visit(TernaryExpression node, Object obj)
520 
521  public override Object Visit(TernaryExpression node, Object obj) {
522  Object aux = null;
523  node.FirstOperand.LeftExpression = node.LeftExpression;
524  node.SecondOperand.LeftExpression = node.LeftExpression;
525  node.ThirdOperand.LeftExpression = node.LeftExpression;
526  if ((aux = node.FirstOperand.Accept(this, false)) is SingleIdentifierExpression)
527  node.FirstOperand = (SingleIdentifierExpression)aux;
528  if ((aux = node.SecondOperand.Accept(this, false)) is SingleIdentifierExpression)
529  node.SecondOperand = (SingleIdentifierExpression)aux;
530  if ((aux = node.ThirdOperand.Accept(this, false)) is SingleIdentifierExpression)
531  node.ThirdOperand = (SingleIdentifierExpression)aux;
532  return null;
533  }
534 
535  #endregion
536 
537  #region Visit(UnaryExpression node, Object obj)
538 
539  public override Object Visit(UnaryExpression node, Object obj)
540  {
541  Object aux = null;
542  node.Operand.LeftExpression = node.LeftExpression;
543 
544  if ((aux = node.Operand.Accept(this, false)) is SingleIdentifierExpression)
545  node.Operand = (SingleIdentifierExpression)aux;
546 
547  return null;
548  }
549 
550  #endregion
551 
552  // Statements
553 
554  #region Visit(Block node, Object obj)
555 
557  public override Object Visit(Block node, Object obj) {
558  bool returnFound = false;
559 
560  for (int i = 0; i < node.StatementCount; i++) {
561  returnFound = false;
562  Object aux = node.GetStatementElement(i).Accept(this, obj);
563  if (aux is bool)
564  returnFound = (bool)aux;
565  }
566  return returnFound;
567  }
568 
569  #endregion
570 
571  #region Visit(ReturnStatement node, Object obj)
572 
573  public override Object Visit(ReturnStatement node, Object obj) {
574  Object aux = null;
575  if (node.ReturnExpression != null)
576  if ((aux = node.ReturnExpression.Accept(this, false)) is SingleIdentifierExpression)
577  node.ReturnExpression = (SingleIdentifierExpression)aux;
578  return true;
579  }
580 
581  #endregion
582 
583  #region Visit(IfElseStatement node, Object obj)
584 
585  public override Object Visit(IfElseStatement node, Object obj) {
586  Object aux = null;
587  if ((aux = node.Condition.Accept(this, false)) is SingleIdentifierExpression)
588  node.Condition = (SingleIdentifierExpression)aux;
589  SSAMap map1 = this.map.Clone();
590  this.map.SetScope();
591 
592  // * Updates the current if statatement body being analyzed
593  IList<SingleIdentifierExpression> previousReferencesUsedInIfElseBody = this.referencesUsedInIfElseBody;
594  this.referencesUsedInIfElseBody = node.ReferencesUsedInTrueBranch;
595  node.TrueBranch.Accept(this, obj);
596 
597  this.addLocalVariable(this.map.ResetScope(), node.TrueBranch);
598  SSAMap map2 = this.map.Clone();
599  this.map.SetScope();
600  this.referencesUsedInIfElseBody = node.ReferencesUsedInFalseBranch;
601  node.FalseBranch.Accept(this, obj);
602  // * Updates the previous if statatement being analyzed
603  this.referencesUsedInIfElseBody = previousReferencesUsedInIfElseBody;
604 
605  this.addLocalVariable(this.map.ResetScope(), node.FalseBranch);
606  // map3 = this.map
607 
608  List<MoveStatement> mvSt = map1.GetMoveStatementsForIf(map2, this.map, node.Location.FileName, node.Condition.Location.Line);
609  if (mvSt.Count != 0)
610  node.AfterCondition = mvSt;
611 
612  SSAInfo info = new SSAInfo(map2, this.map, null, null);
613  node.TrueBranch.Accept(new VisitorSSA2(), info);
614 
615  info = new SSAInfo(this.map, this.map, map2, map1);
616  node.FalseBranch.Accept(new VisitorSSA2(), info);
617 
618  List<ThetaStatement> thSt;
619  if (node.HaveElseBlock())
620  thSt = map2.GetThetaStatements(ref this.map, map1, node.Location.FileName, node.FalseBranch.Location.Line);
621  else
622  thSt = map1.GetThetaStatements(ref this.map, node.Location.FileName, node.FalseBranch.Location.Line);
623 
624  if (thSt.Count != 0)
625  node.ThetaStatements = thSt;
626 
627  return null;
628  }
629 
630  #endregion
631 
632  #region Visit(WhileStatement node, Object obj)
633 
634  public override Object Visit(WhileStatement node, Object obj) {
635  Object aux = null;
636  SSAMap map1 = this.map.Clone();
637  if ((aux = node.Condition.Accept(this, false)) is SingleIdentifierExpression)
638  node.Condition = (SingleIdentifierExpression)aux;
639  SSAMap map2 = this.map.Clone();
640  this.map.SetScope();
641  node.Statements.Accept(this, obj);
642  this.addLocalVariable(this.map.ResetScope(), node.Statements);
643  // map3 = this.map
644 
645  List<MoveStatement> mvSt = map1.GetMoveStatements(this.map, node.Location.FileName, node.Location.Line);
646  if (mvSt.Count != 0)
647  node.InitWhile = mvSt;
648 
649  List<ThetaStatement> thSt = map1.GetThetaStatements(map2, ref this.map, node.Location.FileName, node.Condition.Location.Line);
650  if (thSt.Count != 0)
651  node.BeforeCondition = thSt;
652 
653  mvSt = map1.GetMoveStatements(map2, this.map, node.Location.FileName, node.Condition.Location.Line);
654  if (mvSt.Count != 0)
655  node.AfterCondition = mvSt;
656 
657  SSAInfo info = new SSAInfo(null, null, map1, this.map);
658  node.Condition.Accept(new VisitorSSA2(), info);
659 
660  info = new SSAInfo(this.map, null, map1, this.map);
661  node.Statements.Accept(new VisitorSSA2(), info);
662 
663  return null;
664  }
665 
666  #endregion
667 
668  #region Visit(ForStatement node, Object obj)
669 
670  public override Object Visit(ForStatement node, Object obj) {
671  Object aux = null;
672  this.map.SetScope();
673  for (int i = 0; i < node.InitializerCount; i++) {
674  node.GetInitializerElement(i).Accept(this, false);
675  }
676  SSAMap map1 = this.map.Clone();
677 
678  if ((aux = node.Condition.Accept(this, false)) is SingleIdentifierExpression)
679  node.Condition = (SingleIdentifierExpression)aux;
680  SSAMap map2 = this.map.Clone();
681 
682  node.Statements.Accept(this, obj);
683  SSAMap map3 = this.map.Clone();
684 
685  for (int i = 0; i < node.IteratorCount; i++) {
686  node.GetIteratorElement(i).Accept(this, false);
687  }
688  // map4 = this.map
689 
690  List<MoveStatement> mvSt = map1.GetMoveStatements(this.map, node.Location.FileName, node.Location.Line);
691  if (mvSt.Count != 0)
692  node.AfterInit = mvSt;
693 
694  List<ThetaStatement> thSt = map1.GetThetaStatements(map2, ref this.map, node.Location.FileName, node.Location.Line);
695  if (thSt.Count != 0)
696  node.BeforeCondition = thSt;
697 
698  mvSt = map1.GetMoveStatements(map2, this.map, node.Location.FileName, node.Location.Line);
699  if (mvSt.Count != 0)
700  node.AfterCondition = mvSt;
701 
702  SSAInfo info = new SSAInfo(null, null, map1, this.map);
703  node.Condition.Accept(new VisitorSSA2(), info);
704 
705  info = new SSAInfo(this.map, null, map1, this.map);
706  node.Statements.Accept(new VisitorSSA2(), info);
707 
708  info = new SSAInfo(this.map, null, map1, this.map);
709  for (int i = 0; i < node.IteratorCount; i++) {
710  node.GetIteratorElement(i).Accept(new VisitorSSA2(), info);
711  }
712 
713  this.addLocalVariable(this.map.ResetScope(), node.AuxInitializer);
714  node.UpdateInitializer();
715 
716  return null;
717  }
718 
719  #endregion
720 
721  #region Visit(DoStatement node, Object obj)
722 
723  public override Object Visit(DoStatement node, Object obj) {
724  Object aux = null;
725  SSAMap map1 = this.map.Clone();
726  this.map.SetScope();
727  node.Statements.Accept(this, obj);
728  this.addLocalVariable(this.map.ResetScope(), node.Statements);
729  SSAMap map2 = this.map.Clone();
730  if ((aux = node.Condition.Accept(this, false)) is SingleIdentifierExpression)
731  node.Condition = (SingleIdentifierExpression)aux;
732  // map3 = this.map
733 
734  List<MoveStatement> mvSt = map1.GetMoveStatements(this.map, node.Location.FileName, node.Location.Line);
735  if (mvSt.Count != 0)
736  node.InitDo = mvSt;
737 
738  List<ThetaStatement> thSt = map1.GetThetaStatements(ref this.map, node.Location.FileName, node.Statements.Location.Line);
739  if (thSt.Count != 0)
740  node.BeforeBody = thSt;
741 
742  SSAInfo info = new SSAInfo(this.map, null, map1, this.map);
743  node.Statements.Accept(new VisitorSSA2(), info);
744 
745  info = new SSAInfo(this.map, null, map1, this.map);
746  node.Condition.Accept(new VisitorSSA2(), info);
747 
748  return null;
749  }
750 
751  #endregion
752 
753  #region Visit(SwitchStatement node, Object obj)
754 
755  public override Object Visit(SwitchStatement node, Object obj) {
756  Object aux = null;
757  List<SSAMap> mapsSwitch = new List<SSAMap>();
758  int indexDefaultMap = -1;
759 
760  if ((aux = node.Condition.Accept(this, false)) is SingleIdentifierExpression)
761  node.Condition = (SingleIdentifierExpression)aux;
762  SSAMap map0 = this.map.Clone();
763 
764  IList<SingleIdentifierExpression> previousCaseReferences = this.referencesUsedInCaseBody;
765  for (int i = 0; i < node.SwitchBlockCount; i++) {
766  // * Updates the current if statatement body being analyzed
767  IList<SingleIdentifierExpression> caseReferences=new List<SingleIdentifierExpression>();
768  node.ReferencesUsedCases[node.GetSwitchSectionElement(i).SwitchBlock] = caseReferences;
769  IList<SingleIdentifierExpression> previousReferencesUsedInCaseBody = this.referencesUsedInCaseBody;
770  this.referencesUsedInCaseBody = caseReferences;
771 
772  this.map.SetScope();
773  node.GetSwitchSectionElement(i).Accept(this, false);
774  if (node.GetSwitchSectionElement(i).IsDefaultCase())
775  indexDefaultMap = i;
776  this.addLocalVariable(this.map.ResetScope(), node.GetSwitchSectionElement(i).SwitchBlock);
777  mapsSwitch.Add(this.map.Clone());
778  }
779  this.referencesUsedInCaseBody = previousCaseReferences;
780 
781  // lastMap => this.map
782 
783  List<MoveStatement> mvSt;
784  if (indexDefaultMap == -1)
785  mvSt = map0.GetMoveStatements(this.map, node.Location.FileName, node.Location.Line);
786  else
787  mvSt = map0.GetMoveStatementsForSwitch(mapsSwitch, node.Location.FileName, node.Location.Line);
788  if (mvSt.Count != 0)
789  node.AfterCondition = mvSt;
790 
791 
792  for (int i = 0; i < node.SwitchBlockCount; i++) {
793  SSAInfo info;
794  if (i > 0)
795  info = new SSAInfo(mapsSwitch[i], this.map, mapsSwitch[i - 1], map0);
796  else
797  info = new SSAInfo(mapsSwitch[i], this.map, null, null);
798  node.GetSwitchSectionElement(i).Accept(new VisitorSSA2(), info);
799  }
800 
801  List<ThetaStatement> thSt;
802  if (indexDefaultMap != -1)
803  thSt = map0.GetThetaStatements(mapsSwitch, ref this.map, true, node.Location.FileName, node.GetSwitchSectionElement(node.SwitchBlockCount - 1).SwitchBlock.Location.Line);
804  else
805  thSt = map0.GetThetaStatements(mapsSwitch, ref this.map, false, node.Location.FileName, node.GetSwitchSectionElement(node.SwitchBlockCount - 1).SwitchBlock.Location.Line);
806 
807  if (thSt.Count != 0)
808  node.ThetaStatements = thSt;
809 
810  return null;
811  }
812  #endregion
813 
814  }
815 }
Encapsulates a Return statement of our programming languages.
Encapsulates a definition of a concrete method.
Statement Statements
Gets the block executed when the condition is true.
Definition: DoStatement.cs:75
Encapsulates a binary expression of our programming language.
Expression Identifier
Gets the expression to invoke
Encapsulates the qualified name expression.
Encapsulates a Do statement of our programming language.
Definition: DoStatement.cs:34
Expression FirstOperand
Gets the first operand of the ternary expression
Encapsulates the expression to access a field.
Encapsulates a block of statements.
Definition: Block.cs:34
override Object Visit(CompoundExpression node, Object obj)
Definition: VisitorSSA.cs:411
override Object Visit(ArgumentExpression node, Object obj)
Definition: VisitorSSA.cs:330
override Object Visit(IfElseStatement node, Object obj)
Definition: VisitorSSA.cs:585
override Object Visit(TernaryExpression node, Object obj)
Definition: VisitorSSA.cs:521
This class stores the information to use in SSA algorithm
Definition: SSAInfo.cs:27
override Object Visit(ConstructorDefinition node, Object obj)
Definition: VisitorSSA.cs:227
bool HaveElseBlock()
Returns true if the statement has a else block. Otherwise, false.
Encapsulates a logical binary expression.
Statement Statements
Gets the block executed when the condition is true.
Encapsulates a invocation expression.
Encapsulates a cast expression.
Expression Condition
Gets the condition expression of For Statement.
Definition: ForStatement.cs:94
override Object Visit(IdDeclaration node, Object obj)
Definition: VisitorSSA.cs:155
Abstract class represents a programming language statement.
Definition: Statement.cs:30
override Object Visit(Definition node, Object obj)
Definition: VisitorSSA.cs:167
Block AuxInitializer
Gets the auxiliar block of variable declarations
override Object Visit(IsExpression node, Object obj)
Definition: VisitorSSA.cs:439
Expression Operand
Gets the operand expression of the unary operation
override Object Visit(ArithmeticExpression node, Object obj)
Definition: VisitorSSA.cs:342
Location Location
Definition: AstNode.cs:45
Encapsulates a definition.
Definition: Definition.cs:36
Encapsulates arithmetic binary expressions.
override Object Visit(ArrayAccessExpression node, Object obj)
Definition: VisitorSSA.cs:357
Expression Condition
Gets the condition expression of If-Else statement.
SwitchSection GetSwitchSectionElement(int index)
Gets the element stored in the specified index.
Encapsulates a For statement of our programming languages.
Definition: ForStatement.cs:34
Encapsulates a While statement of our programming languages.
void SetExpressionElement(int index, SingleIdentifierExpression exp)
Updates the value of the element stored in the specified index.
override Object Visit(FieldAccessExpression node, Object obj)
Definition: VisitorSSA.cs:316
Encapsulates a Is expression.
Definition: IsExpression.cs:35
Expression SecondOperand
Gets the second operand of the binary expression
override Object Visit(ForStatement node, Object obj)
Definition: VisitorSSA.cs:670
Statement FalseBranch
Gets the block executed when the condition is false.
Statement TrueBranch
Gets the block executed when the condition is true.
Encapsulates a identifier expression of our programming language.
override Object Visit(Block node, Object obj)
Returns true if the last sentence is a return statement. Otherwise, false.
Definition: VisitorSSA.cs:557
Expression Argument
Represents the option of the argument
Encapsulates a set of expressions.
VisitorSSA()
Constructor of VisitorSSA
Definition: VisitorSSA.cs:70
List< Parameter > ParametersInfo
Gets the parameters information used to obtain its type
Abstract class to define different visits over the abstract syntax tree. It makes a deep walker...
int Line
Gets line where the item is located
Definition: Location.cs:58
Encapsulates a relational binary expression.
This class makes the second part of static single assignment algorithm in which every variable is ass...
Definition: VisitorSSA2.cs:35
bool LeftExpression
Gets or sets true if the expression is allocated in the left part of an assignment, false otherwise.
Definition: Expression.cs:89
Encapsulates assignment binary expressions.
override Object Visit(ReturnStatement node, Object obj)
Definition: VisitorSSA.cs:573
override Object Visit(BinaryExpression node, Object obj)
Definition: VisitorSSA.cs:384
CompoundExpression Init
Gets or sets the array initialization
Encapsulates a ternary expression of our programming language.
Expression Condition
Gets the condition expression of While Statement.
override Object Visit(ClassDefinition node, Object obj)
Definition: VisitorSSA.cs:131
Encapsulates a If-Else statement of our programming language.
override Object Visit(ConstantDefinition node, Object obj)
Definition: VisitorSSA.cs:180
Encapsulates a new array expression.
Encapsulates a definition of a concrete constructor.
Declaration GetMemberElement(int index)
Gets the element stored in the specified index.
override Object Visit(SwitchStatement node, Object obj)
Definition: VisitorSSA.cs:755
override Object Visit(CastExpression node, Object obj)
Definition: VisitorSSA.cs:399
Expression GetExpressionElement(int index)
Gets the element stored in the specified index.
abstract Object Accept(Visitor v, Object o)
Accept method of a concrete visitor.
Encapsulates a declaration.
Encapsulates a argument expression of our programming language.
Expression Condition
Gets the condition expression of Do Statement.
Definition: DoStatement.cs:66
Encapsulates a &#39;this&#39; expression.
override Object Visit(AssignmentExpression node, Object obj)
Definition: VisitorSSA.cs:261
Expression Expression
Gets the expression.
Definition: IsExpression.cs:62
Encapsulates a unary expression of our programming language.
override Object Visit(WhileStatement node, Object obj)
Definition: VisitorSSA.cs:634
This class makes the static single assignment algorithm in which every variable is assigned exactly o...
Definition: VisitorSSA.cs:38
Expression Size
Gets or sets the array size
InvocationExpression Initialization
Gets the base or this initialization of the constructor definition
Expression ReturnExpression
Gets the return expression.
override Object Visit(RelationalExpression node, Object obj)
Definition: VisitorSSA.cs:506
override Object Visit(UnaryExpression node, Object obj)
Definition: VisitorSSA.cs:539
Encapsulates a declaration of a concrete field.
override Object Visit(DoStatement node, Object obj)
Definition: VisitorSSA.cs:723
Encapsulates a constant definition.
Encapsulates the array expression to access the concrete position.
override Object Visit(NewExpression node, Object obj)
Definition: VisitorSSA.cs:484
override Object Accept(Visitor v, Object o)
Accept method of a concrete visitor.
Definition: Block.cs:174
Expression ThirdOperand
Gets the third operand of the ternary expression
override Object Visit(MethodDefinition node, Object obj)
Definition: VisitorSSA.cs:193
string FullName
Gets or sets the nominal type of the declaration
Definition: Declaration.cs:72
Expression SecondOperand
Gets the second operand of the ternary expression
Expression FirstOperand
Gets the first operand of the binary expression
Expression Condition
Gets the condition expression of Switch Statement.
string Identifier
Gets the name associated to the declaration
override Object Visit(BaseCallExpression node, Object obj)
Definition: VisitorSSA.cs:374
string Identifier
Gets the name name.
Implementation of a map to use in static single assignment algorithm.
Definition: SSAMap.cs:27
Block Body
Gets the body of the method
override Object Visit(LogicalExpression node, Object obj)
Definition: VisitorSSA.cs:451
AssignmentOperator
Assignment binary operators
Expression Expression
Gets the expression to access a field.
override Object Visit(QualifiedIdentifierExpression node, Object obj)
Definition: VisitorSSA.cs:494
Encapsulates a invocation expression to base class.
string FileName
Gets de name of the file
Definition: Location.cs:52
Encapsulates a new expression.
Encapsulates a definition of a concrete class.
Expression Expression
Gets the expression to convert.
Encapsulates a Switch statement of our programming languages.
override Object Visit(NewArrayExpression node, Object obj)
Definition: VisitorSSA.cs:466
override Object Visit(InvocationExpression node, Object obj)
Definition: VisitorSSA.cs:425