Exemplo n.º 1
0
        void DoResolveInstanceExpression(ResolveContext ec)
        {
            //
            // Argument is another delegate
            //
            if (delegate_instance_expression != null)
            {
                return;
            }

            if (method_group.IsStatic)
            {
                delegate_instance_expression = null;
                return;
            }

            Expression instance = method_group.InstanceExpression;

            if (instance != null && instance != EmptyExpression.Null)
            {
                delegate_instance_expression = instance;
                Type instance_type = delegate_instance_expression.Type;
                if (TypeManager.IsValueType(instance_type) || TypeManager.IsGenericParameter(instance_type))
                {
                    delegate_instance_expression = new BoxedCast(
                        delegate_instance_expression, TypeManager.object_type);
                }
            }
            else
            {
                delegate_instance_expression = ec.GetThis(loc);
            }
        }
Exemplo n.º 2
0
        protected override Expression DoResolve(ResolveContext ec)
        {
            eclass = ExprClass.Value;

            // FIXME: Hack
            var caller_builder = (Constructor) ec.MemberContext;

            if (argument_list != null) {
                bool dynamic;

                //
                // Spec mandates that constructor initializer will not have `this' access
                //
                using (ec.Set (ResolveContext.Options.BaseInitializer)) {
                    argument_list.Resolve (ec, out dynamic);
                }

                if (dynamic) {
                    ec.Report.Error (1975, loc,
                        "The constructor call cannot be dynamically dispatched within constructor initializer");

                    return null;
                }
            }

            type = ec.CurrentType;
            if (this is ConstructorBaseInitializer) {
                if (ec.CurrentType.BaseType == null)
                    return this;

                type = ec.CurrentType.BaseType;
                if (ec.CurrentType.IsStruct) {
                    ec.Report.Error (522, loc,
                        "`{0}': Struct constructors cannot call base constructors", caller_builder.GetSignatureForError ());
                    return this;
                }
            } else {
                //
                // It is legal to have "this" initializers that take no arguments
                // in structs, they are just no-ops.
                //
                // struct D { public D (int a) : this () {}
                //
                if (TypeManager.IsStruct (ec.CurrentType) && argument_list == null)
                    return this;
            }

            base_constructor_group = MemberLookupFinal (
                ec, null, type, ConstructorBuilder.ConstructorName, 0, MemberKind.Constructor,
                BindingRestriction.AccessibleOnly | BindingRestriction.DeclaredOnly,
                loc) as MethodGroupExpr;

            if (base_constructor_group == null)
                return this;

            base_constructor_group = base_constructor_group.OverloadResolve (
                ec, ref argument_list, false, loc);

            if (base_constructor_group == null)
                return this;

            if (!ec.IsStatic)
                base_constructor_group.InstanceExpression = ec.GetThis (loc);

            var base_ctor = base_constructor_group.BestCandidate;

            // TODO MemberCache: Does it work for inflated types ?
            if (base_ctor == caller_builder.Spec){
                ec.Report.Error (516, loc, "Constructor `{0}' cannot call itself",
                    caller_builder.GetSignatureForError ());
            }

            return this;
        }
