Пример #1
0
		private static TypeReference LiftGenericTypeDefinition(TypeDefinition node)
		{
			var typeRef = new GenericTypeReference(node.LexicalInfo, node.QualifiedName);
			foreach (GenericParameterDeclaration parameter in node.GenericParameters)
				typeRef.GenericArguments.Add(Lift(parameter.Name));
			return typeRef;
		}
Пример #2
0
        private void EnterTypeDefinition(AST.TypeDefinition node, ClassType classType)
        {
            //LoggingService.Debug("Enter " + node.GetType().Name + " (" + node.FullName + ")");
            foreach (AST.Attribute att in node.Attributes)
            {
                if (att.Name == "Boo.Lang.ModuleAttribute")
                {
                    classType = ClassType.Module;
                }
            }
            DomRegion    region = GetClientRegion(node);
            DefaultClass c      = new DefaultClass(_cu, classType, GetModifier(node), region, OuterClass);

            c.FullyQualifiedName = node.FullName;
            if (_currentClass.Count > 0)
            {
                _currentClass.Peek().InnerClasses.Add(c);
            }
            else
            {
                _cu.Classes.Add(c);
            }
            _currentClass.Push(c);
            ConvertAttributes(node, c);
            ConvertTemplates(node, c);
            if (node.BaseTypes != null)
            {
                foreach (AST.TypeReference r in node.BaseTypes)
                {
                    c.BaseTypes.Add(CreateReturnType(r));
                }
            }
        }
		void LeaveTypeDefinition(TypeDefinition node)
		{
			if (!node.IsVisibilitySet)
				node.Modifiers |= Context.Parameters.DefaultTypeVisibility;

			node.Name = NormalizeName(node.Name);
		}
Пример #4
0
		private void OnCandidatePartialDefinition(TypeDefinition node)
		{
			if (!node.IsPartial)
				return;

			var typeName = node.FullName;

			TypeDefinition originalDefinition;
			if (_partials.TryGetValue(typeName, out originalDefinition))
			{
				if (node == originalDefinition) // MergePartialTypes can be executed more than once
					return;

				if (originalDefinition.NodeType != node.NodeType)
				{
					Errors.Add(CompilerErrorFactory.IncompatiblePartialDefinition(node, typeName, AstUtil.TypeKeywordFor(originalDefinition), AstUtil.TypeKeywordFor(node)));
					return;
				}

				MergeImports(node, originalDefinition);
				originalDefinition.Merge(node);
				RemoveCurrentNode();
			}
			else
				_partials[typeName] = node;
		}
Пример #5
0
        //use this to build a type member name unique in the inheritance hierarchy.
        public static string BuildUniqueTypeMemberName(TypeDefinition type, string name)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException("name");

            StringBuilder nameBuilder = new StringBuilder("$");
            nameBuilder.Append(name);
            nameBuilder.Append("__");
            nameBuilder.Append(type.QualifiedName);
            if (type.HasGenericParameters)
            {
                nameBuilder.Append("_");
                string[] parameterNames = Array.ConvertAll<GenericParameterDeclaration, string>(
                    type.GenericParameters.ToArray(),
                    delegate(GenericParameterDeclaration gpd) { return gpd.Name; });
                foreach (string parameterName in parameterNames)
                {
                    nameBuilder.Append("_");
                    nameBuilder.Append(parameterName);
                }
            }
            nameBuilder.Replace('.', '_');
            nameBuilder.Append("$");
            return nameBuilder.ToString();
        }
		void EnterTypeDefinition(TypeDefinition node)
		{
			List<Field> list = new List<Field>();
			fieldStack.Push(list);
			foreach (TypeMember member in node.Members) {
				if (member is Field)
					list.Add((Field)member);
			}
		}
Пример #7
0
        public static void BindMembers(string cacheName,TypeDefinition type){
            if (!cache.ContainsKey(cacheName)) return;
            foreach (var member in cache[cacheName]){ 
                type.Members.Add(member);
            }
            cache.Remove(cacheName);

            new Method().Parameters.Add(new ParameterDeclaration("name",new SimpleTypeReference("string")));
        }
