public static TypeNode TypeNodeFactory(Type targetType) { ArgumentUtility.CheckNotNull("targetType", targetType); string targetLocation = targetType.Assembly.Location; AssemblyNode targetAssembly = AssemblyNode.GetAssembly(targetLocation); Identifier targetNamespace = Identifier.For(targetType.Namespace); Identifier targetName = Identifier.For(targetType.Name); return(targetAssembly.GetType(targetNamespace, targetName)); }
public static TypeNode TypeNodeFactory <T> () { Type targetType = typeof(T); string targetLocation = targetType.Assembly.Location; AssemblyNode targetAssembly = AssemblyNode.GetAssembly(targetLocation); Identifier targetNamespace = Identifier.For(targetType.Namespace); Identifier targetName = Identifier.For(targetType.Name); return(targetAssembly.GetType(targetNamespace, targetName)); }
private TypeNode GetType(AssemblyNode assembly, string ns, string name) { var res = assembly.GetType(Identifier.For(ns), Identifier.For(name)); if (res == null) { Log(new InvalidInteropMessage(null, "can't load required type: " + ns + "." + name)); throw new ExitException(); } return(res); }
internal static void Initialize() { RuntimeAssembly = Runtime.GetRuntimeAssembly(); PostCompilationPluginAttributeType = RuntimeAssembly.GetType(Identifier.For("Microsoft.SpecSharp"), Identifier.For("PostCompilationPluginAttribute")); ObjectConstructor = SystemTypes.Object.GetConstructor(); IListAdd = SystemTypes.IList.GetMethod(StandardIds.Add, SystemTypes.Object); #if CCINamespace const string ContractsNs = "Microsoft.Contracts"; #else const string ContractsNs = "Microsoft.Contracts"; #endif MustOverrideAttribute = (Class)GetCompilerRuntimeTypeNodeFor(ContractsNs, "MustOverrideAttribute"); }
public static TypeNode FindShadow(this TypeNode typeNode, AssemblyNode shadowAssembly) { if (typeNode.DeclaringType != null) { // nested type var parent = typeNode.DeclaringType.FindShadow(shadowAssembly); if (parent == null) { return(null); } return(parent.GetNestedType(typeNode.Name)); } // namespace type return(shadowAssembly.GetType(typeNode.Namespace, typeNode.Name)); }
private void FuzzilyForwardReferencesFromSource2Target(AssemblyNode targetAssembly, AssemblyNode sourceAssembly) { Contract.Requires(targetAssembly != null); Contract.Requires(sourceAssembly != null); for (int i = 1, n = sourceAssembly.Types.Count; i < n; i++) { TypeNode currentType = sourceAssembly.Types[i]; if (currentType == null) { continue; } TypeNode targetType = targetAssembly.GetType(currentType.Namespace, currentType.Name); if (targetType == null) { if (Duplicator.TypesToBeDuplicated[currentType.UniqueKey] == null) { Duplicator.FindTypesToBeDuplicated(new TypeNodeList(currentType)); } Trace("COPYOOB: type to be duplicated {0}", currentType.FullName); } else { if (HelperMethods.IsContractTypeForSomeOtherType(currentType as Class)) { // dummy contract target type. Ignore it. targetType.Members = new MemberList(); targetType.ClearMemberTable(); } Contract.Assume(TemplateParameterCount(currentType) == TemplateParameterCount(targetType), "Name mangling should ensure this"); Duplicator.DuplicateFor[currentType.UniqueKey] = targetType; Trace("COPYOOB: forwarding {1} to {0}", currentType.FullName, targetType.FullName); FuzzilyForwardType(currentType, targetType); } } }
private static TypeNode /*!*/ GetCompilerRuntimeTypeNodeFor(string /*!*/ nspace, string /*!*/ name, int numParams) { if (Cci.TargetPlatform.GenericTypeNamesMangleChar != 0 && numParams > 0) { name = name + Cci.TargetPlatform.GenericTypeNamesMangleChar + numParams; } TypeNode result = null; if (RuntimeAssembly == null) { Debug.Assert(false); } else { result = RuntimeAssembly.GetType(Identifier.For(nspace), Identifier.For(name)); } //if (result == null) result = CoreSystemTypes.GetDummyTypeNode(RuntimeAssembly, nspace, name); //result.typeCode = typeCode; return(result); }
private static TypeNode GetTypeNode(AssemblyNode assembly, string ns, string name) { Contract.Assert(assembly != null); return(assembly.GetType(Identifier.For(ns), Identifier.For(name))); }
private static TypeNode GetTypeNode(AssemblyNode assembly, string ns, string name) { Contract.Assert(assembly != null); return assembly.GetType(Identifier.For(ns), Identifier.For(name)); }
/// <summary> /// Creates a new instance of this class that contains the CCI nodes for contract class. /// </summary> /// <param name="errorHandler">Delegate receiving errors. If null, errors are thrown as exceptions.</param> /// private ContractNodes(AssemblyNode assembly, Action <System.CodeDom.Compiler.CompilerError> errorHandler) { if (errorHandler != null) { this.ErrorFound += errorHandler; } AssemblyNode assemblyContainingContractClass = null; #region Get the contract class and all of its members assemblyContainingContractClass = assembly; ContractClass = assembly.GetType(ContractNamespace, Identifier.For("Contract")) as Class; if (ContractClass == null) { // This is not a candidate, no warning as we try to find the right place where contracts live return; } ContractHelperClass = assembly.GetType(ContractInternalNamespace, Identifier.For("ContractHelper")) as Class; if (ContractHelperClass == null || !ContractHelperClass.IsPublic) // look in alternate location { var alternateNs = Identifier.For("System.Diagnostics.Contracts.Internal"); ContractHelperClass = assembly.GetType(alternateNs, Identifier.For("ContractHelper")) as Class; } #region Get ContractFailureKind ContractFailureKind = assemblyContainingContractClass.GetType(ContractNamespace, Identifier.For("ContractFailureKind")) as EnumNode; #endregion Get ContractFailureKind #region Look for each member of the contract class var requiresMethods = ContractClass.GetMethods(Identifier.For("Requires"), SystemTypes.Boolean); for (int i = 0; i < requiresMethods.Count; i++) { var method = requiresMethods[i]; if (method.TemplateParameters != null && method.TemplateParameters.Count == 1) { // Requires<E> RequiresExceptionMethod = method; } else if (method.TemplateParameters == null || method.TemplateParameters.Count == 0) { RequiresMethod = method; } } // early test to see if the ContractClass we found has a Requires(bool) method. If it doesn't, we // silently think that this is not the right place. // We use this because contract reference assemblies have a Contract class, but it is not the right one, as it holds // just the 3 argument versions of all the contract methods. if (RequiresMethod == null) { ContractClass = null; return; } var requiresMethodsWithMsg = ContractClass.GetMethods(Identifier.For("Requires"), SystemTypes.Boolean, SystemTypes.String); for (int i = 0; i < requiresMethodsWithMsg.Count; i++) { var method = requiresMethodsWithMsg[i]; if (method.TemplateParameters != null && method.TemplateParameters.Count == 1) { // Requires<E> RequiresExceptionWithMsgMethod = method; } else if (method.TemplateParameters == null || method.TemplateParameters.Count == 0) { RequiresWithMsgMethod = method; } } EnsuresMethod = ContractClass.GetMethod(Identifier.For("Ensures"), SystemTypes.Boolean); EnsuresWithMsgMethod = ContractClass.GetMethod(Identifier.For("Ensures"), SystemTypes.Boolean, SystemTypes.String); EnsuresOnThrowTemplate = ContractClass.GetMethod(Identifier.For("EnsuresOnThrow"), SystemTypes.Boolean); EnsuresOnThrowWithMsgTemplate = ContractClass.GetMethod(Identifier.For("EnsuresOnThrow"), SystemTypes.Boolean, SystemTypes.String); InvariantMethod = ContractClass.GetMethod(Identifier.For("Invariant"), SystemTypes.Boolean); InvariantWithMsgMethod = ContractClass.GetMethod(Identifier.For("Invariant"), SystemTypes.Boolean, SystemTypes.String); AssertMethod = ContractClass.GetMethod(Identifier.For("Assert"), SystemTypes.Boolean); AssertWithMsgMethod = ContractClass.GetMethod(Identifier.For("Assert"), SystemTypes.Boolean, SystemTypes.String); AssumeMethod = ContractClass.GetMethod(Identifier.For("Assume"), SystemTypes.Boolean); AssumeWithMsgMethod = ContractClass.GetMethod(Identifier.For("Assume"), SystemTypes.Boolean, SystemTypes.String); ResultTemplate = ContractClass.GetMethod(ResultName); TypeNode GenericPredicate = SystemTypes.SystemAssembly.GetType( Identifier.For("System"), Identifier.For("Predicate" + TargetPlatform.GenericTypeNamesMangleChar + "1") ); if (GenericPredicate != null) { ForAllGenericTemplate = ContractClass.GetMethod(ForallName, SystemTypes.GenericIEnumerable, GenericPredicate); ExistsGenericTemplate = ContractClass.GetMethod(ExistsName, SystemTypes.GenericIEnumerable, GenericPredicate); if (ForAllGenericTemplate == null) { // The problem might be that we are in the pre 4.0 scenario and using an out-of-band contract for mscorlib // in which case the contract library is defined in that out-of-band contract assembly. // If so, then ForAll and Exists are defined in terms of the System.Predicate defined in the out-of-band assembly. var tempGenericPredicate = assemblyContainingContractClass.GetType( Identifier.For("System"), Identifier.For("Predicate" + TargetPlatform.GenericTypeNamesMangleChar + "1") ); if (tempGenericPredicate != null) { GenericPredicate = tempGenericPredicate; TypeNode genericIEnum = assemblyContainingContractClass.GetType(Identifier.For("System.Collections.Generic"), Identifier.For("IEnumerable" + TargetPlatform.GenericTypeNamesMangleChar + "1")); ForAllGenericTemplate = ContractClass.GetMethod(Identifier.For("ForAll"), genericIEnum, GenericPredicate); ExistsGenericTemplate = ContractClass.GetMethod(Identifier.For("Exists"), genericIEnum, GenericPredicate); } } TypeNode PredicateOfInt = GenericPredicate.GetTemplateInstance(ContractClass, SystemTypes.Int32); if (PredicateOfInt != null) { ForAllTemplate = ContractClass.GetMethod(Identifier.For("ForAll"), SystemTypes.Int32, SystemTypes.Int32, PredicateOfInt); ExistsTemplate = ContractClass.GetMethod(Identifier.For("Exists"), SystemTypes.Int32, SystemTypes.Int32, PredicateOfInt); } } foreach (Member member in ContractClass.GetMembersNamed(ValueAtReturnName)) { Method method = member as Method; if (method != null && method.Parameters.Count == 1) { Reference reference = method.Parameters[0].Type as Reference; if (reference != null && reference.ElementType.IsTemplateParameter) { ParameterTemplate = method; break; } } } foreach (Member member in ContractClass.GetMembersNamed(OldName)) { Method method = member as Method; if (method != null && method.Parameters.Count == 1 && method.Parameters[0].Type.IsTemplateParameter) { OldTemplate = method; break; } } EndContract = ContractClass.GetMethod(Identifier.For("EndContractBlock")); if (this.ContractFailureKind != null) { if (ContractHelperClass != null) { RaiseFailedEventMethod = ContractHelperClass.GetMethod(ContractNodes.RaiseContractFailedEventName, this.ContractFailureKind, SystemTypes.String, SystemTypes.String, SystemTypes.Exception); TriggerFailureMethod = ContractHelperClass.GetMethod(ContractNodes.TriggerFailureName, this.ContractFailureKind, SystemTypes.String, SystemTypes.String, SystemTypes.String, SystemTypes.Exception); } } #endregion Look for each member of the contract class #endregion Get the contract class and all of its members #region Get the attributes PureAttribute = assemblyContainingContractClass.GetType(ContractNamespace, Identifier.For("PureAttribute")) as Class; InvariantMethodAttribute = assemblyContainingContractClass.GetType(ContractNamespace, Identifier.For("ContractInvariantMethodAttribute")) as Class; ContractClassAttribute = assemblyContainingContractClass.GetType(ContractNamespace, ContractClassAttributeName) as Class; ContractClassForAttribute = assemblyContainingContractClass.GetType(ContractNamespace, ContractClassForAttributeName) as Class; VerifyAttribute = assemblyContainingContractClass.GetType(ContractNamespace, Identifier.For("ContractVerificationAttribute")) as Class; SpecPublicAttribute = assemblyContainingContractClass.GetType(ContractNamespace, SpecPublicAttributeName) as Class; ReferenceAssemblyAttribute = assemblyContainingContractClass.GetType(ContractNamespace, Identifier.For("ContractReferenceAssemblyAttribute")) as Class; IgnoreAtRuntimeAttribute = assemblyContainingContractClass.GetType(ContractNamespace, RuntimeIgnoredAttributeName) as Class; #endregion #region Check that every field in this class has been set foreach (System.Reflection.FieldInfo field in typeof(ContractNodes).GetFields()) { if (field.GetValue(this) == null) { string sig = null; bool required = false; object[] cas = field.GetCustomAttributes(typeof(RepresentationFor), false); for (int i = 0, n = cas.Length; i < n; i++) // should be exactly one { RepresentationFor rf = cas[i] as RepresentationFor; if (rf != null) { sig = rf.runtimeName; required = rf.required; break; } } if (!required) { continue; } string msg = "Could not find contract node for '" + field.Name + "'"; if (sig != null) { msg = "Could not find the method/type '" + sig + "'"; } if (ContractClass != null && ContractClass.DeclaringModule != null) { msg += " in assembly '" + ContractClass.DeclaringModule.Location + "'"; } Module dm = ContractClass.DeclaringModule; ClearFields(); CallErrorFound(dm, msg); return; } } #region Check that ContractFailureKind is okay if (this.ContractFailureKind.GetField(Identifier.For("Assert")) == null || this.ContractFailureKind.GetField(Identifier.For("Assume")) == null || this.ContractFailureKind.GetField(Identifier.For("Invariant")) == null || this.ContractFailureKind.GetField(Identifier.For("Postcondition")) == null || this.ContractFailureKind.GetField(Identifier.For("Precondition")) == null ) { Module dm = ContractClass.DeclaringModule; ClearFields(); CallErrorFound(dm, "The enum ContractFailureKind must have the values 'Assert', 'Assume', 'Invariant', 'Postcondition', and 'Precondition'."); } #endregion #endregion Check that every field in this class has been set }
private TypeNode GetType(AssemblyNode assembly, string ns, string name) { var res = assembly.GetType(Identifier.For(ns), Identifier.For(name)); if (res == null) { Log(new InvalidInteropMessage(null, "can't load required type: " + ns + "." + name)); throw new ExitException(); } return res; }
private ContractNodes(AssemblyNode assembly, Action <string> errorHandler) { CoreSystemTypes.ModuleDefinition = assembly.Modules.First().Definition; if (errorHandler != null) { ErrorFound += errorHandler; } this.ContractClass = assembly.GetType(ContractNamespace, ContractClassName) as Class; if (this.ContractClass == null) { return; } IEnumerable <Method> methods = this.ContractClass.GetMethods(RequiresName, CoreSystemTypes.Instance.TypeBoolean); foreach (Method method in methods) { if (method.GenericParameters == null || method.GenericParameters.Count == 0) { this.RequiresMethod = method; } } if (this.RequiresMethod == null) { this.ContractClass = null; return; } methods = this.ContractClass.GetMethods(RequiresName, CoreSystemTypes.Instance.TypeBoolean, CoreSystemTypes.Instance.TypeString); foreach (Method method in methods) { if (method.GenericParameters == null || method.GenericParameters.Count == 0) { this.RequiresWithMessageMethod = method; } } this.EnsuresMethod = this.ContractClass.GetMethod(EnsuresName, CoreSystemTypes.Instance.TypeBoolean); this.EnsuresWithMessageMethod = this.ContractClass.GetMethod(EnsuresName, CoreSystemTypes.Instance.TypeBoolean, CoreSystemTypes.Instance.TypeString); this.AssertMethod = this.ContractClass.GetMethod(AssertName, CoreSystemTypes.Instance.TypeBoolean); this.AssertWithMessageMethod = this.ContractClass.GetMethod(AssertName, CoreSystemTypes.Instance.TypeBoolean, CoreSystemTypes.Instance.TypeString); this.AssumeMethod = this.ContractClass.GetMethod(AssumeName, CoreSystemTypes.Instance.TypeBoolean); this.AssumeWithMessageMethod = this.ContractClass.GetMethod(AssumeName, CoreSystemTypes.Instance.TypeBoolean, CoreSystemTypes.Instance.TypeString); this.EndContractBlock = this.ContractClass.GetMethod(EndContractBlockName); foreach (FieldInfo fieldInfo in typeof(ContractNodes).GetFields()) { if (fieldInfo.GetValue(this) != null) { continue; } string runtimeName = null; bool isRequired = false; object[] attributes = fieldInfo.GetCustomAttributes(typeof(RepresentationForAttribute), false); foreach (object attribute in attributes) { var representationForAttribute = attribute as RepresentationForAttribute; if (representationForAttribute != null) { runtimeName = representationForAttribute.RuntimeName; isRequired = representationForAttribute.IsRequired; break; } } if (isRequired) { string message = string.Format("Could not find contract node for '{0}'", fieldInfo.Name); if (runtimeName != null) { message = string.Format("Could not find the method/type '{0}'", runtimeName); } FireErrorFound(message); ClearFields(); } } }