Пример #1
0
        public static bool?ShouldGenericArgumentsBeIncluded(IMethod method, IAttributeStore attributeStore)
        {
            var iga = attributeStore.AttributesFor(method).GetAttribute <IncludeGenericArgumentsAttribute>();

            if (iga != null)
            {
                return(iga.Include);
            }
            var imp = attributeStore.AttributesFor(method.DeclaringTypeDefinition).GetAttribute <ImportedAttribute>();

            if (imp != null)
            {
                return(false);
            }
            var def = attributeStore.AttributesFor(method.ParentAssembly).GetAttribute <IncludeGenericArgumentsDefaultAttribute>();

            switch (def != null ? def.MethodDefault : GenericArgumentsDefault.IncludeExceptImported)
            {
            case GenericArgumentsDefault.IncludeExceptImported:
                return(true);

            case GenericArgumentsDefault.Ignore:
                return(false);

            case GenericArgumentsDefault.RequireExplicitSpecification:
                return(null);

            default:
                throw new ArgumentException("Invalid generic arguments default " + def.TypeDefault);
            }
        }
Пример #2
0
        private JsExpression CompileImportedTypeCheckCode(IType type, ref JsExpression @this, IRuntimeContext context, bool isTypeIs)
        {
            var def = type.GetDefinition();

            if (def == null)
            {
                return(null);
            }
            var ia = _attributeStore.AttributesFor(def).GetAttribute <ImportedAttribute>();

            if (ia == null || string.IsNullOrEmpty(ia.TypeCheckCode))
            {
                return(null);
            }

            // Can ignore errors here because they are caught by the metadata importer
            var method    = MetadataUtils.CreateTypeCheckMethod(type, _compilation);
            var tokens    = InlineCodeMethodCompiler.Tokenize(method, ia.TypeCheckCode, _ => {});
            int thisCount = tokens.Count(t => t.Type == InlineCodeToken.TokenType.This);

            if (!isTypeIs || thisCount > 0)
            {
                @this = context.EnsureCanBeEvaluatedMultipleTimes(@this, new JsExpression[0]);
            }
            return(JsExpression.LogicalAnd(
                       ReferenceNotEquals(@this, JsExpression.Null, context),
                       InlineCodeMethodCompiler.CompileExpressionInlineCodeMethodInvocation(method, tokens, @this, EmptyList <JsExpression> .Instance, n => { var t = ReflectionHelper.ParseReflectionName(n).Resolve(_compilation); return t.Kind == TypeKind.Unknown ? JsExpression.Null : InstantiateType(t, context); }, t => InstantiateTypeForUseAsTypeArgumentInInlineCode(t, context), _ => {})));
        }
Пример #3
0
		public static void MakeKnockoutProperty(IProperty property, IAttributeStore attributeStore) {
			var getter = attributeStore.AttributesFor(property.Getter);
			getter.ReplaceAttribute(new ScriptNameAttribute("{owner}"));
			getter.ReplaceAttribute(new DontGenerateAttribute());
			var setter = attributeStore.AttributesFor(property.Setter);
			setter.ReplaceAttribute(new ScriptNameAttribute("{owner}"));
			setter.ReplaceAttribute(new DontGenerateAttribute());
			var prop = attributeStore.AttributesFor(property);
			prop.ReplaceAttribute(new CustomInitializationAttribute("{$KnockoutApi.Knockout}.observable({value})"));
			prop.ReplaceAttribute(new BackingFieldNameAttribute("{owner}"));
		}
