Beispiel #1
0
            public override ICatchClause Rewrite(ICatchClause catchClause)
            {
                _log.Info("Rewriting ITryCatchFinallyStatement: " + catchClause + " Pass: "******".ctor"));
                    expression = new CreateObjectInstance()
                    {
                        MethodToCall = ctor,
                        Type         = catchClause.ExceptionContainer.Type
                    };
                }
                return(new CatchClause(catchClause)
                {
                    Body = new BlockStatement()
                    {
                        Statements = new ThrowStatement()
                        {
                            Exception = expression
                        }.InList <IStatement>()
                    }
                });
            }
 private void AddEntryPointMethod(Compilation compilation) {
   List<Expression> args = new List<Expression>(1);
   args.Add(new CompileTimeConstant(0, SourceDummy.SourceLocation));
   Expression createInstance = new CreateObjectInstance(new NamedTypeExpression(new SimpleName(this.Name, SourceDummy.SourceLocation, false)), args, SourceDummy.SourceLocation);
   Expression callMain = new MethodCall(
     new QualifiedName(createInstance, new SimpleName(compilation.NameTable.GetNameFor("Main"), SourceDummy.SourceLocation, false), SourceDummy.SourceLocation),
     Expression.EmptyCollection, SourceDummy.SourceLocation);
   List<Statement> body = new List<Statement>(1);
   body.Add(new ExpressionStatement(callMain));
   MethodDeclaration entryPointMethod = new MethodDeclaration(null, MethodDeclaration.Flags.Static, TypeMemberVisibility.Public,
     TypeExpression.For(compilation.PlatformType.SystemVoid.ResolvedType), null, new NameDeclaration(compilation.NameTable.GetNameFor("EntryPoint"), SourceDummy.SourceLocation), null, 
     null, null, new BlockStatement(body, this.SourceLocation), this.SourceLocation);
   this.members.Add(entryPointMethod);
 }
        public override IExpression VisitObjectCreationExpression(ObjectCreationExpressionSyntax node)
        {
            var o = this.semanticModel.GetSymbolInfo(node);
            var s = o.Symbol;

            if (s != null)
            {
                MethodSymbol ms = s as MethodSymbol;
                var          mr = this.mapper.Map(ms);
                var          e  = new CreateObjectInstance()
                {
                    Locations    = Helper.SourceLocation(this.tree, node),
                    MethodToCall = mr,
                    Type         = mr.ContainingType,
                };
                return(e);
            }
            throw new InvalidDataException("VisitObjectCreationExpression couldn't find something to return");
        }
