Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
TypeBase.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 
6 namespace SiliconStudio.Shaders.Ast
7 {
8  /// <summary>
9  /// Base type for all types.
10  /// </summary>
12  {
13  #region Constructors and Destructors
14 
15  /// <summary>
16  /// Initializes a new instance of the <see cref = "TypeBase" /> class.
17  /// </summary>
18  protected TypeBase() : this((Identifier)null)
19  {
20  }
21 
22  /// <summary>
23  /// Initializes a new instance of the <see cref="TypeBase"/> class.
24  /// </summary>
25  /// <param name="name">
26  /// The name.
27  /// </param>
28  protected TypeBase(string name)
29  : this(name != null ? new Identifier(name) : null)
30  {
31  Name = name != null ? new Identifier(name) : null;
32  Attributes = new List<AttributeBase>();
33  Qualifiers = Qualifier.None;
35  }
36 
37  /// <summary>
38  /// Initializes a new instance of the <see cref="TypeBase"/> class.
39  /// </summary>
40  /// <param name="name">
41  /// The name.
42  /// </param>
43  protected TypeBase(Identifier name)
44  {
45  Name = name;
46  Attributes = new List<AttributeBase>();
47  Qualifiers = Qualifier.None;
49  }
50 
51  #endregion
52 
53  #region Public Properties
54 
55  /// <summary>
56  /// Gets or sets the attributes.
57  /// </summary>
58  /// <value>
59  /// The attributes.
60  /// </value>
61  public List<AttributeBase> Attributes { get; set; }
62 
63  /// <summary>
64  /// Gets or sets the resolved reference.
65  /// </summary>
66  /// <value>
67  /// The resolved reference.
68  /// </value>
69  public TypeInference TypeInference { get; set; }
70 
71  /// <summary>
72  /// Gets or sets the type name.
73  /// </summary>
74  /// <value>
75  /// The type name.
76  /// </value>
77  public Identifier Name { get; set; }
78 
79  /// <summary>
80  /// Gets or sets the qualifiers.
81  /// </summary>
82  /// <value>
83  /// The qualifiers.
84  /// </value>
85  public Qualifier Qualifiers { get; set; }
86 
87  /// <summary>
88  /// Gets or sets a value indicating whether this instance is built in.
89  /// </summary>
90  /// <value>
91  /// <c>true</c> if this instance is built in; otherwise, <c>false</c>.
92  /// </value>
93  public bool IsBuiltIn { get; set; }
94 
95  /// <summary>
96  /// Resolves the type.
97  /// </summary>
98  /// <returns>
99  /// The resolved type.
100  /// </returns>
101  public virtual TypeBase ResolveType()
102  {
103  return TypeInference.TargetType ?? this;
104  }
105 
106  #endregion
107 
108  #region Public Methods
109 
110  /// <summary>
111  /// Creates a type based on a new base type. If type is a matrix or vector, then the base type is changed to match the newBaseType.
112  /// </summary>
113  /// <param name="type">The type.</param>
114  /// <param name="newBaseType">New type of the base.</param>
115  /// <returns>A new type</returns>
116  public static TypeBase CreateWithBaseType(TypeBase type, ScalarType newBaseType)
117  {
118  if (type is MatrixType)
119  return new MatrixType(newBaseType, ((MatrixType)type).RowCount, ((MatrixType)type).ColumnCount);
120 
121  if (type is VectorType)
122  return new VectorType(newBaseType, ((VectorType)type).Dimension);
123 
124  return newBaseType;
125  }
126 
127  public static TypeBase GetBaseType(TypeBase type)
128  {
129  if (type is MatrixType) return ((MatrixType)type).Type;
130  if (type is VectorType) return ((VectorType)type).Type;
131  return type;
132  }
133 
134  public static bool HasDimensions(TypeBase typeDeclaration)
135  {
136  return (typeDeclaration is ScalarType) || (typeDeclaration is VectorType) || (typeDeclaration is MatrixType);
137  }
138 
139  public static int GetDimensionSize(TypeBase typeDeclaration, int dimension)
140  {
141  if (typeDeclaration is VectorType)
142  {
143  if (dimension != 1) return 1;
144  return ((VectorType)typeDeclaration).Dimension;
145  }
146 
147  if (typeDeclaration is MatrixType)
148  {
149  var matrixType = (MatrixType)typeDeclaration;
150  return dimension == 0 ? matrixType.RowCount : matrixType.ColumnCount;
151  }
152 
153  return 1;
154  }
155 
156  /// <summary>
157  /// Equalses the specified other.
158  /// </summary>
159  /// <param name="other">
160  /// The other.
161  /// </param>
162  /// <returns>
163  /// <c>true</c> if the specified <see cref="TypeBase"/> is equal to this instance; otherwise, <c>false</c>.
164  /// </returns>
165  public bool Equals(TypeBase other)
166  {
167  if (ReferenceEquals(null, other))
168  {
169  return false;
170  }
171 
172  if (ReferenceEquals(this, other))
173  {
174  return true;
175  }
176 
177  return Equals(other.Name, Name);
178  }
179 
180  /// <summary>
181  /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
182  /// </summary>
183  /// <param name="obj">
184  /// The <see cref="System.Object"/> to compare with this instance.
185  /// </param>
186  /// <returns>
187  /// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
188  /// </returns>
189  public override bool Equals(object obj)
190  {
191  if (ReferenceEquals(null, obj))
192  {
193  return false;
194  }
195 
196  if (ReferenceEquals(this, obj))
197  {
198  return true;
199  }
200 
201  if (!typeof(TypeBase).IsAssignableFrom(obj.GetType()))
202  {
203  return false;
204  }
205 
206  return Equals((TypeBase)obj);
207  }
208 
209  /// <summary>
210  /// Returns a hash code for this instance.
211  /// </summary>
212  /// <returns>
213  /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
214  /// </returns>
215  public override int GetHashCode()
216  {
217  return Name.GetHashCode();
218  }
219 
220  /// <summary>
221  /// Returns a <see cref="System.String"/> that represents this instance.
222  /// </summary>
223  /// <returns>
224  /// A <see cref="System.String"/> that represents this instance.
225  /// </returns>
226  public override string ToString()
227  {
228  return Name.ToString();
229  }
230 
231  #endregion
232 
233  #region Operators
234 
235  /// <summary>
236  /// Implements the operator ==.
237  /// </summary>
238  /// <param name = "left">The left.</param>
239  /// <param name = "right">The right.</param>
240  /// <returns>
241  /// The result of the operator.
242  /// </returns>
243  public static bool operator ==(TypeBase left, TypeBase right)
244  {
245  return Equals(left, right);
246  }
247 
248  /// <summary>
249  /// Implements the operator !=.
250  /// </summary>
251  /// <param name = "left">The left.</param>
252  /// <param name = "right">The right.</param>
253  /// <returns>
254  /// The result of the operator.
255  /// </returns>
256  public static bool operator !=(TypeBase left, TypeBase right)
257  {
258  return !Equals(left, right);
259  }
260 
261  #endregion
262 
263  /// <summary>
264  /// Scalar void. TODO this is not a scalar!
265  /// </summary>
266  public static readonly ScalarType Void = new ScalarType("void", typeof(void));
267 
268  /// <summary>
269  /// Scalar void. TODO this is not a scalar!
270  /// </summary>
271  public static readonly ScalarType String = new ScalarType("string", typeof(string));
272  }
273 }
Base class for all vector types
Definition: VectorType.cs:10
TypeBase()
Initializes a new instance of the TypeBase class.
Definition: TypeBase.cs:18
override bool Equals(object obj)
Determines whether the specified System.Object is equal to this instance.
Definition: TypeBase.cs:189
TypeBase(Identifier name)
Initializes a new instance of the TypeBase class.
Definition: TypeBase.cs:43
virtual TypeBase ResolveType()
Resolves the type.
Definition: TypeBase.cs:101
static bool HasDimensions(TypeBase typeDeclaration)
Definition: TypeBase.cs:134
Base interface for all node providing qualifiers.
Definition: IQualifiers.cs:8
TypeBase(string name)
Initializes a new instance of the TypeBase class.
Definition: TypeBase.cs:28
Abstract node.
Definition: Node.cs:15
static TypeBase CreateWithBaseType(TypeBase type, ScalarType newBaseType)
Creates a type based on a new base type. If type is a matrix or vector, then the base type is changed...
Definition: TypeBase.cs:116
bool Equals(TypeBase other)
Equalses the specified other.
Definition: TypeBase.cs:165
override int GetHashCode()
Returns a hash code for this instance.
Definition: TypeBase.cs:215
static TypeBase GetBaseType(TypeBase type)
Definition: TypeBase.cs:127
Base type for all types.
Definition: TypeBase.cs:11
A tag interface for an object referencing a type.
static int GetDimensionSize(TypeBase typeDeclaration, int dimension)
Definition: TypeBase.cs:139
override string ToString()
Returns a System.String that represents this instance.
Definition: TypeBase.cs:226
Identifier Name
Gets or sets the type name.
Definition: TypeBase.cs:77