Exemplo n.º 3
0
        /// <remarks>
        ///   7.5.2: Simple Names. 
        ///
        ///   Local Variables and Parameters are handled at
        ///   parse time, so they never occur as SimpleNames.
        ///
        ///   The `intermediate' flag is used by MemberAccess only
        ///   and it is used to inform us that it is ok for us to 
        ///   avoid the static check, because MemberAccess might end
        ///   up resolving the Name as a Type name and the access as
        ///   a static type access.
        ///
        ///   ie: Type Type; .... { Type.GetType (""); }
        ///
        ///   Type is both an instance variable and a Type;  Type.GetType
        ///   is the static method not an instance method of type.
        /// </remarks>
        Expression DoSimpleNameResolve(ResolveContext ec, Expression right_side, bool intermediate)
        {
            Expression e = null;

            //
            // Stage 1: Performed by the parser (binding to locals or parameters).
            //
            Block current_block = ec.CurrentBlock;
            if (current_block != null){
                LocalInfo vi = current_block.GetLocalInfo (Name);
                if (vi != null){
                    e = new LocalVariableReference (ec.CurrentBlock, Name, loc);

                    if (right_side != null) {
                        e = e.ResolveLValue (ec, right_side);
                    } else {
                        if (intermediate) {
                            using (ec.With (ResolveContext.Options.DoFlowAnalysis, false)) {
                                e = e.Resolve (ec, ResolveFlags.VariableOrValue);
                            }
                        } else {
                            e = e.Resolve (ec, ResolveFlags.VariableOrValue);
                        }
                    }

                    if (HasTypeArguments && e != null)
                        e.Error_TypeArgumentsCannotBeUsed (ec.Report, loc, null, 0);

                    return e;
                }

                e = current_block.Toplevel.GetParameterReference (Name, loc);
                if (e != null) {
                    if (right_side != null)
                        e = e.ResolveLValue (ec, right_side);
                    else
                        e = e.Resolve (ec);

                    if (HasTypeArguments && e != null)
                        e.Error_TypeArgumentsCannotBeUsed (ec.Report, loc, null, 0);

                    return e;
                }
            }

            //
            // Stage 2: Lookup members
            //
            int arity = HasTypeArguments ? Arity : -1;
            //			TypeSpec almost_matched_type = null;
            //			IList<MemberSpec> almost_matched = null;
            for (TypeSpec lookup_ds = ec.CurrentType; lookup_ds != null; lookup_ds = lookup_ds.DeclaringType) {
                e = MemberLookup (ec.Compiler, ec.CurrentType, lookup_ds, Name, arity, BindingRestriction.NoOverrides, loc);
                if (e != null) {
                    PropertyExpr pe = e as PropertyExpr;
                    if (pe != null) {
                        // since TypeManager.MemberLookup doesn't know if we're doing a lvalue access or not,
                        // it doesn't know which accessor to check permissions against
                        if (pe.PropertyInfo.Kind == MemberKind.Property && pe.IsAccessibleFrom (ec.CurrentType, right_side != null))
                            break;
                    } else if (e is EventExpr) {
                        if (((EventExpr) e).IsAccessibleFrom (ec.CurrentType))
                            break;
                    } else if (HasTypeArguments && e is TypeExpression) {
                        e = new GenericTypeExpr (e.Type, targs, loc).ResolveAsTypeStep (ec, false);
                        break;
                    } else {
                        break;
                    }
                    e = null;
                }
            /*
                if (almost_matched == null && almost_matched_members.Count > 0) {
                    almost_matched_type = lookup_ds;
                    almost_matched = new List<MemberSpec>(almost_matched_members);
                }
            */
            }

            if (e == null) {
            /*
                if (almost_matched == null && almost_matched_members.Count > 0) {
                    almost_matched_type = ec.CurrentType;
                    almost_matched = new List<MemberSpec> (almost_matched_members);
                }
            */
                e = ResolveAsTypeStep (ec, true);
            }

            if (e == null) {
                if (current_block != null) {
                    IKnownVariable ikv = current_block.Explicit.GetKnownVariable (Name);
                    if (ikv != null) {
                        LocalInfo li = ikv as LocalInfo;
                        // Supress CS0219 warning
                        if (li != null)
                            li.Used = true;

                        Error_VariableIsUsedBeforeItIsDeclared (ec.Report, Name);
                        return null;
                    }
                }

                if (RootContext.EvalMode){
                    FieldInfo fi = Evaluator.LookupField (Name);
                    if (fi != null)
                        return new FieldExpr (Import.CreateField (fi, null), loc).Resolve (ec);
                }
            /*
                if (almost_matched != null)
                    almost_matched_members = almost_matched;
                if (almost_matched_type == null)
                    almost_matched_type = ec.CurrentType;
            */
                string type_name = ec.MemberContext.CurrentType == null ? null : ec.MemberContext.CurrentType.Name;
                return Error_MemberLookupFailed (ec, ec.CurrentType, null, ec.CurrentType, Name, arity,
                    type_name, MemberKind.All, BindingRestriction.AccessibleOnly);
            }

            if (e is MemberExpr) {
                MemberExpr me = (MemberExpr) e;

                Expression left;
                if (me.IsInstance) {
                    if (ec.IsStatic || ec.HasAny (ResolveContext.Options.FieldInitializerScope | ResolveContext.Options.BaseInitializer | ResolveContext.Options.ConstantScope)) {
                        //
                        // Note that an MemberExpr can be both IsInstance and IsStatic.
                        // An unresolved MethodGroupExpr can contain both kinds of methods
                        // and each predicate is true if the MethodGroupExpr contains
                        // at least one of that kind of method.
                        //
            /*
                        if (!me.IsStatic &&
                            (!intermediate || !IdenticalNameAndTypeName (ec, me, loc))) {
                            Error_ObjectRefRequired (ec, loc, me.GetSignatureForError ());
                            return null;
                        }
            */
                        //
                        // Pass the buck to MemberAccess and Invocation.
                        //
                        left = EmptyExpression.Null;
                    } else {
                        left = ec.GetThis (loc);
                    }
                } else {
                    left = new TypeExpression (ec.CurrentType, loc);
                }

                me = me.ResolveMemberAccess (ec, left, loc, null);
                if (me == null)
                    return null;

                if (HasTypeArguments) {
                    if (!targs.Resolve (ec))
                        return null;

                    me.SetTypeArguments (ec, targs);
                }

                if (!me.IsStatic && (me.InstanceExpression != null && me.InstanceExpression != EmptyExpression.Null) &&
                    TypeManager.IsNestedFamilyAccessible (me.InstanceExpression.Type, me.DeclaringType) &&
                    me.InstanceExpression.Type != me.DeclaringType &&
                    !TypeManager.IsFamilyAccessible (me.InstanceExpression.Type, me.DeclaringType) &&
                    (!intermediate || !IdenticalNameAndTypeName (ec, e, loc))) {
                    ec.Report.Error (38, loc, "Cannot access a nonstatic member of outer type `{0}' via nested type `{1}'",
                        TypeManager.CSharpName (me.DeclaringType), TypeManager.CSharpName (me.InstanceExpression.Type));
                    return null;
                }

                return (right_side != null)
                    ? me.DoResolveLValue (ec, right_side)
                    : me.Resolve (ec);
            }

            return e;
        }