Beispiel #4
0
        public override void TraverseChildren(IBlockStatement blockStatement)
        {
            base.TraverseChildren(blockStatement);
            Contract.Assume(blockStatement is BlockStatement);
            var block = this.currentBlock = (BlockStatement)blockStatement;
            var n     = block.Statements.Count;
            int i;

            for (i = 0; i < n; i++)
            {
                var statement = block.Statements[i];
                Contract.Assume(statement != null);
                if (statement is EmptyStatement)
                {
                    continue;
                }
                var decl = statement as LocalDeclarationStatement;
                if (decl == null)
                {
                    break;
                }
                this.declarationFor.Add(decl.LocalVariable, decl);
            }
            int unifications = 0;

            for (; i < n; i++)
            {
                var statement = block.Statements[i];
                Contract.Assume(statement != null);
                var es = statement as ExpressionStatement;
                if (es == null)
                {
                    if (statement is EmptyStatement || statement is LabeledStatement)
                    {
                        continue;
                    }
                    break;
                }
                ILocalDefinition local   = null;
                var         assignment   = es.Expression as Assignment;
                IExpression initialValue = null;
                if (assignment != null)
                {
                    initialValue = assignment.Source;
                    local        = assignment.Target.Definition as ILocalDefinition;
                    if (local == null)
                    {
                        var addressDeref = assignment.Target.Definition as IAddressDereference;
                        if (addressDeref == null)
                        {
                            continue;
                        }
                        var addressOf = addressDeref.Address as IAddressOf;
                        if (addressOf == null)
                        {
                            continue;
                        }
                        var addressableExpression = addressOf.Expression as IAddressableExpression;
                        if (addressableExpression == null)
                        {
                            continue;
                        }
                        local = addressableExpression.Definition as ILocalDefinition;
                        if (local == null || this.firstReferenceToLocal[local] != addressableExpression)
                        {
                            continue;
                        }
                    }
                    else
                    {
                        if (this.firstReferenceToLocal[local] != assignment.Target)
                        {
                            continue;
                        }
                    }
                }
                else
                {
                    var mcall = es.Expression as MethodCall;
                    if (mcall == null || mcall.IsStaticCall || mcall.IsJumpCall || mcall.MethodToCall.Type.TypeCode != PrimitiveTypeCode.Void || mcall.MethodToCall.Name.Value != ".ctor")
                    {
                        continue;
                    }
                    var addressOf = mcall.ThisArgument as IAddressOf;
                    if (addressOf == null)
                    {
                        continue;
                    }
                    var addressableExpression = addressOf.Expression as IAddressableExpression;
                    if (addressableExpression == null)
                    {
                        continue;
                    }
                    local = addressableExpression.Definition as ILocalDefinition;
                    if (local == null || this.firstReferenceToLocal[local] != addressableExpression)
                    {
                        continue;
                    }
                    if (this.declarationFor.ContainsKey(local))
                    {
                        initialValue = new CreateObjectInstance()
                        {
                            Arguments = mcall.Arguments, Locations = mcall.Locations, MethodToCall = mcall.MethodToCall, Type = mcall.MethodToCall.ContainingType
                        }
                    }
                    ;
                }
                if (local == null || initialValue == null)
                {
                    continue;
                }
                LocalDeclarationStatement decl;
                if (!this.declarationFor.TryGetValue(local, out decl))
                {
                    continue;
                }
                Contract.Assume(decl != null);
                this.unifiedDeclarations.Add(decl);
                this.declarationFor.Remove(local);
                decl.InitialValue   = initialValue;
                block.Statements[i] = decl;
                unifications++;
            }
            if (unifications == 0)
            {
                return;
            }
            var newStatements = new List <IStatement>(block.Statements.Count - unifications);

            for (i = 0; i < n; i++)
            {
                var statement = block.Statements[i];
                var decl      = statement as LocalDeclarationStatement;
                if (decl != null)
                {
                    if (this.unifiedDeclarations.Contains(decl))
                    {
                        this.unifiedDeclarations.Remove(decl);
                        continue;
                    }
                    if (this.numberOfReferencesToLocal[decl.LocalVariable] == 0 && this.numberOfAssignmentsToLocal[decl.LocalVariable] == 0)
                    {
                        continue;
                    }
                }
                newStatements.Add(statement);
            }
            block.Statements = newStatements;
        }
