예제 #1
0
        public ExpressionStatement CreateExpressionTreeVariable(BlockContext ec)
        {
            //
            // A parameter is not hoisted when used directly as ET
            //
            HoistedVariableReference = null;

            if ((modFlags & Modifier.ISBYREF) != 0)
            {
                ec.Report.Error(1951, Location, "An expression tree parameter cannot use `ref' or `out' modifier");
            }

            LocalInfo variable = ec.CurrentBlock.AddTemporaryVariable(
                ResolveParameterExpressionType(ec, Location), Location);

            variable.Resolve(ec);

            expr_tree_variable = new LocalVariableReference(
                ec.CurrentBlock, variable.Name, Location, variable, false);

            Arguments arguments = new Arguments(2);

            arguments.Add(new Argument(new TypeOf(
                                           new TypeExpression(parameter_type, Location), Location)));
            arguments.Add(new Argument(new StringConstant(Name, Location)));
            return(new SimpleAssign(ExpressionTreeVariableReference(),
                                    Expression.CreateExpressionFactoryCall(ec, "Parameter", null, arguments, Location)));
        }
        public LocalVariableDeclarationCompiler(ILocalVariableDeclaration localVariableDeclaration,
                                                AbstractILCompilerParams @params) : base(@params)
        {
            myLocalVariableDeclaration = localVariableDeclaration;
            var variableName = myLocalVariableDeclaration.DeclaredName;

            myLocalVariableReference = MyParams.LocalVariableIndexer.GetNextVariable(variableName);
        }
예제 #3
0
 /// <summary>
 /// Variable ctor
 /// </summary>
 public AstJavaVariable(XTypeReference type, LocalVariableReference varRef)
 {
     this.varRef = varRef;
     index       = varRef.Index;
     isParameter = varRef.IsParameter;
     isThis      = varRef.IsThis;
     Name        = (isParameter ? "_P" : "_V") + varRef;
     Type        = type;
 }
예제 #4
0
        /// <summary>
        /// Gets a local variable reference used in an instruction starting at the given offset, to a variable with given index in the stack frame.
        /// </summary>
        private LocalVariableReference GetLocalVariable(int instructionOffset, int index, string name, TypeReference type)
        {
            foreach (var entry in localVariableMap)
            {
                if (/*entry.Item1.IsValidForOffset(instructionOffset) &&*/ (entry.Item1.Index == index))
                {
                    return(entry.Item2);
                }
            }

            // Not found, create a new one
            var v      = new LocalVariable(0, code.Length, name, type, index);
            var varRef = new LocalVariableReference(method, index);

            localVariableMap.Add(Tuple.Create(v, varRef));
            return(varRef);
        }
예제 #5
0
        public void EmitSetException(EmitContext ec, LocalVariableReference exceptionVariable)
        {
            //
            // $builder.SetException (Exception)
            //
            var mg = MethodGroupExpr.CreatePredefined(set_exception, set_exception.DeclaringType, Location);

            mg.InstanceExpression = new FieldExpr(builder, Location)
            {
                InstanceExpression = new CompilerGeneratedThis(ec.CurrentType, Location)
            };

            Arguments args = new Arguments(1);

            args.Add(new Argument(exceptionVariable));

            mg.EmitCall(ec, args);
        }
예제 #6
0
 public virtual object Visit(LocalVariableReference localVariableReference)
 {
     return(null);
 }
			public override object Visit (LocalVariableReference localVariableReference)
			{
				return Identifier.Create (localVariableReference.Name, Convert (localVariableReference.Location));
			}
예제 #8
0
 public bool TryGetVariable(VariableName varname, out LocalVariableReference variable) => _dict.TryGetValue(varname, out variable);
예제 #9
0
		public virtual object Visit (LocalVariableReference localVariableReference)
		{
			return null;
		}
