Exemplo n.º 1
0
        /// <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="parentReference">
        /// The parent code unit.
        /// </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, Reference<ICodePart> parentReference, bool unsafeCode)
        {
            Param.AssertNotNull(leftHandSide, "leftHandSide");
            Param.Ignore(previousPrecedence);
            Param.AssertNotNull(parentReference, "parentReference");
            Param.Ignore(unsafeCode);

            IsExpression expression = null;

            // Check the previous precedence to see if we are allowed to gather up the is expression.
            if (CheckPrecedence(previousPrecedence, ExpressionPrecedence.Relational))
            {
                Reference<ICodePart> expressionReference = new Reference<ICodePart>();

                // 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, parentReference, expressionReference));

                // The next token must be the type.
                this.GetNextSymbol(SymbolType.Other, expressionReference);

                // Get the expression representing the type.
                LiteralExpression rightHandSide = this.GetTypeTokenExpression(expressionReference, 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);
                expressionReference.Target = expression;
            }

            return expression;
        }
 /// <summary>
 /// The save.
 /// </summary>
 /// <param name="isExpression">
 /// The is expression.
 /// </param>
 private void Save(IsExpression isExpression)
 {
     this.cppWriter.Write("dynamic_cast<");
     this.Save(isExpression.Type, this.cppWriter, SavingOptions.None);
     this.cppWriter.Write(">(");
     @switch(isExpression.Value);
     this.cppWriter.Write(") != nullptr");
 }