Exemplo n.º 1
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.º 2
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 (EmitContext 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){
					LocalVariableReference var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
					if (right_side != null) {
						return var.ResolveLValue (ec, right_side, loc);
					} else {
						ResolveFlags rf = ResolveFlags.VariableOrValue;
						if (intermediate)
							rf |= ResolveFlags.DisableFlowAnalysis;
						return var.Resolve (ec, rf);
					}
				}

				Expression expr = current_block.Toplevel.GetParameterReference (Name, loc);
				if (expr != null) {
					if (right_side != null)
						return expr.ResolveLValue (ec, right_side, loc);

					return expr.Resolve (ec);
				}
			}
			
			//
			// Stage 2: Lookup members 
			//

			Type almost_matched_type = null;
			ArrayList almost_matched = null;
			for (DeclSpace lookup_ds = ec.DeclContainer; lookup_ds != null; lookup_ds = lookup_ds.Parent) {
				// either RootDeclSpace or GenericMethod
				if (lookup_ds.TypeBuilder == null)
					continue;

				e = MemberLookup (ec.ContainerType, lookup_ds.TypeBuilder, Name, loc);
				if (e != null) {
					PropertyExpr pe = e as PropertyExpr;
					if (pe != null) {
						AParametersCollection param = TypeManager.GetParameterData (pe.PropertyInfo);

						// 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 (param.IsEmpty && pe.IsAccessibleFrom (ec.ContainerType, right_side != null))
							break;
					} else if (e is EventExpr) {
						if (((EventExpr) e).IsAccessibleFrom (ec.ContainerType))
							break;
					} else if (targs != null && 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.TypeBuilder;
					almost_matched = (ArrayList) almost_matched_members.Clone ();
				}
			}

			if (e == null) {
				if (almost_matched == null && almost_matched_members.Count > 0) {
					almost_matched_type = ec.ContainerType;
					almost_matched = (ArrayList) almost_matched_members.Clone ();
				}
				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 (Name);
						return null;
					}
				}

				if (RootContext.EvalMode){
					FieldInfo fi = Evaluator.LookupField (Name);
					if (fi != null)
						return new FieldExpr (fi, loc).Resolve (ec);
				}

				if (almost_matched != null)
					almost_matched_members = almost_matched;
				if (almost_matched_type == null)
					almost_matched_type = ec.ContainerType;
				return Error_MemberLookupFailed (ec.ContainerType, null, almost_matched_type, Name,
					ec.DeclContainer.Name, AllMemberTypes, AllBindingFlags);
			}

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

				Expression left;
				if (me.IsInstance) {
					if (ec.IsStatic || ec.IsInFieldInitializer) {
						//
						// 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.ContainerType, loc);
				}

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

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

					me.SetTypeArguments (targs);
				}

				if (!me.IsStatic && (me.InstanceExpression != 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))) {
					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.DoResolve (ec);
			}

			return e;
		}