Пример #4
0
        private JsType ConvertType(JsClass type)
        {
            if (type.InstanceMethods.Any(m => m.Name == "runTests"))
            {
                _errorReporter.Region = type.CSharpTypeDefinition.Region;
                _errorReporter.Message(MessageSeverity.Error, 7019, string.Format("The type {0} cannot define a method named 'runTests' because it has a [TestFixtureAttribute].", type.CSharpTypeDefinition.FullName));
                return(type);
            }

            var instanceMethods = new List <JsMethod>();
            var tests           = new List <Tuple <string, string, bool, int?, JsFunctionDefinitionExpression> >();

            foreach (var method in type.InstanceMethods)
            {
                var testAttr = _attributeStore.AttributesFor(method.CSharpMember).GetAttribute <TestAttribute>();
                if (testAttr != null)
                {
                    if (!method.CSharpMember.IsPublic || !method.CSharpMember.ReturnType.IsKnownType(KnownTypeCode.Void) || ((IMethod)method.CSharpMember).Parameters.Count > 0 || ((IMethod)method.CSharpMember).TypeParameters.Count > 0)
                    {
                        _errorReporter.Region = method.CSharpMember.Region;
                        _errorReporter.Message(MessageSeverity.Error, 7020, string.Format("Method {0}: Methods decorated with a [TestAttribute] must be public, non-generic, parameterless instance methods that return void.", method.CSharpMember.FullName));
                    }

                    tests.Add(Tuple.Create(testAttr.Description ?? method.CSharpMember.Name, testAttr.Category, testAttr.IsAsync, testAttr.ExpectedAssertionCount >= 0 ? (int?)testAttr.ExpectedAssertionCount : null, method.Definition));
                }
                else
                {
                    instanceMethods.Add(method);
                }
            }

            var testInvocations = new List <JsExpression>();

            foreach (var category in tests.GroupBy(t => t.Item2).Select(g => new { Category = g.Key, Tests = g.Select(x => new { Description = x.Item1, IsAsync = x.Item3, ExpectedAssertionCount = x.Item4, Function = x.Item5 }) }).OrderBy(x => x.Category))
            {
                if (category.Category != null)
                {
                    testInvocations.Add(JsExpression.Invocation(JsExpression.Member(JsExpression.Identifier("QUnit"), "module"), JsExpression.String(category.Category)));
                }
                testInvocations.AddRange(category.Tests.Select(t => JsExpression.Invocation(JsExpression.Identifier(t.IsAsync ? "asyncTest" : "test"), t.ExpectedAssertionCount != null ? new JsExpression[] { JsExpression.String(t.Description), JsExpression.Number(t.ExpectedAssertionCount.Value), _runtimeLibrary.Bind(t.Function, JsExpression.This, this) } : new JsExpression[] { JsExpression.String(t.Description), _runtimeLibrary.Bind(t.Function, JsExpression.This, this) })));
            }

            instanceMethods.Add(new JsMethod(null, "runTests", null, JsExpression.FunctionDefinition(new string[0], JsStatement.Block(testInvocations.Select(t => (JsStatement)t)))));

            var result = type.Clone();

            result.InstanceMethods.Clear();
            foreach (var m in instanceMethods)
            {
                result.InstanceMethods.Add(m);
            }
            return(result);
        }
Пример #5
0
        public static void MakeKnockoutProperty(IProperty property, IAttributeStore attributeStore)
        {
            var getter = attributeStore.AttributesFor(property.Getter);

            getter.ReplaceAttribute(new ScriptNameAttribute("{owner}"));
            getter.ReplaceAttribute(new DontGenerateAttribute());
            var setter = attributeStore.AttributesFor(property.Setter);

            setter.ReplaceAttribute(new ScriptNameAttribute("{owner}"));
            setter.ReplaceAttribute(new DontGenerateAttribute());
            var prop = attributeStore.AttributesFor(property);

            prop.ReplaceAttribute(new CustomInitializationAttribute("{$KnockoutApi.Knockout}.observable({value})"));
            prop.ReplaceAttribute(new BackingFieldNameAttribute("{owner}"));
        }
		public override void ApplyTo(IAssembly assembly, IAttributeStore attributeStore, IErrorReporter errorReporter) {
			foreach (var t in assembly.GetAllTypeDefinitions()) {
				if (!attributeStore.AttributesFor(t).HasAttribute<DefaultMemberReflectabilityAttribute>()) {
					ApplyTo(t, attributeStore, errorReporter);
				}
			}
		}
		public MetadataImporter(IErrorReporter errorReporter, ICompilation compilation, IAttributeStore attributeStore, CompilerOptions options) {
			_errorReporter = errorReporter;
			_compilation = compilation;
			_attributeStore = attributeStore;
			_minimizeNames = options.MinimizeScript;
			_systemObject = compilation.MainAssembly.Compilation.FindType(KnownTypeCode.Object);
			_typeSemantics = new Dictionary<ITypeDefinition, TypeSemantics>();
			_delegateSemantics = new Dictionary<ITypeDefinition, DelegateScriptSemantics>();
			_instanceMemberNamesByType = new Dictionary<ITypeDefinition, HashSet<string>>();
			_staticMemberNamesByType = new Dictionary<ITypeDefinition, HashSet<string>>();
			_methodSemantics = new Dictionary<IMethod, MethodScriptSemantics>();
			_propertySemantics = new Dictionary<IProperty, PropertyScriptSemantics>();
			_fieldSemantics = new Dictionary<IField, FieldScriptSemantics>();
			_eventSemantics = new Dictionary<IEvent, EventScriptSemantics>();
			_constructorSemantics = new Dictionary<IMethod, ConstructorScriptSemantics>();
			_propertyBackingFieldNames = new Dictionary<IProperty, Tuple<string, bool>>();
			_eventBackingFieldNames = new Dictionary<IEvent, Tuple<string, bool>>();
			_backingFieldCountPerType = new Dictionary<ITypeDefinition, int>();
			_internalTypeCountPerAssemblyAndNamespace = new Dictionary<Tuple<IAssembly, string>, int>();
			_ignoredMembers = new HashSet<IMember>();

			var sna = _attributeStore.AttributesFor(compilation.MainAssembly).GetAttribute<ScriptNamespaceAttribute>();
			if (sna != null) {
				if (sna.Name == null || (sna.Name != "" && !sna.Name.IsValidNestedJavaScriptIdentifier())) {
					Message(Messages._7002, default(DomRegion), "assembly");
				}
			}
		}