Пример #8
0
		private void OnTypeDefinition(TypeDefinition node)
		{
			var old = _currentType;
			_currentType = (IType)node.Entity;
			Visit(node.Attributes);
			Visit(node.BaseTypes);
			Visit(node.Members);
			Visit(node.GenericParameters);
			_currentType = old;
		}
 protected override void ExtendBaseClass(TypeDefinition definition)
 {
     Property property = new Property("Environment");
     property.Getter = new Method("getter_Environment");
     property.Getter.Body.Add(
         new ReturnStatement(
             new StringLiteralExpression(environment ?? "")
             )
         );
     definition.Members.Add(property);
 }
Пример #10
0
        public InternalGenericParameter(TypeSystemServices tss, GenericParameterDeclaration declaration)
        {
            _tss = tss;
            _declaration = declaration;

            // Determine and remember declaring type and declaring method (if applicable)
            _declaringMethod = declaration.ParentNode as Method;
            _declaringType = (
                _declaringMethod == null ?
                declaration.ParentNode as TypeDefinition : _declaringMethod.DeclaringType);
        }
Пример #11
0
		void CheckMembers(TypeDefinition node)
		{
			_members.Clear();
			
			foreach (TypeMember member in node.Members)
			{
				if (member.NodeType == NodeType.StatementTypeMember)
					continue;

				var list = GetMemberList(member.Name);
				CheckMember(list, member);
				list.Add(member);
			}
		}
Пример #12
0
        static void MergeImports(TypeDefinition from, TypeDefinition to)
        {
            Module fromModule = from.EnclosingModule;
            Module toModule = to.EnclosingModule;
            if (fromModule == toModule) return;
            if (toModule.ContainsAnnotation(fromModule)) return;

            toModule.Imports.ExtendWithClones(fromModule.Imports.Where(i => !toModule.Imports.Any(i2 => i.Matches(i2))));
            // annotate so we remember not to merge the imports
            // again in the future
            toModule.Annotate(fromModule);

            //annotate so that we know these modules have been merged
            //this is used by checkneverusedmembers step
            if (!fromModule.ContainsAnnotation("merged-module"))
                fromModule.Annotate("merged-module");
            if (!toModule.ContainsAnnotation("merged-module"))
                toModule.Annotate("merged-module");
        }
Пример #13
0
		override public void LeaveTypeDefinition(TypeDefinition node)
		{
			_safeVars.Clear(); //clear type var cache (CheckAmbiguousVariableNames)

			if (node.NodeType == NodeType.Module)
				return;

			string qualifiedName = node.QualifiedName;
			if (node.HasGenericParameters)
			{
				qualifiedName += "`" + node.GenericParameters.Count;
			}

			if (_types.ContainsKey(qualifiedName))
			{
				Errors.Add(CompilerErrorFactory.NamespaceAlreadyContainsMember(node, GetNamespace(node), node.Name));
				return;
			}
			_types.Add(qualifiedName, node); 
		}
Пример #14
0
		public BaseTypeResolution(CompilerContext context, TypeDefinition typeDefinition, List<TypeDefinition> visited) : base(context)
		{
			_typeDefinition = typeDefinition;
			_visited = visited;
			_visited.Add(_typeDefinition);

			_removed = 0;
			_index = -1;

			NameResolutionService nameResolution = NameResolutionService;
			INamespace previous = nameResolution.CurrentNamespace;
			nameResolution.EnterNamespace(ParentNamespaceOf(_typeDefinition));
			try
			{
				Run();
			}
			finally
			{
				nameResolution.EnterNamespace(previous);
			}
		}
Пример #15
0
		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));
				}
			}
		}
Пример #16
0
        private void LeaveTypeDefinition(AST.TypeDefinition node)
        {
            DefaultClass c = _currentClass.Pop();

            foreach (AST.Attribute att in node.Attributes)
            {
                if (att.Name == "System.Reflection.DefaultMemberAttribute" && att.Arguments.Count == 1)
                {
                    AST.StringLiteralExpression sle = att.Arguments[0] as AST.StringLiteralExpression;
                    if (sle != null)
                    {
                        foreach (DefaultProperty p in c.Properties)
                        {
                            if (p.Name == sle.Value)
                            {
                                p.IsIndexer = true;
                            }
                        }
                    }
                }
            }
            //LoggingService.Debug("Leave "+node.GetType().Name+" "+node.FullName+" (Class = "+c.FullyQualifiedName+")");
        }
