本文整理汇总了C#中IsExpression类的典型用法代码示例。如果您正苦于以下问题:C# IsExpression类的具体用法?C# IsExpression怎么用?C# IsExpression使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
IsExpression类属于命名空间,在下文中一共展示了IsExpression类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: E
/// <summary>
/// http://dlang.org/expression.html#IsExpression
/// </summary>
public ISemantic E(IsExpression isExpression)
{
if(!eval)
return new PrimitiveType(DTokens.Bool);
bool retTrue = false;
if (isExpression.TestedType != null)
{
var typeToCheck = DResolver.StripMemberSymbols(TypeDeclarationResolver.ResolveSingle(isExpression.TestedType, ctxt));
if (typeToCheck != null)
{
// case 1, 4
if (isExpression.TypeSpecialization == null && isExpression.TypeSpecializationToken == 0)
retTrue = true;
// The probably most frequented usage of this expression
else if (isExpression.TypeAliasIdentifierHash == 0)
retTrue = evalIsExpression_NoAlias(isExpression, typeToCheck);
else
retTrue = evalIsExpression_WithAliases(isExpression, typeToCheck);
}
}
return new PrimitiveValue(DTokens.Bool, retTrue?1:0, isExpression);
}
示例2: VisitIsExpression
public override void VisitIsExpression (IsExpression isExpression)
{
base.VisitIsExpression (isExpression);
// var conversions = CSharpConversions.Get(ctx.Compilation);
var exprType = ctx.Resolve (isExpression.Expression).Type;
var providedType = ctx.ResolveType (isExpression.Type);
if (exprType.Kind == TypeKind.Unknown || providedType.Kind == TypeKind.Unknown)
return;
if (IsValidReferenceOrBoxingConversion(exprType, providedType))
return;
var exprTP = exprType as ITypeParameter;
var providedTP = providedType as ITypeParameter;
if (exprTP != null) {
if (IsValidReferenceOrBoxingConversion(exprTP.EffectiveBaseClass, providedType)
&& exprTP.EffectiveInterfaceSet.All(i => IsValidReferenceOrBoxingConversion(i, providedType)))
return;
}
if (providedTP != null) {
if (IsValidReferenceOrBoxingConversion(exprType, providedTP.EffectiveBaseClass))
return;
}
AddIssue (isExpression, ctx.TranslateString ("Given expression is never of the provided type"));
}
示例3: VisitIsExpression
public override void VisitIsExpression (IsExpression isExpression)
{
base.VisitIsExpression (isExpression);
var exprType = ctx.Resolve (isExpression.Expression).Type;
var providedType = ctx.ResolveType (isExpression.Type);
if (TypeCompatibilityHelper.CheckTypeCompatibility(exprType, providedType) ==
TypeCompatibilityHelper.TypeCompatiblity.NeverOfProvidedType)
AddIssue (isExpression, ctx.TranslateString ("Given expression is never of the provided type"));
}
示例4: evalIsExpression_WithAliases
private bool evalIsExpression_WithAliases(IsExpression isExpression, AbstractType typeToCheck)
{
/*
* Note: It's needed to let the abstract ast scanner also scan through IsExpressions etc.
* in order to find aliases and/or specified template parameters!
*/
var expectedTemplateParams = new TemplateParameter[isExpression.TemplateParameterList == null ? 1 : (isExpression.TemplateParameterList.Length + 1)];
expectedTemplateParams [0] = isExpression.ArtificialFirstSpecParam;
if(expectedTemplateParams.Length > 1)
isExpression.TemplateParameterList.CopyTo (expectedTemplateParams, 1);
var tpl_params = new DeducedTypeDictionary(expectedTemplateParams);
var tpd = new TemplateParameterDeduction(tpl_params, ctxt);
bool retTrue = false;
if (isExpression.EqualityTest) // 6.
{
// a)
if (isExpression.TypeSpecialization != null)
{
tpd.EnforceTypeEqualityWhenDeducing = true;
retTrue = tpd.Handle(isExpression.ArtificialFirstSpecParam, typeToCheck);
tpd.EnforceTypeEqualityWhenDeducing = false;
}
else // b)
{
var r = evalIsExpression_EvalSpecToken(isExpression, typeToCheck, true);
retTrue = r.Item1;
tpl_params[isExpression.ArtificialFirstSpecParam] = new TemplateParameterSymbol(isExpression.ArtificialFirstSpecParam, r.Item2);
}
}
else // 5.
retTrue = tpd.Handle(isExpression.ArtificialFirstSpecParam, typeToCheck);
if (retTrue && isExpression.TemplateParameterList != null)
foreach (var p in isExpression.TemplateParameterList)
if (!tpd.Handle(p, tpl_params[p] != null ? tpl_params[p].Base : null))
return false;
if (retTrue)
{
foreach (var kv in tpl_params)
ctxt.CurrentContext.DeducedTemplateParameters[kv.Key] = kv.Value;
}
return retTrue;
}
示例5: VisitIsExpression
public override void VisitIsExpression (IsExpression isExpression)
{
base.VisitIsExpression (isExpression);
var type = ctx.Resolve (isExpression.Expression).Type;
var providedType = ctx.ResolveType (isExpression.Type);
if (type.GetAllBaseTypes ().All (t => !t.Equals (providedType)))
return;
var action = new CodeAction (ctx.TranslateString ("Compare with 'null'"),
scrpit => scrpit.Replace (isExpression, new BinaryOperatorExpression (
isExpression.Expression.Clone (), BinaryOperatorType.InEquality, new PrimitiveExpression (null))));
AddIssue (isExpression, ctx.TranslateString ("Given expression is always of the provided type. " +
"Consider comparing with 'null' instead"), new [] { action });
}
示例6: evalIsExpression_WithAliases
private bool evalIsExpression_WithAliases(IsExpression isExpression, AbstractType typeToCheck)
{
/*
* Note: It's needed to let the abstract ast scanner also scan through IsExpressions etc.
* in order to find aliases and/or specified template parameters!
*/
var expectedTemplateParams = new TemplateParameter[isExpression.TemplateParameterList.Length + 1];
expectedTemplateParams [0] = isExpression.ArtificialFirstSpecParam;
if(expectedTemplateParams.Length > 1)
isExpression.TemplateParameterList.CopyTo (expectedTemplateParams, 1);
var tpl_params = new DeducedTypeDictionary(expectedTemplateParams);
var tpd = new TemplateParameterDeduction(tpl_params, ctxt);
bool retTrue = false;
if (isExpression.EqualityTest) // 6.
{
// a)
if (isExpression.TypeSpecialization != null)
{
tpd.EnforceTypeEqualityWhenDeducing = true;
retTrue = tpd.Handle(isExpression.ArtificialFirstSpecParam, typeToCheck);
tpd.EnforceTypeEqualityWhenDeducing = false;
}
else // b)
{
var r = evalIsExpression_EvalSpecToken(isExpression, typeToCheck, true);
retTrue = r.Item1;
tpl_params[isExpression.TypeAliasIdentifierHash] = new TemplateParameterSymbol(null, r.Item2);
}
}
else // 5.
retTrue = tpd.Handle(isExpression.ArtificialFirstSpecParam, typeToCheck);
if (retTrue && isExpression.TemplateParameterList != null)
foreach (var p in isExpression.TemplateParameterList)
if (!tpd.Handle(p, tpl_params[p.NameHash] != null ? tpl_params[p.NameHash].Base : null))
return false;
//TODO: Put all tpl_params results into the resolver context or make a new scope or something!
return retTrue;
}
示例7: VisitIsExpression
public override void VisitIsExpression (IsExpression isExpression)
{
base.VisitIsExpression (isExpression);
var type = ctx.Resolve (isExpression.Expression).Type;
var providedType = ctx.ResolveType (isExpression.Type);
// var foundConversion = conversions.ImplicitConversion(type, providedType);
if (!IsValidReferenceOrBoxingConversion(type, providedType))
return;
var action = new CodeAction (ctx.TranslateString ("Compare with 'null'"),
scrpit => scrpit.Replace (isExpression, new BinaryOperatorExpression (
isExpression.Expression.Clone (), BinaryOperatorType.InEquality, new PrimitiveExpression (null))));
AddIssue (isExpression, ctx.TranslateString ("Given expression is always of the provided type. " +
"Consider comparing with 'null' instead"), new [] { action });
}
示例8: GetIsExpression
/// <summary>
/// Reads an is expression.
/// </summary>
/// <param name="leftHandSide">The expression on the left hand side of the operator.</param>
/// <param name="previousPrecedence">The precedence of the expression just before this one.</param>
/// <param name="unsafeCode">Indicates whether the code being parsed resides in an unsafe code block.</param>
/// <returns>Returns the expression.</returns>
private IsExpression GetIsExpression(
Expression leftHandSide, ExpressionPrecedence previousPrecedence, bool unsafeCode)
{
Param.AssertNotNull(leftHandSide, "leftHandSide");
Param.Ignore(previousPrecedence);
Param.Ignore(unsafeCode);
IsExpression expression = null;
// Check the previous precedence to see if we are allowed to gather up the is expression.
if (this.CheckPrecedence(previousPrecedence, ExpressionPrecedence.Relational))
{
// Make sure the left hand side has at least one token.
Debug.Assert(leftHandSide.Tokens.First != null, "The left hand side should not be empty");
// Get the is symbol.
this.tokens.Add(this.GetToken(CsTokenType.Is, SymbolType.Is));
// The next token must be the type.
this.GetNextSymbol(SymbolType.Other);
// Get the expression representing the type.
LiteralExpression rightHandSide = this.GetTypeTokenExpression(unsafeCode, true, true);
if (rightHandSide == null || rightHandSide.Tokens.First == null)
{
throw this.CreateSyntaxException();
}
// Create the partial token list for the expression.
CsTokenList partialTokens = new CsTokenList(this.tokens, leftHandSide.Tokens.First, this.tokens.Last);
// Create and return the expression.
expression = new IsExpression(partialTokens, leftHandSide, rightHandSide);
}
return expression;
}
示例9: VisitIsExpression
public StringBuilder VisitIsExpression(IsExpression isExpression, int data)
{
throw new SLSharpException("SL# does not have reflection.");
}
示例10: PrimaryExpression
//.........这里部分代码省略.........
}
#endregion
if (laKind == (Typeof))
{
var startLoc = la.Location;
return new TypeDeclarationExpression(TypeOf()) {Location=startLoc,EndLocation=t.EndLocation};
}
// TypeidExpression
if (laKind == (Typeid))
{
Step();
var ce = new TypeidExpression() { Location=t.Location};
LastParsedObject = ce;
Expect(OpenParenthesis);
AllowWeakTypeParsing = true;
ce.Type = Type();
AllowWeakTypeParsing = false;
if (ce.Type==null)
ce.Expression = AssignExpression();
Expect(CloseParenthesis);
ce.EndLocation = t.EndLocation;
return ce;
}
#region IsExpression
if (laKind == Is)
{
Step();
var ce = new IsExpression() { Location=t.Location};
LastParsedObject = ce;
Expect(OpenParenthesis);
var LookAheadBackup = la;
AllowWeakTypeParsing = true;
ce.TestedType = Type();
AllowWeakTypeParsing = false;
if (ce.TestedType!=null && laKind == Identifier && (Lexer.CurrentPeekToken.Kind == CloseParenthesis || Lexer.CurrentPeekToken.Kind == Equal
|| Lexer.CurrentPeekToken.Kind == Colon))
{
Step();
ce.TypeAliasIdentifier = strVal;
}
else
// D Language specs mistake: In an IsExpression there also can be expressions!
if(ce.TestedType==null || !(laKind==CloseParenthesis || laKind==Equal||laKind==Colon))
{
// Reset lookahead token to prior position
la = LookAheadBackup;
// Reset wrongly parsed type declaration
ce.TestedType = null;
ce.TestedExpression = ConditionalExpression();
}
if(ce.TestedExpression==null && ce.TestedType==null)
SynErr(laKind,"In an IsExpression, either a type or an expression is required!");
if (laKind == CloseParenthesis)
{
Step();
示例11: VisitIsExpression
public void VisitIsExpression(IsExpression isExpression)
{
StartNode(isExpression);
isExpression.Expression.AcceptVisitor(this);
Space();
WriteKeyword(IsExpression.IsKeywordRole);
isExpression.Type.AcceptVisitor(this);
EndNode(isExpression);
}
示例12: VisitIsExpression
public virtual void VisitIsExpression (IsExpression isExpression)
{
VisitChildren (isExpression);
}
示例13: VisitIsExpression
public virtual void VisitIsExpression(IsExpression isExpression)
{
if (this.ThrowException)
{
throw (Exception)this.CreateException(isExpression);
}
}
示例14: CreateEqualsOverrides
List<MethodDeclaration> CreateEqualsOverrides(IType currentClass)
{
List<MethodDeclaration> methods = new List<MethodDeclaration>();
AstType boolReference = ConvertType(KnownTypeCode.Boolean);
AstType objectReference = ConvertType(KnownTypeCode.Object);
MethodDeclaration method = new MethodDeclaration {
Name = "Equals",
Modifiers = Modifiers.Public | Modifiers.Override,
ReturnType = boolReference
};
method.Parameters.Add(new ParameterDeclaration(objectReference, "obj"));
method.Body = new BlockStatement();
AstType currentType = ConvertType(currentClass);
Expression expr = null;
if (currentClass.Kind == TypeKind.Struct) {
// return obj is CurrentType && Equals((CurrentType)obj);
expr = new IsExpression() {
Expression = new IdentifierExpression("obj"),
Type = currentType.Clone()
};
expr = new ParenthesizedExpression(expr);
expr = new BinaryOperatorExpression(
expr, BinaryOperatorType.ConditionalAnd,
new InvocationExpression(
new IdentifierExpression("Equals"),
new List<Expression> {
new CastExpression(currentType.Clone(), new IdentifierExpression("obj"))
}));
method.Body.Add(new ReturnStatement(expr));
methods.Add(method);
// IEquatable implementation:
method = new MethodDeclaration {
Name = "Equals",
Modifiers = Modifiers.Public,
ReturnType = boolReference.Clone()
};
method.Parameters.Add(new ParameterDeclaration(currentType, "other"));
method.Body = new BlockStatement();
} else {
method.Body.Add(new VariableDeclarationStatement(
currentType.Clone(),
"other",
new IdentifierExpression("obj").CastAs(currentType.Clone())));
method.Body.Add(new IfElseStatement(
new BinaryOperatorExpression(new IdentifierExpression("other"), BinaryOperatorType.Equality, new PrimitiveExpression(null, "null")),
new ReturnStatement(new PrimitiveExpression(false, "false"))));
}
expr = null;
foreach (IField field in currentClass.GetFields()) {
if (field.IsStatic) continue;
if (expr == null) {
expr = TestEquality("other", field);
} else {
expr = new BinaryOperatorExpression(expr, BinaryOperatorType.ConditionalAnd,
TestEquality("other", field));
}
}
foreach (IProperty property in currentClass.GetProperties()) {
if (property.IsStatic || !property.IsAutoImplemented()) continue;
if (expr == null) {
expr = TestEquality("other", property);
} else {
expr = new BinaryOperatorExpression(expr, BinaryOperatorType.ConditionalAnd,
TestEquality("other", property));
}
}
method.Body.Add(new ReturnStatement(expr ?? new PrimitiveExpression(true, "true")));
methods.Add(method);
return methods;
}
示例15: CastBlock
public CastBlock(IEmitter emitter, IsExpression isExpression)
: base(emitter, isExpression)
{
this.Emitter = emitter;
this.IsExpression = isExpression;
}