本文整理汇总了C#中TypeViewer类的典型用法代码示例。如果您正苦于以下问题:C# TypeViewer类的具体用法?C# TypeViewer怎么用?C# TypeViewer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TypeViewer类属于命名空间,在下文中一共展示了TypeViewer类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: TryImplicitCoercion
public virtual Expression TryImplicitCoercion(Expression source, TypeNode targetType, TypeViewer typeViewer) {
ErrorHandler oldEH = this.ErrorHandler;
this.ErrorHandler = null;
Expression e = null;
try { e = this.ImplicitCoercion(source, targetType, typeViewer); }
finally {
this.ErrorHandler = oldEH;
};
return e;
}
开发者ID:hesam,项目名称:SketchSharp,代码行数:10,代码来源:TypeSystem.cs
示例2: GetCardinality
public virtual Cardinality GetCardinality(TypeNode collectionType, TypeViewer typeViewer) {
if (collectionType == null) return Cardinality.None;
TypeAlias ta = collectionType as TypeAlias;
if (ta != null) collectionType = ta.AliasedType;
if (collectionType is TupleType) {
return Cardinality.One;
}
else if (collectionType.Template == SystemTypes.GenericBoxed) {
return Cardinality.ZeroOrOne;
}
else if (collectionType.Template == SystemTypes.GenericNonNull) {
return Cardinality.One;
}
else if (collectionType.Template == SystemTypes.GenericNonEmptyIEnumerable) {
return Cardinality.OneOrMore;
}
else if (TypeViewer.GetTypeView(typeViewer, collectionType).IsAssignableTo(SystemTypes.INullable)) {
return Cardinality.ZeroOrOne;
}
else {
TypeUnion tu = collectionType as TypeUnion;
if (tu != null && tu.Types.Count > 0) {
Cardinality c = this.GetCardinality(tu.Types[0], typeViewer);
for( int i = 1, n = tu.Types.Count; i < n; i++ ) {
TypeNode tn = tu.Types[i];
if (tn == null) continue;
c = this.GetCardinalityOr(c, this.GetCardinality(tn, typeViewer));
}
return c;
}
TypeNode elementType = this.GetStreamElementType(collectionType, typeViewer);
if (elementType != collectionType) {
return Cardinality.ZeroOrMore;
}
else if (collectionType.IsValueType) {
return Cardinality.One;
}
else {
return Cardinality.None;
}
}
}
开发者ID:hesam,项目名称:SketchSharp,代码行数:42,代码来源:TypeSystem.cs
示例3: NotAccessible
public static bool NotAccessible(Member member, ref TypeNode qualifierType, int visibility, Module currentModule, TypeNode currentType, TypeViewer typeViewer) {
TypeNode type = member.DeclaringType;
return NotAccessible(member, type, ref qualifierType, visibility, currentModule, currentType, typeViewer);
}
开发者ID:hesam,项目名称:SketchSharp,代码行数:4,代码来源:Checker.cs
示例4: GetCollectionElementType
public virtual TypeNode GetCollectionElementType(TypeNode t, TypeViewer typeViewer){
bool foundObject = false;
TypeAlias ta = t as TypeAlias;
while (ta != null){t = ta.AliasedType; ta = t as TypeAlias;}
if (t == null || t == SystemTypes.String || t is TupleType) return null;
// look for get_Item indexer
MemberList list = TypeViewer.GetTypeView(typeViewer, t).GetMembersNamed(StandardIds.getItem);
if (list != null) {
for( int i = 0, n = list.Count; i < n; i++ ) {
Method m = list[i] as Method;
if (m == null) continue;
if (m.ReturnType != SystemTypes.Object) return m.ReturnType;
foundObject = true;
}
}
// look for enumerable pattern
Method mge = TypeViewer.GetTypeView(typeViewer, t).GetMethod(StandardIds.GetEnumerator);
if (mge != null) {
Method mgc = TypeViewer.GetTypeView(typeViewer, mge.ReturnType).GetMethod(StandardIds.getCurrent);
if (mgc != null) {
if (mgc.ReturnType != SystemTypes.Object) return mgc.ReturnType;
foundObject = true;
}
}
InterfaceList ilist = TypeViewer.GetTypeView(typeViewer, t).Interfaces;
if (ilist != null) {
for( int i = 0, n = ilist.Count; i < n; i++ ) {
Interface iface = ilist[i];
if (iface == null) continue;
TypeNode tn = this.GetCollectionElementType(iface, typeViewer);
if (tn == null) continue;
if (tn != SystemTypes.Object) return tn;
foundObject = true;
}
}
if (foundObject) return SystemTypes.Object;
if (t.BaseType != null && t.BaseType != SystemTypes.Object) {
return this.GetCollectionElementType(t.BaseType, typeViewer);
}
return null;
}
开发者ID:hesam,项目名称:SketchSharp,代码行数:41,代码来源:TypeSystem.cs
示例5: GetMemberElementType
public virtual TypeNode GetMemberElementType(Member member, TypeViewer typeViewer) {
if (member == null) return null;
AttributeNode attr = MetadataHelper.GetCustomAttribute(member, SystemTypes.ElementTypeAttribute);
if (attr != null){
Literal litType = MetadataHelper.GetNamedAttributeValue(attr, StandardIds.ElementType);
if (litType != null) return litType.Value as TypeNode;
}
return this.GetStreamElementType(this.GetMemberType(member), typeViewer);
}
开发者ID:hesam,项目名称:SketchSharp,代码行数:9,代码来源:TypeSystem.cs
示例6: UnifiedType
public virtual TypeNode UnifiedType(Literal lit, TypeNode t, TypeViewer typeViewer){
if (lit == null || lit.Type == null || t == null){Debug.Assert(false); return SystemTypes.Object;}
t = this.Unwrap(t);
MemberList coercions = TypeViewer.GetTypeView(typeViewer, t).ImplicitCoercionMethods;
for (int i = 0, n = coercions == null ? 0 : coercions.Count; i < n; i++){
Method coercion = coercions[i] as Method;
if (coercion == null) continue;
TypeNode t2 = coercion.ReturnType;
if (t2 == t || t2 == null || !t2.IsPrimitive) continue;
if (this.ImplicitLiteralCoercionFromTo(lit, lit.Type, t2)) return t2;
}
return this.UnifiedType(lit.Type, t);
}
开发者ID:hesam,项目名称:SketchSharp,代码行数:13,代码来源:TypeSystem.cs
示例7: SupportedInterfaces
public virtual void SupportedInterfaces(TypeNode t, InterfaceList ifaceList, TypeViewer typeViewer){
if (ifaceList == null) return;
TypeNode unwrappedT = this.Unwrap(t);
Interface iface = unwrappedT as Interface;
if (iface != null){
// possibly not needed, but seems better to keep ifaceList as a set
int i = 0;
while (i < ifaceList.Count){
if (ifaceList[i] == iface)
break;
i++;
}
if (i == ifaceList.Count) // not found
ifaceList.Add(iface);
}else{
// nop
}
InterfaceList ifaces = TypeViewer.GetTypeView(typeViewer, unwrappedT).Interfaces;
for (int i = 0, n = ifaces == null ? 0 : ifaces.Count; i < n; i++){
this.SupportedInterfaces(ifaces[i],ifaceList,typeViewer);
}
return;
}
开发者ID:hesam,项目名称:SketchSharp,代码行数:23,代码来源:TypeSystem.cs
示例8: CoerceToTypeIntersection
public virtual Expression CoerceToTypeIntersection(Expression source, TypeNode sourceType, TypeIntersection targetType, bool explicitCoercion, TypeViewer typeViewer){
if (source == null || sourceType == null || targetType == null) return null;
if (!explicitCoercion){
TypeNodeList types = targetType.Types;
for (int i = 0, n = types == null ? 0 : types.Count; i < n; i++){
TypeNode t = types[i]; if (t == null) continue;
if (!TypeViewer.GetTypeView(typeViewer, sourceType).IsAssignableTo(t)) return null;
}
}
Method fromObject = TypeViewer.GetTypeView(typeViewer, targetType).GetMethod(StandardIds.FromObject, SystemTypes.Object);
Method getType = Runtime.GetType;
MethodCall fromObjectCall = new MethodCall(new MemberBinding(null, fromObject), new ExpressionList(source), NodeType.Call);
fromObjectCall.Type = targetType;
return fromObjectCall;
}
开发者ID:hesam,项目名称:SketchSharp,代码行数:15,代码来源:TypeSystem.cs
示例9: CoerceObjectToTypeUnion
public virtual Expression CoerceObjectToTypeUnion(Expression source, TypeUnion targetType, TypeViewer typeViewer){
Method fromObject = TypeViewer.GetTypeView(typeViewer, targetType).GetMethod(StandardIds.FromObject, SystemTypes.Object, SystemTypes.Type);
Method getType = Runtime.GetType;
ExpressionList arguments = new ExpressionList(2);
arguments.Add(source);
arguments.Add(new MethodCall(new MemberBinding(new Expression(NodeType.Dup), getType), null, NodeType.Call));
MethodCall fromObjectCall = new MethodCall(new MemberBinding(null, fromObject), arguments, NodeType.Call);
fromObjectCall.Type = targetType;
return fromObjectCall;
}
开发者ID:hesam,项目名称:SketchSharp,代码行数:10,代码来源:TypeSystem.cs
示例10: TupleCoercion
public virtual Expression TupleCoercion(Expression source, TypeNode sourceType, TypeNode targetType, bool explicitCoercion, TypeViewer typeViewer){
TupleType sTuple = sourceType as TupleType;
TupleType tTuple = targetType as TupleType;
if (sTuple == null){
if (!explicitCoercion) return null;
if (tTuple == null) return null;
MemberList tMems = tTuple.Members;
if (tMems == null || tMems.Count != 3) return null;
ConstructTuple consTuple = new ConstructTuple();
consTuple.Type = tTuple;
Field f = (Field)tMems[0].Clone();
consTuple.Fields = new FieldList(f);
if (f.Type is TypeAlias)
f.Initializer = this.ExplicitCoercion(source, f.Type, typeViewer);
else
f.Initializer = this.StandardExplicitCoercion(source, this.IsNonNullType(sourceType), sourceType, this.IsNonNullType(f.Type), f.Type, f.Type, typeViewer);
if (f.Initializer == null) return null;
return consTuple;
}
MemberList sMembers = sTuple.Members;
if (sMembers == null) return null;
int n = sMembers.Count;
if (tTuple == null){
if (n == 3){
TypeUnion tUnion = targetType as TypeUnion;
if (tUnion != null){
Method coercion = this.UserDefinedImplicitCoercionMethod(source, sourceType, targetType, true, typeViewer);
if (coercion != null)
return new MethodCall(new MemberBinding(null, coercion), new ExpressionList(this.ImplicitCoercion(source, coercion.Parameters[0].Type, typeViewer)),
NodeType.Call, coercion.ReturnType, source.SourceContext);
}
Field sField = sMembers[0] as Field;
if (sField == null || (!explicitCoercion && !this.ImplicitCoercionFromTo(sField.Type, targetType, typeViewer))) return null;
if (!sField.IsAnonymous && targetType == SystemTypes.Object)
return new BinaryExpression(source, new MemberBinding(null, sTuple), NodeType.Box, SystemTypes.Object);
ConstructTuple cTuple = source as ConstructTuple;
if (cTuple != null){
//TODO: give a warning
source = cTuple.Fields[0].Initializer;
}else{
MemberBinding mb = new MemberBinding(new UnaryExpression(source, NodeType.AddressOf), sField);
mb.Type = sField.Type;
source = mb;
}
if (explicitCoercion)
return this.ExplicitCoercion(source, targetType, typeViewer);
else
return this.ImplicitCoercion(source, targetType, typeViewer);
}
if (targetType == SystemTypes.Object)
return new BinaryExpression(source, new MemberBinding(null, sTuple), NodeType.Box, SystemTypes.Object);
return null;
}
MemberList tMembers = tTuple.Members;
if (sMembers == tMembers) return source;
if (tMembers == null) return null;
if (n != tMembers.Count) return null;
n-=2;
ConstructTuple consTup = source as ConstructTuple;
if (consTup != null){
FieldList consFields = consTup.Fields;
for (int i = 0; i < n; i++){
Field cField = consFields[i];
if (cField == null) continue;
Field tField = tMembers[i] as Field;
if (tField == null) return null;
if (explicitCoercion)
cField.Initializer = this.ExplicitCoercion(cField.Initializer, tField.Type, typeViewer);
else{
if (!tField.IsAnonymous && tField.Name != null &&
(cField.IsAnonymous || cField.Name == null || cField.Name.UniqueIdKey != tField.Name.UniqueIdKey)) return null;
cField.Initializer = this.ImplicitCoercion(cField.Initializer, tField.Type, typeViewer);
}
if (cField.Initializer == null) return null;
cField.Type = tField.Type;
}
consTup.Type = tTuple;
return consTup;
}
Local loc = new Local(sTuple);
CoerceTuple cTup = new CoerceTuple();
cTup.OriginalTuple = source;
cTup.Temp = loc;
cTup.Type = tTuple;
FieldList cFields = cTup.Fields = new FieldList(n);
for (int i = 0; i < n; i++){
Field sField = sMembers[i] as Field;
if (sField == null) return null;
Field tField = tMembers[i] as Field;
if (tField == null) return null;
Field cField = new Field();
cField.Type = tField.Type;
MemberBinding mb = new MemberBinding(loc, sField);
if (explicitCoercion)
cField.Initializer = this.ExplicitCoercion(mb, tField.Type, typeViewer);
else{
if (!tField.IsAnonymous && tField.Name != null &&
(sField.IsAnonymous || sField.Name == null || sField.Name.UniqueIdKey != tField.Name.UniqueIdKey)) return null;
cField.Initializer = this.ImplicitCoercion(mb, tField.Type, typeViewer);
}
//.........这里部分代码省略.........
开发者ID:hesam,项目名称:SketchSharp,代码行数:101,代码来源:TypeSystem.cs
示例11: ImplicitCoercionToIntersection
public virtual bool ImplicitCoercionToIntersection(TypeNode sourceType, TypeIntersection intersect, TypeViewer typeViewer){
if (intersect == null) return false;
TypeNodeList types = intersect.Types;
for (int i = 0, n = types == null ? 0 : types.Count; i < n; i++){
TypeNode t = types[i];
if (t == null) continue;
if (!this.ImplicitCoercionFromTo(sourceType, t, typeViewer)) return false;
}
return true;
}
开发者ID:hesam,项目名称:SketchSharp,代码行数:10,代码来源:TypeSystem.cs
示例12: StandardImplicitCoercion
protected virtual Expression StandardImplicitCoercion(Expression source, bool sourceIsNonNullType, TypeNode sourceType, bool targetIsNonNullType, TypeNode targetType, TypeNode originalTargetType, TypeViewer typeViewer){
if (Literal.IsNullLiteral(source)) {
if (this.IsNullableType(targetType)) {
Local temp = new Local(targetType);
StatementList statements = new StatementList();
BlockExpression result = new BlockExpression(new Block(statements));
statements.Add(new AssignmentStatement(new AddressDereference(new UnaryExpression(temp, NodeType.AddressOf), targetType), new Literal(null, CoreSystemTypes.Object)));
statements.Add(new ExpressionStatement(temp));
return result;
}
if (targetType.IsTemplateParameter && !targetType.IsReferenceType) {
// Check for reference constraint
this.HandleError(source, Error.TypeVarCantBeNull, targetType.Name.Name);
return new Local(targetType);
}
}
//Identity coercion
if (sourceType == targetType && (!targetIsNonNullType || (targetIsNonNullType == sourceIsNonNullType))) return source;
ITypeParameter stp = sourceType as ITypeParameter;
ITypeParameter ttp = targetType as ITypeParameter;
if (stp != null && ttp != null && stp.ParameterListIndex == ttp.ParameterListIndex && stp.DeclaringMember == ttp.DeclaringMember &&
(!targetIsNonNullType || (targetIsNonNullType == sourceIsNonNullType))) return source;
if (source is This && targetType != null && sourceType == targetType.Template && targetType.IsNotFullySpecialized)
//TODO: add check for sourceType.TemplateParameters == targetType.TemplateArguments
return source;
//Dereference source
Reference sr = sourceType as Reference;
if (sr != null){
sourceType = sr.ElementType;
Pointer pType = targetType as Pointer;
if (pType != null && this.StandardImplicitCoercionFromTo(null, sourceType, pType.ElementType, typeViewer))
return source;
else if (pType != null && pType.ElementType == SystemTypes.Void)
return source;
bool sourceIsThis = source is This;
source = new AddressDereference(source, sourceType, source.SourceContext);
source.Type = sourceType;
sourceIsNonNullType = this.IsNonNullType(sourceType);
sourceType = TypeNode.StripModifier(sourceType, SystemTypes.NonNullType);
//Special case for coercion of this in template class
if (sourceIsThis && targetType != null && sourceType == targetType.Template && targetType.IsNotFullySpecialized)
//TODO: add check for sourceType.TemplateParameters == targetType.TemplateArguments
return source;
}
//Identity coercion after dereference
if (sourceType == targetType && (!targetIsNonNullType || (targetIsNonNullType == sourceIsNonNullType))) return source;
//Special case for null literal
if (Literal.IsNullLiteral(source)){
if (targetIsNonNullType)
return ImplicitNonNullCoercion(this.ErrorHandler, source, originalTargetType);
if (targetType is ITypeParameter && this.useGenerics)
return new BinaryExpression(source, new Literal(targetType, SystemTypes.Type), NodeType.UnboxAny);
if (!targetType.IsValueType || targetType.Template == SystemTypes.GenericBoxed)
return new Literal(null, targetType, source.SourceContext);
if (this.IsNullableType(targetType))
return new Local(StandardIds.NewObj, targetType, source.SourceContext);
TypeAlias tAlias = targetType as TypeAlias;
if (tAlias != null){
if (tAlias.RequireExplicitCoercionFromUnderlyingType) return null;
source = this.ImplicitCoercion(source, tAlias.AliasedType, typeViewer);
if (source == null) return null;
Method coercion = this.UserDefinedImplicitCoercionMethod(source, tAlias.AliasedType, targetType, false, typeViewer);
if (coercion != null){
ExpressionList args = new ExpressionList(this.ImplicitCoercion(source, coercion.Parameters[0].Type, typeViewer));
return new MethodCall(new MemberBinding(null, coercion), args, NodeType.Call, coercion.ReturnType);
}
}else{
Method coercion = this.UserDefinedImplicitCoercionMethod(source, source.Type, targetType, true, typeViewer);
if (coercion != null){
ExpressionList args = new ExpressionList(this.ImplicitCoercion(source, coercion.Parameters[0].Type, typeViewer));
return new MethodCall(new MemberBinding(null, coercion), args, NodeType.Call, coercion.ReturnType);
}
}
this.HandleError(source, Error.CannotCoerceNullToValueType, this.GetTypeName(targetType));
return new Local(targetType);
}
//Special case for string literal
if (source.NodeType == NodeType.Literal && sourceType == SystemTypes.String &&
(targetType.Template == SystemTypes.GenericNonNull || targetType.Template == SystemTypes.GenericInvariant) &&
this.GetStreamElementType(targetType, typeViewer) == SystemTypes.String)
return this.ExplicitCoercion(source, targetType, typeViewer);
//Implicit numeric coercions + implicit enumeration coercions + implicit constant expression coercions
if (sourceType.IsPrimitive && sourceType != SystemTypes.String && (targetType.IsPrimitive || targetType == SystemTypes.Decimal || targetType is EnumNode)){
Expression primitiveCoercion = this.ImplicitPrimitiveCoercion(source, sourceType, targetType, typeViewer);
if (primitiveCoercion != null) return primitiveCoercion;
}
//Implicit coercion from string literal to numbers or eums
if (this.allowStringLiteralToOtherPrimitiveCoercion && sourceType == SystemTypes.String && (targetType.IsPrimitive || targetType == SystemTypes.Decimal || targetType is EnumNode)){
Expression primitiveCoercion = this.ImplicitPrimitiveCoercion(source, sourceType, targetType, typeViewer);
if (primitiveCoercion != null) return primitiveCoercion;
}
//Implicit reference coercions
if (TypeViewer.GetTypeView(typeViewer, sourceType).IsAssignableTo(targetType)){
if (targetIsNonNullType && !(sourceIsNonNullType) && !sourceType.IsValueType) {
//Handling for non null types
return ImplicitNonNullCoercion(this.ErrorHandler, source, originalTargetType);
}else if (sourceType.IsValueType && !targetType.IsValueType){
if (sourceType.NodeType == NodeType.TypeUnion){
Debug.Assert(targetType == SystemTypes.Object);
//.........这里部分代码省略.........
开发者ID:hesam,项目名称:SketchSharp,代码行数:101,代码来源:TypeSystem.cs
示例13: CoerceWithLiftedCoercion
public virtual Expression CoerceWithLiftedCoercion(Expression source, TypeNode sourceType, TypeNode targetType, Method coercion, bool explicitCoercion, TypeViewer typeViewer){
if (source == null || sourceType == null || targetType == null || coercion == null){Debug.Assert(false); return null;}
Block nullCase = new Block(new StatementList(1));
Block nonNullCase = new Block(new StatementList(1));
Block done = new Block();
Block coercionBlock = new Block(new StatementList(7));
Local copyOfSource = new Local(source.Type);
Local result = new Local(targetType);
//null case
StatementList statements = nullCase.Statements;
statements.Add(new AssignmentStatement(result, new Local(StandardIds.NewObj, targetType)));
//nonNull case
statements = nonNullCase.Statements;
Method getValue = TypeViewer.GetTypeView(typeViewer, sourceType).GetMethod(Identifier.For("get_Value"));
Expression getVal = new MethodCall(
new MemberBinding(new UnaryExpression(copyOfSource, NodeType.AddressOf, TypeViewer.GetTypeView(typeViewer, sourceType).GetReferenceType()), getValue), null, NodeType.Call, getValue.ReturnType);
if (explicitCoercion)
getVal = this.ExplicitCoercion(getVal, coercion.Parameters[0].Type, typeViewer);
else
getVal = this.ImplicitCoercion(getVal, coercion.Parameters[0].Type, typeViewer);
if (getVal == null) return null;
Expression nonNullVal = new MethodCall(new MemberBinding(null, coercion), new ExpressionList(getVal), NodeType.Call, coercion.ReturnType);
statements.Add(new AssignmentStatement(result, this.ImplicitCoercion(nonNullVal, targetType, typeViewer)));
//coercion block
statements = coercionBlock.Statements;
statements.Add(new AssignmentStatement(copyOfSource, source));
Method hasValue = TypeViewer.GetTypeView(typeViewer, sourceType).GetMethod(StandardIds.getHasValue);
if (hasValue == null){Debug.Assert(false); return null;}
Expression ifNonNull = new MethodCall(
new MemberBinding(new UnaryExpression(copyOfSource, NodeType.AddressOf, TypeViewer.GetTypeView(typeViewer, sourceType).GetReferenceType()), hasValue), null, NodeType.Call);
statements.Add(new Branch(ifNonNull, nonNullCase));
statements.Add(nullCase);
statements.Add(new Branch(null, done));
statements.Add(nonNullCase);
statements.Add(done);
statements.Add(new ExpressionStatement(result));
return new BlockExpression(coercionBlock, targetType);
}
开发者ID:hesam,项目名称:SketchSharp,代码行数:38,代码来源:TypeSystem.cs
示例14: ImplicitCoercion
public virtual Expression ImplicitCoercion(Expression source, TypeNode targetType, TypeViewer typeViewer){
TypeNode originalTargetType = targetType;
if (targetType == null || targetType.Name == Looker.NotFound) return source;
if (source == null) return null;
//HS D
if (source is Hole)
{
source.Type = targetType;
return source;
}
//HS D
if (source is LambdaHole)
{
if (targetType == SystemTypes.Boolean)
source = new LambdaHole(source, new Literal(0), NodeType.Ge, source.SourceContext);
source.Type = targetType;
return source;
}
Literal sourceLit = source as Literal;
if (sourceLit != null && sourceLit.Value is TypeNode){
this.HandleError(source, Error.TypeInVariableContext, this.GetTypeName((TypeNode)sourceLit.Value), "class", "variable");
return null;
}
//Ignore parentheses
if (source.NodeType == NodeType.Parentheses){
UnaryExpression uex = (UnaryExpression)source;
uex.Operand = this.ImplicitCoercion(uex.Operand, targetType, typeViewer);
if (uex.Operand == null) return null;
uex.Type = uex.Operand.Type;
return uex;
}
bool targetIsNonNullType = this.IsNonNullType(targetType);
targetType = TypeNode.StripModifier(targetType, SystemTypes.NonNullType);
targetType = TypeNode.StripModifier(targetType, SystemTypes.NullableType);
//TODO: handle SkipCheck and EnforceCheck
//Special case for closure expressions
if (source.NodeType == NodeType.AnonymousNestedFunction)
return this.CoerceAnonymousNestedFunction((AnonymousNestedFunction)source, targetType, false, typeViewer);
TypeNode sourceType = source.Type;
if (sourceType == null) sourceType = SystemTypes.Object;
bool sourceIsNonNullType = this.IsNonNullType(source.Type);
sourceType = TypeNode.StripModifier(sourceType, SystemTypes.NonNullType);
sourceType = TypeNode.StripModifier(sourceType, SystemTypes.NullableType);
if (sourceType == SystemTypes.String && !sourceIsNonNullType && source is Literal)
sourceIsNonNullType = ((Literal)source).Value != null;
if (this.currentParameter != null && targetType is Reference){
UnaryExpression uex = source as UnaryExpression;
if (uex != null){
if (sourceIsNonNullType && !targetIsNonNullType){
string ttypeName = this.GetTypeName(targetType);
string stypeName = this.GetTypeName(source.Type);
this.HandleError(source, Error.NoImplicitCoercion, stypeName, ttypeName);
return null;
}
if (!sourceIsNonNullType && targetIsNonNullType){
string ttypeName = this.GetTypeName(targetType);
string stypeName = this.GetTypeName(source.Type);
this.HandleError(source, Error.NoImplicitCoercion, stypeName, ttypeName);
return null;
}
if (uex.NodeType == NodeType.OutAddress){
if ((this.currentParameter.Flags & ParameterFlags.Out) == 0){
this.currentParameter.Flags |= ParameterFlags.Out;
string stypeName = this.GetTypeName(sourceType);
this.currentParameter.Flags &= ~ParameterFlags.Out;
this.HandleError(source, Error.NoImplicitCoercion, stypeName, this.GetTypeName(targetType));
return null;
}
}else if (uex.NodeType == NodeType.RefAddress){
if ((this.currentParameter.Flags & ParameterFlags.Out) != 0){
this.currentParameter.Flags &= ~ParameterFlags.Out;
string stypeName = this.GetTypeName(sourceType);
this.currentParameter.Flags |= ParameterFlags.Out;
this.HandleError(source, Error.NoImplicitCoercion, stypeName, this.GetTypeName(targetType));
return null;
}
}
}
}
Expression result = this.StandardImplicitCoercion(source, sourceIsNonNullType, sourceType, targetIsNonNullType, targetType, originalTargetType, typeViewer);
if (result != null) return result;
Method coercion = this.UserDefinedImplicitCoercionMethod(source, sourceType, targetType, true, typeViewer);
if (coercion != null){
if (this.IsNullableType(targetType) && this.IsNullableType(sourceType) && !this.IsNullableType(coercion.Parameters[0].Type))
return this.CoerceWithLiftedCoercion(source, sourceType, targetType, coercion, false, typeViewer);
ExpressionList args = new ExpressionList(1);
args.Add(this.ImplicitCoercion(source, coercion.Parameters[0].Type, typeViewer));
return this.ImplicitCoercion(new MethodCall(new MemberBinding(null, coercion), args, NodeType.Call, coercion.ReturnType, source.SourceContext), targetType, typeViewer);
}
if (sourceType == SystemTypes.Type && source is Literal)
this.HandleError(source, Error.TypeInVariableContext, this.GetTypeName((TypeNode)((Literal)source).Value), "class", "variable");
else if (this.IsNullableType(sourceType) && this.IsNullableType(targetType) && this.ImplicitCoercionFromTo(this.RemoveNullableWrapper(sourceType), this.RemoveNullableWrapper(targetType))) {
TypeNode usType = this.RemoveNullableWrapper(sourceType);
TypeNode utType = this.RemoveNullableWrapper(targetType);
Local tempSrc = new Local(sourceType);
Local tempTar = new Local(targetType);
StatementList statements = new StatementList();
BlockExpression result1 = new BlockExpression(new Block(statements));
statements.Add(new AssignmentStatement(tempSrc, source));
//.........这里部分代码省略.........
开发者ID:hesam,项目名称:SketchSharp,代码行数:101,代码来源:TypeSystem.cs
示例15: UserDefinedImplicitCoercionMethod
public virtual Method UserDefinedImplicitCoercionMethod(Expression source, TypeNode sourceType, TypeNode targetType, bool tryStandardCoercions, TypeViewer typeViewer){
Reference rtype = sourceType as Reference;
if (rtype != null) sourceType = rtype.ElementType;
if (tryStandardCoercions && this.IsNullableType(sourceType) && this.IsNullableType(targetType)) {
sourceType = sourceType.TemplateArguments[0];
targetType = targetType.TemplateArguments[0];
}
//First do efficient searches for a method that implicitly coerces directly between source and target type
//If the source type knows how to convert to the target type, give it preference
Method coercion = TypeViewer.GetTypeView(typeViewer, sourceType).GetImplicitCoercionToMethod(targetType);
if (coercion != null) return coercion;
//If the target type knows how to convert from the source type, that is dandy too
coercion = TypeViewer.GetTypeView(typeViewer, targetType).GetImplicitCoercionFromMethod(sourceType);
if (coercion != null) return coercion;
//Perhaps the base type can convert to the target type, or the target type can convert from the base type
if (sourceType.BaseType != null && sourceType != SystemTypes.Object){
coercion = this.UserDefinedImplicitCoercionMethod(source, sourceType.BaseType, targetType, tryStandardCoercions, typeViewer);
if (coercion != null) return coercion;
}
if (!tryStandardCoercions) return null;
//Now resort to desperate measures
//See if the source type has a conversion that results in a type that can be converted to the target type via a standard coercion
MemberList coercions = TypeViewer.GetTypeView(typeViewer, sourceType).ImplicitCoercionMethods;
for (int i = 0, n = coercions == null ? 0 : coercions.Count; i < n; i++){
coercion = coercions[i] as Method;
if (coercion == null) continue;
if (coercion.ReturnType == sourceType) continue;
if (this.StandardImplicitCoercionFromTo(source, coercion.ReturnType, targetType, typeViewer)) return coercion;
}
//See if the target type has a conversion that can convert the source after a standard coercion has been applied
coercions = TypeViewer.GetTypeView(typeViewer, targetType).ImplicitCoercionMethods;
for (int i = 0, n = coercions == null ? 0 : coercions.Count; i < n; i++){
coercion = coercions[i] as Method;
if (coercion == null) continue;
if (coercion.ReturnType != targetType) continue;
ParameterList pars = coercion.Parameters;
if (pars == null || pars.Count != 1) continue;
Parameter par = pars[0];
if (par.Type == null) continue;
if (this.StandardImplicitCoercionFromTo(source, sourceType, par.Type, typeViewer)) return coercion;
}
return null;
}
开发者ID:hesam,项目名称:SketchSharp,代码行数:43,代码来源:TypeSystem.cs
示例16: CoerceToTypeUnion
public virtual Expression CoerceToTypeUnion(Expression source, TypeNode sourceType, TypeUnion targetType, TypeViewer typeViewer){
if (source == null || sourceType == null || targetType == null) return null;
if (this.UserDefinedImplicitCoercionMethod(source, sourceType, targetType, true, typeViewer) != null) return null;
TypeUnion tType = targetType.UnlabeledUnion;
if (tType == null) return null;
Method coercion = this.UserDefinedImplicitCoercionMethod(source, sourceType, tType, true, typeViewer);
if (coercion == null) return null; //No coercion possible
TypeNode chosenType = coercion.Parameters[0].Type;
TypeNodeList types1 = tType.Types;
TypeNodeList types2 = targetType.Types;
for (int i = 0, n = types1.Count; i < n; i++){
TypeNode t = types1[i];
if (t == chosenType){
source = this.ExplicitCoercion(source, types2[i], typeViewer);
if (source == null) return null;
return this.ExplicitCoercion(source, targetType, typeViewer);
}
}
Debug.Assert(false);
return null;
}
开发者ID:hesam,项目名称:SketchSharp,代码行数:21,代码来源:TypeSystem.cs
示例17: IsBetterMatch
/// <summary>
/// Returns true if conversion from t3 to t1 exists and is better (closer) than the conversion from t3 to t2
/// Call this only if both conversions exist.
/// </summary>
public virtual bool IsBetterMatch(TypeNode t1, TypeNode t2, TypeNode t3, TypeViewer typeViewer){
if (t1 == null) return false;
if (t2 == null || t2 == TypeSystem.DoesNotMatchAnyType) return true; //this type always loses
if (t1 == t2) return false;
if (t1 == t3) return true; //t2 is different from t3 while t1 == t3, so t1 must be a better match
if (t2 == t3) return false; //t1 is different from t3, while t2 == t3, so t1 cannot be a better match
//t3 can go to t1 and t2 only via conversions. Try to establish which conversion is better (closer).
bool t1tot2 = this.ImplicitCoercionFromTo(t1, t2, typeViewer);
bool t2tot1 = this.ImplicitCoercionFromTo(t2, t1, typeViewer);
if (t1tot2 && !t2tot1) return this.ImplicitCoercionFromTo(t3, t1, typeViewer); //Can get from t3 to t2 via t1, but can't get from t3 to t1 via t2, so t3 is closer to t1
if (t2tot1 && !t1tot2) return !this.ImplicitCoercionFromTo(t3, t2, typeViewer); //Get get from t3 to t1 via t2, but can't get from t3 to t2 via t1, so t2 is closer to t1
//Special rule for integer types:
//Prefer conversions to signed integers over conversions to unsigned integers.
//But always prefer smaller int over larger int.
switch (t1.TypeCode){
case TypeCode.SByte:
switch (t2.TypeCode){
case TypeCode.Byte:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
return true;
}
break;
case TypeCode.Byte:
switch (t2.TypeCode){
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
return true;
case TypeCode.SByte:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
return false;
}
break;
case TypeCode.Int16:
switch (t2.TypeCode){
case TypeCode.Byte:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
case TypeCode.Int32:
case TypeCode.Int64:
return true;
case TypeCode.SByte:
return false;
}
break;
case TypeCode.UInt16:
switch (t2.TypeCode){
case TypeCode.UInt32:
case TypeCode.UInt64:
return true;
case TypeCode.Byte:
case TypeCode.SByte:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
return false;
}
break;
case TypeCode.Int32:
switch (t2.TypeCode){
case TypeCode.Byte:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
case TypeCode.Int64:
return true;
case TypeCode.SByte:
case TypeCode.Int16:
return false;
}
break;
case TypeCode.UInt32:
switch (t2.TypeCode){
case TypeCode.UInt64:
return true;
case TypeCode.Byte:
case TypeCode.UInt16:
case TypeCode.SByte:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
return false;
}
break;
case TypeCode.Int64:
switch (t2.TypeCode){
case TypeCode.Byte:
case TypeCode.UInt16:
//.........这里部分代码省略.........
开发者ID:hesam,项目名称:SketchSharp,代码行数:101,代码来源:TypeSystem.cs
示例18: CoerceFromTypeUnion
protected virtual Expression CoerceFromTypeUnion(Expression source, TypeUnion sourceType, TypeNode targetType, bool explicitCoercion, TypeNode originalTargetType, TypeViewer typeViewer){
if (source == null || sourceType == null || targetType == null) return null;
if (targetType == SystemTypes.Object) return this.CoerceTypeUnionToObject(source, typeViewer);
int cErrors = (this.Errors != null) ? this.Errors.Count : 0;
if (explicitCoercion){
Method coercion = this.UserDefinedExplicitCoercionMethod(source, sourceType, targetType, false, originalTargetType, typeViewer);
if (coercion != null && coercion.ReturnType == targetType && coercion.Parameters != null && coercion.Parameters[0] != null &&
this.ImplicitCoercionFromTo(sourceType, coercion.Parameters[0].Type, typeViewer))
return this.ImplicitCoercion(new MethodCall(new MemberBinding(null, coercion), new ExpressionList(source), NodeType.Call, coercion.ReturnType),
targetType, typeViewer);
}
Method getTag = TypeViewer.GetTypeView(typeViewer, sourceType).GetMethod(StandardIds.GetTag);
if (getTag == null) return null;
Method getValue = TypeViewer.GetTypeView(typeViewer, sourceType).GetMethod(StandardIds.GetValue);
if (getValue == null) return null;
Local src = new Local(sourceType);
Local srcOb = new Local(SystemTypes.Object, source.SourceContext);
Local tgt = new Local(targetType);
Expression callGetTag = new MethodCall(new MemberBinding(new UnaryExpression(src, NodeType.AddressOf), getTag), null);
Expression callGetValue = new MethodCall(new MemberBinding(new UnaryExpression(src, NodeType.AddressOf), getValue), null);
TypeNodeList types = sourceType.Types;
int n = types == null ? 0 : types.Count;
Block endOfSwitch = new Block();
StatementList statements = new StatementList(5+n);
statements.Add(new AssignmentStatement(src, source));
statements.Add(new AssignmentStatement(srcOb, callGetValue));
BlockList cases = new BlockList(n);
statements.Add(new SwitchInstruction(callGetTag, cases));
bool hadCoercion = false;
Block eb = new Block(new StatementList(1));
Construct c = new Construct(new MemberBinding(null, SystemTypes.InvalidCastException.GetConstructor()), null, SystemTypes.InvalidCastException);
eb.Statements.Add(new Throw(c));
for (int i = 0; i < n; i++){
TypeNode t = types[i];
if (t == null) continue;
if (!explicitCoercion && !this.ImplicitCoercionFromTo(t, targetType, typeViewer)) return null;
Expression expr = this.ExplicitCoercion(srcOb, t, typeViewer);
if (expr == null) return null;
expr = this.ExplicitCoercion(expr, targetType, typeViewer);
if (expr == null) {
cases.Add(eb);
statements.Add(eb);
}
else {
Block b = new Block(new StatementList(2));
hadCoercion = true;
expr.SourceContext = srcOb.SourceContext;
b.Statements.Add(new AssignmentStatement(tgt, expr));
b.Statements.Add(new Branch(null, endOfSwitch));
cases.Add(b);
statements.Add(b);
}
}
if (this.Errors != null) {
for (int ie = cErrors, ne = this.Errors.Count; ie < ne; ie++) {
this.Errors[ie] = null;
}
}
if (!hadCoercion) return null;
statements.Add(endOfSwitch);
statements.Add(new ExpressionStatement(tgt));
return new BlockExpression(new Block(statements));
//TODO: wrap this in a CoerceTypeUnion node so that source code can be reconstructed easily
}
开发者ID:hesam,项目名称:SketchSharp,代码行数:64,代码来源:TypeSystem.cs
示例19: ImplicitCoercionToUnion
|
请发表评论