Пример #17
0
 Constructor GetStaticConstructor(TypeDefinition type)
 {
     return CodeBuilder.GetOrCreateStaticConstructorFor(type);
 }
Пример #18
0
 private Method GetInitializerFor(TypeDefinition type, bool isStatic)
 {
     string methodName = isStatic ? "$static_initializer$" : "$initializer$";
     Method method = (Method)type[methodName];
     if (null == method)
     {
         if (isStatic)
         {
             if (!type.HasStaticConstructor)
             {
                 // when the class doesnt have a static constructor
                 // yet, create one and use it as the static
                 // field initializer method
                 method = CodeBuilder.CreateStaticConstructor(type);
             }
             else
             {
                 method = CreateInitializerMethod(type, methodName, TypeMemberModifiers.Static);
                 AddInitializerToStaticConstructor(type, (InternalMethod)method.Entity);
             }
         }
         else
         {
             method = CreateInitializerMethod(type, methodName, TypeMemberModifiers.None);
         }
         type[methodName] = method;
     }
     return method;
 }
Пример #19
0
        private Field GetFieldsInitializerInitializedField(TypeDefinition type)
        {
            string name = AstUtil.BuildUniqueTypeMemberName(type, "initialized");
            Field field= (Field) type.Members[name];

            if (null == field)
            {
                field = CodeBuilder.CreateField(name, TypeSystemServices.BoolType);
                field.Visibility = TypeMemberModifiers.Private;
                type.Members.Add(field);
                MarkVisited(field);
            }
            return field;
        }
Пример #20
0
 Method CreateInitializerMethod(TypeDefinition type, string name, TypeMemberModifiers modifiers)
 {
     Method method = CodeBuilder.CreateMethod(name, TypeSystemServices.VoidType, modifiers);
     type.Members.Add(method);
     MarkVisited(method);
     return method;
 }
Пример #21
0
 void AddInitializerToStaticConstructor(TypeDefinition type, InternalMethod initializer)
 {
     GetStaticConstructor(type).Body.Insert(0,
                                            CodeBuilder.CreateMethodInvocation(initializer));
 }
Пример #22
0
 public Constructor CreateStaticConstructor(TypeDefinition type)
 {
     var constructor = new Constructor();
     constructor.IsSynthetic = true;
     constructor.Modifiers = TypeMemberModifiers.Private | TypeMemberModifiers.Static;
     EnsureEntityFor(constructor);
     type.Members.Add(constructor);
     return constructor;
 }
Пример #23
0
		public static CompilerContext compile_(TypeDefinition klass, params Assembly[] references)
		{
			return compile_(CreateCompileUnit(klass), references);
		}
Пример #24
0
		public static Type compile(TypeDefinition klass, params Assembly[] references)
		{
			var result = compile_(klass, references);
			AssertNoErrors(result);
			return result.GeneratedAssembly.GetType(klass.Name);
		}
Пример #25
0
		private static Module CreateModule(TypeDefinition klass)
		{
			Module module = new Module();
			module.Name = klass.Name;
			module.Members.Add(klass);
			return module;
		}
Пример #26
0
		private static CompileUnit CreateCompileUnit(TypeDefinition klass)
		{
			return new CompileUnit(CreateModule(klass));
		}
Пример #27
0
 void CantBeMarkedPartialIfNested(TypeDefinition type)
 {
     if (type.IsNested)
         CantBeMarkedPartial(type);
 }