Пример #8
0
        private JsExpression GetMetadataDescriptor(ITypeDefinition type, bool isGenericSpecialization)
        {
            var properties           = new List <JsObjectLiteralProperty>();
            var scriptableAttributes = MetadataUtils.GetScriptableAttributes(type.Attributes, _metadataImporter).ToList();

            if (scriptableAttributes.Count != 0)
            {
                properties.Add(new JsObjectLiteralProperty("attr", JsExpression.ArrayLiteral(scriptableAttributes.Select(a => MetadataUtils.ConstructAttribute(a, type, _compilation, _metadataImporter, _namer, _runtimeLibrary, _errorReporter)))));
            }
            if (type.Kind == TypeKind.Interface && MetadataUtils.IsJsGeneric(type, _metadataImporter) && type.TypeParameters != null && type.TypeParameters.Any(typeParameter => typeParameter.Variance != VarianceModifier.Invariant))
            {
                properties.Add(new JsObjectLiteralProperty("variance", JsExpression.ArrayLiteral(type.TypeParameters.Select(typeParameter => JsExpression.Number(ConvertVarianceToInt(typeParameter.Variance))))));
            }
            if (type.Kind == TypeKind.Class || type.Kind == TypeKind.Struct || type.Kind == TypeKind.Interface)
            {
                var members = type.Members.Where(m => MetadataUtils.IsReflectable(m, _attributeStore))
                              .OrderBy(m => m, MemberOrderer.Instance)
                              .Select(m => {
                    _errorReporter.Region = m.Region;
                    return(MetadataUtils.ConstructMemberInfo(m, _compilation, _metadataImporter, _namer, _runtimeLibrary, _errorReporter, t => _runtimeLibrary.InstantiateType(t, isGenericSpecialization ? _genericSpecializationReflectionRuntimeContext : _defaultReflectionRuntimeContext), includeDeclaringType: false));
                })
                              .ToList();
                if (members.Count > 0)
                {
                    properties.Add(new JsObjectLiteralProperty("members", JsExpression.ArrayLiteral(members)));
                }

                var aua = _attributeStore.AttributesFor(type).GetAttribute <AttributeUsageAttribute>();
                if (aua != null)
                {
                    if (!aua.Inherited)
                    {
                        properties.Add(new JsObjectLiteralProperty("attrNoInherit", JsExpression.True));
                    }
                    if (aua.AllowMultiple)
                    {
                        properties.Add(new JsObjectLiteralProperty("attrAllowMultiple", JsExpression.True));
                    }
                }
            }
            if (type.Kind == TypeKind.Enum && _attributeStore.AttributesFor(type).HasAttribute <FlagsAttribute>())
            {
                properties.Add(new JsObjectLiteralProperty("enumFlags", JsExpression.True));
            }

            return(properties.Count > 0 ? JsExpression.ObjectLiteral(properties) : null);
        }
