/// <summary> /// Reads the next fixed-statement from the file and returns it. /// </summary> /// <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 statement. /// </returns> private FixedStatement ParseFixedStatement(Reference<ICodePart> parentReference, bool unsafeCode) { Param.AssertNotNull(parentReference, "parentReference"); Param.Ignore(unsafeCode); Reference<ICodePart> statementReference = new Reference<ICodePart>(); // Move past the fixed keyword. CsToken firstToken = this.GetToken(CsTokenType.Fixed, SymbolType.Fixed, parentReference, statementReference); Node<CsToken> firstTokenNode = this.tokens.InsertLast(firstToken); // Make sure we're sitting on the opening parenthesis now. Bracket openParenthesis = this.GetBracketToken(CsTokenType.OpenParenthesis, SymbolType.OpenParenthesis, statementReference); Node<CsToken> openParenthesisNode = this.tokens.InsertLast(openParenthesis); // Get the expression within the parenthesis. It must be a variable declaration. VariableDeclarationExpression expression = this.GetNextExpression(ExpressionPrecedence.None, statementReference, unsafeCode, true, false) as VariableDeclarationExpression; if (expression == null) { throw this.CreateSyntaxException(); } // Get the closing parenthesis. Bracket closeParenthesis = this.GetBracketToken(CsTokenType.CloseParenthesis, SymbolType.CloseParenthesis, statementReference); Node<CsToken> closeParenthesisNode = this.tokens.InsertLast(closeParenthesis); openParenthesis.MatchingBracketNode = closeParenthesisNode; closeParenthesis.MatchingBracketNode = openParenthesisNode; // Get the embedded statement. Statement childStatement = this.GetNextStatement(statementReference, unsafeCode); if (childStatement == null) { throw this.CreateSyntaxException(); } // Create the token list for the statement. CsTokenList partialTokens = new CsTokenList(this.tokens, firstTokenNode, this.tokens.Last); // Create the fixed-statement. FixedStatement statement = new FixedStatement(partialTokens, expression); statement.EmbeddedStatement = childStatement; statementReference.Target = statement; // Add the variable if there is one. foreach (VariableDeclaratorExpression declarator in expression.Declarators) { Variable variable = new Variable( expression.Type, declarator.Identifier.Token.Text, VariableModifiers.None, CodeLocation.Join(expression.Type.Location, declarator.Identifier.Token.Location), statementReference, expression.Type.Generated || declarator.Identifier.Token.Generated); // If there is already a variable in this scope with the same name, ignore this one. if (!statement.Variables.Contains(declarator.Identifier.Token.Text)) { statement.Variables.Add(variable); } } return statement; }
/// <summary> /// The save. /// </summary> /// <param name="fixedStatement"> /// The fixed statement. /// </param> private void Save(FixedStatement fixedStatement) { this.cppWriter.WriteLine("/* fixed */"); this.cppWriter.WriteLine("{"); this.cppWriter.Indent++; this.Save(fixedStatement.FixedVariable); this.cppWriter.Write(";"); this.SetMarkEndOfBlock(); this.Save(fixedStatement.EmbeddedStatement); this.cppWriter.Indent--; this.cppWriter.WriteLine("}"); }