The StaDyn Programming Language
Source code documentation of StaDyn, a hybrid static and dynamic typing language.
TypeExpression.cs
Go to the documentation of this file.
1 // -------------------------------------------------------------------------- //
3 // Project rROTOR //
4 // -------------------------------------------------------------------------- //
5 // File: TypeExpression.cs //
6 // Authors: Cristina Gonzalez Muņoz - cristi.gm@gmail.com //
7 // Francisco Ortin - francisco.ortin@gmail.com //
8 // Description: //
9 // Abstract class that represents all different types. //
10 // Implements Composite pattern [Component]. //
11 // -------------------------------------------------------------------------- //
12 // Create date: 15-10-2006 //
13 // Modification date: 05-06-2007 //
15 
16 using System;
17 using System.Diagnostics;
18 using System.Collections.Generic;
19 using System.Text;
20 
21 using AST;
22 using ErrorManagement;
23 using Tools;
24 using TypeSystem.Operations;
25 
26 namespace TypeSystem {
34  public abstract class TypeExpression {
35 
36  #region Fields
37 
43  internal protected string typeExpression;
44 
50  internal protected string fullName;
51 
55  protected bool validTypeExpression = false;
56 
60  protected bool hasTypeVariablesCache;
61 
65  protected bool validHasTypeVariables = false;
66 
71  public const int MAX_DEPTH_LEVEL_TYPE_EXPRESSION = 1;
72 
76  protected bool isDynamic;
77  #endregion
78 
79  #region Properties
80 
86  public virtual string FullName {
87  get { return this.fullName; }
88  set { this.fullName = value; }
89  }
90 
94  internal virtual bool ValidTypeExpression {
95  get { return validTypeExpression; }
96  set {
97  validTypeExpression = value;
98  if (!value) {
99  // * Updates the full name
100  this.BuildFullName();
101  this.typeExpression = this.fullName;
102  }
103  }
104  }
105 
109  public virtual bool IsDynamic {
110  get { return this.isDynamic; }
111  set { this.isDynamic = value; }
112  }
113  #endregion
114 
115  // * Constructors
116 
117  #region Construtors
118  public TypeExpression() {
119  this.isDynamic = false;
120  }
121  public TypeExpression(bool isDynamic) {
122  this.isDynamic = isDynamic;
123  }
124  #endregion
125 
126  // * Debug
127 
128  #region ToString()
129 
136  public override string ToString() {
137  if (!this.ValidTypeExpression) {
138  this.typeExpression = this.BuildTypeExpressionString(MAX_DEPTH_LEVEL_TYPE_EXPRESSION);
139  this.ValidTypeExpression = true;
140  }
141  return this.typeExpression;
142  }
143 
144  #endregion
145 
146  #region BuildTypeExpressionString()
147  public virtual string BuildTypeExpressionString(int depthLevel) {
152  return this.typeExpression;
153  }
154  #endregion
155 
156  #region BuildFullName()
157  public virtual void BuildFullName() { }
161  #endregion
162 
163  #region Dispatcher
164  public virtual object AcceptOperation(TypeSystemOperation op, object arg) { return op.Exec(this, arg); }
165  #endregion
166  // WriteType Inference
167 
168  #region Dot() ANULADA
169  //public virtual TypeExpression Dot(string member, MethodType methodAnalyzed, IList<TypeExpression> previousDot, Location loc) {
182  // ErrorManager.Instance.NotifyError(new OperationNotAllowedError(".", this.fullName, loc));
183  // return null;
184  //}
193  //public virtual TypeExpression Dot(string memberName, IList<TypeExpression> previousDot) {
194  // return null;
195  //}
196  #endregion
197 
198  #region Parenthesis() ANULADA
199  //public virtual TypeExpression Parenthesis(TypeExpression actualImplicitObject, TypeExpression[] arguments, MethodType methodAnalyzed, SortOfUnification activeSortOfUnification, Location loc) {
212  // ErrorManager.Instance.NotifyError(new OperationNotAllowedError("()", this.fullName, loc));
213  // return null;
214  //}
215  #endregion
216 
217  #region Bracket() ANULADA
218  //public virtual TypeExpression Bracket(TypeExpression index, MethodType methodAnalized, bool showErrorMessage, Location loc) {
229  // if (showErrorMessage)
230  // ErrorManager.Instance.NotifyError(new OperationNotAllowedError("[]", this.fullName, loc));
231  // return null;
232  //}
233 
234  #endregion
235 
236  #region Assignment() ANULADA
237  //public virtual TypeExpression Assignment(TypeExpression operand, AssignmentOperator op, MethodType methodAnalyzed, SortOfUnification unification,
251  // TypeExpression actualImplicitObject, Location location) {
252  // ErrorManager.Instance.NotifyError(new AssignmentError(operand.FullName, this.fullName, location));
253  // return null;
254  //}
255  #endregion
256 
257  #region Arithmetic() ANULADA
258  /*
259  * /// <summary>
270  public virtual TypeExpression Arithmetic(TypeExpression operand, Enum op, MethodType methodAnalyzed, bool showErrorMessage, Location loc) {
271  if (showErrorMessage)
272  ErrorManager.Instance.NotifyError(new OperationNotAllowedError(op.ToString(), this.fullName, operand.fullName, loc));
273  return null;
274  }
275 
276 
287  public virtual TypeExpression Arithmetic(UnaryOperator op, MethodType methodAnalyzed, bool showErrorMessage, Location loc) {
288  if (showErrorMessage)
289  ErrorManager.Instance.NotifyError(new OperationNotAllowedError(op.ToString(), this.fullName, loc));
290  return null;
291  }
292  */
293  #endregion
294 
295  #region Relational() ANULADA
296  /* /// <summary>
307  public virtual TypeExpression Relational(TypeExpression operand, RelationalOperator op, MethodType methodAnalyzed, bool showErrorMessage, Location loc) {
308  if (showErrorMessage)
309  ErrorManager.Instance.NotifyError(new OperationNotAllowedError(op.ToString(), this.fullName, operand.fullName, loc));
310  return null;
311  }
312 
313  */
314  #endregion
315 
316  #region Equivalent() ANULADA
317  //public virtual bool Equivalent(TypeExpression type) {
323  // if (this == type)
324  // return true;
325  // TypeVariable typeVariable = type as TypeVariable;
326  // if (typeVariable != null)
327  // return typeVariable.Equivalent(this);
328  // return this.fullName.Equals(type.fullName);
329  //}
330  #endregion
331 
332  #region AsClassType()
333  public virtual ClassType AsClassType() {
339  return null;
340  }
341 
342  #endregion
343 
344  // WriteType Promotion
345 
346  #region PromotionLevel() ANULADA
347  //public virtual int PromotionLevel(TypeExpression type) {
348  // return -1;
349  //}
350  #endregion
351 
352  #region Promotion() ANULADA
353  //public virtual TypeExpression Promotion(TypeExpression type, MethodType methodAnalyzed, Location location) {
364  // if ((int) this.AcceptOperation(new PromotionLevelOperation(type)) == -1) {
365  // ErrorManager.Instance.NotifyError(new TypePromotionError(this.FullName, type.FullName, location));
366  // return null;
367  // }
368  // return type;
369  //}
370  //public virtual TypeExpression Promotion(TypeExpression type, Enum op, MethodType methodAnalyzed, Location location) {
371  // if ((int)this.AcceptOperation( new PromotionLevelOperation(type)) == -1) {
372  // ErrorManager.Instance.NotifyError(new TypePromotionError(this.FullName, type.FullName, op.ToString(), location));
373  // return null;
374  // }
375  // return type;
376  //}
377  #endregion
378 
379  #region Cast() ANULADA
380  //public virtual TypeExpression Cast(TypeExpression castType, MethodType methodAnalyzed, Location loc) {
390  // if (castType == null)
391  // return null;
392  // if (((int)castType.AcceptOperation(new PromotionLevelOperation(this)) != -1) || ((int)this.AcceptOperation(new PromotionLevelOperation(castType)) != -1))
393  // return castType;
394  // ErrorManager.Instance.NotifyError(new TypeCastError(this.FullName, castType.FullName, loc));
395  // return null;
396  //}
397  #endregion
398 
399  #region EqualsForOverload() ANULADA
400  //public virtual bool EqualsForOverload(object typeExpression) {
406  // // * By default, we use the equals comparison
407  // return this.Equals(typeExpression);
408  //}
409  #endregion
410 
411  // WriteType Unification
412 
413  #region Unify()
414  public abstract bool Unify(TypeExpression te, SortOfUnification unification, IList<Pair<TypeExpression, TypeExpression>> previouslyUnified);
422  #endregion
423 
424  #region HasTypeVariables()
425  public virtual bool HasTypeVariables() {
431  return false;
432  }
433  #endregion
434 
435  #region IsFreshVariable()
436  public virtual bool IsFreshVariable() {
441  return false;
442  }
443  #endregion
444 
445  #region HasFreshVariable()
446  public virtual bool HasFreshVariable() {
451  return IsFreshVariable();
452  }
453  #endregion
454 
455  #region HasIntersectionVariable()
456  public virtual bool HasIntersectionVariable() {
461  return false;
462  }
463  #endregion
464 
465 
466  #region CloneType()
467  public virtual TypeExpression CloneType(IDictionary<TypeVariable, TypeVariable> typeVariableMappings) {
476  // * By default, no clone is performed (built-in types)
477  return this;
478  }
479  #endregion
480 
481  #region CloneTypeVariables()
482  public virtual TypeExpression CloneTypeVariables(IDictionary<TypeVariable, TypeVariable> typeVariableMappings, IList<EquivalenceClass> equivalenceClasses, IList<ClassType> clonedClasses) {
493  return this;
494  }
495  #endregion
496 
497  #region UpdateEquivalenceClass()
498  public virtual void UpdateEquivalenceClass(IDictionary<TypeVariable, TypeVariable> typeVariableMappings, IList<TypeExpression> previouslyUpdated) {
505  // * Does nothing (built-in types are not recursive)
506  }
507  #endregion
508 
509  #region ReplaceTypeVariables()
510  public virtual void ReplaceTypeVariables(IDictionary<TypeVariable, TypeVariable> typeVariableMappings) {
516  // * Nothing to to (built-in types)
517  }
518  #endregion
519 
520  #region Freeze()
521  public virtual TypeExpression Freeze() {
528  return this;
529  }
530  #endregion
531 
532  // SSA
533 
534  #region Clone()
535  internal virtual TypeExpression Clone(IDictionary<int, TypeVariable> clonedTypeVariables, IList<EquivalenceClass> equivalenceClasses, MethodType methodAnalyzed) {
546  if (this.HasTypeVariables())
547  throw new InvalidOperationException("The type should implement a Clone method.");
548  // * Default implementation (types with no type variables)
549  return this;
550  }
551  #endregion
552 
553  // Loop Detection
554 
555  #region Remove()
556  public virtual bool Remove(TypeVariable toRemove) {
563  return false;
564  }
565  #endregion
566 
567  // Code Generation
568 
569  #region ILType()
570  public virtual string ILType() {
575  return this.fullName;
576  }
577 
578  #endregion
579 
580  #region IsValueType()
581  public abstract bool IsValueType();
586  #endregion
587 
588  // Helper Methods
589 
590  #region As<T>()
591  public static T As<T>(TypeExpression type) where T : TypeExpression {
599  TypeVariable typeVariable = type as TypeVariable;
600  if (typeVariable != null)
601  type = typeVariable.Substitution;
602  T castType = type as T;
603  return castType;
604  }
605  #endregion
606 
607  #region Is<T>()
608  public static bool Is<T>(TypeExpression type) where T : TypeExpression {
616  return TypeExpression.As<T>(type) != null;
617  }
618  #endregion
619 
620  virtual public BCLClassType getBCLType() {
621  System.Diagnostics.Debug.Assert(true, "getBCLType called in type expression inconsistence in the program");
622  return null;
623  }
624 
625  }
626 }
virtual bool HasFreshVariable()
To know if it is a type variable with no substitution
virtual bool HasIntersectionVariable()
To know if it is a type variable with no substitution
virtual TypeExpression Freeze()
WriteType variable may change its type&#39;s substitution (e.g., field type variables) This method return...
TypeExpression(bool isDynamic)
virtual void BuildFullName()
Creates/Updates the full name of the type expression
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...
virtual ClassType AsClassType()
Check if the type can make a method operation.
bool validTypeExpression
To implement a type expression cache
Abstract class that represents all different types.
virtual void ReplaceTypeVariables(IDictionary< TypeVariable, TypeVariable > typeVariableMappings)
Replaces type variables substituting the old type variables for the new ones.
static bool Is< T >(TypeExpression type)
Tells if a type expression is a type or a type variable unified to a type
virtual bool Remove(TypeVariable toRemove)
When loops are detected, it is necesary to suppress a new extra variable returned in the return type ...
virtual string FullName
Gets the full name of the type Note: WriteType expression is the longest recursive representation of ...
virtual BCLClassType getBCLType()
virtual bool IsFreshVariable()
To know if it is a type variable with no substitution
abstract bool IsValueType()
True if type expression is a ValueType. Otherwise, false.
virtual string BuildTypeExpressionString(int depthLevel)
Returns the type expression The maximum depth of recursion to construct type expressions
bool isDynamic
Indicates if the type has been set as dynamic
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...
static T As< T >(TypeExpression type)
Returns a concrete type expression from a general one. It takes into accout that it can be a type var...
virtual string ILType()
Gets the type name to use in IL code.
virtual bool IsDynamic
Indicates if the type has been set as dynamic
string fullName
Represents the full name of the type Note: WriteType expression is the longest recursive representati...
virtual bool HasTypeVariables()
To know if the type expression has some type variables and requieres unification The default implemen...
string typeExpression
Represents the type by a debug string Note: WriteType expression is the longest recursive representat...
virtual 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...
const int MAX_DEPTH_LEVEL_TYPE_EXPRESSION
In order to avoid stack overflow in the construction of typeexpression string (ToString), we set a maximum level of depth
Represents a class type.
Definition: ClassType.cs:35
override string ToString()
Returns the type expression cached in the typeExpression field. Note: WriteType expression is the lon...
abstract bool Unify(TypeExpression te, SortOfUnification unification, IList< Pair< TypeExpression, TypeExpression >> previouslyUnified)
Requires the implicit object to be a subtype of the type parameter
bool hasTypeVariablesCache
The cached value ot the HasTypeVariables method
virtual object AcceptOperation(TypeSystemOperation op, object arg)
virtual TypeExpression CloneType(IDictionary< TypeVariable, TypeVariable > typeVariableMappings)
This method creates a new type, creating new type variables for type expression. It these type variab...