Пример #9
0
 public override void ApplyTo(IAssembly assembly, IAttributeStore attributeStore, IErrorReporter errorReporter)
 {
     foreach (var t in assembly.GetAllTypeDefinitions())
     {
         if (!attributeStore.AttributesFor(t).HasAttribute <DefaultMemberReflectabilityAttribute>())
         {
             ApplyTo(t, attributeStore, errorReporter);
         }
     }
 }
Пример #10
0
        private void ProcessMember(IMember member)
        {
            var attributes = _attributeStore.AttributesFor(member);

            if (!attributes.HasAttribute <ReflectableAttribute>())
            {
                if (member.Attributes.Any(a => a.AttributeType.Kind == TypeKind.Class && !_attributeStore.AttributesFor((IEntity)a.AttributeType.GetDefinition()).HasAttribute <NonScriptableAttribute>()))
                {
                    attributes.Add(new ReflectableAttribute(true));
                }
            }
        }
Пример #11
0
 public static string GetModuleName(ITypeDefinition type, IAttributeStore attributeStore)
 {
     for (var current = type; current != null; current = current.DeclaringTypeDefinition)
     {
         var mna = attributeStore.AttributesFor(type).GetAttribute <ModuleNameAttribute>();
         if (mna != null)
         {
             return(!String.IsNullOrEmpty(mna.ModuleName) ? mna.ModuleName : null);
         }
     }
     return(GetModuleName(type.ParentAssembly, attributeStore));
 }
		public override void ApplyTo(IEntity entity, IAttributeStore attributeStore, IErrorReporter errorReporter) {
			var type = entity as ITypeDefinition;
			if (type == null)
				return;

			foreach (var m in type.Members) {
				var attributes = attributeStore.AttributesFor(m);
				if (!attributes.HasAttribute<ReflectableAttribute>()) {
					if (IsMemberReflectable(m)) {
						attributes.Add(new ReflectableAttribute(true));
					}
				}
			}
		}
Пример #13
0
		public override void ApplyTo(IEntity entity, IAttributeStore attributeStore, IErrorReporter errorReporter) {
			foreach (var p in ((ITypeDefinition)entity).GetProperties(options: GetMemberOptions.IgnoreInheritedMembers)) {
				if (attributeStore.AttributesFor(p).HasAttribute<KnockoutPropertyAttribute>())
					continue;
				if (p.IsStatic)
					continue;

				if (MetadataUtils.IsAutoProperty(p) == false) {
					errorReporter.Message(MessageSeverity.Error, 8001, "The property {0} is not an auto-property so because its containing type has a [KnockoutModelAttribute], the property must be decorated with [KnockoutPropertyAttribute(false)]", p.FullName);
					continue;
				}

				KnockoutPropertyAttribute.MakeKnockoutProperty(p, attributeStore);
			}
		}
Пример #14
0
        public override void ApplyTo(IEntity entity, IAttributeStore attributeStore, IErrorReporter errorReporter)
        {
            var type = entity as ITypeDefinition;

            if (type == null)
            {
                return;
            }

            foreach (var m in type.Members)
            {
                var attributes = attributeStore.AttributesFor(m);
                if (!attributes.HasAttribute <ReflectableAttribute>())
                {
                    if (IsMemberReflectable(m))
                    {
                        attributes.Add(new ReflectableAttribute(true));
                    }
                }
            }
        }
Пример #15
0
        public override void ApplyTo(IEntity entity, IAttributeStore attributeStore, IErrorReporter errorReporter)
        {
            foreach (var p in ((ITypeDefinition)entity).GetProperties(options: GetMemberOptions.IgnoreInheritedMembers))
            {
                if (attributeStore.AttributesFor(p).HasAttribute <KnockoutPropertyAttribute>())
                {
                    continue;
                }
                if (p.IsStatic)
                {
                    continue;
                }

                if (MetadataUtils.IsAutoProperty(p) == false)
                {
                    errorReporter.Message(MessageSeverity.Error, 8001, "The property {0} is not an auto-property so because its containing type has a [KnockoutModelAttribute], the property must be decorated with [KnockoutPropertyAttribute(false)]", p.FullName);
                    continue;
                }

                KnockoutPropertyAttribute.MakeKnockoutProperty(p, attributeStore);
            }
        }
		public static bool CanBeMinimized(IMember member, IAttributeStore attributeStore) {
			return !member.IsExternallyVisible() || attributeStore.AttributesFor(member.ParentAssembly).HasAttribute<MinimizePublicNamesAttribute>();
		}