예제 #10
0
		protected override bool DoDefineMembers ()
		{
			if (!base.DoDefineMembers ())
				return false;

			Location loc = Location;

			var equals_parameters = ParametersCompiled.CreateFullyResolved (
				new Parameter (new TypeExpression (Compiler.BuiltinTypes.Object, loc), "obj", 0, null, loc), Compiler.BuiltinTypes.Object);

			Method equals = new Method (this, new TypeExpression (Compiler.BuiltinTypes.Bool, loc),
				Modifiers.PUBLIC | Modifiers.OVERRIDE | Modifiers.DEBUGGER_HIDDEN, new MemberName ("Equals", loc),
				equals_parameters, null);

			equals_parameters[0].Resolve (equals, 0);

			Method tostring = new Method (this, new TypeExpression (Compiler.BuiltinTypes.String, loc),
				Modifiers.PUBLIC | Modifiers.OVERRIDE | Modifiers.DEBUGGER_HIDDEN, new MemberName ("ToString", loc),
				ParametersCompiled.EmptyReadOnlyParameters, null);

			ToplevelBlock equals_block = new ToplevelBlock (Compiler, equals.ParameterInfo, loc);

			TypeExpr current_type;
			if (CurrentTypeParameters != null) {
				var targs = new TypeArguments ();
				for (int i = 0; i < CurrentTypeParameters.Count; ++i) {
					targs.Add (new TypeParameterExpr (CurrentTypeParameters[i], Location));
				}

				current_type = new GenericTypeExpr (Definition, targs, loc);
			} else {
				current_type = new TypeExpression (Definition, loc);
			}

			var li_other = LocalVariable.CreateCompilerGenerated (CurrentType, equals_block, loc);
			equals_block.AddStatement (new BlockVariable (new TypeExpression (li_other.Type, loc), li_other));
			var other_variable = new LocalVariableReference (li_other, loc);

			MemberAccess system_collections_generic = new MemberAccess (new MemberAccess (
				new QualifiedAliasMember ("global", "System", loc), "Collections", loc), "Generic", loc);

			Expression rs_equals = null;
			Expression string_concat = new StringConstant (Compiler.BuiltinTypes, "{", loc);
			Expression rs_hashcode = new IntConstant (Compiler.BuiltinTypes, -2128831035, loc);
			for (int i = 0; i < parameters.Count; ++i) {
				var p = parameters [i];
				var f = (Field) Members [i * 2];

				MemberAccess equality_comparer = new MemberAccess (new MemberAccess (
					system_collections_generic, "EqualityComparer",
						new TypeArguments (new SimpleName (CurrentTypeParameters [i].Name, loc)), loc),
						"Default", loc);

				Arguments arguments_equal = new Arguments (2);
				arguments_equal.Add (new Argument (new MemberAccess (new This (f.Location), f.Name)));
				arguments_equal.Add (new Argument (new MemberAccess (other_variable, f.Name)));

				Expression field_equal = new Invocation (new MemberAccess (equality_comparer,
					"Equals", loc), arguments_equal);

				Arguments arguments_hashcode = new Arguments (1);
				arguments_hashcode.Add (new Argument (new MemberAccess (new This (f.Location), f.Name)));
				Expression field_hashcode = new Invocation (new MemberAccess (equality_comparer,
					"GetHashCode", loc), arguments_hashcode);

				IntConstant FNV_prime = new IntConstant (Compiler.BuiltinTypes, 16777619, loc);				
				rs_hashcode = new Binary (Binary.Operator.Multiply,
					new Binary (Binary.Operator.ExclusiveOr, rs_hashcode, field_hashcode),
					FNV_prime);

				Expression field_to_string = new Conditional (new BooleanExpression (new Binary (Binary.Operator.Inequality,
					new MemberAccess (new This (f.Location), f.Name), new NullLiteral (loc))),
					new Invocation (new MemberAccess (
						new MemberAccess (new This (f.Location), f.Name), "ToString"), null),
					new StringConstant (Compiler.BuiltinTypes, string.Empty, loc), loc);

				if (rs_equals == null) {
					rs_equals = field_equal;
					string_concat = new Binary (Binary.Operator.Addition,
						string_concat,
						new Binary (Binary.Operator.Addition,
							new StringConstant (Compiler.BuiltinTypes, " " + p.Name + " = ", loc),
							field_to_string));
					continue;
				}

				//
				// Implementation of ToString () body using string concatenation
				//				
				string_concat = new Binary (Binary.Operator.Addition,
					new Binary (Binary.Operator.Addition,
						string_concat,
						new StringConstant (Compiler.BuiltinTypes, ", " + p.Name + " = ", loc)),
					field_to_string);

				rs_equals = new Binary (Binary.Operator.LogicalAnd, rs_equals, field_equal);
			}

			string_concat = new Binary (Binary.Operator.Addition,
				string_concat,
				new StringConstant (Compiler.BuiltinTypes, " }", loc));

			//
			// Equals (object obj) override
			//		
			var other_variable_assign = new TemporaryVariableReference (li_other, loc);
			equals_block.AddStatement (new StatementExpression (
				new SimpleAssign (other_variable_assign,
					new As (equals_block.GetParameterReference (0, loc),
						current_type, loc), loc)));

			Expression equals_test = new Binary (Binary.Operator.Inequality, other_variable, new NullLiteral (loc));
			if (rs_equals != null)
				equals_test = new Binary (Binary.Operator.LogicalAnd, equals_test, rs_equals);
			equals_block.AddStatement (new Return (equals_test, loc));

			equals.Block = equals_block;
			equals.Define ();
			Members.Add (equals);

			//
			// GetHashCode () override
			//
			Method hashcode = new Method (this, new TypeExpression (Compiler.BuiltinTypes.Int, loc),
				Modifiers.PUBLIC | Modifiers.OVERRIDE | Modifiers.DEBUGGER_HIDDEN,
				new MemberName ("GetHashCode", loc),
				ParametersCompiled.EmptyReadOnlyParameters, null);

			//
			// Modified FNV with good avalanche behavior and uniform
			// distribution with larger hash sizes.
			//
			// const int FNV_prime = 16777619;
			// int hash = (int) 2166136261;
			// foreach (int d in data)
			//     hash = (hash ^ d) * FNV_prime;
			// hash += hash << 13;
			// hash ^= hash >> 7;
			// hash += hash << 3;
			// hash ^= hash >> 17;
			// hash += hash << 5;

			ToplevelBlock hashcode_top = new ToplevelBlock (Compiler, loc);
			Block hashcode_block = new Block (hashcode_top, loc, loc);
			hashcode_top.AddStatement (new Unchecked (hashcode_block, loc));

			var li_hash = LocalVariable.CreateCompilerGenerated (Compiler.BuiltinTypes.Int, hashcode_top, loc);
			hashcode_block.AddStatement (new BlockVariable (new TypeExpression (li_hash.Type, loc), li_hash));
			LocalVariableReference hash_variable_assign = new LocalVariableReference (li_hash, loc);
			hashcode_block.AddStatement (new StatementExpression (
				new SimpleAssign (hash_variable_assign, rs_hashcode)));

			var hash_variable = new LocalVariableReference (li_hash, loc);
			hashcode_block.AddStatement (new StatementExpression (
				new CompoundAssign (Binary.Operator.Addition, hash_variable,
					new Binary (Binary.Operator.LeftShift, hash_variable, new IntConstant (Compiler.BuiltinTypes, 13, loc)))));
			hashcode_block.AddStatement (new StatementExpression (
				new CompoundAssign (Binary.Operator.ExclusiveOr, hash_variable,
					new Binary (Binary.Operator.RightShift, hash_variable, new IntConstant (Compiler.BuiltinTypes, 7, loc)))));
			hashcode_block.AddStatement (new StatementExpression (
				new CompoundAssign (Binary.Operator.Addition, hash_variable,
					new Binary (Binary.Operator.LeftShift, hash_variable, new IntConstant (Compiler.BuiltinTypes, 3, loc)))));
			hashcode_block.AddStatement (new StatementExpression (
				new CompoundAssign (Binary.Operator.ExclusiveOr, hash_variable,
					new Binary (Binary.Operator.RightShift, hash_variable, new IntConstant (Compiler.BuiltinTypes, 17, loc)))));
			hashcode_block.AddStatement (new StatementExpression (
				new CompoundAssign (Binary.Operator.Addition, hash_variable,
					new Binary (Binary.Operator.LeftShift, hash_variable, new IntConstant (Compiler.BuiltinTypes, 5, loc)))));

			hashcode_block.AddStatement (new Return (hash_variable, loc));
			hashcode.Block = hashcode_top;
			hashcode.Define ();
			Members.Add (hashcode);

			//
			// ToString () override
			//

			ToplevelBlock tostring_block = new ToplevelBlock (Compiler, loc);
			tostring_block.AddStatement (new Return (string_concat, loc));
			tostring.Block = tostring_block;
			tostring.Define ();
			Members.Add (tostring);

			return true;
		}
