void Error_WrongAwaiterPattern(ResolveContext rc, TypeSpec awaiter) { rc.Report.Error(4011, loc, "The awaiter type `{0}' must have suitable IsCompleted and GetResult members", awaiter.GetSignatureForError()); }
protected override Expression DoResolve(ResolveContext ec) { constructor_method = Delegate.GetConstructor(type); var invoke_method = Delegate.GetInvokeMethod(type); Arguments arguments = CreateDelegateMethodArguments(ec, invoke_method.Parameters, invoke_method.Parameters.Types, loc); method_group = method_group.OverloadResolve(ec, ref arguments, this, OverloadResolver.Restrictions.CovariantDelegate); if (method_group == null) { return(null); } var delegate_method = method_group.BestCandidate; if (delegate_method.DeclaringType.IsNullableType) { ec.Report.Error(1728, loc, "Cannot create delegate from method `{0}' because it is a member of System.Nullable<T> type", delegate_method.GetSignatureForError()); return(null); } if (!AllowSpecialMethodsInvocation) { Invocation.IsSpecialMethodInvocation(ec, delegate_method, loc); } ExtensionMethodGroupExpr emg = method_group as ExtensionMethodGroupExpr; if (emg != null) { method_group.InstanceExpression = emg.ExtensionExpression; TypeSpec e_type = emg.ExtensionExpression.Type; if (TypeSpec.IsValueType(e_type)) { ec.Report.Error(1113, loc, "Extension method `{0}' of value type `{1}' cannot be used to create delegates", delegate_method.GetSignatureForError(), e_type.GetSignatureForError()); } } TypeSpec rt = delegate_method.ReturnType; if (rt.BuiltinType == BuiltinTypeSpec.Type.Dynamic) { rt = ec.BuiltinTypes.Object; } if (!Delegate.IsTypeCovariant(ec, rt, invoke_method.ReturnType)) { Expression ret_expr = new TypeExpression(delegate_method.ReturnType, loc); Error_ConversionFailed(ec, delegate_method, ret_expr); } if (delegate_method.IsConditionallyExcluded(ec)) { ec.Report.SymbolRelatedToPreviousError(delegate_method); MethodOrOperator m = delegate_method.MemberDefinition as MethodOrOperator; if (m != null && m.IsPartialDefinition) { ec.Report.Error(762, loc, "Cannot create delegate from partial method declaration `{0}'", delegate_method.GetSignatureForError()); } else { ec.Report.Error(1618, loc, "Cannot create delegate with `{0}' because it has a Conditional attribute", TypeManager.CSharpSignature(delegate_method)); } } var expr = method_group.InstanceExpression; if (expr != null && (expr.Type.IsGenericParameter || !TypeSpec.IsReferenceType(expr.Type))) { method_group.InstanceExpression = new BoxedCast(expr, ec.BuiltinTypes.Object); } eclass = ExprClass.Value; return(this); }
public void IsClsCompliant(IMemberContext ctx) { if (parameter_type.IsCLSCompliant()) { return; } ctx.Module.Compiler.Report.Warning(3001, 1, Location, "Argument type `{0}' is not CLS-compliant", parameter_type.GetSignatureForError()); }
static void Error_WrongGetAwaiter(ResolveContext rc, Location loc, TypeSpec type) { rc.Report.Error(1986, loc, "The `await' operand type `{0}' must have suitable GetAwaiter method", type.GetSignatureForError()); }
public override void Error_ValueCannotBeConverted(ResolveContext rc, TypeSpec target, bool expl) { rc.Report.Error(8135, Location, "Tuple literal `{0}' cannot be converted to type `{1}'", type.GetSignatureForError(), target.GetSignatureForError()); }
public TypeExpr LookupType(IMemberContext ctx, string name, int arity, bool silent, Location loc) { if (types == null) { return(null); } TypeExpr te; if (arity == 0 && cached_types.TryGetValue(name, out te)) { return(te); } IList <TypeSpec> found; if (!types.TryGetValue(name, out found)) { return(null); } TypeSpec best = null; foreach (var ts in found) { if (ts.Arity == arity) { if (best == null) { best = ts; continue; } var pts = best as BuiltinTypeSpec; if (pts == null) { pts = ts as BuiltinTypeSpec; } if (pts != null) { ctx.Module.Compiler.Report.SymbolRelatedToPreviousError(best); ctx.Module.Compiler.Report.SymbolRelatedToPreviousError(ts); // TODO: This should use different warning number but we want to be csc compatible ctx.Module.Compiler.Report.Warning(1685, 1, loc, "The predefined type `{0}.{1}' is redefined in the source code. Ignoring the local type definition", pts.Namespace, pts.Name); best = pts; continue; } if (best.MemberDefinition.IsImported && ts.MemberDefinition.IsImported) { ctx.Module.Compiler.Report.SymbolRelatedToPreviousError(best); ctx.Module.Compiler.Report.SymbolRelatedToPreviousError(ts); if (silent) { ctx.Module.Compiler.Report.Warning(1685, 1, loc, "The predefined type `{0}' is defined in multiple assemblies. Using definition from `{1}'", ts.GetSignatureForError(), best.MemberDefinition.DeclaringAssembly.Name); } else { ctx.Module.Compiler.Report.Error(433, loc, "The imported type `{0}' is defined multiple times", ts.GetSignatureForError()); } break; } if (best.MemberDefinition.IsImported) { best = ts; } if ((best.Modifiers & Modifiers.INTERNAL) != 0 && !best.MemberDefinition.IsInternalAsPublic(ctx.Module.DeclaringAssembly)) { continue; } if (silent) { continue; } if (ts.MemberDefinition.IsImported) { ctx.Module.Compiler.Report.SymbolRelatedToPreviousError(ts); } ctx.Module.Compiler.Report.Warning(436, 2, loc, "The type `{0}' conflicts with the imported type of same name'. Ignoring the imported type definition", best.GetSignatureForError()); } // // Lookup for the best candidate with closest arity match // if (arity < 0) { if (best == null) { best = ts; } else if (System.Math.Abs(ts.Arity + arity) < System.Math.Abs(best.Arity + arity)) { best = ts; } } } if (best == null) { return(null); } if ((best.Modifiers & Modifiers.INTERNAL) != 0 && !best.MemberDefinition.IsInternalAsPublic(ctx.Module.DeclaringAssembly)) { return(null); } te = new TypeExpression(best, Location.Null); // TODO MemberCache: Cache more if (arity == 0 && !silent) { cached_types.Add(name, te); } return(te); }
protected override void Error_OperatorCannotBeApplied(ResolveContext rc, TypeSpec type) { rc.Report.Error(1991, loc, "Cannot await `{0}' expression", type.GetSignatureForError()); }
public TypeExpr LookupType(IMemberContext ctx, string name, int arity, Location loc) { if (types == null) { return(null); } TypeExpr te; if (arity == 0 && cached_types.TryGetValue(name, out te)) { return(te); } IList <TypeSpec> found; if (!types.TryGetValue(name, out found)) { return(null); } TypeSpec best = null; foreach (var ts in found) { if (ts.Arity == arity) { if (best == null) { best = ts; continue; } if (best.MemberDefinition.IsImported && ts.MemberDefinition.IsImported) { ctx.Module.Compiler.Report.SymbolRelatedToPreviousError(best); ctx.Module.Compiler.Report.SymbolRelatedToPreviousError(ts); ctx.Module.Compiler.Report.Error(433, loc, "The imported type `{0}' is defined multiple times", ts.GetSignatureForError()); break; } if (best.MemberDefinition.IsImported) { best = ts; } if ((best.Modifiers & Modifiers.INTERNAL) != 0 && !best.MemberDefinition.IsInternalAsPublic(ctx.Module.DeclaringAssembly)) { continue; } if (ts.MemberDefinition.IsImported) { ctx.Module.Compiler.Report.SymbolRelatedToPreviousError(ts); } ctx.Module.Compiler.Report.Warning(436, 2, loc, "The type `{0}' conflicts with the imported type of same name'. Ignoring the imported type definition", best.GetSignatureForError()); } // // Lookup for the best candidate with closest arity match // if (arity < 0) { if (best == null) { best = ts; } else if (System.Math.Abs(ts.Arity + arity) < System.Math.Abs(best.Arity + arity)) { best = ts; } } } if (best == null) { return(null); } if ((best.Modifiers & Modifiers.INTERNAL) != 0 && !best.MemberDefinition.IsInternalAsPublic(ctx.Module.DeclaringAssembly)) { return(null); } te = new TypeExpression(best, Location.Null); // TODO MemberCache: Cache more if (arity == 0) { cached_types.Add(name, te); } return(te); }
public void ApplyAttributeBuilder(Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa) { if (a.IsValidSecurityAttribute()) { a.ExtractSecurityPermissionSet(ctor, ref declarative_security); return; } if (a.Type == pa.AssemblyCulture) { string value = a.GetString(); if (value == null || value.Length == 0) { return; } if (Compiler.Settings.Target == Target.Exe) { a.Error_AttributeEmitError("The executables cannot be satelite assemblies, remove the attribute or keep it empty"); return; } if (value == "neutral") { value = ""; } if (Compiler.Settings.Target == Target.Module) { SetCustomAttribute(ctor, cdata); } else { builder_extra.SetCulture(value, a.Location); } IsSatelliteAssembly = true; return; } if (a.Type == pa.AssemblyVersion) { string value = a.GetString(); if (value == null || value.Length == 0) { return; } var vinfo = IsValidAssemblyVersion(value, true); if (vinfo == null) { a.Error_AttributeEmitError(string.Format("Specified version `{0}' is not valid", value)); return; } if (Compiler.Settings.Target == Target.Module) { SetCustomAttribute(ctor, cdata); } else { builder_extra.SetVersion(vinfo, a.Location); } return; } if (a.Type == pa.AssemblyAlgorithmId) { const int pos = 2; // skip CA header uint alg = (uint)cdata [pos]; alg |= ((uint)cdata [pos + 1]) << 8; alg |= ((uint)cdata [pos + 2]) << 16; alg |= ((uint)cdata [pos + 3]) << 24; if (Compiler.Settings.Target == Target.Module) { SetCustomAttribute(ctor, cdata); } else { builder_extra.SetAlgorithmId(alg, a.Location); } return; } if (a.Type == pa.AssemblyFlags) { const int pos = 2; // skip CA header uint flags = (uint)cdata[pos]; flags |= ((uint)cdata [pos + 1]) << 8; flags |= ((uint)cdata [pos + 2]) << 16; flags |= ((uint)cdata [pos + 3]) << 24; // Ignore set PublicKey flag if assembly is not strongnamed if ((flags & (uint)AssemblyNameFlags.PublicKey) != 0 && public_key == null) { flags &= ~(uint)AssemblyNameFlags.PublicKey; } if (Compiler.Settings.Target == Target.Module) { SetCustomAttribute(ctor, cdata); } else { builder_extra.SetFlags(flags, a.Location); } return; } if (a.Type == pa.TypeForwarder) { TypeSpec t = a.GetArgumentType(); if (t == null || TypeManager.HasElementType(t)) { Report.Error(735, a.Location, "Invalid type specified as an argument for TypeForwardedTo attribute"); return; } if (emitted_forwarders == null) { emitted_forwarders = new Dictionary <ITypeDefinition, Attribute> (); } else if (emitted_forwarders.ContainsKey(t.MemberDefinition)) { Report.SymbolRelatedToPreviousError(emitted_forwarders[t.MemberDefinition].Location, null); Report.Error(739, a.Location, "A duplicate type forward of type `{0}'", t.GetSignatureForError()); return; } emitted_forwarders.Add(t.MemberDefinition, a); if (t.MemberDefinition.DeclaringAssembly == this) { Report.SymbolRelatedToPreviousError(t); Report.Error(729, a.Location, "Cannot forward type `{0}' because it is defined in this assembly", t.GetSignatureForError()); return; } if (t.IsNested) { Report.Error(730, a.Location, "Cannot forward type `{0}' because it is a nested type", t.GetSignatureForError()); return; } builder_extra.AddTypeForwarder(t.GetDefinition(), a.Location); return; } if (a.Type == pa.Extension) { a.Error_MisusedExtensionAttribute(); return; } if (a.Type == pa.InternalsVisibleTo) { string assembly_name = a.GetString(); if (assembly_name.Length == 0) { return; } #if STATIC ParsedAssemblyName aname; ParseAssemblyResult r = Fusion.ParseAssemblyName(assembly_name, out aname); if (r != ParseAssemblyResult.OK) { Report.Warning(1700, 3, a.Location, "Assembly reference `{0}' is invalid and cannot be resolved", assembly_name); return; } if (aname.Version != null || aname.Culture != null || aname.ProcessorArchitecture != ProcessorArchitecture.None) { Report.Error(1725, a.Location, "Friend assembly reference `{0}' is invalid. InternalsVisibleTo declarations cannot have a version, culture or processor architecture specified", assembly_name); return; } if (public_key != null && !aname.HasPublicKey) { Report.Error(1726, a.Location, "Friend assembly reference `{0}' is invalid. Strong named assemblies must specify a public key in their InternalsVisibleTo declarations", assembly_name); return; } #endif } else if (a.Type == pa.RuntimeCompatibility) { wrap_non_exception_throws_custom = true; } else if (a.Type == pa.AssemblyFileVersion) { string value = a.GetString(); if (string.IsNullOrEmpty(value) || IsValidAssemblyVersion(value, false) == null) { Report.Warning(1607, 1, a.Location, "The version number `{0}' specified for `{1}' is invalid", value, a.Name); return; } } SetCustomAttribute(ctor, cdata); }
/// <summary> /// Returns the C# name of a type if possible, or the full type name otherwise /// </summary> static public string CSharpName(TypeSpec t) { return(t.GetSignatureForError()); }
private bool DetermineVirtualState(string signature, MethodInfo methodInfo) { if (methodInfo.Type != VirtualType.Unknown) { // Has been already determined, no further work needed (parent methods have been scanned too) return(methodInfo.Type != VirtualType.NotVirtual); } bool isVirtual = ((methodInfo.Member.ModFlags & Modifiers.VIRTUAL) != 0); if (methodInfo.Member is PropertyBase.PropertyMethod) { // If property (or indexer), we also look at the property itself isVirtual |= (((PropertyBase.PropertyMethod)(methodInfo.Member)).Property.ModFlags & Modifiers.VIRTUAL) != 0; } bool isOverride = ((methodInfo.Member.ModFlags & Modifiers.OVERRIDE) != 0); if (methodInfo.Member is PropertyBase.PropertyMethod) { // If property (or indexer), we also look at the property itself isOverride |= (((PropertyBase.PropertyMethod)(methodInfo.Member)).Property.ModFlags & Modifiers.OVERRIDE) != 0; } if (isVirtual || isOverride) { methodInfo.Type = VirtualType.FirstAndOnlyVirtual; // Initial state if virtual // Now we need to recursively go up the base classes, and find methods with the same signature // And if there is one and it was already virtual, we need to change the current method to VirtualType.OverrideVirtual. // We have to double-check if a class skip a level TypeSpec parentType = methodInfo.Member.Parent.CurrentType.BaseType; while (parentType != null) { MethodInfo parentMethodInfo = GetMethodInfoFromCache(parentType, signature); if (parentMethodInfo != null) { // Recurse here, it will go through each base method, one parent class at a time bool isParentVirtual = DetermineVirtualState(signature, parentMethodInfo); // We should expect the base method to be virtual as this method is virtual if (isParentVirtual == false) { if (verbose) { Console.WriteLine("[Auto-sealing] Error with method {0}. Base method is not virtual, child method is.", parentMethodInfo.Member.GetSignatureForError()); } } if (parentMethodInfo.Type == VirtualType.FirstAndOnlyVirtual) { // The parent method is actually not the the only virtual in the tree, mark it as top of the tree parentMethodInfo.Type = VirtualType.FirstVirtual; } // But in any case, we know that the current method is overriden (we don't support new, just override) methodInfo.Type = VirtualType.OverrideVirtual; break; // No need to continue with all the base types as they have been already parsed } else { // No parent method, we keep the state FirstAndOnlyVirtual for the moment // But we have to go through all parents to make sure if (parentType.MemberDefinition is ImportedTypeDefinition) { // Due to the lack of full parsing of imported types (to be done later), // There is only one case where we rely on the flags of the method. // If the base type is imported and the current method is override, we assume that it is true, // And up the chain one method was FirstVirtual (but it won't be visited) if (isOverride) { methodInfo.Type = VirtualType.OverrideVirtual; if (verbose) { Console.WriteLine("[Auto-sealing] Assume method {0} is override due to imported base class {1}", methodInfo.Member.GetSignatureForError(), parentType.GetSignatureForError()); } } // We stop at the first imported type, as we won't get more information going further up the chain // as visited types have not been visited break; } } parentType = parentType.BaseType; } return(true); } else { methodInfo.Type = VirtualType.NotVirtual; return(false); } }
private void VisitClass(Class c) { if (visitedClasses.Contains(c)) { Skip = true; return; } visitedClasses.Add(c); switch (CurrentPass) { case Pass.DiscoverClassesAndMethods: { TypeSpec baseType = c.BaseType; if (baseType != null) { if (verbose) { Console.WriteLine("[Auto-sealing] Found parent class {0} for class {1}.", baseType.GetSignatureForError(), c.GetSignatureForError()); } baseTypes.Add(baseType); if (baseType.MemberDefinition is ImportedTypeDefinition) { // The base class is coming from another assembly - It will not be visited as part of this assembly // But we still have to get some of its information recursively // and visit all the methods // TODO: Do the parsing work } } break; } case Pass.FinalizeModifierFlags: // Last class of a hierarchy are auto-sealed if they are not static if (IsLeafClass(c.CurrentType) && ((c.ModFlags & Modifiers.STATIC) == 0)) { if (verbose) { Console.WriteLine("[Auto-sealing] Making class {0} sealed.", c.GetSignatureForError()); } // When we seal here, we get proper compile error, however the class does not seem to be marked as sealed in IL //c.ModFlags |= Modifiers.SEALED; } break; } }