Esempio n. 1
0
        IAttribute ConvertAttribute(CSharp.Attribute attr)
        {
            DomRegion      region = MakeRegion(attr);
            ITypeReference type   = ConvertType(attr.Type);

            if (!attr.Type.GetChildByRole(AstNode.Roles.Identifier).IsVerbatim)
            {
                // Try to add "Attribute" suffix, but only if the identifier
                // (=last identifier in fully qualified name) isn't a verbatim identifier.
                SimpleTypeOrNamespaceReference st = type as SimpleTypeOrNamespaceReference;
                MemberTypeOrNamespaceReference mt = type as MemberTypeOrNamespaceReference;
                if (st != null)
                {
                    type = new AttributeTypeReference(st, st.AddSuffix("Attribute"));
                }
                else if (mt != null)
                {
                    type = new AttributeTypeReference(mt, mt.AddSuffix("Attribute"));
                }
            }
            List <IConstantValue> positionalArguments = null;
            List <KeyValuePair <string, IConstantValue> > namedCtorArguments = null;
            List <KeyValuePair <string, IConstantValue> > namedArguments     = null;

            foreach (Expression expr in attr.Arguments)
            {
                NamedArgumentExpression nae = expr as NamedArgumentExpression;
                if (nae != null)
                {
                    if (namedCtorArguments == null)
                    {
                        namedCtorArguments = new List <KeyValuePair <string, IConstantValue> >();
                    }
                    namedCtorArguments.Add(new KeyValuePair <string, IConstantValue>(nae.Identifier, ConvertAttributeArgument(nae.Expression)));
                }
                else
                {
                    AssignmentExpression ae = expr as AssignmentExpression;
                    if (ae != null && ae.Left is IdentifierExpression && ae.Operator == AssignmentOperatorType.Assign)
                    {
                        string name = ((IdentifierExpression)ae.Left).Identifier;
                        if (namedArguments == null)
                        {
                            namedArguments = new List <KeyValuePair <string, IConstantValue> >();
                        }
                        namedArguments.Add(new KeyValuePair <string, IConstantValue>(name, ConvertAttributeArgument(nae.Expression)));
                    }
                    else
                    {
                        if (positionalArguments == null)
                        {
                            positionalArguments = new List <IConstantValue>();
                        }
                        positionalArguments.Add(ConvertAttributeArgument(nae.Expression));
                    }
                }
            }
            return(new CSharpAttribute(type, region, positionalArguments, namedCtorArguments, namedArguments));
        }
        /// <remarks>Does not support type arguments!</remarks>
        public static void AddSimpleUsing(this UsingScope scope, string fullName)
        {
            if (scope == null)
            {
                throw new ArgumentNullException("scope");
            }
            string[] parts = fullName.Trim().Split('.');
            TypeOrNamespaceReference reference = null;

            foreach (var part in parts)
            {
                if (reference != null)
                {
                    reference = new MemberTypeOrNamespaceReference(reference, part, EmptyList <ITypeReference> .Instance);
                }
                else
                {
                    reference = new SimpleTypeOrNamespaceReference(part, EmptyList <ITypeReference> .Instance);
                }
            }

            scope.Usings.AddIfNotNull(reference);
        }
Esempio n. 3
0
        public AstType ConvertTypeReference(ITypeReference typeRef)
        {
            ArrayTypeReference array = typeRef as ArrayTypeReference;

            if (array != null)
            {
                return(ConvertTypeReference(array.ElementType).MakeArrayType(array.Dimensions));
            }
            PointerTypeReference pointer = typeRef as PointerTypeReference;

            if (pointer != null)
            {
                return(ConvertTypeReference(pointer.ElementType).MakePointerType());
            }
            ByReferenceType brt = typeRef as ByReferenceType;

            if (brt != null)
            {
                return(ConvertTypeReference(brt.ElementType));
            }

            IType type = typeRef.Resolve(context);

            if (type.Kind != TypeKind.Unknown)
            {
                return(ConvertType(type));
            }
            // Unknown type, let's try if we can find an appropriate type
            // (anything is better than displaying a question mark)
            KnownTypeReference knownType = typeRef as KnownTypeReference;

            if (knownType != null)
            {
                string keyword = ReflectionHelper.GetCSharpNameByTypeCode(knownType.TypeCode);
                if (keyword != null)
                {
                    return(new PrimitiveType(keyword));
                }
            }
            SimpleTypeOrNamespaceReference str = typeRef as SimpleTypeOrNamespaceReference;

            if (str != null)
            {
                return(new SimpleType(str.Identifier, str.TypeArguments.Select(ConvertTypeReference)));
            }
            MemberTypeOrNamespaceReference mtr = typeRef as MemberTypeOrNamespaceReference;

            if (mtr != null)
            {
                return(new MemberType(ConvertTypeReference(mtr.Target), mtr.Identifier, mtr.TypeArguments.Select(ConvertTypeReference))
                {
                    IsDoubleColon = mtr.Target is AliasNamespaceReference
                });
            }
            AliasNamespaceReference alias = typeRef as AliasNamespaceReference;

            if (alias != null)
            {
                return(new SimpleType(alias.Identifier));
            }
            // Unknown type reference that couldn't be resolved
            return(new SimpleType("?"));
        }
		/// <remarks>Does not support type arguments!</remarks>
		public static void AddSimpleUsing(this UsingScope scope, string fullName)
		{
			if (scope == null)
				throw new ArgumentNullException("scope");
			string[] parts = fullName.Trim().Split('.');
			TypeOrNamespaceReference reference = null;
			foreach (var part in parts) {
				if (reference != null) {
					reference = new MemberTypeOrNamespaceReference(reference, part, EmptyList<ITypeReference>.Instance);
				} else {
					reference = new SimpleTypeOrNamespaceReference(part, EmptyList<ITypeReference>.Instance);
				}
			}
			
			scope.Usings.AddIfNotNull(reference);
		}