Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
AnalysisBase.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 SiliconStudio.Shaders.Ast;
5 using SiliconStudio.Shaders.Parser;
6 using SiliconStudio.Shaders.Utility;
7 using SiliconStudio.Shaders.Visitor;
8 
9 namespace SiliconStudio.Shaders.Analysis
10 {
11  /// <summary>
12  /// Base class for analysis.
13  /// </summary>
14  public abstract class AnalysisBase : ShaderVisitor
15  {
16  /// <summary>
17  /// Initializes a new instance of the <see cref="AnalysisBase"/> class.
18  /// </summary>
19  /// <param name="result">The result.</param>
20  protected AnalysisBase(ParsingResult result) : base(true, true)
21  {
22  ParsingResult = result;
23  }
24 
25  /// <summary>
26  /// Gets the parsing result.
27  /// </summary>
28  public ParsingResult ParsingResult { get; private set; }
29 
30  /// <summary>
31  /// Runs this instance.
32  /// </summary>
33  public abstract void Run();
34 
35  /// <summary>
36  /// Logs an Error with the specified message.
37  /// </summary>
38  /// <param name="message">The message.</param>
39  /// <param name="span">The span.</param>
40  protected void Error(MessageCode message, SourceSpan span)
41  {
42  ParsingResult.Error(message, span);
43  }
44 
45  /// <summary>
46  /// Logs an Error with the specified message.
47  /// </summary>
48  /// <param name="message">The message.</param>
49  /// <param name="span">The span.</param>
50  /// <param name="parameters">The parameters.</param>
51  protected void Error(MessageCode message, SourceSpan span, params object[] parameters)
52  {
53  ParsingResult.Error(message, span, parameters);
54  }
55 
56  /// <summary>
57  /// Logs an Info with the specified message.
58  /// </summary>
59  /// <param name="message">The message.</param>
60  /// <param name="span">The span.</param>
61  protected void Info(string message, SourceSpan span)
62  {
63  ParsingResult.Info(message, span);
64  }
65 
66  /// <summary>
67  /// Logs an Info with the specified message.
68  /// </summary>
69  /// <param name="message">The message.</param>
70  /// <param name="span">The span.</param>
71  /// <param name="parameters">The parameters.</param>
72  protected void Info(string message, SourceSpan span, params object[] parameters)
73  {
74  ParsingResult.Info(message, span, parameters);
75  }
76 
77  /// <summary>
78  /// Logs an Warning with the specified message.
79  /// </summary>
80  /// <param name="message">The message.</param>
81  /// <param name="span">The span.</param>
82  protected void Warning(MessageCode message, SourceSpan span)
83  {
84  ParsingResult.Warning(message, span);
85  }
86 
87  /// <summary>
88  /// Logs an Warning with the specified message.
89  /// </summary>
90  /// <param name="message">The message.</param>
91  /// <param name="span">The span.</param>
92  /// <param name="parameters">The parameters.</param>
93  protected void Warning(MessageCode message, SourceSpan span, params object[] parameters)
94  {
95  ParsingResult.Warning(message, span, parameters);
96  }
97 
98  /// <summary>
99  /// Gets the type of the binary implicit conversion.
100  /// </summary>
101  /// <param name="span">The span.</param>
102  /// <param name="left">The left.</param>
103  /// <param name="right">The right.</param>
104  /// <param name="isBinaryOperator">if set to <c>true</c> [is binary operator].</param>
105  /// <returns>
106  /// The implicit conversion between between to two types
107  /// </returns>
108  protected virtual TypeBase GetBinaryImplicitConversionType(SourceSpan span, TypeBase left, TypeBase right, bool isBinaryOperator)
109  {
110  var result = CastHelper.GetBinaryImplicitConversionType(left, right, isBinaryOperator);
111 
112  if (result == null)
113  Error(MessageCode.ErrorBinaryTypeDeduction, span, left, right);
114 
115  return result;
116  }
117 
118  /// <summary>
119  /// Gets the type of the binary implicit conversion.
120  /// </summary>
121  /// <param name="span">The span.</param>
122  /// <param name="left">The left.</param>
123  /// <param name="right">The right.</param>
124  /// <returns>The implicit conversion between between to two types</returns>
126  {
127  var result = CastHelper.GetMultiplyImplicitConversionType(left.ResolveType(), right.ResolveType());
128 
129  if (result == null)
130  Error(MessageCode.ErrorBinaryTypeDeduction, span, left, right);
131 
132  return result;
133  }
134 
135  /// <summary>
136  /// Gets the type of the binary implicit conversion.
137  /// </summary>
138  /// <param name="span">The span.</param>
139  /// <param name="left">The left.</param>
140  /// <param name="right">The right.</param>
141  /// <returns>The implicit conversion between between to two types</returns>
143  {
144  var result = CastHelper.GetDivideImplicitConversionType(left.ResolveType(), right.ResolveType());
145 
146  if (result == null)
147  Error(MessageCode.ErrorBinaryTypeDeduction, span, left, right);
148 
149  return result;
150  }
151 
152  /// <summary>
153  /// Gets the type of the binary implicit scalar conversion.
154  /// </summary>
155  /// <param name="span">The span.</param>
156  /// <param name="left">The left.</param>
157  /// <param name="right">The right.</param>
158  /// <returns>
159  /// The implicit conversion between the two scalar types
160  /// </returns>
162  {
163  var result = CastHelper.GetBinaryImplicitScalarConversionType(left, right);
164 
165  if (result == null)
166  Error(MessageCode.ErrorScalarTypeConversion, span, left, right);
167  return result;
168  }
169  }
170 }
static readonly MessageCode ErrorScalarTypeConversion
Definition: MessageCode.cs:40
virtual TypeBase ResolveType()
Resolves the type.
Definition: TypeBase.cs:101
virtual TypeBase GetDivideImplicitConversionType(SourceSpan span, TypeBase left, TypeBase right)
Gets the type of the binary implicit conversion.
virtual TypeBase GetBinaryImplicitConversionType(SourceSpan span, TypeBase left, TypeBase right, bool isBinaryOperator)
Gets the type of the binary implicit conversion.
void Error(MessageCode message, SourceSpan span, params object[] parameters)
Logs an Error with the specified message.
Definition: AnalysisBase.cs:51
void Warning(MessageCode message, SourceSpan span, params object[] parameters)
Logs an Warning with the specified message.
Definition: AnalysisBase.cs:93
Base type for all types.
Definition: TypeBase.cs:11
static readonly MessageCode ErrorBinaryTypeDeduction
Definition: MessageCode.cs:39
virtual TypeBase GetMultiplyImplicitConversionType(SourceSpan span, TypeBase left, TypeBase right)
Gets the type of the binary implicit conversion.
void Info(string message, SourceSpan span)
Logs an Info with the specified message.
Definition: AnalysisBase.cs:61
AnalysisBase(ParsingResult result)
Initializes a new instance of the AnalysisBase class.
Definition: AnalysisBase.cs:20
void Error(MessageCode message, SourceSpan span)
Logs an Error with the specified message.
Definition: AnalysisBase.cs:40
void Info(string message, SourceSpan span, params object[] parameters)
Logs an Info with the specified message.
Definition: AnalysisBase.cs:72
void Warning(MessageCode message, SourceSpan span)
Logs an Warning with the specified message.
Definition: AnalysisBase.cs:82
ScalarType GetBinaryImplicitScalarConversionType(SourceSpan span, TypeBase left, TypeBase right)
Gets the type of the binary implicit scalar conversion.