예제 #11
0
 /// <summary>
 /// Default ctor
 /// </summary>
 private ParameterWrapper(LocalVariableReference parameter)
 {
     this.parameter = parameter;
     name           = "P" + parameter.Index;
 }
예제 #12
0
        public override ICompilationResult GetResult()
        {
            // TODO: refactor
            var resultsWithoutTypeParameter = MyChildToResult.Where(kvp => !(kvp.Key is ITypeArgumentList))
                                              .Select(kvp => kvp.Value)
                                              .ToList();
            var instructionBlock0 = GetInstructionsConnectedSequentially(resultsWithoutTypeParameter);
            var reference0        = GetReferenceFromResults(resultsWithoutTypeParameter);
            var withOutGetter     = new ExpressionCompilationResult(instructionBlock0, null, reference0);
            //
            var resultWithGetter = withOutGetter.TryConvertToGetter(MyParams);
            var instructions     = resultWithGetter.GetInstructionBlock();
            var childReference   = resultWithGetter.GetReference();


            var declaredElement = myReferenceExpression.Reference.Resolve().DeclaredElement;

            if (declaredElement == null)
            {
                return(new ExpressionCompilationResult(instructions, GetLocation(myReferenceExpression)));
            }
            var referenceName = declaredElement.GetNameWithHash();

            switch (declaredElement)
            {
            case IField field:
                var defaultFieldType = field.Type.IsClassType() ? new ClassId(field.Type.ToString()) : null;
                myReference = new ClassFieldReference(GetReferenceToOwner(childReference), referenceName, defaultFieldType);
                break;

            case ILocalFunctionDeclaration _:
                if (childReference != null)
                {
                    throw MyParams.CreateException($"local function invocation({referenceName}) has owner({childReference})");
                }
                myReference = new ClassMethodReference(new ClassReference(new ClassId(MyParams.GetOwnerForLocalFunction())), referenceName);
                break;

            case IMethod _:
                myReference = new ClassMethodReference(GetReferenceToOwner(childReference), referenceName);
                break;

            case IProperty _:
                myReference = new ClassPropertyReference(GetReferenceToOwner(childReference), referenceName);
                break;

            case IParameter param:
                var owner           = param.ContainingParametersOwner ?? throw MyParams.CreateException("method parameter's owner is null");
                var parameterNumber = owner.Parameters.IndexOf(param);

                var defaultType = param.Type.IsClassType() ? new ClassId(param.Type.ToString()) : null;
                myReference = new LocalVariableReference(parameterNumber, defaultType);

                break;

            case IClass @class:
                myReference = @class.GetClassReference();
                break;

            case IStruct @struct:
                myReference = @struct.GetClassReference();
                break;

            case IEnum @enum:
                myReference = @enum.GetClassReference();
                break;

            case IExternAlias _:
            case INamespace _:
                myReference = null;
                break;

            case IInterface @interface:
                myReference = @interface.GetClassReference();
                break;

            case IEvent @event:
                myReference = new ClassFieldReference(GetReferenceToOwner(childReference), referenceName);
                break;

            case IAnonymousTypeProperty _:
                //todo
                break;

            case ITypeParameter _:
                //todo
                break;

//                case ISingleVariableDesignation _:
//                case ILocalConstantDeclaration _:
//                case ILocalVariableDeclaration _:
//                case ICatchVariableDeclaration _:
            case IVariableDeclaration variableDeclaration:
                if (childReference != null)
                {
                    throw MyParams.CreateException("");
                }
                myReference = MyParams.LocalVariableIndexer.GetVariableIndex(referenceName);
                ((LocalVariableReference)myReference).DefaultType =
                    variableDeclaration.Type.IsClassType() ? new ClassId(variableDeclaration.Type.ToString()) : null;
                break;

            case IDelegate @delegate:
                myReference = new ClassMethodReference(GetReferenceToOwner(childReference), referenceName);
                break;

            default:
            {
                throw MyParams.CreateException($"unsupported reference type {declaredElement.GetType()}");
                // reference to method or field
//                    var nameOfIdentifier = myReferenceExpression.NameIdentifier.Name;
//
//                    if (myReferenceExpression.FirstChild != null)
//                    {
//                        if (MyChildToResult[myReferenceExpression.FirstChild] is IExpressionCompilationResult varOwner)
//                        {
//                            myReference = new ClassMemberReference(varOwner.GetReference(), nameOfIdentifier);
//                        }
//                        else
//                        {
//                            throw MyParams.GetException("Owner of class member is not an expression");
//                        }
//                    }
//                    else
//                    {
//                        throw MyParams.GetException("Class member has no no owner");
//                    }
//
//                    break;
            }
            }

            if (myReference is LocalVariableReference localVariable)
            {
            }

            return(new ExpressionCompilationResult(instructions, GetLocation(myReferenceExpression), myReference));
        }
예제 #13
0
 public bool Equals(LocalVariableReference other)
 {
     return (other != null) && (other.method == method) && (other.index == index);
 }
예제 #14
0
 public bool Equals(LocalVariableReference other)
 {
     return((other != null) && (other.method == method) && (other.index == index));
 }
예제 #15
0
 /// <summary>
 /// Variable ctor
 /// </summary>
 public AstJavaVariable(XTypeReference type, LocalVariableReference varRef, string name)
     : this(type, varRef)
 {
     Name = name;
 }
예제 #16
0
 public LocalVariableTarget(LocalVariableReference localVariableReference)
 {
     Index = localVariableReference.Index;
 }