The StaDyn Programming Language
Source code documentation of StaDyn, a hybrid static and dynamic typing language.
InterfaceType.cs
Go to the documentation of this file.
1 // -------------------------------------------------------------------------- //
3 // Project rROTOR //
4 // -------------------------------------------------------------------------- //
5 // File: InterfaceType.cs //
6 // Authors: Cristina Gonzalez Muņoz - cristi.gm@gmail.com //
7 // Francisco Ortin - francisco.ortin@gmail.com //
8 // Description: //
9 // Represents a interface type. //
10 // Inheritance: UserType. //
11 // Implements Composite pattern [Composite]. //
12 // Implements Template Method pattern. //
13 // -------------------------------------------------------------------------- //
14 // Create date: 22-10-2006 //
15 // Modification date: 09-03-2007 //
17 
18 using System;
19 using System.Collections.Generic;
20 using System.Text;
21 
22 using ErrorManagement;
23 using AST;
24 using Tools;
25 using TypeSystem.Operations;
26 
27 namespace TypeSystem {
35  //visto
37  public class InterfaceType : UserType {
38  #region Constructor
39 
46  public InterfaceType(string identifier, string fullName, List<Modifier> modifiers)
47  : base(fullName) {
48  this.name = identifier;
49  this.Modifiers = modifiers;
50  }
51 
56  public InterfaceType(string name)
57  : base(name) {
58  this.name = name;
59  this.Modifiers = new List<Modifier>();
60  }
61  #endregion
62 
63  #region AddBaseClass()
64  public override void AddBaseClass(ClassType inheritedClass, Location location) {
72  System.Diagnostics.Debug.Assert(false, "A base class cannot be added to an interface.");
73  }
74  #endregion
75 
76  #region BuiltTypeExpression()
77 
81  public override string BuildTypeExpressionString(int depthLevel) {
82  if (this.ValidTypeExpression) return this.typeExpression;
83  if (depthLevel <= 0) return this.FullName;
84 
85  StringBuilder tE = new StringBuilder();
86  // tE: Interface(id, modifiers, interfaces, members)
87  tE.AppendFormat("Interface({0},", this.fullName);
88  // modifiers
89  if (this.modifierList.Count != 0) {
90  for (int i = 0; i < this.modifierList.Count - 1; i++) {
91  tE.AppendFormat(" {0} x", this.modifierList[i]);
92  }
93  tE.AppendFormat(" {0}", this.modifierList[this.modifierList.Count - 1]);
94  }
95  tE.Append(", ");
96 
97  // interfaces
98  if (this.interfaceList.Count != 0) {
99  for (int i = 0; i < this.interfaceList.Count - 1; i++) {
100  tE.AppendFormat(" {0} x", this.interfaceList[i].FullName);
101  }
102  tE.AppendFormat(" {0}", this.interfaceList[this.interfaceList.Count - 1].FullName);
103  }
104  tE.Append(", ");
105 
106  // members
107  if (this.Members.Count != 0) {
108  Dictionary<string, AccessModifier>.KeyCollection keys = this.Members.Keys;
109  int i = 0;
110  foreach (string key in keys) {
111  tE.Append(this.Members[key].Type.BuildTypeExpressionString(depthLevel - 1));
112  if (i < keys.Count - 1)
113  tE.Append(" x");
114  i++;
115  }
116  }
117  tE.Append(")");
118  this.ValidTypeExpression = true;
119  return this.typeExpression = tE.ToString();
120  }
121 
122  #endregion
123 
124  // WriteType Inference
125  #region Dispatcher
126  public override object AcceptOperation(TypeSystemOperation op, object arg) { return op.Exec(this, arg); }
127  #endregion
128 
129  #region Dot() ANULADA
130  //public override TypeExpression Dot(string memberName, MethodType methodAnalyzed, IList<TypeExpression> previousDot, Location loc) {
144  // // Try to find the appropriate attribute
145  // TypeExpression member = this.Dot(memberName, previousDot);
146  // if (member == null)
147  // // * Otherwise, error
148  // ErrorManager.Instance.NotifyError(new UnknownMemberError(memberName, loc));
149  // return member;
150  //}
159  //public override TypeExpression Dot(string memberName, IList<TypeExpression> previousDot) {
160  // if (this.Members.ContainsKey(memberName))
161  // return this.Members[memberName].WriteType;
162  // foreach (InterfaceType interfaze in this.interfaceList) {
163  // // * Does this interface support this attribute?
164  // TypeExpression member = interfaze.Dot(memberName, previousDot);
165  // if (member != null)
166  // return member;
167  // }
168  // // * not found
169  // return null;
170  //}
171  #endregion
172 
173  #region Assignment = ... ANULADA
174  //public override TypeExpression Assignment(TypeExpression operand, AssignmentOperator op, MethodType methodAnalyzed, SortOfUnification unification,
188  // TypeExpression actualImplicitObject, Location location) {
189  // if (op == AssignmentOperator.Assign)
190  // return operand.Promotion(this, op, methodAnalyzed, location);
191  // ErrorManager.Instance.NotifyError(new AssignmentError(operand.FullName, this.FullName, location));
192  // return null;
193  //}
194  #endregion
195 
196  // WriteType Promotion
197 
198  #region PromotionLevel() ANULADA
199 
205  //public override int PromotionLevel(TypeExpression type) {
206  // int aux, less = -1;
207 
208  // // * Equivalent types
209  // if ((bool)this.AcceptOperation(new EquivalentOperation((type))))
210  // less = 0;
211 
212  // // * WriteType variable
213  // TypeVariable typeVariable = type as TypeVariable;
214  // if (typeVariable != null) {
215  // if (typeVariable.Substitution != null)
216  // // * If the variable is bounded, the promotion is the one of its substitution
217  // return this.PromotionLevel(typeVariable.EquivalenceClass.Substitution);
218  // // * A free variable is complete promotion
219  // return 0;
220  // }
221 
222  // // * Field type and bounded type variable
223  // FieldType fieldType = TypeExpression.As<FieldType>(type);
224  // if (fieldType != null)
225  // return this.PromotionLevel(fieldType.FieldTypeExpression);
226 
227  // // * Interface List
228  // if (this.interfaceList.Count != 0) {
229  // for (int i = 0; i < this.interfaceList.Count; i++) {
230  // if ((bool)this.interfaceList[i].AcceptOperation(new EquivalentOperation(type))) {
231  // if ((less > 1) || (less == -1))
232  // less = 1;
233  // }
234  // else {
235  // aux = this.interfaceList[i].PromotionLevel(type);
236  // if (aux != -1) {
237  // if ((less > (aux + 1)) || (less == -1))
238  // less = aux + 1;
239  // }
240  // }
241  // }
242  // }
243  // if (less != -1)
244  // return less;
245 
246  // // * Union type
247  // UnionType unionType = TypeExpression.As<UnionType>(type);
248  // if (unionType != null)
249  // return unionType.SuperType(this);
250 
251  // // * No promotion
252  // return -1;
253  //}
254 
255  #endregion
256 
257  // WriteType Unification
258 
259  #region ILType()
260 
265  public override string ILType() {
266  StringBuilder aux = new StringBuilder();
267  aux.AppendFormat("class {0}", this.fullName);
268  return aux.ToString();
269  }
270 
271  #endregion
272 
273 
274 
275 
276 
277 
278 
279 
280 
281 
282 
283 
284 
285 
286 
287  #region Unify
288  public override bool Unify(TypeExpression te, SortOfUnification unification, IList<Pair<TypeExpression, TypeExpression>> previouslyUnified) {
296  InterfaceType it = TypeExpression.As<InterfaceType>(te);
297  if (it != null) {
298  bool success = (bool)this.AcceptOperation(new EquivalentOperation(it), null);
299  // * Clears the type expression cache
300  this.ValidTypeExpression = false;
301  te.ValidTypeExpression = false;
302  return success;
303  }
304  if (te is TypeVariable && unification!=SortOfUnification.Incremental)
305  // * No incremental unification is commutative
306  return te.Unify(this, unification, previouslyUnified);
307  return false;
308  }
309  #endregion
310 
311  #region IsValueType()
312 
317  public override bool IsValueType()
318  {
319  return false;
320  }
321 
322  #endregion
323 
324  }
325 }
List< InterfaceType > interfaceList
Represents the identifiers of the interfaces
Definition: UserType.cs:58
List< Modifier > modifierList
Stores the modifiers belong to the class type
Definition: UserType.cs:63
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.
Definition: Location.cs:24
This class represent the entry wnen sending a message to an operation object derived from TypeExpress...
override string ILType()
Check if the type can make an assignment operation.
Represents a interface type.
override bool Unify(TypeExpression te, SortOfUnification unification, IList< Pair< TypeExpression, TypeExpression >> previouslyUnified)
This method unifies two type expressions (this and te)
Abstract class that represents all different types.
System.Text.StringBuilder StringBuilder
Definition: CSharpParser.cs:4
No location information is provided within this class, because the exec methods invoked by the proper...
string name
Class identifier;
Definition: UserType.cs:73
Dictionary< string, AccessModifier > Members
Gets and sets the attribute list
Definition: UserType.cs:103
override bool IsValueType()
True if type expression is a ValueType. Otherwise, false.
override void AddBaseClass(ClassType inheritedClass, Location location)
Adds a new inherited type
string fullName
Represents the full name of the type Note: WriteType expression is the longest recursive representati...
InterfaceType(string identifier, string fullName, List< Modifier > modifiers)
Constructor of InterfaceType
override object AcceptOperation(TypeSystemOperation op, object arg)
override string BuildTypeExpressionString(int depthLevel)
Creates the type expression string.
InterfaceType(string name)
Constructor of InterfaceType
Represents a class type.
Definition: ClassType.cs:35
Represents a class or interface type.
Definition: UserType.cs:31