The StaDyn Programming Language
Source code documentation of StaDyn, a hybrid static and dynamic typing language.
UserType.cs
Go to the documentation of this file.
1 // -------------------------------------------------------------------------- //
3 // Project rROTOR //
4 // -------------------------------------------------------------------------- //
5 // File: UserType.cs //
6 // Authors: Cristina Gonzalez Muņoz - cristi.gm@gmail.com //
7 // Francisco Ortin - francisco.ortin@gmail.com //
8 // Description: //
9 // Represents a class or interface type. //
10 // Inheritance: TypeExpression. //
11 // Implements Composite pattern [Composite]. //
12 // -------------------------------------------------------------------------- //
13 // Create date: 26-01-2007 //
14 // Modification date: 29-01-2007 //
16 //visto
17 using System;
18 using System.Collections.Generic;
19 
20 using ErrorManagement;
21 using TypeSystem.Operations;
22 
23 namespace TypeSystem {
31  public abstract class UserType : TypeExpression {
32  #region Fields
33 
38  private Dictionary<string, AccessModifier> memberList = new Dictionary<string, AccessModifier>();
39 
43  protected Dictionary<string, AccessModifier> fieldList = new Dictionary<string, AccessModifier>();
44 
48  private AccessModifier constructorList;
49 
53  private Dictionary<string, AccessModifier> methodList = new Dictionary<string, AccessModifier>();
54 
58  protected List<InterfaceType> interfaceList;
59 
63  protected List<Modifier> modifierList;
64 
69 
73  protected string name;
74 
75  #endregion
76 
77  #region Properties
78 
82  public Dictionary<string, AccessModifier> Fields {
83  get { return this.fieldList; }
84  }
85 
90  get { return this.constructorList; }
91  }
92 
96  public Dictionary<string, AccessModifier> Methods {
97  get { return this.methodList; }
98  }
99 
103  public Dictionary<string, AccessModifier> Members {
104  get { return this.memberList; }
105  set {
106  this.memberList = value;
107  foreach (KeyValuePair<string, AccessModifier> pair in value) {
108  TypeExpression memberType = pair.Value.Type;
109  if (memberType is FieldType)
110  fieldList[pair.Key] = pair.Value;
111  else if (pair.Key.Equals(pair.Value.Class.Name)||pair.Key.Equals(pair.Value.Class.FullName))
112  if (this.constructorList != null)
113  System.Diagnostics.Debug.Assert(false, "Use intersection types for multiple constructors.");
114  else this.constructorList = pair.Value;
115  else
116  methodList[pair.Key] = pair.Value;
117  }
118  }
119  }
120 
121 
125  public List<Modifier> Modifiers {
126  set {
127  for (int i = 0; i < value.Count; i++) {
128  this.modifierMask |= value[i];
129  }
130  if ((this.modifierMask & Modifier.AccessLevel) == 0) {
131  value.Add(Modifier.Internal);
132  this.modifierMask |= Modifier.Internal;
133  }
134  this.modifierList = value;
135  }
136  }
137 
141  public string Name {
142  get { return this.name; }
143  }
144 
148  public Modifier ModifierMask
149  {
150  get { return this.modifierMask; }
151  }
152 
156  public List<InterfaceType> InterfaceList
157  {
158  get { return this.interfaceList; }
159  }
160 
161  #endregion
162 
163  #region Constructor
164 
169  protected UserType(string identifier) {
170  this.fullName = identifier;
171  this.interfaceList = new List<InterfaceType>();
172  }
173 
174  #endregion
175 
176  #region Dispatcher
177  public override object AcceptOperation(TypeSystemOperation op, object arg) { return op.Exec(this, arg); }
178  #endregion
179 
180  #region AddMember()
181  public void AddMember(string name, AccessModifier accessModifier) {
189  TypeExpression memberType = accessModifier.Type;
190  memberList[name] = accessModifier;
191  if (memberType is FieldType)
192  fieldList[name] = accessModifier;
193  else if (memberType is MethodType && name.Equals(accessModifier.Class.Name))
194  if (this.constructorList != null)
195  System.Diagnostics.Debug.Assert(false, "Use intersection types for multiple constructors.");
196  else this.constructorList = accessModifier;
197  else
198  methodList[name] = accessModifier;
199  }
200  #endregion
201 
202  #region AddBaseClass()
203  public abstract void AddBaseClass(ClassType inheritedType, Location location);
211  #endregion
212 
213  #region AddBaseInterface()
214  public void AddBaseInterface(InterfaceType inheritedType, Location location) {
222  this.interfaceList.Add((InterfaceType)inheritedType);
223  }
224  #endregion
225 
226  #region HasTypeVariables()
227  public override bool HasTypeVariables() {
233  return HasTypeVariables(new List<String>());
234  }
235 
236  public bool HasTypeVariables(IList<String> evaluated)
237  {
238  if (this.validHasTypeVariables)
239  return this.hasTypeVariablesCache;
240  bool toReturn = false;
241  foreach (AccessModifier am in this.Fields.Values)
242  {
243  if (evaluated.Contains(am.Type.FullName))
244  continue;
245  else
246  {
247  evaluated.Add(am.Type.FullName);
248  bool result;
249  if (am.Type is FieldType)
250  result = ((FieldType)am.Type).HasTypeVariables(evaluated);
251  else
252  result = am.Type.HasTypeVariables();
253  if (result)
254  toReturn = true;
255  }
256  if (toReturn)
257  break;
258  }
259  this.validHasTypeVariables = true;
260  return this.hasTypeVariablesCache = toReturn;
261  }
262  #endregion
263 
264  #region InheritsFrom()
265  public virtual bool InheritsFrom(UserType superType) {
271  // * A class does not inherit from itself
272  if ((bool)this.AcceptOperation(new EquivalentOperation(superType), null))
273  return false;
274  // * Depth first search
275  foreach (InterfaceType interfaze in this.interfaceList)
276  if (interfaze == superType || (bool)interfaze.AcceptOperation(new EquivalentOperation(superType), null))
277  return true;
278  foreach (InterfaceType interfaze in this.interfaceList)
279  if (interfaze.InheritsFrom(superType))
280  return true;
281  // * No inheritance
282  return false;
283  }
284  #endregion
285 
286  }
287 }
288 
AccessModifier Constructors
Gets the constructor list
Definition: UserType.cs:89
List< InterfaceType > interfaceList
Represents the identifiers of the interfaces
Definition: UserType.cs:58
Dictionary< string, AccessModifier > Fields
Gets the field list
Definition: UserType.cs:82
List< Modifier > modifierList
Stores the modifiers belong to the class type
Definition: UserType.cs:63
bool validHasTypeVariables
To cache the result of the HasTypeVariables method
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...
string Name
Class identifier;
Definition: UserType.cs:141
UserType(string identifier)
Constructor of UserType
Definition: UserType.cs:169
Dictionary< string, AccessModifier > Methods
Gets the method list
Definition: UserType.cs:96
override object AcceptOperation(TypeSystemOperation op, object arg)
Definition: UserType.cs:177
Represents a interface type.
void AddMember(string name, AccessModifier accessModifier)
Use allways this method to add any attribute. It takes into account if that attribute is a constructo...
Definition: UserType.cs:188
Modifier modifierMask
Stores a mask with the modifier information
Definition: UserType.cs:68
override bool HasTypeVariables()
To know if the type expression has some type variables and requieres unification The default implemen...
Definition: UserType.cs:232
Abstract class that represents all different types.
virtual bool InheritsFrom(UserType superType)
Tells if the implicit object is a subtype of the superType
Definition: UserType.cs:270
List< Modifier > Modifiers
Gets and sets the attribute modifiers
Definition: UserType.cs:125
virtual string FullName
Gets the full name of the type Note: WriteType expression is the longest recursive representation of ...
No location information is provided within this class, because the exec methods invoked by the proper...
bool HasTypeVariables(IList< String > evaluated)
Definition: UserType.cs:236
abstract void AddBaseClass(ClassType inheritedType, Location location)
Adds a new inherited class
Dictionary< string, AccessModifier > fieldList
Represents all class fields.
Definition: UserType.cs:43
string name
Class identifier;
Definition: UserType.cs:73
Dictionary< string, AccessModifier > Members
Gets and sets the attribute list
Definition: UserType.cs:103
List< InterfaceType > InterfaceList
Gets the list of interfaces
Definition: UserType.cs:157
Modifier
Indicates differents modifiers to use in class (only public, internal or static), fields or methods...
UserType Class
Gets or sets the class type reference
Representa a method type.
Definition: MethodType.cs:37
override object AcceptOperation(TypeSystemOperation op, object arg)
Association Class between ClassType and MethodType (or Fields). Represents the access modifier inform...
void AddBaseInterface(InterfaceType inheritedType, Location location)
Adds a new inherited interface
Definition: UserType.cs:221
TypeExpression Type
Gets or sets the attribute type expression
Representa a field type.
Definition: FieldType.cs:37
Represents a class or interface type.
Definition: UserType.cs:31
Modifier ModifierMask
Gets the modifier information
Definition: UserType.cs:149