The StaDyn Programming Language
Source code documentation of StaDyn, a hybrid static and dynamic typing language.
FieldType.cs
Go to the documentation of this file.
1 // -------------------------------------------------------------------------- //
3 // Project rROTOR //
4 // -------------------------------------------------------------------------- //
5 // File: FieldType.cs //
6 // Authors: Cristina Gonzalez Muņoz - cristi.gm@gmail.com //
7 // Francisco Ortin - francisco.ortin@gmail.com //
8 // Description: //
9 // Represents a field type. //
10 // Inheritance: MemberType. //
11 // Implements Composite pattern [Composite]. //
12 // -------------------------------------------------------------------------- //
13 // Create date: 27-01-2007 //
14 // Modification date: 11-06-2007 //
16 
17 using System;
18 using System.Collections.Generic;
19 using System.Text;
20 using System.Text.RegularExpressions;
21 
22 using AST;
23 using ErrorManagement;
24 using TypeSystem.Constraints;
25 using Tools;
26 using DynVarManagement;
27 using TypeSystem.Operations;
28 //visto
29 namespace TypeSystem {
38  #region Fields
39 
43  private TypeExpression fieldType;
44 
48  private AccessModifier memberInfo;
49 
50 
51  #endregion
52 
53  #region Properties
54 
59  get { return this.fieldType; }
60  }
61 
66  get { return this.memberInfo; }
67  set {
68  if (this.memberInfo == null) {
69  this.memberInfo = value;
70  }
71  else
72  ErrorManager.Instance.NotifyError(new ClassTypeInfoError(this.memberInfo.MemberIdentifier, this.memberInfo.Class.FullName, value.Class.FullName));
73  }
74  }
75 
79  internal override bool ValidTypeExpression {
80  set {
81  validTypeExpression = value;
82  if (!value) {
83  // * Updates the full name
84  this.BuildFullName();
85  }
86  }
87  }
88 
89  #endregion
90 
91  #region Constructor
92 
96  public FieldType(TypeExpression type) {
97  if (type != null) {
98  this.fieldType = type;
99  this.fullName = type.fullName;
100  }
101  }
102 
103  #endregion
104 
105  #region BuildTypeExpressionString
106 
110  public override string BuildTypeExpressionString(int depthLevel) {
111  if (this.ValidTypeExpression) return this.typeExpression;
112  if (depthLevel <= 0)
113  return this.FullName;
114 
115  //this.fullName = this.MemberInfo.Class.FullName + "." + this.MemberInfo.MemberIdentifier;
116  if (this.fieldType == null)
117  return "null";
118  this.fullName = this.fieldType.FullName;
119 
120  StringBuilder tE = new StringBuilder();
121  // tE: Field(IdClass, IdField, mods, type)
122  tE.AppendFormat("Field({0}, {1},", this.MemberInfo.Class.BuildTypeExpressionString(depthLevel - 1), this.MemberInfo.MemberIdentifier);
123  // modifiers
124  if (this.MemberInfo.Modifiers.Count != 0) {
125  for (int i = 0; i < this.MemberInfo.Modifiers.Count - 1; i++) {
126  tE.AppendFormat(" {0} x", this.MemberInfo.Modifiers[i]);
127  }
128  tE.AppendFormat(" {0}", this.MemberInfo.Modifiers[this.MemberInfo.Modifiers.Count - 1]);
129  }
130  tE.Append(", ");
131 
132  // type
133  tE.Append(this.fieldType.BuildTypeExpressionString(depthLevel - 1));
134  tE.Append(")");
135  this.ValidTypeExpression = true;
136  return this.typeExpression = tE.ToString();
137  }
138  #endregion
139 
140  #region BuildFullName()
141  public override void BuildFullName() {
145  this.fieldType.BuildFullName();
146  this.fullName = this.fieldType.fullName;
147  }
148  #endregion
149 
150  // WriteType inference
151 
152  #region Dispatcher
153  public override object AcceptOperation(TypeSystemOperation op, object arg) { return op.Exec(this, arg); }
154  #endregion
155 
156  #region Dot() ANULADA
157 
170  //public override TypeExpression Dot(string field, MethodType methodAnalyzed, IList<TypeExpression> previousDot, Location loc) {
171  // if (this.fieldType != null) {
172  // // * If the field type is a dynamic union type, so it is the union
173  // UnionType unionType = As<UnionType>(this.fieldType);
174  // if (unionType != null)
175  // DynVarOptions.Instance.AssignDynamism(this.fieldType, this.IsDynamic);
176  // return this.fieldType.Dot(field, methodAnalyzed, previousDot, loc);
177  // }
178  // return null;
179  //}
188  //public override TypeExpression Dot(string memberName, IList<TypeExpression> previousDot) {
189  // if (this.fieldType != null)
190  // return this.fieldType.Dot(memberName, previousDot);
191  // return null;
192  //}
193  #endregion
194 
195  //#region Bracket() anulada
196 
207  //public override TypeExpression Bracket(TypeExpression index, MethodType methodAnalyzed, bool showErrorMessage, Location loc) {
208  // if (this.fieldType != null)
209  // return this.fieldType.Bracket(index, methodAnalyzed, showErrorMessage, loc);
210  // return null;
211  //}
212 
213  //#endregion
214 
215  #region Assignment() ANULADA
216  //public override TypeExpression Assignment(TypeExpression operand, AssignmentOperator op, MethodType methodAnalyzed, SortOfUnification unification,
228  // TypeExpression actualImplicitObject, Location location) {
229  // // * We check if a constraint must be generated. Is it an assignment of the implicit object's field?
230  // bool found = false;
231  // // * In case it has free variables and the reference used is this, we add a constraint to the method
232  // if (this.HasTypeVariables() && methodAnalyzed != null && ClassType.IsConcreteType(actualImplicitObject) == null) {
233  // // * They should be the same exact (sub)classes. This represent the same instance, not another instance of the same class.
234  // ClassType methodSuperClass = (ClassType)methodAnalyzed.MemberInfo.Class;
235  // while (!(found = (this.MemberInfo.Class == methodSuperClass)) && methodSuperClass != null)
236  // methodSuperClass = methodSuperClass.BaseClass;
237  // if (found) {
238  // // * An assignment constraint is added, postponing the type inference
239  // // * If an actual implicit object is used, we take its field's type
240  // FieldType fieldType = this;
241  // ClassType thisType = TypeExpression.As<ClassType>(actualImplicitObject);
242  // if (thisType == null) {
243  // FieldType field = TypeExpression.As<FieldType>(actualImplicitObject);
244  // if (field != null)
245  // thisType = TypeExpression.As<ClassType>(field.FieldTypeExpression);
246  // }
247  // if (thisType != null)
248  // fieldType = (FieldType)thisType.Fields[this.MemberInfo.MemberIdentifier].WriteType;
249  // methodAnalyzed.AddConstraint(new FieldTypeAssignmentConstraint(fieldType, operand, unification));
250  // methodAnalyzed.ValidTypeExpression = false;
251  // return this.fieldType;
252  // }
253  // }
254  // if (!found && this.fieldType != null)
255  // return this.fieldType.Assignment(operand, op, null, unification, actualImplicitObject, location);
256  // return null;
257  //}
258 
259  #endregion
260 
261  #region Arithmetic() >NUL>C>
262  /*
274  public override TypeExpression Arithmetic(TypeExpression operand, Enum op, MethodType methodAnalyzed, bool showErrorMessage, Location loc) {
275  if (this.fieldType != null)
276  return this.fieldType.Arithmetic(operand, op, methodAnalyzed, showErrorMessage, loc);
277  return null;
278  }
279 
290  public override TypeExpression Arithmetic(UnaryOperator op, MethodType methodAnalyzed, bool showErrorMessage, Location loc) {
291  return this.fieldType.Arithmetic(op, methodAnalyzed, showErrorMessage, loc);
292  }
293  */
294  #endregion
295 
296  #region Relational()ANULADA
297  /* /// <summary>
308  public override TypeExpression Relational(TypeExpression operand, RelationalOperator op, MethodType methodAnalyzed, bool showErrorMessage, Location loc) {
309  if (this.fieldType != null)
310  return this.fieldType.Relational(operand, op, methodAnalyzed, showErrorMessage, loc);
311  return null;
312  }
313 
314  */
315  #endregion
316 
317  #region PromotionLevl()-->#
318  //public override int PromotionLevel(TypeExpression type) {
323  // if (this.fieldType != null)
324  // return this.fieldType.PromotionLevel(type);
325  // return -1;
326  //}
328 #endregion
329  #region Promotion ANULADA
330  //public override TypeExpression Promotion(TypeExpression type, Enum op, MethodType methodAnalyzed, Location loc) {
340  // if (this.fieldType != null)
341  // return this.fieldType.Promotion(type, op, methodAnalyzed, loc);
342  // return null;
343  //}
344  #endregion
345 
346  #region Cast() ANULADA
347  //public override TypeExpression Cast(TypeExpression castType, MethodType methodAnalyzed, Location loc) {
357  // if (this.fieldType != null)
358  // return this.fieldType.Cast(castType, methodAnalyzed, loc);
359  // return null;
360  //}
361  #endregion
362 
363 
364  // WriteType Unification
365 
366  #region Unify
367  public override bool Unify(TypeExpression te, SortOfUnification unification, IList<Pair<TypeExpression, TypeExpression>> previouslyUnified) {
375  FieldType ft = te as FieldType;
376  if (ft != null) {
377  bool success = this.fieldType.Unify(ft.fieldType, unification, previouslyUnified);
378  if (success) // * Dynamic type
379  DynVarOptions.Instance.AssignDynamism(this, ft.isDynamic);
380  // * Clears the type expression cache
381  this.ValidTypeExpression = false;
382  te.ValidTypeExpression = false;
383  return success;
384  }
385  if (te is TypeVariable && unification != SortOfUnification.Incremental)
386  // * No incremental unification is commutative
387  return te.Unify(this, unification, previouslyUnified);
388  if (te is ClassType && unification == SortOfUnification.Equivalent)
389  return te.Unify(this, unification, previouslyUnified);
390  if (te.IsValueType() && unification == SortOfUnification.Equivalent)
391  return te.Unify(this, unification, previouslyUnified);
392  return false;
393  }
394  #endregion
395 
396  #region HasTypeVariables()
397  public override bool HasTypeVariables() {
403  if (this.validHasTypeVariables)
404  return this.hasTypeVariablesCache;
405  bool toReturn = this.fieldType.HasTypeVariables();
406  this.validHasTypeVariables = true;
407  return this.hasTypeVariablesCache = toReturn;
408 
409  }
410 
411  public bool HasTypeVariables(IList<String> evaluated)
412  {
413  if (this.validHasTypeVariables)
414  return this.hasTypeVariablesCache;
415 
416  bool toReturn;
417  if (this.fieldType is UserType)
418  toReturn = ((UserType) this.fieldType).HasTypeVariables(evaluated);
419  else if(this.fieldType is ClassTypeProxy)
420  toReturn = ((ClassTypeProxy)this.fieldType).HasTypeVariables(evaluated);
421  else
422  toReturn = this.fieldType.HasTypeVariables();
423  this.validHasTypeVariables = true;
424  return this.hasTypeVariablesCache = toReturn;
425 
426  }
427  #endregion
428 
429  #region CloneTypeVariables()
430  public override TypeExpression CloneTypeVariables(IDictionary<TypeVariable, TypeVariable> typeVariableMappings, IList<EquivalenceClass> equivalenceClasses, IList<ClassType> clonedClasses) {
441  if (!this.HasTypeVariables())
442  return this;
443  AccessModifier accesModifier = (AccessModifier)this.MemberInfo.Clone();
444  FieldType fieldType = new FieldType(this.FieldTypeExpression.CloneTypeVariables(typeVariableMappings, equivalenceClasses, clonedClasses));
445  accesModifier.Class = this.MemberInfo.Class;
446  accesModifier.Type = fieldType;
447  fieldType.IsDynamic = this.IsDynamic;
448  fieldType.MemberInfo = accesModifier;
449  this.ValidTypeExpression = fieldType.ValidTypeExpression = false;
450  return fieldType;
451  }
452  #endregion
453 
454  #region UpdateEquivalenceClass()
455  public override void UpdateEquivalenceClass(IDictionary<TypeVariable, TypeVariable> typeVariableMappings, IList<TypeExpression> previouslyUpdated) {
462  // * Checks infinite loops
463  if (previouslyUpdated.Contains(this))
464  return;
465  previouslyUpdated.Add(this);
466 
467  // * Updates the equivalence class
468  if (!this.HasTypeVariables())
469  return;
470  this.fieldType.UpdateEquivalenceClass(typeVariableMappings, previouslyUpdated);
471  this.ValidTypeExpression = false;
472  }
473  #endregion
474 
475  #region ReplaceTypeVariables()
476  public override void ReplaceTypeVariables(IDictionary<TypeVariable, TypeVariable> typeVariableMappings) {
482  TypeVariable fieldTypeVariable = this.fieldType as TypeVariable;
483  if (fieldTypeVariable == null) {
484  if (this.fieldType.HasTypeVariables())
485  this.fieldType.ReplaceTypeVariables(typeVariableMappings);
486  return;
487  }
488  if (typeVariableMappings.ContainsKey(fieldTypeVariable))
489  this.fieldType = typeVariableMappings[fieldTypeVariable];
490  this.ValidTypeExpression = false;
491  }
492  #endregion
493 
494  #region Equivalent ANULADA
495  //public override bool Equivalent(TypeExpression type) {
501  // FieldType fieldType = type as FieldType;
502  // if (fieldType != null)
503  // return this.fieldType.Equivalent(fieldType.fieldType);
504  // return this.fieldType.Equivalent(type);
505  //}
506 
507  #endregion
508  #region Freeze()
509  public override TypeExpression Freeze() {
516  if (this.FieldTypeExpression is TypeVariable && !this.FieldTypeExpression.IsFreshVariable()) {
517  FieldType newFieldType = (FieldType)this.MemberwiseClone();
518  newFieldType.fieldType = this.FieldTypeExpression.Freeze();
519  newFieldType.ValidTypeExpression = false;
520  return newFieldType;
521  }
522  return this;
523  }
524  #endregion
525 
526  // SSA
527 
528  #region Clone()
529  internal override TypeExpression Clone(IDictionary<int, TypeVariable> clonedTypeVariables, IList<EquivalenceClass> equivalenceClasses, MethodType methodAnalyzed) {
540  if (!this.HasTypeVariables())
541  return this;
542  FieldType newFieldType = (FieldType)this.MemberwiseClone();
543  if (newFieldType.fieldType != null)
544  newFieldType.fieldType = newFieldType.fieldType.Clone(clonedTypeVariables, equivalenceClasses, methodAnalyzed);
545  newFieldType.ValidTypeExpression = false;
546  return newFieldType;
547  }
548 
549  internal TypeExpression Clone(IDictionary<int, TypeVariable> clonedTypeVariables, IList<EquivalenceClass> equivalenceClasses, MethodType methodAnalyzed, IDictionary<String, TypeExpression> evaluated)
550  {
551  if (!this.HasTypeVariables())
552  return this;
553  FieldType newFieldType = (FieldType)this.MemberwiseClone();
554  if (newFieldType.fieldType is ClassType)
555  newFieldType.fieldType = ((ClassType)newFieldType.fieldType).Clone(clonedTypeVariables, equivalenceClasses, methodAnalyzed, evaluated);
556  else if (newFieldType.fieldType != null)
557  newFieldType.fieldType = newFieldType.fieldType.Clone(clonedTypeVariables, equivalenceClasses, methodAnalyzed);
558  newFieldType.ValidTypeExpression = false;
559  return newFieldType;
560  }
561  #endregion
562 
563  // Code Generation
564 
565  #region ILType()
566 
571  public override string ILType() {
572  return this.fieldType.ILType();
573  }
574 
575  #endregion
576 
577  #region IsValueType()
578 
583  public override bool IsValueType() {
584  return this.fieldType.IsValueType();
585  }
586 
587  #endregion
588 
589 
590  }
591 }
override TypeExpression CloneTypeVariables(IDictionary< TypeVariable, TypeVariable > typeVariableMappings, IList< EquivalenceClass > equivalenceClasses, IList< ClassType > clonedClasses)
Method that clones each type variable of a type expression. Equivalence classes are not cloned (but i...
Definition: FieldType.cs:440
bool HasTypeVariables(IList< String > evaluated)
Definition: FieldType.cs:411
Represents a error produced when a MethodType has class information and tries to assign other class i...
bool validHasTypeVariables
To cache the result of the HasTypeVariables method
This class represent the entry wnen sending a message to an operation object derived from TypeExpress...
string MemberIdentifier
Gets the attribute name
bool validTypeExpression
To implement a type expression cache
Abstract class that represents all different types.
override bool Unify(TypeExpression te, SortOfUnification unification, IList< Pair< TypeExpression, TypeExpression >> previouslyUnified)
Check if the type can make an assignment operation.
Definition: FieldType.cs:374
Represents a proxy of a class type. It implements the unfold operatations of theoretical type systes...
System.Text.StringBuilder StringBuilder
Definition: CSharpParser.cs:4
AccessModifier MemberInfo
Gets or sets the attribute information of method type
Definition: FieldType.cs:65
FieldType(TypeExpression type)
Constructor of FieldType
Definition: FieldType.cs:96
virtual bool IsFreshVariable()
To know if it is a type variable with no substitution
override TypeExpression Freeze()
WriteType equivalence. Tells us if two types are the same
Definition: FieldType.cs:515
abstract bool IsValueType()
True if type expression is a ValueType. Otherwise, false.
virtual TypeExpression CloneTypeVariables(IDictionary< TypeVariable, TypeVariable > typeVariableMappings, IList< EquivalenceClass > equivalenceClasses, IList< ClassType > clonedClasses)
Method that clones each type variable of a type expression. Equivalence classes are not cloned (but i...
TypeExpression FieldTypeExpression
Gets the field type.
Definition: FieldType.cs:58
override void ReplaceTypeVariables(IDictionary< TypeVariable, TypeVariable > typeVariableMappings)
Replaces type variables substituting the old type variables for the new ones.
Definition: FieldType.cs:481
override string BuildTypeExpressionString(int depthLevel)
Creates the type expression string.
Definition: FieldType.cs:110
override void UpdateEquivalenceClass(IDictionary< TypeVariable, TypeVariable > typeVariableMappings, IList< TypeExpression > previouslyUpdated)
Replaces the equivalence class of type variables substituting the old type variables for the new ones...
Definition: FieldType.cs:461
override string ILType()
Gets the string type to use in IL code.
Definition: FieldType.cs:571
override object AcceptOperation(TypeSystemOperation op, object arg)
Definition: FieldType.cs:153
List< Modifier > Modifiers
Gets the modifiers of the element
override void BuildFullName()
Creates/Updates the full name of the type expression
Definition: FieldType.cs:144
override bool IsValueType()
True if type expression is a ValueType. Otherwise, false.
Definition: FieldType.cs:583
Association Class between ClassType and MethodType (or Fields). Represents the access modifier inform...
Representa a field type.
Definition: FieldType.cs:37
abstract bool Unify(TypeExpression te, SortOfUnification unification, IList< Pair< TypeExpression, TypeExpression >> previouslyUnified)
Requires the implicit object to be a subtype of the type parameter
Represents a class or interface type.
Definition: UserType.cs:31
override bool HasTypeVariables()
To know if the type expression has some type variables and requieres unification The default implemen...
Definition: FieldType.cs:402
Representa a class attribute (fields or methods).
Definition: IMemberType.cs:32