Пример #17
0
 public static IEnumerable <KeyValuePair <string, string> > GetAdditionalDependencies(IAssembly assembly, IAttributeStore attributeStore)
 {
     return(attributeStore.AttributesFor(assembly).GetAttributes <AdditionalDependencyAttribute>()
            .Select(a => new KeyValuePair <string, string>(a.ModuleName, a.InstanceName)));
 }
Пример #18
0
        public static bool OmitDowncasts(ICompilation compilation, IAttributeStore attributeStore)
        {
            var sca = attributeStore.AttributesFor(compilation.MainAssembly).GetAttribute <ScriptSharpCompatibilityAttribute>();

            return(sca != null && sca.OmitDowncasts);
        }
Пример #19
0
 public static bool IsGlobalMethods(ITypeDefinition type, IAttributeStore attributeStore)
 {
     return(attributeStore.AttributesFor(type).HasAttribute <GlobalMethodsAttribute>());
 }
Пример #20
0
 public static bool IsResources(ITypeDefinition type, IAttributeStore attributeStore)
 {
     return(attributeStore.AttributesFor(type).HasAttribute <ResourcesAttribute>());
 }
Пример #21
0
 public static bool IsMixin(ITypeDefinition type, IAttributeStore attributeStore)
 {
     return(attributeStore.AttributesFor(type).HasAttribute <MixinAttribute>());
 }
Пример #22
0
 public static bool CanBeMinimized(IMember member, IAttributeStore attributeStore)
 {
     return(!member.IsExternallyVisible() || attributeStore.AttributesFor(member.ParentAssembly).HasAttribute <MinimizePublicNamesAttribute>());
 }
		public static bool IsMixin(ITypeDefinition type, IAttributeStore attributeStore) {
			return attributeStore.AttributesFor(type).HasAttribute<MixinAttribute>();
		}
		public static bool DoesTypeObeyTypeSystem(ITypeDefinition type, IAttributeStore attributeStore) {
			var ia = attributeStore.AttributesFor(type).GetAttribute<ImportedAttribute>();
			return ia == null || ia.ObeysTypeSystem;
		}
		public static string GetSerializableTypeCheckCode(ITypeDefinition type, IAttributeStore attributeStore) {
			var attr = attributeStore.AttributesFor(type).GetAttribute<ScriptSerializableAttribute>();
			return attr != null ? attr.TypeCheckCode : null;
		}
		public static bool IsSerializable(ITypeDefinition type, IAttributeStore attributeStore) {
			return attributeStore.AttributesFor(type).HasAttribute<ScriptSerializableAttribute>() || (type.GetAllBaseTypeDefinitions().Any(td => td.FullName == "System.Record") && type.FullName != "System.Record");
		}
		public static bool IsReflectable(IMember member, IAttributeStore attributeStore) {
			var ra = attributeStore.AttributesFor(member).GetAttribute<ReflectableAttribute>();
			return ra != null && ra.Reflectable;
		}
		/// <summary>
		/// Determines the preferred name for a member. The first item is the name, the second item is true if the name was explicitly specified.
		/// </summary>
		public static Tuple<string, bool> DeterminePreferredMemberName(IMember member, bool minimizeNames, IAttributeStore attributeStore) {
			member = UnwrapValueTypeConstructor(member);

			bool isConstructor = member is IMethod && ((IMethod)member).IsConstructor;
			bool isAccessor = member is IMethod && ((IMethod)member).IsAccessor;
			bool isPreserveMemberCase = IsPreserveMemberCase(member.DeclaringTypeDefinition, attributeStore);

			string defaultName;
			if (isConstructor) {
				defaultName = "$ctor";
			}
			else if (!CanBeMinimized(member, attributeStore)) {
				defaultName = isPreserveMemberCase ? member.Name : MakeCamelCase(member.Name);
			}
			else {
				if (minimizeNames && member.DeclaringType.Kind != TypeKind.Interface)
					defaultName = null;
				else
					defaultName = "$" + (isPreserveMemberCase ? member.Name : MakeCamelCase(member.Name));
			}

			var attributes = attributeStore.AttributesFor(member);

			var asa = attributes.GetAttribute<AlternateSignatureAttribute>();
			if (asa != null) {
				var otherMembers = member.DeclaringTypeDefinition.Methods.Where(m => m.Name == member.Name && !attributeStore.AttributesFor(m).HasAttribute<AlternateSignatureAttribute>() && !attributeStore.AttributesFor(m).HasAttribute<NonScriptableAttribute>() && !attributeStore.AttributesFor(m).HasAttribute<InlineCodeAttribute>()).ToList();
				if (otherMembers.Count == 1) {
					return DeterminePreferredMemberName(otherMembers[0], minimizeNames, attributeStore);
				}
				else {
					return Tuple.Create(member.Name, false);	// Error
				}
			}

			var sna = attributes.GetAttribute<ScriptNameAttribute>();
			if (sna != null) {
				string name = sna.Name;
				if (IsNamedValues(member.DeclaringTypeDefinition, attributeStore) && (name == "" || !name.IsValidJavaScriptIdentifier())) {
					return Tuple.Create(defaultName, false);	// For named values enum, allow the use to specify an empty or invalid value, which will only be used as the literal value for the field, not for the name.
				}
				if (name == "" && isConstructor)
					name = "$ctor";
				return Tuple.Create(name, true);
			}
			
			if (isConstructor && IsImported(member.DeclaringTypeDefinition, attributeStore)) {
				return Tuple.Create("$ctor", true);
			}

			var ica = attributes.GetAttribute<InlineCodeAttribute>();
			if (ica != null) {
				if (ica.GeneratedMethodName != null)
					return Tuple.Create(ica.GeneratedMethodName, true);
			}

			if (attributes.HasAttribute<PreserveCaseAttribute>())
				return Tuple.Create(member.Name, true);

			bool preserveName = (!isConstructor && !isAccessor && (   attributes.HasAttribute<PreserveNameAttribute>()
			                                                       || attributes.HasAttribute<InstanceMethodOnFirstArgumentAttribute>()
			                                                       || IsPreserveMemberNames(member.DeclaringTypeDefinition, attributeStore) && member.ImplementedInterfaceMembers.Count == 0 && !member.IsOverride)
			                                                       || (IsSerializable(member.DeclaringTypeDefinition, attributeStore) && !member.IsStatic && (member is IProperty || member is IField)))
			                                                       || (IsNamedValues(member.DeclaringTypeDefinition, attributeStore) && member is IField);

			if (preserveName)
				return Tuple.Create(isPreserveMemberCase ? member.Name : MakeCamelCase(member.Name), true);

			return Tuple.Create(defaultName, false);
		}
		public static bool OmitDowncasts(ICompilation compilation, IAttributeStore attributeStore) {
			var sca = attributeStore.AttributesFor(compilation.MainAssembly).GetAttribute<ScriptSharpCompatibilityAttribute>();
			return sca != null && sca.OmitDowncasts;
		}
