16 using System.Collections.Generic;
21 using TypeSystem.Constraints;
22 using ErrorManagement;
23 using TypeSystem.Operations;
25 namespace Semantic.SSAAlgorithm {
29 static class SSAHelper {
31 #region SimplifyReferencesUsedInBodies()
32 public static void SimplifyReferences(IList<SingleIdentifierExpression> references) {
37 for (
int i = references.Count - 1; i >= 0; i--) {
40 references.RemoveAt(i);
44 if (references[i].IdSymbol!=null && (symbolType = references[i].IdSymbol.SymbolType) != null)
52 references.RemoveAt(i);
57 #region CloneTypesOfReferences()
58 public static void CloneTypesOfReferences(IList<SingleIdentifierExpression> references,
MethodType methodAnalyzed, IDictionary<SingleIdentifierExpression, TypeExpression> typeExpressions) {
65 IDictionary<int, TypeVariable> clonedTypeVariables =
new Dictionary<int, TypeVariable>();
66 IList<EquivalenceClass> equivalenceClasses =
new List<EquivalenceClass>();
70 if (!typeExpressions.ContainsKey(singleId))
72 typeExpressions[singleId] = null;
74 typeExpressions[singleId] = singleId.IdSymbol.SymbolType.Clone(clonedTypeVariables, equivalenceClasses, methodAnalyzed);
77 CloneEquivalenceClasses(clonedTypeVariables, equivalenceClasses, methodAnalyzed);
79 public static void CloneTypesOfReferences(IDictionary<SingleIdentifierExpression, TypeExpression> references,
MethodType methodAnalyzed, IDictionary<SingleIdentifierExpression, TypeExpression> typeExpressions) {
80 IList<SingleIdentifierExpression> list =
new List<SingleIdentifierExpression>();
81 foreach (KeyValuePair<SingleIdentifierExpression, TypeExpression> pair
in references) {
82 pair.Key.IdSymbol.SymbolType = pair.Value;
85 CloneTypesOfReferences(list, methodAnalyzed, typeExpressions);
89 #region CloneEquivalenceClasses()
90 private static void CloneEquivalenceClasses(IDictionary<int, TypeVariable> clonedTypeVariables, IList<EquivalenceClass> equivalenceClasses,
MethodType methodAnalyzed) {
102 newSubstitution = oldEquivalenceClass.Substitution.Clone(clonedTypeVariables, equivalenceClasses, methodAnalyzed);
104 newEquivalenceClass.add(newSubstitution, SortOfUnification.Equivalent,
new List<Pair<TypeExpression, TypeExpression>>());
105 foreach (
int typeVariableNumber
in oldEquivalenceClass.
TypeVariables.Keys) {
107 TypeVariable newTypeVariable = clonedTypeVariables[typeVariableNumber];
108 newEquivalenceClass.TypeVariables[newTypeVariable.Variable] = newTypeVariable;
110 newTypeVariable.EquivalenceClass = newEquivalenceClass;
111 clonedTypeVariables[typeVariableNumber].ValidTypeExpression =
false;
125 IDictionary<int, TypeVariable> clonedTypeVariables =
new Dictionary<int, TypeVariable>();
126 IList<EquivalenceClass> equivalenceClasses =
new List<EquivalenceClass>();
127 TypeExpression newTypeExpression = typeExpression.Clone(clonedTypeVariables, equivalenceClasses, methodAnalyzed);
128 CloneEquivalenceClasses(clonedTypeVariables, equivalenceClasses, methodAnalyzed);
129 return newTypeExpression;
133 #region GetTypesOfReferences()
134 public static IDictionary<SingleIdentifierExpression, TypeExpression> GetTypesOfReferences(IList<SingleIdentifierExpression> references) {
140 IDictionary<SingleIdentifierExpression, TypeExpression> typeExpressions =
new Dictionary<SingleIdentifierExpression, TypeExpression>();
143 typeExpressions[singleId] = singleId.IdSymbol.SymbolType;
144 return typeExpressions;
156 public static IDictionary<SingleIdentifierExpression, TypeExpression> Intersection(IDictionary<SingleIdentifierExpression, TypeExpression> setA, IList<SingleIdentifierExpression> setB) {
157 IDictionary<SingleIdentifierExpression, TypeExpression> typeExpressionIntersectionSet =
new Dictionary<SingleIdentifierExpression, TypeExpression>();
158 foreach (KeyValuePair<SingleIdentifierExpression, TypeExpression> pair
in setA)
159 if (setB.Contains(pair.Key))
160 typeExpressionIntersectionSet[pair.Key] = pair.Value;
161 return typeExpressionIntersectionSet;
165 #region SetTypesOfReferences()
166 public static void SetTypesOfReferences(IDictionary<SingleIdentifierExpression, TypeExpression> source, IList<SingleIdentifierExpression> destination) {
174 if (source.ContainsKey(singleId))
175 singleId.
IdSymbol.SymbolType = source[singleId];
179 #region SetUnionTypesOfReferences()
180 public static void SetUnionTypesOfReferences(IDictionary<SingleIdentifierExpression, TypeExpression> referencesBeforeIfElse,
191 IDictionary<SingleIdentifierExpression, TypeExpression> referencesAfterIf,
192 IDictionary<SingleIdentifierExpression, TypeExpression> referencesAfterElse,
194 IList<SingleIdentifierExpression> alreadyVisited =
new List<SingleIdentifierExpression>();
195 foreach (KeyValuePair<SingleIdentifierExpression, TypeExpression> pair
in referencesAfterIf) {
196 alreadyVisited.Add(pair.Key);
197 if (referencesAfterElse.ContainsKey(pair.Key))
199 pair.Key.IdSymbol.SymbolType =
UnionType.
collect(pair.Value, referencesAfterElse[pair.Key]);
202 pair.Key.IdSymbol.SymbolType = UnionType.collect(pair.Value, referencesBeforeIfElse[pair.Key]);
205 foreach (KeyValuePair<SingleIdentifierExpression, TypeExpression> pair
in referencesAfterElse) {
206 if (alreadyVisited.Contains(pair.Key))
208 alreadyVisited.Add(pair.Key);
210 pair.Key.IdSymbol.SymbolType = UnionType.collect(pair.Value, referencesBeforeIfElse[pair.Key]);
227 public static void SetUnionTypesOfReferences(IDictionary<
Block, IDictionary<SingleIdentifierExpression, TypeExpression>> typeExpressionsBeforeSwitch,
228 IDictionary<
Block, IDictionary<SingleIdentifierExpression, TypeExpression>> typeExpressionsAfterCases,
229 IDictionary<SingleIdentifierExpression, TypeExpression> typeExpressionIntersectionSet,
232 foreach (KeyValuePair<
Block, IDictionary<SingleIdentifierExpression, TypeExpression>> pair1
in typeExpressionsBeforeSwitch)
233 foreach (KeyValuePair<SingleIdentifierExpression, TypeExpression> pair2
in pair1.Value)
234 pair2.Key.IdSymbol.SymbolType = null;
237 foreach (KeyValuePair<
Block, IDictionary<SingleIdentifierExpression, TypeExpression>> pair1
in typeExpressionsAfterCases)
238 foreach (KeyValuePair<SingleIdentifierExpression, TypeExpression> pair2
in pair1.Value)
239 pair2.Key.IdSymbol.SymbolType = UnionType.collect(pair2.Key.IdSymbol.SymbolType, pair2.Value);
242 foreach (KeyValuePair<
Block, IDictionary<SingleIdentifierExpression, TypeExpression>> pair1
in typeExpressionsBeforeSwitch)
243 foreach (KeyValuePair<SingleIdentifierExpression, TypeExpression> pair2
in pair1.Value)
244 if (typeExpressionIntersectionSet == null || !typeExpressionIntersectionSet.ContainsKey(pair2.Key))
245 pair2.Key.IdSymbol.SymbolType =
UnionType.
collect(pair2.Key.IdSymbol.SymbolType, pair2.Value);
248 #region AssignAttributes()
249 public static void AssignAttributes(
UserType classType, IList<TypeExpression> typesOfThisAfterCases,
259 if (methodAnalyzed == null)
261 foreach (KeyValuePair<string, AccessModifier> pair
in classType.
Fields) {
265 TypeExpression eachFieldType = getFieldType(typeOfThis, pair.Key);
266 unionType = UnionType.collect(unionType, eachFieldType);
284 if (methodAnalyzed == null)
286 foreach (KeyValuePair<string, AccessModifier> pair
in classType.
Fields) {
288 fieldType1 = getFieldType(typeOfThis1, pair.Key),
289 fieldType2 = getFieldType(typeOfThis2, pair.Key);
290 TypeExpression unionType = UnionType.collect(fieldType1, fieldType2);
296 if (classType != null) {
297 if (!classType.
Fields.ContainsKey(fieldName))
301 if (fieldType == null)
303 return fieldType.FieldTypeExpression;
306 if (unionType != null) {
309 TypeExpression newType = SSAHelper.getFieldType(typeInUnion, fieldName);
310 newUnionType.AddType(newType);
Dictionary< string, AccessModifier > Fields
Gets the field list
Encapsulates a block of statements.
TypeExpression Substitution
A equivalence class could be bounded to a unique no variable type
Representa an array type.
This class encapsulates a location in a specific file. Implements an Inmutable pattern. So it can be used in any context, that is his internal fields never change.
IDictionary< int, TypeVariable > TypeVariables
All the type variables that must be substituted with the same value
Abstract class that represents all different types.
Represents a generic type expression
Encapsulates a identifier expression of our programming language.
Implements Double dispatcher Pattern. The class encapsulates encapsulates all it's needed to perform ...
virtual bool HasTypeVariables()
To know if the type expression has some type variables and requieres unification The default implemen...
Representa a property type.
Representa a method type.
Symbol IdSymbol
Gets or sets the symbol of the identifier.
IList< TypeExpression > TypeSet
Gets the list of type expressions
static TypeExpression collect(TypeExpression collection, TypeExpression newType)
This static helper method group a newType into a union collection of types.
AssignmentOperator
Assignment binary operators
Represents a class or interface type.