Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GenericType.Extensions.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 SiliconStudio.Shaders.Visitor;
4 
5 namespace SiliconStudio.Shaders.Ast.Hlsl
6 {
7  public static class GenericTypeExtensions
8  {
9 
10  public static TypeBase MakeGenericInstance(this GenericType genericType, TypeBase genericTemplateType)
11  {
12  // TODO cache generic instance that are using predefined hlsl types
13  var newType = genericTemplateType.DeepClone();
14 
15  var genericParameters = ((IGenerics)genericTemplateType).GenericParameters;
16  var genericArguments = ((IGenerics)newType).GenericArguments;
17  var genericInstanceParameters = genericType.Parameters;
18 
19  var genericParameterTypes = new TypeBase[genericParameters.Count];
20  var genericBaseParameterTypes = new TypeBase[genericParameters.Count];
21 
22  // Look for parameter instance types
23  for (int i = 0; i < genericInstanceParameters.Count; i++)
24  {
25  var genericInstanceParameter = genericInstanceParameters[i];
26  if (genericInstanceParameter is TypeBase)
27  {
28  var genericInstanceParameterType = (TypeBase)genericInstanceParameter;
29  genericParameterTypes[i] = genericInstanceParameterType;
30  genericBaseParameterTypes[i] = TypeBase.GetBaseType(genericInstanceParameterType);
31  genericParameters[i] = genericParameterTypes[i];
32  genericArguments.Add(genericInstanceParameterType);
33  }
34  }
35 
36  // Replace all references to template arguments to their respective generic instance types
37  SearchVisitor.Run(
38  newType,
39  node =>
40  {
41  var typeInferencer = node as ITypeInferencer;
42  if (typeInferencer != null && typeInferencer.TypeInference.Declaration is GenericDeclaration)
43  {
44  var genericDeclaration = (GenericDeclaration)typeInferencer.TypeInference.Declaration;
45  var i = genericDeclaration.Index;
46  var targeType = genericDeclaration.IsUsingBase ? genericBaseParameterTypes[i] : genericParameterTypes[i];
47 
48  if (node is TypeBase)
49  return targeType.ResolveType();
50  }
51 
52  return node;
53  });
54 
55 
56  return newType;
57  }
58  }
59 }
A generic declaration. This is used internally to identify a generic declaration. ...
An interface used by generic definitions and instance.
Definition: IGenerics.cs:10
IDeclaration Declaration
Gets or sets the declaration.
static TypeBase MakeGenericInstance(this GenericType genericType, TypeBase genericTemplateType)
Base type for all types.
Definition: TypeBase.cs:11
Base class for all generic types.
Definition: GenericType.cs:14
TypeInference TypeInference
Gets or sets the reference.
A tag interface for an object referencing a type.