add a LocalDeclarationStatement

Dec 16, 2010 at 6:03 AM

Hi, I use the following code to insert a new localDeclarationStatment, but It seems not work, could you give me some suggestion?

        public override IBlockStatement Visit(BlockStatement blockStatement)      

               {            

                  List<IStatement> statements = blockStatement.Statements;          

                  List<IStatement> newList = new List<IStatement>();

                  MethodDefinition curMethod = this.GetCurrentMethod() as MethodDefinition;          

                  if(!curMethod.Name.Value.Equals(".cctor")&&!curMethod.Name.Value.Equals(".ctor"))          

                           {                    

                                LocalDeclarationStatement localDeclare = new LocalDeclarationStatement();

                                LocalDefinition localDef = new LocalDefinition();                  

                                localDef.Name = this.host.NameTable.GetNameFor("test");                    

                               localDef.Type = this.host.PlatformType.SystemString;                  

                               localDeclare.LocalVariable = localDef;

                              CompileTimeConstant initialValue = new CompileTimeConstant();                    

                              initialValue.Value = null;                    

                              initialValue.Type = this.host.PlatformType.SystemString;

                               localDeclare.InitialValue = initialValue;
                               newList.Add(localDeclare);
            }            

                   newList.AddRange(statements);            

                   blockStatement.Statements = newList;            

                   blockStatement.Statements = Visit(blockStatement.Statements);            

                  return blockStatement;       

    }

Coordinator
Dec 17, 2010 at 4:09 PM

Can you provide more details about what exactly it is that doesn't work?

Dec 17, 2010 at 4:39 PM

It won't generate a new Dll file which include the added local variable. Actually, now, What I want to do is insert a new statement "Console.WriteLine(this.pre)" in the original file, "pre" is a field definition. I know how to insert"Console.Write(pre)", but I don't know how to code "this.pre".

Coordinator
Dec 17, 2010 at 4:48 PM

If my memory serves me right, you want to add an expression statement that contains a method call expression where the argument to the method call is a bound expression where the definition is an IFieldReference to "pre" and the instance is an instance of ThisReference.

In general you can figure out these things by coding up the desired construct in a (very small) C# program, compiling that and then running peToText on it. If you set some breakpoints in the final visitor that prints out the decompiled object model, you'll be able to visualize the object model that you need to construct.

Dec 17, 2010 at 5:26 PM

Thanks for your quick responding. Since I can get FieldDefinition for "pre",  can you implement the following code for me?

MethodCall fieldValue = new MethodCall();                        

Microsoft.Cci.MethodReference fieldValueRef = new Microsoft.Cci.MethodReference(this.host, (this.host as HostEnvironment).MyPlatformType.SystemIOWriter, CallingConvention.HasThis, this.host.PlatformType.SystemVoid, this.host.NameTable.GetNameFor("WriteLine"), 0, this.host.PlatformType.SystemString);                        

fieldValue.ThisArgument = new BoundExpression() { Definition = this.GetCurrentType().Fields.ElementAt(fieldCount - 1) };                        

fieldValue.IsStaticCall = false;                      

fieldValue.Arguments.Add(new BoundExpression() { Definition = ? });                                                    

fieldValue.MethodToCall = fieldValueRef;                      

fieldValue.Type = this.host.PlatformType.SystemVoid;                        

ExpressionStatement fieldValueSta = new ExpressionStatement() { Expression = fieldValue };                        

fieldStateBlock.Statements.Add(fieldValueSta);

Coordinator
Dec 17, 2010 at 6:05 PM

The HelloCodeModel sample should be helpful here.

Dec 17, 2010 at 6:13 PM

Thanks very much, I get it done correctly under your guidance.