{
public Expression Expr;
- public ParenthesizedExpression (Expression expr, Location loc)
+ public ParenthesizedExpression (Expression expr)
{
this.Expr = expr;
- this.loc = loc;
+ this.loc = expr.Location;
}
public override Expression DoResolve (EmitContext ec)
Expression e;
e = Convert.ImplicitConversion (ec, Expr, TypeManager.int32_type, loc);
- if (e != null){
- type = TypeManager.int32_type;
- return this;
- }
+ if (e != null)
+ goto ok;
e = Convert.ImplicitConversion (ec, Expr, TypeManager.uint32_type, loc);
- if (e != null){
- type = TypeManager.uint32_type;
- return this;
- }
+ if (e != null)
+ goto ok;
e = Convert.ImplicitConversion (ec, Expr, TypeManager.int64_type, loc);
- if (e != null){
- type = TypeManager.int64_type;
- return this;
- }
+ if (e != null)
+ goto ok;
e = Convert.ImplicitConversion (ec, Expr, TypeManager.uint64_type, loc);
- if (e != null){
- type = TypeManager.uint64_type;
- return this;
- }
+ if (e != null)
+ goto ok;
Error23 (expr_type);
return null;
+ ok:
+ Expr = e;
+ expr_type = e.Type;
}
+
type = expr_type;
return this;
eclass = ExprClass.Value;
Type etype = expr.Type;
- if (TypeManager.IsValueType (probe_type)){
+ if (probe_type.IsValueType) {
Report.Error (77, loc, "The as operator must be used with a reference type (`" +
TypeManager.CSharpName (probe_type) + "' is a value type)");
return null;
Expression target_type;
Expression expr;
+ public Cast (Expression cast_type, Expression expr)
+ : this (cast_type, expr, cast_type.Location)
+ {
+ }
+
public Cast (Expression cast_type, Expression expr, Location loc)
{
this.target_type = cast_type;
return true;
}
+ // TODO: move to constant
/// <summary>
/// Attempts to do a compile-time folding of a constant cast.
/// </summary>
Expression TryReduce (EmitContext ec, Type target_type)
{
+ if (expr.Type == target_type)
+ return expr;
+
+ if (TypeManager.IsEnumType (target_type) && TypeManager.EnumToUnderlying (target_type) == expr.Type)
+ return new EnumConstant ((Constant)expr, target_type);
+
Expression real_expr = expr;
if (real_expr is EnumConstant)
real_expr = ((EnumConstant) real_expr).Child;
oper_names [(int) Operator.LogicalAnd] = "op_LogicalAnd";
}
- public Binary (Operator oper, Expression left, Expression right, Location loc)
+ public Binary (Operator oper, Expression left, Expression right)
{
this.oper = oper;
this.left = left;
this.right = right;
- this.loc = loc;
+ this.loc = left.Location;
}
public Operator Oper {
// type, otherwise ConvertImplict() already finds the user-defined conversion for us,
// so we don't explicitly check for performance reasons.
//
- bool DoNumericPromotions (EmitContext ec, Type l, Type r, bool check_user_conv)
+ bool DoNumericPromotions (EmitContext ec, Type l, Type r, Expression lexpr, Expression rexpr, bool check_user_conv)
{
if (IsOfType (ec, l, r, TypeManager.double_type, check_user_conv)){
//
left = ForceConversion (ec, left, TypeManager.int32_type);
right = ForceConversion (ec, right, TypeManager.int32_type);
+ bool strConv =
+ Convert.ImplicitConversionExists (ec, lexpr, TypeManager.string_type) &&
+ Convert.ImplicitConversionExists (ec, rexpr, TypeManager.string_type);
+ if (strConv && left != null && right != null)
+ Error_OperatorAmbiguous (loc, oper, l, r);
+
type = TypeManager.int32_type;
}
type = e.Type;
if (type == TypeManager.int32_type || type == TypeManager.uint32_type){
- right = new Binary (Binary.Operator.BitwiseAnd, right, new IntLiteral (31), loc);
+ right = new Binary (Binary.Operator.BitwiseAnd, right, new IntLiteral (31));
right = right.DoResolve (ec);
} else {
- right = new Binary (Binary.Operator.BitwiseAnd, right, new IntLiteral (63), loc);
+ right = new Binary (Binary.Operator.BitwiseAnd, right, new IntLiteral (63));
right = right.DoResolve (ec);
}
return null;
}
- left = new BoxedCast (left);
+ left = new BoxedCast (left, TypeManager.object_type);
Type = TypeManager.bool_type;
return this;
}
return null;
}
- right = new BoxedCast (right);
+ right = new BoxedCast (right, TypeManager.object_type);
Type = TypeManager.bool_type;
return this;
}
// This will leave left or right set to null if there is an error
//
bool check_user_conv = is_user_defined (l) && is_user_defined (r);
- DoNumericPromotions (ec, l, r, check_user_conv);
+ DoNumericPromotions (ec, l, r, left, right, check_user_conv);
if (left == null || right == null){
Error_OperatorCannotBeApplied (loc, OperName (oper), l, r);
return null;
}
if (rc != null && lc != null){
+ int prev_e = Report.Errors;
Expression e = ConstantFold.BinaryFold (
ec, oper, lc, rc, loc);
- if (e != null)
+ if (e != null || Report.Errors != prev_e)
return e;
}
if (TypeManager.IsNullableType (left.Type) || TypeManager.IsNullableType (right.Type))
return new Nullable.LiftedBinaryOperator (oper, left, right, loc).Resolve (ec);
+ // Check CS0652 warning here (before resolving operator).
+ if (oper == Operator.Equality ||
+ oper == Operator.Inequality ||
+ oper == Operator.LessThanOrEqual ||
+ oper == Operator.LessThan ||
+ oper == Operator.GreaterThanOrEqual ||
+ oper == Operator.GreaterThan){
+ CheckUselessComparison (left as Constant, right.Type);
+ CheckUselessComparison (right as Constant, left.Type);
+ }
+
return ResolveOperator (ec);
}
+ private void CheckUselessComparison (Constant c, Type type)
+ {
+ if (c == null || !IsTypeIntegral (type)
+ || c is StringConstant
+ || c is BoolConstant
+ || c is CharConstant
+ || c is FloatConstant
+ || c is DoubleConstant
+ || c is DecimalConstant
+ )
+ return;
+
+ long value = 0;
+
+ if (c is ULongConstant) {
+ ulong uvalue = ((ULongConstant) c).Value;
+ if (uvalue > long.MaxValue) {
+ if (type == TypeManager.byte_type ||
+ type == TypeManager.sbyte_type ||
+ type == TypeManager.short_type ||
+ type == TypeManager.ushort_type ||
+ type == TypeManager.int32_type ||
+ type == TypeManager.uint32_type ||
+ type == TypeManager.int64_type)
+ WarnUselessComparison (type);
+ return;
+ }
+ value = (long) uvalue;
+ }
+ else if (c is ByteConstant)
+ value = ((ByteConstant) c).Value;
+ else if (c is SByteConstant)
+ value = ((SByteConstant) c).Value;
+ else if (c is ShortConstant)
+ value = ((ShortConstant) c).Value;
+ else if (c is UShortConstant)
+ value = ((UShortConstant) c).Value;
+ else if (c is IntConstant)
+ value = ((IntConstant) c).Value;
+ else if (c is UIntConstant)
+ value = ((UIntConstant) c).Value;
+ else if (c is LongConstant)
+ value = ((LongConstant) c).Value;
+
+ if (value != 0) {
+ if (IsValueOutOfRange (value, type))
+ WarnUselessComparison (type);
+ return;
+ }
+ }
+
+ private bool IsValueOutOfRange (long value, Type type)
+ {
+ if (IsTypeUnsigned (type) && value < 0)
+ return true;
+ return type == TypeManager.sbyte_type && (value >= 0x80 || value < -0x80) ||
+ type == TypeManager.byte_type && value >= 0x100 ||
+ type == TypeManager.short_type && (value >= 0x8000 || value < -0x8000) ||
+ type == TypeManager.ushort_type && value >= 0x10000 ||
+ type == TypeManager.int32_type && (value >= 0x80000000 || value < -0x80000000) ||
+ type == TypeManager.uint32_type && value >= 0x100000000;
+ }
+
+ private static bool IsTypeIntegral (Type type)
+ {
+ return type == TypeManager.uint64_type ||
+ type == TypeManager.int64_type ||
+ type == TypeManager.uint32_type ||
+ type == TypeManager.int32_type ||
+ type == TypeManager.ushort_type ||
+ type == TypeManager.short_type ||
+ type == TypeManager.sbyte_type ||
+ type == TypeManager.byte_type;
+ }
+
+ private static bool IsTypeUnsigned (Type type)
+ {
+ return type == TypeManager.uint64_type ||
+ type == TypeManager.uint32_type ||
+ type == TypeManager.ushort_type ||
+ type == TypeManager.byte_type;
+ }
+
+ private void WarnUselessComparison (Type type)
+ {
+ Report.Warning (652, 2, loc, "Comparison to integral constant is useless; the constant is outside the range of type `{0}'",
+ TypeManager.CSharpName (type));
+ }
+
/// <remarks>
/// EmitBranchable is called from Statement.EmitBoolExpression in the
/// context of a conditional bool expression. This function will return
public class Conditional : Expression {
Expression expr, trueExpr, falseExpr;
- public Conditional (Expression expr, Expression trueExpr, Expression falseExpr, Location l)
+ public Conditional (Expression expr, Expression trueExpr, Expression falseExpr)
{
this.expr = expr;
this.trueExpr = trueExpr;
this.falseExpr = falseExpr;
- this.loc = l;
+ this.loc = expr.Location;
}
public Expression Expr {
return null;
}
+ Assign ass = expr as Assign;
+ if (ass != null && ass.Source is Constant) {
+ Report.Warning (665, 3, loc, "Assignment in conditional expression is always constant; did you mean to use == instead of = ?");
+ }
+
trueExpr = trueExpr.Resolve (ec);
falseExpr = falseExpr.Resolve (ec);
}
}
+ public bool VerifyAssigned (EmitContext ec)
+ {
+ VariableInfo variable_info = local_info.VariableInfo;
+ return variable_info == null || variable_info.IsAssigned (ec, loc);
+ }
+
protected Expression DoResolveBase (EmitContext ec, Expression lvalue_right_side)
{
if (local_info == null) {
return e.Resolve (ec);
}
- if ((variable_info != null) && !variable_info.IsAssigned (ec, loc))
+ if (!VerifyAssigned (ec))
return null;
if (lvalue_right_side == null)
{
DoResolveBase (ec);
- if (is_out && ec.DoFlowAnalysis && !IsAssigned (ec, loc))
+ if (is_out && ec.DoFlowAnalysis && (!ec.OmitStructFlowAnalysis || !vi.TypeInfo.IsStruct) && !IsAssigned (ec, loc))
return null;
return this;
// FIXME: only allow expr to be a method invocation or a
// delegate invocation (7.5.5)
//
- public Invocation (Expression expr, ArrayList arguments, Location l)
+ public Invocation (Expression expr, ArrayList arguments)
{
this.expr = expr;
Arguments = arguments;
- loc = l;
+ loc = expr.Location;
}
public Expression Expr {
}
/// <summary>
- /// Determines "better conversion" as specified in 7.4.2.3
+ /// Determines "better conversion" as specified in 14.4.2.3
///
/// Returns : p if a->p is better,
/// q if a->q is better,
if (a_mod == p_mod ||
(a_mod == Parameter.Modifier.NONE && p_mod == Parameter.Modifier.PARAMS)) {
if (a_mod == Parameter.Modifier.NONE) {
- if (!Convert.ImplicitConversionExists (ec,
+ if (!TypeManager.IsEqual (a.Type, pd.ParameterType (i)) && !Convert.ImplicitConversionExists (ec,
a.Expr,
pd.ParameterType (i)))
return false;
VerifyArgumentsCompat (ec, Arguments, arg_count,
c, false, null, may_fail, loc);
- break;
+
+ if (!may_fail && errors == Report.Errors)
+ throw new InternalErrorException (
+ "VerifyArgumentsCompat and IsApplicable do not agree; " +
+ "likely reason: ImplicitConversion and ImplicitConversionExists have gone out of sync");
+
+ break;
}
if (!may_fail && errors == Report.Errors) {
method_params = cand_params;
}
}
-
//
// Now check that there are no ambiguities i.e the selected method
// should be better than all the others
return null;
}
- if ((method.Attributes & MethodAttributes.SpecialName) != 0){
- if (TypeManager.LookupDeclSpace (method.DeclaringType) != null || TypeManager.IsSpecialMethod (method)) {
- Report.Error (571, loc, "`{0}': cannot explicitly call operator or accessor",
- TypeManager.CSharpSignature (method, true));
- return null;
- }
+ if ((method.Attributes & MethodAttributes.SpecialName) != 0 && IsSpecialMethodInvocation (method)) {
+ return null;
}
if (mg.InstanceExpression != null)
return this;
}
+ bool IsSpecialMethodInvocation (MethodBase method)
+ {
+ IMethodData md = TypeManager.GetMethod (method);
+ if (md != null) {
+ if (!(md is AbstractPropertyEventMethod) && !(md is Operator))
+ return false;
+ } else {
+ if (!TypeManager.IsSpecialMethod (method))
+ return false;
+
+ int args = TypeManager.GetParameterData (method).Count;
+ if (method.Name.StartsWith ("get_") && args > 0)
+ return false;
+ else if (method.Name.StartsWith ("set_") && args > 2)
+ return false;
+
+ // TODO: check operators and events as well ?
+ }
+
+ Report.SymbolRelatedToPreviousError (method);
+ Report.Error (571, loc, "`{0}': cannot explicitly call operator or accessor",
+ TypeManager.CSharpSignature (method, true));
+
+ return true;
+ }
+
// <summary>
// Emits the list of arguments as an array
// </summary>
if (!omit_args) {
Type t = null;
if (this_call) {
- ig.Emit (OpCodes.Ldarg_0);
+ ec.EmitThis ();
t = decl_type;
} else {
Type iexpr_type = instance_expr.Type;
Expression expr;
Expression argument;
- public InvocationOrCast (Expression expr, Expression argument, Location loc)
+ public InvocationOrCast (Expression expr, Expression argument)
{
this.expr = expr;
this.argument = argument;
- this.loc = loc;
+ this.loc = expr.Location;
}
public override Expression DoResolve (EmitContext ec)
/// <summary>
/// Converts complex core type syntax like 'new int ()' to simple constant
/// </summary>
- Expression Constantify (Type t)
+ public static Constant Constantify (Type t)
{
if (t == TypeManager.int32_type)
return new IntConstant (0);
}
if (type.IsAbstract && type.IsSealed) {
+ Report.SymbolRelatedToPreviousError (type);
Report.Error (712, loc, "Cannot create an instance of the static class `{0}'", TypeManager.CSharpName (type));
return null;
}
if (type.IsInterface || type.IsAbstract){
+ Report.SymbolRelatedToPreviousError (type);
Report.Error (144, loc, "Cannot create an instance of the abstract class or interface `{0}'", TypeManager.CSharpName (type));
return null;
}
if (e is StringConstant || e is DecimalConstant || !(e is Constant) ||
num_automatic_initializers <= max_automatic_initializers) {
Type etype = e.Type;
-
+
ig.Emit (OpCodes.Dup);
for (int idx = 0; idx < dims; idx++)
// If we are dealing with a struct, get the
// address of it, so we can store it.
//
- if ((dims == 1) && etype.IsValueType &&
+ if ((dims == 1) &&
+ TypeManager.IsValueType (etype) &&
(!TypeManager.IsBuiltinOrEnum (etype) ||
etype == TypeManager.decimal_type)) {
if (e is New){
if (!ResolveBase (ec))
return null;
- if ((variable_info != null) && !variable_info.IsAssigned (ec)) {
+ if ((variable_info != null) && !(type.IsValueType && ec.OmitStructFlowAnalysis) && !variable_info.IsAssigned (ec)) {
Error (188, "The `this' object cannot be used before all of its fields are assigned to");
variable_info.SetAssigned (ec);
return this;
}
}
+ /// <summary>
+ /// Implements the qualified-alias-member (::) expression.
+ /// </summary>
+ public class QualifiedAliasMember : Expression
+ {
+ string alias, identifier;
+
+ public QualifiedAliasMember (string alias, string identifier, Location l)
+ {
+ this.alias = alias;
+ this.identifier = identifier;
+ loc = l;
+ }
+
+ public override FullNamedExpression ResolveAsTypeStep (EmitContext ec, bool silent)
+ {
+ if (alias == "global")
+ return new MemberAccess (Namespace.Root, identifier, loc).ResolveAsTypeStep (ec, silent);
+
+ int errors = Report.Errors;
+ FullNamedExpression fne = ec.DeclSpace.NamespaceEntry.LookupAlias (alias);
+ if (fne == null) {
+ if (errors == Report.Errors)
+ Report.Error (432, loc, "Alias `{0}' not found", alias);
+ return null;
+ }
+ if (fne.eclass != ExprClass.Namespace) {
+ if (!silent)
+ Report.Error (431, loc, "`{0}' cannot be used with '::' since it denotes a type", alias);
+ return null;
+ }
+ return new MemberAccess (fne, identifier, loc).ResolveAsTypeStep (ec, silent);
+ }
+
+ public override Expression DoResolve (EmitContext ec)
+ {
+ FullNamedExpression fne;
+ if (alias == "global") {
+ fne = Namespace.Root;
+ } else {
+ int errors = Report.Errors;
+ fne = ec.DeclSpace.NamespaceEntry.LookupAlias (alias);
+ if (fne == null) {
+ if (errors == Report.Errors)
+ Report.Error (432, loc, "Alias `{0}' not found", alias);
+ return null;
+ }
+ }
+
+ Expression retval = new MemberAccess (fne, identifier, loc).DoResolve (ec);
+ if (retval == null)
+ return null;
+
+ if (!(retval is FullNamedExpression)) {
+ Report.Error (687, loc, "The expression `{0}::{1}' did not resolve to a namespace or a type", alias, identifier);
+ return null;
+ }
+
+ // We defer this check till the end to match the behaviour of CSC
+ if (fne.eclass != ExprClass.Namespace) {
+ Report.Error (431, loc, "`{0}' cannot be used with '::' since it denotes a type", alias);
+ return null;
+ }
+ return retval;
+ }
+
+ public override void Emit (EmitContext ec)
+ {
+ throw new InternalErrorException ("QualifiedAliasMember found in resolved tree");
+ }
+
+
+ public override string ToString ()
+ {
+ return alias + "::" + identifier;
+ }
+
+ public override string GetSignatureForError ()
+ {
+ return ToString ();
+ }
+ }
+
/// <summary>
/// Implements the member access expression
/// </summary>
public class MemberAccess : Expression {
- public string Identifier;
+ public readonly string Identifier; // TODO: LocatedToken
Expression expr;
TypeArguments args;
get { return expr; }
}
+ // TODO: this method has very poor performace for Enum fields and
+ // probably for other constants as well
Expression DoResolve (EmitContext ec, Expression right_side)
{
if (type != null)
SimpleName original = expr as SimpleName;
Expression new_expr = expr.Resolve (ec,
ResolveFlags.VariableOrValue | ResolveFlags.Type |
- ResolveFlags.Intermediate | ResolveFlags.DisableFlowAnalysis);
+ ResolveFlags.Intermediate | ResolveFlags.DisableStructFlowAnalysis);
if (new_expr == null)
return null;
return retval;
}
- //
- // TODO: I mailed Ravi about this, and apparently we can get rid
- // of this and put it in the right place.
- //
- // Handle enums here when they are in transit.
- // Note that we cannot afford to hit MemberLookup in this case because
- // it will fail to find any members at all
- //
-
Type expr_type = new_expr.Type;
- if (new_expr is TypeExpr){
- if (!ec.DeclSpace.CheckAccessLevel (expr_type)){
- ErrorIsInaccesible (loc, TypeManager.CSharpName (expr_type));
- return null;
- }
-
- if (expr_type == TypeManager.enum_type || expr_type.IsSubclassOf (TypeManager.enum_type)){
- Enum en = TypeManager.LookupEnum (expr_type);
-
- if (en != null) {
- object value = en.LookupEnumValue (Identifier, loc);
- if (value != null){
- MemberCore mc = en.GetDefinition (Identifier);
- ObsoleteAttribute oa = mc.GetObsoleteAttribute (en);
- if (oa != null) {
- AttributeTester.Report_ObsoleteMessage (oa, mc.GetSignatureForError (), Location);
- }
- oa = en.GetObsoleteAttribute (en);
- if (oa != null) {
- AttributeTester.Report_ObsoleteMessage (oa, en.GetSignatureForError (), Location);
- }
-
- Constant c = Constantify (value, en.UnderlyingType);
- return new EnumConstant (c, expr_type);
- }
- } else {
- CheckObsoleteAttribute (expr_type);
-
- FieldInfo fi = expr_type.GetField (Identifier);
- if (fi != null) {
- ObsoleteAttribute oa = AttributeTester.GetMemberObsoleteAttribute (fi);
- if (oa != null)
- AttributeTester.Report_ObsoleteMessage (oa, TypeManager.GetFullNameSignature (fi), Location);
- }
- }
- }
- }
-
if (expr_type.IsPointer){
Error (23, "The `.' operator can not be applied to pointer operands (" +
TypeManager.CSharpName (expr_type) + ")");
return mg.ResolveGeneric (ec, args);
}
+ if (original != null && !TypeManager.IsValueType (expr_type)) {
+ me = member_lookup as MemberExpr;
+ if (me != null && me.IsInstance) {
+ LocalVariableReference var = new_expr as LocalVariableReference;
+ if (var != null && !var.VerifyAssigned (ec))
+ return null;
+ }
+ }
+
// The following DoResolve/DoResolveLValue will do the definite assignment
// check.
if (right_side != null)
- member_lookup = member_lookup.DoResolveLValue (ec, right_side);
+ return member_lookup.DoResolveLValue (ec, right_side);
else
- member_lookup = member_lookup.DoResolve (ec);
-
- return member_lookup;
+ return member_lookup.DoResolve (ec);
}
public override Expression DoResolve (EmitContext ec)
public ArrayList Arguments;
public Expression Expr;
- public ElementAccess (Expression e, ArrayList e_list, Location l)
+ public ElementAccess (Expression e, ArrayList e_list)
{
Expr = e;
- loc = l;
+ loc = e.Location;
if (e_list == null)
return;
ig.Emit (OpCodes.Ldobj, type);
} else if (type.IsGenericParameter)
ig.Emit (OpCodes.Ldelem_Any, type);
+ else if (type.IsPointer)
+ ig.Emit (OpCodes.Ldelem_I);
else
ig.Emit (OpCodes.Ldelem_Ref);
}
} else if (t.IsGenericParameter) {
has_type_arg = true;
return OpCodes.Stelem_Any;
- } else
+ } else if (t.IsPointer)
+ return OpCodes.Stelem_I;
+ else
return OpCodes.Stelem_Ref;
}
Expression left;
string dim;
+ public ComposedCast (Expression left, string dim)
+ : this (left, dim, left.Location)
+ {
+ }
+
public ComposedCast (Expression left, string dim, Location l)
{
this.left = left;