Пример #30
0
        public static bool IsReflectable(IMember member, IAttributeStore attributeStore)
        {
            var ra = attributeStore.AttributesFor(member).GetAttribute <ReflectableAttribute>();

            return(ra != null && ra.Reflectable);
        }
Пример #31
0
        public static string GetSerializableTypeCheckCode(ITypeDefinition type, IAttributeStore attributeStore)
        {
            var attr = attributeStore.AttributesFor(type).GetAttribute <ScriptSerializableAttribute>();

            return(attr != null ? attr.TypeCheckCode : null);
        }
Пример #32
0
 public static bool IsImported(ITypeDefinition type, IAttributeStore attributeStore)
 {
     return(attributeStore.AttributesFor(type).HasAttribute <ImportedAttribute>());
 }
		public static string GetModuleName(ITypeDefinition type, IAttributeStore attributeStore) {
			for (var current = type; current != null; current = current.DeclaringTypeDefinition) {
				var mna = attributeStore.AttributesFor(type).GetAttribute<ModuleNameAttribute>();
				if (mna != null)
					return !String.IsNullOrEmpty(mna.ModuleName) ? mna.ModuleName : null;
			}
			return GetModuleName(type.ParentAssembly, attributeStore);
		}
Пример #34
0
 public static bool IsNamedValues(ITypeDefinition type, IAttributeStore attributeStore)
 {
     return(attributeStore.AttributesFor(type).HasAttribute <NamedValuesAttribute>());
 }
		public static bool IsPreserveMemberCase(ITypeDefinition type, IAttributeStore attributeStore) {
			var pmca = attributeStore.AttributesFor(type).GetAttribute<PreserveMemberCaseAttribute>() ?? attributeStore.AttributesFor(type.ParentAssembly).GetAttribute<PreserveMemberCaseAttribute>();
			return pmca != null && pmca.Preserve;
		}
