public void LiftSimpleTypeReference() { var simpleTypeRef = new SimpleTypeReference("foo"); var parameter = GenericParameterDeclaration.Lift(simpleTypeRef); Assert.AreEqual(simpleTypeRef.Name, parameter.Name); }
public static TypeReference ReadTypeReference(XmlReader reader) { if (reader == null || reader.NodeType != XmlNodeType.Element) { return(null); } switch (reader.Name) { // For the TypeReference(s)... case "SimpleTypeReference": SimpleTypeReference simpleTypeReference = new SimpleTypeReference(); simpleTypeReference.ReadXml(reader); return(simpleTypeReference); case "SpecializedTypeReference": SpecializedTypeReference specializedTypeReference = new SpecializedTypeReference(); specializedTypeReference.ReadXml(reader); return(specializedTypeReference); case "ArrayTypeReference": ArrayTypeReference arrayTypeReference = new ArrayTypeReference(); arrayTypeReference.ReadXml(reader); return(arrayTypeReference); case "ReferenceTypeReference": ReferenceTypeReference referenceTypeReference = new ReferenceTypeReference(); referenceTypeReference.ReadXml(reader); return(referenceTypeReference); case "PointerTypeReference": PointerTypeReference pointerTypeReference = new PointerTypeReference(); pointerTypeReference.ReadXml(reader); return(pointerTypeReference); // For the TemplateTypeReference(s)... case "IndexedTemplateTypeReference": IndexedTemplateTypeReference indexedTemplateTypeReference = new IndexedTemplateTypeReference(); indexedTemplateTypeReference.ReadXml(reader); return(indexedTemplateTypeReference); case "NamedTemplateTypeReference": NamedTemplateTypeReference namedTemplateTypeReference = new NamedTemplateTypeReference(); namedTemplateTypeReference.ReadXml(reader); return(namedTemplateTypeReference); case "TypeTemplateTypeReference": TypeTemplateTypeReference typeTemplateTypeReference = new TypeTemplateTypeReference(); typeTemplateTypeReference.ReadXml(reader); return(typeTemplateTypeReference); case "MethodTemplateTypeReference": MethodTemplateTypeReference methodTemplateTypeReference = new MethodTemplateTypeReference(); methodTemplateTypeReference.ReadXml(reader); return(methodTemplateTypeReference); } return(null); }
/* the simple type references that happen while visiting a * class defintion are its base class and implemented interfaces * */ override public void OnSimpleTypeReference(SimpleTypeReference node) { if (_current != null && !_current.BaseTypes.Contains(node.Name)) { _current.BaseTypes.Add(node); } }
public override void OnSimpleTypeReference(SimpleTypeReference node) { if (node.Entity.EntityType == EntityType.Type && TryFixNameClash(node.Name, out string fixedName)) { node.Name = fixedName; } }
private IEntity NameNotType(SimpleTypeReference node, IEntity actual) { string suggestion = GetMostSimilarTypeName(node.Name); CompilerErrors().Add(CompilerErrorFactory.NameNotType(node, node.Name, actual, suggestion)); return(TypeSystemServices.ErrorEntity); }
private static SpecializedTypeReference CreateSpecializedTypeReference(string api) { List <Specialization> specializations = new List <Specialization>(); string text = String.Copy(api); // at the moment we are only handling one specialization; need to iterate int specializationStart = text.IndexOf('{'); int specializationEnd = FindMatchingEndBracket(text, specializationStart); string list = text.Substring(specializationStart + 1, specializationEnd - specializationStart - 1); IList <string> types = SeparateTypes(list); string template = text.Substring(0, specializationStart) + String.Format("`{0}", types.Count); SimpleTypeReference templateReference = CreateSimpleTypeReference(template); List <TypeReference> argumentReferences = new List <TypeReference>(types.Count); for (int i = 0; i < types.Count; i++) { argumentReferences.Add(CreateTypeReference(types[i])); } Specialization specialization = new Specialization(templateReference, argumentReferences); specializations.Add(specialization); // end iteration return(new SpecializedTypeReference(specializations)); }
protected ParameterDeclaration MapParamType(ParameterDeclaration parameter) { if (parameter.Type.NodeType == NodeType.GenericTypeReference) { var gen = (GenericTypeReference)parameter.Type; var genEntityType = gen.Entity as IConstructedTypeInfo; if (genEntityType == null) { return(parameter); } var trc = new TypeReferenceCollection(); foreach (var genArg in gen.GenericArguments) { var replacement = genArg; foreach (var genParam in _genericParams) { if (genParam.Name.Equals(genArg.Entity.Name)) { replacement = new SimpleTypeReference(genParam.Name) { Entity = genParam.Entity }; break; } } trc.Add(replacement); } parameter = parameter.CloneNode(); gen = (GenericTypeReference)parameter.Type; gen.GenericArguments = trc; gen.Entity = new GenericConstructedType(genEntityType.GenericDefinition, trc.Select(a => a.Entity).Cast <IType>().ToArray()); } return(parameter); }
private void GenerateConstructors(TypeDefinition definition) { ConstructorInfo[] ctors = baseClass.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); foreach (ConstructorInfo ctor in ctors) { if (ctor.IsPrivate) { continue; } Constructor constructor = new Constructor(definition.LexicalInfo); definition.Members.Add(constructor); MethodInvocationExpression super = new MethodInvocationExpression(new SuperLiteralExpression()); constructor.Body.Add(super); foreach (ParameterInfo info in ctor.GetParameters()) { SimpleTypeReference typeReference = new SimpleTypeReference(TypeUtilities.GetFullName(info.ParameterType)); constructor.Parameters.Add(new ParameterDeclaration(info.Name, typeReference) ); super.Arguments.Add(new ReferenceExpression(info.Name)); } } }
IType GetExtendedMacroType(IMethod method) { InternalMethod internalMethod = method as InternalMethod; if (null != internalMethod) { Method extension = internalMethod.Method; if (!extension.Attributes.Contains(Types.CompilerGeneratedAttribute.FullName)) { return(null); } SimpleTypeReference sref = extension.Parameters[0].Type as SimpleTypeReference; if (null != sref && extension.Parameters.Count == 2) { IType type = NameResolutionService.ResolveQualifiedName(sref.Name) as IType; if (type != null && type.Name.EndsWith("Macro")) //no entity yet { return(type); } } } else if (method is ExternalMethod && method.IsExtension) { var parameters = method.GetParameters(); if (parameters.Length == 2 && TypeSystemServices.IsMacro(parameters[0].Type)) { return(parameters[0].Type); } } return(null); }
public void ResolveSimpleTypeReference(SimpleTypeReference node) { if (null != node.Entity) { return; } IEntity info = null; if (IsQualifiedName(node.Name)) { info = ResolveQualifiedName(node.Name); } else { info = Resolve(node.Name, EntityType.Type); } if (null == info || EntityType.Type != info.EntityType) { _context.Errors.Add(CompilerErrorFactory.NameNotType(node, node.Name)); info = TypeSystemServices.ErrorEntity; } else { node.Name = info.FullName; } node.Entity = info; }
public override void OnSimpleTypeReference(SimpleTypeReference node) { if (node.Name == "System.Collections.IEnumerator") { node.Name = "IEnumerator"; } }
private string GetTypeTemplateName(SimpleTypeReference type, int position) { TypeTarget target = type.Resolve(_targets) as TypeTarget; if (target != null) { IList <string> templates = target.Templates; if (templates.Count > position) { return(templates[position]); } else if (target.OuterType != null) { return(GetTypeTemplateName(target.OuterType, position)); } else { return("UTT"); } } else { throw new InvalidOperationException(String.Format("Unknown type reference '{0}'", type.Id)); } }
public void ResolveSimpleTypeReference(SimpleTypeReference node) { if (null != node.Entity) { return; } IEntity entity = ResolveTypeName(node); if (null == entity) { node.Entity = NameNotType(node, "not found"); return; } GenericTypeReference gtr = node as GenericTypeReference; if (null != gtr) { entity = ResolveGenericTypeReference(gtr, entity); } GenericTypeDefinitionReference gtdr = node as GenericTypeDefinitionReference; if (null != gtdr) { IType type = (IType)entity; if (gtdr.GenericPlaceholders != type.GenericInfo.GenericParameters.Length) { GenericArgumentsCountMismatch(gtdr, type); return; } } entity = Entities.PreferInternalEntitiesOverExternalOnes(entity); if (EntityType.Type != entity.EntityType) { if (EntityType.Ambiguous == entity.EntityType) { entity = AmbiguousReference(node, (Ambiguous)entity); } else if (EntityType.Error != entity.EntityType) { entity = NameNotType(node, entity.ToString()); } } else { node.Name = entity.FullName; } if (node.IsPointer && EntityType.Type == entity.EntityType) { entity = ((IType)entity).MakePointerType(); } node.Entity = entity; }
public override void OnSimpleTypeReference(SimpleTypeReference node) { if (node.LexicalInfo != null) { results.MapParsedNode(new MappedTypeReference(results, node)); } base.OnSimpleTypeReference(node); }
public void LiftCastExpressionWithSelfTarget() { var self = new SelfLiteralExpression(); var typeReference = new SimpleTypeReference("T"); Expression cast = new TryCastExpression(self, typeReference); var parameter = ParameterDeclaration.Lift(cast); Assert.AreEqual("self", parameter.Name); Assert.IsTrue(typeReference.Matches(parameter.Type)); Assert.AreNotSame(typeReference, parameter.Type); }
public void LiftCastExpression() { var referenceExpression = new ReferenceExpression("foo"); var typeReference = new SimpleTypeReference("T"); Expression cast = new TryCastExpression(referenceExpression, typeReference); var parameter = ParameterDeclaration.Lift(cast); Assert.AreEqual(referenceExpression.Name, parameter.Name); Assert.IsTrue(typeReference.Matches(parameter.Type)); Assert.AreNotSame(typeReference, parameter.Type); }
public static SimpleTypeReference CreateSimpleTypeReference(XPathNavigator node) { if (node == null) { throw new ArgumentNullException("node"); } string api = node.GetAttribute("api", String.Empty); SimpleTypeReference reference = new SimpleTypeReference(api); return(reference); }
public override void OnSimpleTypeReference(SimpleTypeReference node) { if (node.Name == "Array") { var arrayReference = new ArrayTypeReference(CodeBuilder.CreateTypeReference(TypeSystemServices.ObjectType)) { Rank = Context.CodeBuilder.CreateIntegerLiteral(1) }; ReplaceCurrentNode(arrayReference); } }
private void FilterGenericTypes(Set <IEntity> types, SimpleTypeReference node) { bool genericRequested = (node is GenericTypeReference || node is GenericTypeDefinitionReference); if (genericRequested) { types.RemoveAll(IsNotGenericType); } else { types.RemoveAll(IsGenericType); } }
public override void LeaveMethod(Method node) { TypeReference returnType = node.ReturnType; if (returnType is SimpleTypeReference) { SimpleTypeReference simpleTypeReference = (SimpleTypeReference)returnType; if (simpleTypeReference.Name == "void") { node.ReturnType = null; } } }
public void ResolveSimpleTypeReference(SimpleTypeReference node) { if (null != node.Entity) { return; } IEntity entity = ResolveTypeName(node); if (null == entity) { node.Entity = NameNotType(node); return; } if (EntityType.Type != entity.EntityType) { if (EntityType.Ambiguous == entity.EntityType) { entity = AmbiguousReference(node, (Ambiguous)entity); } else { entity = NameNotType(node); } } else { #if NET_2_0 GenericTypeReference gtr = node as GenericTypeReference; if (null != gtr) { entity = MakeGenericType(gtr, (IType)entity); } GenericTypeDefinitionReference gtdr = node as GenericTypeDefinitionReference; if (null != gtdr) { IType type = (IType)entity; if (gtdr.GenericPlaceholders != type.GenericTypeDefinitionInfo.GenericParameters.Length) { entity = GenericArgumentsCountMismatch(gtdr, type); } } #endif node.Name = entity.FullName; } node.Entity = entity; }
private void WriteSimpleType(SimpleTypeReference simple, ReferenceLinkDisplayOptions options, bool showOuterType, XmlWriter writer) { TypeTarget type = simple.Resolve(_targets) as TypeTarget; if (type != null) { WriteTypeTarget(type, options, showOuterType, writer); } else { ReferenceTextUtilities.WriteSimpleTypeReference(simple, options, writer); } }
private static Specialization CreateSpecialization(XPathNavigator node) { SimpleTypeReference template = CreateSimpleTypeReference(node); List <TypeReference> arguments = new List <TypeReference>(); XPathNodeIterator specializationNodes = node.Select("specialization/*"); foreach (XPathNavigator specializationNode in specializationNodes) { arguments.Add(CreateTypeReference(specializationNode)); } Specialization specialization = new Specialization(template, arguments); return(specialization); }
private IEntity ResolveTypeName(SimpleTypeReference node) { _buffer.Clear(); if (IsQualifiedName(node.Name)) { ResolveQualifiedName(_buffer, node.Name); } else { Resolve(_buffer, node.Name, EntityType.Type); } // Remove from the buffer types that do not match requested generity FilterGenericTypes(_buffer, node); return(GetEntityFromBuffer()); }
internal IEntity ResolveTypeName(SimpleTypeReference node) { var resolved = ResolveQualifiedName(node.Name, EntityType.Type); if (resolved == null) { return(null); } if (EntityType.Ambiguous == resolved.EntityType) { // Remove from the buffer types that do not match requested generity var resultingSet = new Set <IEntity>(((Ambiguous)resolved).Entities); FilterGenericTypes(resultingSet, node); return(Entities.EntityFromList(resultingSet)); } return(resolved); }
protected override void OnReadXml(XmlReader reader) { base.OnReadXml(reader); // This reads only the target node... if (!String.Equals(reader.Name, "MemberTarget", StringComparison.OrdinalIgnoreCase)) { return; } name = reader.GetAttribute("name"); overload = reader.GetAttribute("overload"); XmlNodeType nodeType = XmlNodeType.None; while (reader.Read()) { nodeType = reader.NodeType; if (nodeType == XmlNodeType.Element) { // Read the base contents in... if (String.Equals(reader.Name, "Target", StringComparison.OrdinalIgnoreCase)) { base.OnReadXml(reader); } else if (String.Equals(reader.Name, "SimpleTypeReference", StringComparison.OrdinalIgnoreCase)) { containingType = new SimpleTypeReference(); containingType.ReadXml(reader); } } else if (nodeType == XmlNodeType.EndElement) { if (String.Equals(reader.Name, "MemberTarget", StringComparison.OrdinalIgnoreCase)) { break; } } } }
internal void Ensure(CompilerContext context, Method method) { if (method.Parameters.Count != this.Parameters.Count) { context.Errors.Add(CompilerErrorFactory.MethodArgumentCount(method, method.Name, this.Parameters.Count)); return; } //ensure return type if (method.ReturnType == null) { method.ReturnType = new SimpleTypeReference(ReturnType.FullName); } //ensure parameter types for (var i = 0; i < this.Parameters.Count; i++) { var paramtTypeRef = method.Parameters[i].Type; if (paramtTypeRef == null) { var ensuredType = this.Parameters[i].Type; TypeReference typeRef = null; if (ensuredType.IsGenericType) { var args = new SimpleTypeReference(ensuredType.GenericTypeArguments[0].Name); var name = ensuredType.Name; var tick = name.IndexOf('`'); name = name.Substring(0, tick); typeRef = new GenericTypeReference(name, args); } else { typeRef = new SimpleTypeReference(ensuredType.FullName); } method.Parameters[i].Type = typeRef; } } }
internal static void WriteSimpleMemberReference(SimpleMemberReference member, ReferenceLinkDisplayOptions options, XmlWriter writer, ReferenceLinkTextResolver resolver) { string cer = member.Id; string typeCer, memberName, arguments; DecomposeMemberIdentifier(cer, out typeCer, out memberName, out arguments); if ((options & ReferenceLinkDisplayOptions.ShowContainer) > 0) { SimpleTypeReference type = CreateSimpleTypeReference(typeCer); WriteSimpleTypeReference(type, options & ~ReferenceLinkDisplayOptions.ShowContainer, writer); } // change this so that we deal with EII names correctly, too writer.WriteString(memberName); if ((options & ReferenceLinkDisplayOptions.ShowParameters) > 0) { if (String.IsNullOrEmpty(arguments)) { Parameter[] parameters = new Parameter[0]; resolver.WriteMethodParameters(parameters, writer); } else { IList <string> parameterTypeCers = SeparateTypes(arguments); Parameter[] parameters = new Parameter[parameterTypeCers.Count]; for (int i = 0; i < parameterTypeCers.Count; i++) { TypeReference parameterType = CreateTypeReference(parameterTypeCers[i]); if (parameterType == null) { parameterType = new NamedTemplateTypeReference("UAT"); } parameters[i] = new Parameter(String.Empty, parameterType); } resolver.WriteMethodParameters(parameters, writer); } } }
internal static void WriteSimpleTypeReference(SimpleTypeReference type, ReferenceLinkDisplayOptions options, XmlWriter writer) { // this logic won't correctly deal with nested types, but type cer strings simply don't include that // information, so this is out best guess under the assumption of a non-nested type string cer = type.Id; // get the name string name; int lastDotPosition = cer.LastIndexOf('.'); if (lastDotPosition > 0) { // usually, the name will start after the last dot name = cer.Substring(lastDotPosition + 1); } else { // but if there is no dot, this is a type in the default namespace and the name is everything after the colon name = cer.Substring(2); } // remove any generic tics from the name int tickPosition = name.IndexOf('`'); if (tickPosition > 0) { name = name.Substring(0, tickPosition); } if ((options & ReferenceLinkDisplayOptions.ShowContainer) > 0) { // work out namespace } writer.WriteString(name); if ((options & ReferenceLinkDisplayOptions.ShowTemplates) > 0) { // work out templates } }
public TypeReference CreateTypeReference(IType tag) { TypeReference typeReference = null; if (tag.IsArray) { IType elementType = ((IArrayType)tag).GetElementType(); //typeReference = new ArrayTypeReference(); //((ArrayTypeReference)typeReference).ElementType = CreateTypeReference(elementType); // FIXME: This is what it *should* be, but it causes major breakage. ?? typeReference = new ArrayTypeReference(CreateTypeReference(elementType), CreateIntegerLiteral(((IArrayType)tag).GetArrayRank())); } else { typeReference = new SimpleTypeReference(tag.FullName); } typeReference.Entity = tag; return(typeReference); }
public void TypeLookupThroughUsing() { _compilationUnit.UsingDirectives.Add(new UsingNamespaceDirective("System")); var type = new SimpleTypeReference("Math"); var result = type.Resolve(_compilationUnit.GetScope()); Assert.IsInstanceOfType(result, typeof(MemberResolveResult)); Assert.IsInstanceOfType(result.ScopeProvider, typeof(TypeDefinition)); Assert.AreEqual("System.Math", ((TypeDefinition)result.ScopeProvider).FullName); }
public void VisitSimpleTypeReference(SimpleTypeReference typeReference) { Formatter.StartNode(typeReference); typeReference.Identifier.AcceptVisitor(this); WriteTypeParametersOrArguments(typeReference.TypeArguments); Formatter.EndNode(); }