Exemplo n.º 1
0
        /// <summary>
        /// Reads a cast expression.
        /// </summary>
        /// <param name="unsafeCode">
        /// Indicates whether the code being parsed resides in an unsafe code block.
        /// </param>
        /// <returns>
        /// Returns the expression.
        /// </returns>
        private CastExpression GetCastExpression(bool unsafeCode)
        {
            Param.Ignore(unsafeCode);

            Reference<ICodePart> expressionReference = new Reference<ICodePart>();

            // Get the opening parenthesis.
            Bracket openParenthesis = this.GetBracketToken(CsTokenType.OpenParenthesis, SymbolType.OpenParenthesis, expressionReference);
            Node<CsToken> openParenthesisNode = this.tokens.InsertLast(openParenthesis);

            // Get the next token. It must be an unknown word.
            this.GetNextSymbol(SymbolType.Other, expressionReference);

            // Get the casted expression.
            LiteralExpression type = this.GetTypeTokenExpression(expressionReference, unsafeCode, true);
            if (type == null || type.Tokens.First == null)
            {
                throw this.CreateSyntaxException();
            }

            // Get the closing parenthesis.
            Bracket closeParenthesis = this.GetBracketToken(CsTokenType.CloseParenthesis, SymbolType.CloseParenthesis, expressionReference);
            Node<CsToken> closeParenthesisNode = this.tokens.InsertLast(closeParenthesis);

            openParenthesis.MatchingBracketNode = closeParenthesisNode;
            closeParenthesis.MatchingBracketNode = openParenthesisNode;

            // Get the embedded expression being casted.
            Expression castedExpression = this.GetNextExpression(ExpressionPrecedence.Unary, expressionReference, unsafeCode);
            if (castedExpression == null || castedExpression.Tokens.First == null)
            {
                throw this.CreateSyntaxException();
            }

            // Create the token list for the expression.
            CsTokenList partialTokens = new CsTokenList(this.tokens, openParenthesisNode, this.tokens.Last);

            // Create and return the expression.
            CastExpression expression = new CastExpression(partialTokens, type, castedExpression);
            expressionReference.Target = expression;

            return expression;
        }
 /// <summary>
 /// The save.
 /// </summary>
 /// <param name="castExpression">
 /// The cast expression.
 /// </param>
 private void Save(CastExpression castExpression)
 {
     this.cppWriter.Write("safe_cast<");
     this.Save(castExpression.Type, this.cppWriter, SavingOptions.None);
     this.cppWriter.Write(">(");
     @switch(castExpression.CastedExpression);
     this.cppWriter.Write(")");
 }