Пример #36
0
        public static bool IsPreserveMemberCase(ITypeDefinition type, IAttributeStore attributeStore)
        {
            var pmca = attributeStore.AttributesFor(type).GetAttribute <PreserveMemberCaseAttribute>() ?? attributeStore.AttributesFor(type.ParentAssembly).GetAttribute <PreserveMemberCaseAttribute>();

            return(pmca != null && pmca.Preserve);
        }
		public static string GetModuleName(IAssembly assembly, IAttributeStore attributeStore) {
			var mna = attributeStore.AttributesFor(assembly).GetAttribute<ModuleNameAttribute>();
			return (mna != null && !String.IsNullOrEmpty(mna.ModuleName) ? mna.ModuleName : null);
		}
Пример #38
0
 public static bool IsAsyncModule(IAssembly assembly, IAttributeStore attributeStore)
 {
     return(attributeStore.AttributesFor(assembly).HasAttribute <AsyncModuleAttribute>());
 }
		public static IEnumerable<KeyValuePair<string,string>> GetAdditionalDependencies(IAssembly assembly, IAttributeStore attributeStore)
		{
			return attributeStore.AttributesFor(assembly).GetAttributes<AdditionalDependencyAttribute>()
				.Select(a => new KeyValuePair<string,string>(a.ModuleName, a.InstanceName));
		}
Пример #40
0
        public static string GetModuleName(IAssembly assembly, IAttributeStore attributeStore)
        {
            var mna = attributeStore.AttributesFor(assembly).GetAttribute <ModuleNameAttribute>();

            return(mna != null && !String.IsNullOrEmpty(mna.ModuleName) ? mna.ModuleName : null);
        }
		public static bool IsAsyncModule(IAssembly assembly, IAttributeStore attributeStore) {
			return attributeStore.AttributesFor(assembly).HasAttribute<AsyncModuleAttribute>();
		}
		public static bool IsImported(ITypeDefinition type, IAttributeStore attributeStore) {
			return attributeStore.AttributesFor(type).HasAttribute<ImportedAttribute>();
		}
		public static bool? ShouldGenericArgumentsBeIncluded(IMethod method, IAttributeStore attributeStore) {
			var iga = attributeStore.AttributesFor(method).GetAttribute<IncludeGenericArgumentsAttribute>();
			if (iga != null)
				return iga.Include;
			var imp = attributeStore.AttributesFor(method.DeclaringTypeDefinition).GetAttribute<ImportedAttribute>();
			if (imp != null)
				return false;
			var def = attributeStore.AttributesFor(method.ParentAssembly).GetAttribute<IncludeGenericArgumentsDefaultAttribute>();
			switch (def != null ? def.MethodDefault : GenericArgumentsDefault.IncludeExceptImported) {
				case GenericArgumentsDefault.IncludeExceptImported:
					return true;
				case GenericArgumentsDefault.Ignore:
					return false;
				case GenericArgumentsDefault.RequireExplicitSpecification:
					return null;
				default:
					throw new ArgumentException("Invalid generic arguments default " + def.TypeDefault);
			}
		}