Exemplo n.º 4
0
		protected override void CommonResolve (ResolveContext ec)
		{
			instance_expr = ec.GetThis (loc);

			current_type = ec.CurrentType.BaseType;
			indexer_type = current_type;
		}
Exemplo n.º 5
0
		Expression CommonResolve (ResolveContext ec)
		{
			Expression member_lookup;
			Type current_type = ec.CurrentType;
			Type base_type = current_type.BaseType;

			if (!This.IsThisAvailable (ec)) {
				if (ec.IsStatic) {
					ec.Report.Error (1511, loc, "Keyword `base' is not available in a static method");
				} else {
					ec.Report.Error (1512, loc, "Keyword `base' is not available in the current context");
				}
				return null;
			}
			
			member_lookup = MemberLookup (ec.Compiler, ec.CurrentType, null, base_type, Identifier,
						      AllMemberTypes, AllBindingFlags, loc);
			if (member_lookup == null) {
				Error_MemberLookupFailed (ec, ec.CurrentType, base_type, base_type, Identifier,
					null, AllMemberTypes, AllBindingFlags);
				return null;
			}

			Expression left;
			
			if (ec.IsStatic)
				left = new TypeExpression (base_type, loc);
			else
				left = ec.GetThis (loc);

			MemberExpr me = member_lookup as MemberExpr;
			if (me == null){
				if (member_lookup is TypeExpression){
					ec.Report.Error (582, loc, "{0}: Can not reference a type through an expression, try `{1}' instead",
							 Identifier, member_lookup.GetSignatureForError ());
				} else {
					ec.Report.Error (582, loc, "{0}: Can not reference a {1} through an expression", 
							 Identifier, member_lookup.ExprClassName);
				}
				
				return null;
			}
			
			me = me.ResolveMemberAccess (ec, left, loc, null);
			if (me == null)
				return null;

			me.IsBase = true;
			if (args != null) {
				args.Resolve (ec);
				me.SetTypeArguments (ec, args);
			}

			return me;
		}