Пример #28
0
        public object VisitTypeDeclaration(TypeDeclaration typeDeclaration, object data)
        {
            if (typeDeclaration.Templates.Count > 0)
            {
                AddError(typeDeclaration, "Generic type definitions are not supported.");
            }
            B.TypeDefinition oldType = currentType;
            B.TypeDefinition typeDef;
            switch (typeDeclaration.Type)
            {
            case ClassType.Class:
                typeDef = new B.ClassDefinition(GetLexicalInfo(typeDeclaration));
                break;

            case ClassType.Interface:
                typeDef = new B.InterfaceDefinition(GetLexicalInfo(typeDeclaration));
                break;

            case ClassType.Enum:
                typeDef = new B.EnumDefinition(GetLexicalInfo(typeDeclaration));
                break;

            case ClassType.Struct:
                typeDef = new B.StructDefinition(GetLexicalInfo(typeDeclaration));
                break;

            case ClassType.Module:
                typeDef = new B.ClassDefinition(GetLexicalInfo(typeDeclaration));
                typeDeclaration.Modifier |= Modifiers.Static;
                break;

            default:
                AddError(typeDeclaration, "Unknown class type.");
                return(null);
            }
            if (currentType != null)
            {
                typeDef.Modifiers = ConvertModifier(typeDeclaration, B.TypeMemberModifiers.Private);
            }
            else
            {
                typeDef.Modifiers = ConvertModifier(typeDeclaration, B.TypeMemberModifiers.Internal);
            }
            typeDef.Name = typeDeclaration.Name;
            typeDef.EndSourceLocation = GetLocation(typeDeclaration.EndLocation);
            ConvertAttributes(typeDeclaration.Attributes, typeDef.Attributes);
            ConvertTypeReferences(typeDeclaration.BaseTypes, typeDef.BaseTypes);

            if (currentType != null)
            {
                currentType.Members.Add(typeDef);
            }
            else
            {
                module.Members.Add(typeDef);
            }
            currentType = typeDef;
            typeDeclaration.AcceptChildren(this, data);
            currentType = oldType;
            return(typeDef);
        }
Пример #29
0
 private void VisitBaseTypes(TypeDefinition node)
 {
     foreach (var baseTypeRef in node.BaseTypes)
         EnsureRelatedNodeWasVisited(baseTypeRef, baseTypeRef.Entity);
 }
Пример #30
0
 private void VisitTypeDefinition(TypeDefinition node)
 {
     var ns = (INamespace)GetEntity(node);
     EnterNamespace(ns);
     VisitBaseTypes(node);
     Visit(node.Attributes);
     Visit(node.Members);
     LeaveNamespace();
 }
Пример #31
0
        void AddInitializedGuardToInitializer(TypeDefinition type, Method initializer)
        {
            Field field = GetFieldsInitializerInitializedField(type);

            //run initializer code only if $initialized$ is false
            //hmm quasi-notation would be lovely here
            Block trueBlock = new Block();
            trueBlock.Add(new GotoStatement(LexicalInfo.Empty, new ReferenceExpression("___initialized___")));
            IfStatement cond = new IfStatement(CodeBuilder.CreateReference(field),
                trueBlock, null);
            initializer.Body.Insert(0, cond);

            //set $initialized$ field to true
            initializer.Body.Add(
                CodeBuilder.CreateFieldAssignment(field, new BoolLiteralExpression(true)));

            //label we're past the initializer
            initializer.Body.Add(
                new LabelStatement(LexicalInfo.Empty, "___initialized___"));
        }
Пример #32
0
 public Constructor GetOrCreateStaticConstructorFor(TypeDefinition type)
 {
     return type.GetStaticConstructor() ?? CreateStaticConstructor(type);
 }
Пример #33
0
        void AddInitializerToInstanceConstructors(TypeDefinition type, Method initializer)
        {
            int n = 0;

            //count number of non-static constructors
            foreach (TypeMember node in type.Members)
            {
                if (NodeType.Constructor == node.NodeType && !node.IsStatic)
                    n++;
            }

            //if there is more than one we need $initialized$ guard check
            if (n > 1)
                AddInitializedGuardToInitializer(type, initializer);

            foreach (TypeMember node in type.Members)
            {
                if (NodeType.Constructor == node.NodeType && !node.IsStatic)
                {
                    Constructor constructor = (Constructor) node;
                    n = GetIndexAfterSuperInvocation(constructor.Body);
                    foreach (Statement st in initializer.Body.Statements)
                    {
                        constructor.Body.Insert(n, (Statement) st.Clone());
                        n++;
                    }
                    foreach (Local loc in initializer.Locals)
                    {
                        constructor.Locals.Add(loc);
                    }
                }
            }
        }