Пример #44
0
        /// <summary>
        /// Determines the preferred name for a member. The first item is the name, the second item is true if the name was explicitly specified.
        /// </summary>
        public static Tuple <string, bool> DeterminePreferredMemberName(IMember member, bool minimizeNames, IAttributeStore attributeStore)
        {
            member = UnwrapValueTypeConstructor(member);

            bool isConstructor        = member is IMethod && ((IMethod)member).IsConstructor;
            bool isAccessor           = member is IMethod && ((IMethod)member).IsAccessor;
            bool isPreserveMemberCase = IsPreserveMemberCase(member.DeclaringTypeDefinition, attributeStore);

            string defaultName;

            if (isConstructor)
            {
                defaultName = "$ctor";
            }
            else if (!CanBeMinimized(member, attributeStore))
            {
                defaultName = isPreserveMemberCase ? member.Name : MakeCamelCase(member.Name);
            }
            else
            {
                if (minimizeNames && member.DeclaringType.Kind != TypeKind.Interface)
                {
                    defaultName = null;
                }
                else
                {
                    defaultName = "$" + (isPreserveMemberCase ? member.Name : MakeCamelCase(member.Name));
                }
            }

            var attributes = attributeStore.AttributesFor(member);

            var asa = attributes.GetAttribute <AlternateSignatureAttribute>();

            if (asa != null)
            {
                var otherMembers = member.DeclaringTypeDefinition.Methods.Where(m => m.Name == member.Name && !attributeStore.AttributesFor(m).HasAttribute <AlternateSignatureAttribute>() && !attributeStore.AttributesFor(m).HasAttribute <NonScriptableAttribute>() && !attributeStore.AttributesFor(m).HasAttribute <InlineCodeAttribute>()).ToList();
                if (otherMembers.Count == 1)
                {
                    return(DeterminePreferredMemberName(otherMembers[0], minimizeNames, attributeStore));
                }
                else
                {
                    return(Tuple.Create(member.Name, false));                           // Error
                }
            }

            var sna = attributes.GetAttribute <ScriptNameAttribute>();

            if (sna != null)
            {
                string name = sna.Name;
                if (IsNamedValues(member.DeclaringTypeDefinition, attributeStore) && (name == "" || !name.IsValidJavaScriptIdentifier()))
                {
                    return(Tuple.Create(defaultName, false));                           // For named values enum, allow the use to specify an empty or invalid value, which will only be used as the literal value for the field, not for the name.
                }
                if (name == "" && isConstructor)
                {
                    name = "$ctor";
                }
                return(Tuple.Create(name, true));
            }

            if (isConstructor && IsImported(member.DeclaringTypeDefinition, attributeStore))
            {
                return(Tuple.Create("$ctor", true));
            }

            var ica = attributes.GetAttribute <InlineCodeAttribute>();

            if (ica != null)
            {
                if (ica.GeneratedMethodName != null)
                {
                    return(Tuple.Create(ica.GeneratedMethodName, true));
                }
            }

            if (attributes.HasAttribute <PreserveCaseAttribute>())
            {
                return(Tuple.Create(member.Name, true));
            }

            bool preserveName = (!isConstructor && !isAccessor && (attributes.HasAttribute <PreserveNameAttribute>() ||
                                                                   attributes.HasAttribute <InstanceMethodOnFirstArgumentAttribute>() ||
                                                                   IsPreserveMemberNames(member.DeclaringTypeDefinition, attributeStore) && member.ImplementedInterfaceMembers.Count == 0 && !member.IsOverride) ||
                                 (IsSerializable(member.DeclaringTypeDefinition, attributeStore) && !member.IsStatic && (member is IProperty || member is IField))) ||
                                (IsNamedValues(member.DeclaringTypeDefinition, attributeStore) && member is IField);

            if (preserveName)
            {
                return(Tuple.Create(isPreserveMemberCase ? member.Name : MakeCamelCase(member.Name), true));
            }

            return(Tuple.Create(defaultName, false));
        }
		public static bool IsResources(ITypeDefinition type, IAttributeStore attributeStore) {
			return attributeStore.AttributesFor(type).HasAttribute<ResourcesAttribute>();
		}
Пример #46
0
 public static bool IsSerializable(ITypeDefinition type, IAttributeStore attributeStore)
 {
     return(attributeStore.AttributesFor(type).HasAttribute <ScriptSerializableAttribute>() || (type.GetAllBaseTypeDefinitions().Any(td => td.FullName == "System.Record") && type.FullName != "System.Record"));
 }
		public static bool IsNamedValues(ITypeDefinition type, IAttributeStore attributeStore) {
			return attributeStore.AttributesFor(type).HasAttribute<NamedValuesAttribute>();
		}
Пример #48
0
        public static bool DoesTypeObeyTypeSystem(ITypeDefinition type, IAttributeStore attributeStore)
        {
            var ia = attributeStore.AttributesFor(type).GetAttribute <ImportedAttribute>();

            return(ia == null || ia.ObeysTypeSystem);
        }
		public static bool IsGlobalMethods(ITypeDefinition type, IAttributeStore attributeStore) {
			return attributeStore.AttributesFor(type).HasAttribute<GlobalMethodsAttribute>();
		}