Exemplo n.º 6
0
		void DoResolveInstanceExpression (ResolveContext ec)
		{
			//
			// Argument is another delegate
			//
			if (delegate_instance_expression != null)
				return;

			if (method_group.IsStatic) {
				delegate_instance_expression = null;
				return;
			}

			Expression instance = method_group.InstanceExpression;
			if (instance != null && instance != EmptyExpression.Null) {
				delegate_instance_expression = instance;
				Type instance_type = delegate_instance_expression.Type;
				if (TypeManager.IsValueType (instance_type) || TypeManager.IsGenericParameter (instance_type)) {
					delegate_instance_expression = new BoxedCast (
						delegate_instance_expression, TypeManager.object_type);
				}
			} else {
				delegate_instance_expression = ec.GetThis (loc);
			}
		}
Exemplo n.º 7
0
		public override Expression DoResolve (ResolveContext ec)
		{
			eclass = ExprClass.Value;

			// TODO: ec.GetSignatureForError ()
			ConstructorBuilder caller_builder = ((Constructor) ec.MemberContext).ConstructorBuilder;

			if (argument_list != null) {
				bool dynamic;

				//
				// Spec mandates that constructor initializer will not have `this' access
				//
				using (ec.Set (ResolveContext.Options.BaseInitializer)) {
					argument_list.Resolve (ec, out dynamic);
				}

				if (dynamic) {
					SimpleName ctor = new SimpleName (ConstructorBuilder.ConstructorName, loc);
					return new DynamicInvocation (ctor, argument_list, loc).Resolve (ec) as ExpressionStatement;
				}
			}

			type = ec.CurrentType;
			if (this is ConstructorBaseInitializer) {
				if (ec.CurrentType.BaseType == null)
					return this;

				type = ec.CurrentType.BaseType;
				if (TypeManager.IsStruct (ec.CurrentType)) {
					ec.Report.Error (522, loc,
						"`{0}': Struct constructors cannot call base constructors", TypeManager.CSharpSignature (caller_builder));
					return this;
				}
			} else {
				//
				// It is legal to have "this" initializers that take no arguments
				// in structs, they are just no-ops.
				//
				// struct D { public D (int a) : this () {}
				//
				if (TypeManager.IsStruct (ec.CurrentType) && argument_list == null)
					return this;			
			}

			base_constructor_group = MemberLookupFinal (
				ec, null, type, ConstructorBuilder.ConstructorName, MemberTypes.Constructor,
				BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
				loc) as MethodGroupExpr;
			
			if (base_constructor_group == null)
				return this;
			
			base_constructor_group = base_constructor_group.OverloadResolve (
				ec, ref argument_list, false, loc);
			
			if (base_constructor_group == null)
				return this;

			if (!ec.IsStatic)
				base_constructor_group.InstanceExpression = ec.GetThis (loc);
			
			ConstructorInfo base_ctor = (ConstructorInfo)base_constructor_group;

			if (base_ctor == caller_builder){
				ec.Report.Error (516, loc, "Constructor `{0}' cannot call itself", TypeManager.CSharpSignature (caller_builder));
			}
						
			return this;
		}