Beispiel #5
0
 private Expression ParseNew(TokenSet followers) 
   //^ requires this.currentToken == Token.New;
   //^ ensures followers[this.currentToken] || this.currentToken == Token.EndOfFile;
 {
   SourceLocationBuilder ctx = new SourceLocationBuilder(this.scanner.SourceLocationOfLastScannedToken);
   this.GetNextToken();
   if (this.currentToken == Token.LeftBracket)
     return this.ParseNewImplicitlyTypedArray(ctx, followers);
   if (this.currentToken == Token.LeftBrace)
     return this.ParseNewAnonymousTypeInstance(ctx, followers);
   TypeExpression t = this.ParseBaseTypeExpression(false, followers|Parser.InfixOperators|Token.LeftBracket|Token.LeftParenthesis|Token.RightParenthesis);
   if (this.currentToken == Token.Conditional) {
     SourceLocationBuilder slb = new SourceLocationBuilder(t.SourceLocation);
     slb.UpdateToSpan(this.scanner.SourceLocationOfLastScannedToken);
     //^ assume this.currentToken == Token.Conditional; //no side effects from the methods can touch this.currentToken
     this.GetNextToken();
     t = new NullableTypeExpression(t, slb);
   //} else if (this.currentToken == Token.LogicalNot) {
   //  TypeExpression type = t;
   //  t = new NonNullableTypeExpression(type);
   //  t.SourceContext = type.SourceContext;
   //  t.SourceContext.EndPos = this.scanner.endPos;
   //  this.GetNextToken();
   } else if (this.currentToken == Token.Multiply) {
     SourceLocationBuilder slb = new SourceLocationBuilder(t.SourceLocation);
     slb.UpdateToSpan(this.scanner.SourceLocationOfLastScannedToken);
     //^ assume this.currentToken == Token.Multiply; //no side effects from the methods can touch this.currentToken
     this.GetNextToken();
     t = new PointerTypeExpression(t, slb);
   }
   ctx.UpdateToSpan(t.SourceLocation);
   TypeExpression et = t;
   uint rank = 0;
   while (this.currentToken == Token.LeftBracket) {
     Token nextTok = this.PeekNextToken();
     if (nextTok != Token.Comma && nextTok != Token.RightBracket) break; //not a rank specifier, but a size specifier
     rank = this.ParseRankSpecifier(ctx, followers|Token.LeftBrace|Token.LeftBracket|Token.LeftParenthesis|Token.RightParenthesis);
     et = t;
     t = new ArrayTypeExpression(et, rank, ctx);
   }
   if (rank > 0) {
     //new T[] {...} or new T[,] {{..} {...}...}, etc where T can also be an array type
     List<Expression> initializers;
     if (this.currentToken == Token.LeftBrace)
       initializers = this.ParseArrayInitializers(rank, et, followers, false, ctx);
     else {
       initializers = new List<Expression>(0);
       if (Parser.UnaryStart[this.currentToken])
         this.HandleError(Error.ExpectedLeftBrace);
       else
         this.HandleError(Error.MissingArraySize);
       while (Parser.UnaryStart[this.currentToken]) {
         this.ParseExpression(followers|Token.Comma|Token.RightBrace);
         if (this.currentToken != Token.Comma) break;
         this.GetNextToken();
       }
       ctx.UpdateToSpan(this.scanner.SourceLocationOfLastScannedToken);
       this.SkipOverTo(Token.RightBrace, followers);
     }
     return new CreateArray(et, initializers.AsReadOnly(), new List<Expression>(0).AsReadOnly(), rank, new List<Expression>(0).AsReadOnly(), ctx);
   }
   if (this.currentToken == Token.LeftBracket) {
     //new T[x] or new T[x,y] etc. possibly followed by an initializer or element type rank specifier
     this.GetNextToken();
     List<Expression> sizes = this.ParseExpressionList(ctx, followers|Token.LeftBrace|Token.LeftBracket);
     rank = (uint)sizes.Count;
     List<Expression> initializers;
     if (this.currentToken == Token.LeftBrace)
       initializers = this.ParseArrayInitializers(rank, t, followers, false, ctx);
     else {
       uint elementRank = 0;
     tryAgain:
       while (this.currentToken == Token.LeftBracket) {
         Token nextTok = this.PeekNextToken();
         if (nextTok != Token.Comma && nextTok != Token.RightBracket) break; //not a rank specifier, but a size specifier
         elementRank = this.ParseRankSpecifier(ctx, followers|Token.LeftBrace|Token.LeftBracket|Token.LeftParenthesis|Token.RightParenthesis);
         t = new ArrayTypeExpression(t, elementRank, ctx);
       }
       if (this.currentToken == Token.LeftBrace)
         initializers = this.ParseArrayInitializers(rank, t, followers, false, ctx);
       else {
         if (this.currentToken == Token.LeftBracket) { //new T[x][y] or something like that
           this.GetNextToken();
           this.HandleError(Error.InvalidArray);
           elementRank = (uint)this.ParseExpressionList(ctx, followers).Count;
           goto tryAgain;
         } else {
           initializers = new List<Expression>(0);
           ctx.UpdateToSpan(this.scanner.SourceLocationOfLastScannedToken);
           this.SkipTo(followers);
         }
       }
     }
     return new CreateArray(t, initializers.AsReadOnly(), new List<Expression>(0).AsReadOnly(), rank, sizes.AsReadOnly(), ctx);
   }
   //new T(...)
   IEnumerable<Expression> arguments = Expression.EmptyCollection;
   IEnumerable<Expression> elementValues = Expression.EmptyCollection;
   IEnumerable<NamedArgument> namedArguments = NamedArgument.EmptyCollection;
   if (this.currentToken == Token.LeftParenthesis) {
     //if (t is NonNullableTypeExpression) {
     //  this.SkipTo(followers, Error.BadNewExpr);
     //  return null;
     //}
     arguments = this.ParseArgumentList(ctx, followers|Token.LeftBrace).AsReadOnly();
   } else if (this.currentToken != Token.LeftBrace) {
     this.SkipTo(followers, Error.BadNewExpr);
   }
   Expression result = new CreateObjectInstance(t, arguments, ctx.GetSourceLocation());
   if (this.currentToken == Token.LeftBrace) {
     this.ParseElementValuesOrNamedArguments(ref elementValues, ref namedArguments, ctx, followers);
     if (elementValues != Expression.EmptyCollection)
       return new PopulateCollection(result, elementValues, ctx);
     else if (namedArguments != NamedArgument.EmptyCollection)
       return new InitializeObject(result, namedArguments, ctx);
     else {
       this.HandleError(Error.SyntaxError); //TODO: better error
     }
   }
   return result;
 }