Exemple #1
0
        public override void OnArrayLiteralExpression(ArrayLiteralExpression node)
        {
            if (node.Type != null)
            {
                MakeResult(ConvertType(node.Type));
                return;
            }
            IReturnType elementType = null;

            foreach (Expression expr in node.Items)
            {
                ClearResult();
                expr.Accept(this);
                IReturnType thisType = (resolveResult != null) ? resolveResult.ResolvedType : null;
                if (elementType == null)
                {
                    elementType = thisType;
                }
                else if (thisType != null)
                {
                    elementType = MemberLookupHelper.GetCommonType(projectContent, elementType, thisType);
                }
            }
            if (elementType == null)
            {
                elementType = ConvertVisitor.GetDefaultReturnType(projectContent);
            }
            MakeResult(new ArrayReturnType(projectContent, elementType, 1));
        }
            public override void OnYieldStatement(YieldStatement node)
            {
                noReturnStatement = false;

                IProjectContent pc         = context != null ? context.ProjectContent : ParserService.CurrentProjectContent;
                IReturnType     enumerable = new GetClassReturnType(pc, "System.Collections.Generic.IEnumerable", 1);

                // Prevent creating an infinite number of InferredReturnTypes in inferring cycles
                parentReturnType.expression = new NullLiteralExpression();
                IReturnType returnType;

                if (node.Expression == null)
                {
                    returnType = ConvertVisitor.GetDefaultReturnType(pc);
                }
                else
                {
                    returnType = new BooResolver().GetTypeOfExpression(node.Expression, context);
                }
                if (returnType != null)
                {
                    returnType.GetUnderlyingClass();                     // force to infer type
                }
                if (parentReturnType.expression == null)
                {
                    // inferrence cycle with parentReturnType
                    returnType = new GetClassReturnType(pc, "?", 0);
                }
                parentReturnType.expression = null;

                result = new ConstructedReturnType(enumerable, new IReturnType[] { returnType });
            }
Exemple #3
0
        public override void OnBlockExpression(BlockExpression node)
        {
            AnonymousMethodReturnType amrt = new AnonymousMethodReturnType(cu);

            if (node.ReturnType != null)
            {
                amrt.MethodReturnType = ConvertType(node.ReturnType);
            }
            else
            {
                amrt.MethodReturnType = new BooInferredReturnType(node.Body, resolver.CallingClass,
                                                                  node.ContainsAnnotation("inline"));
            }
            amrt.MethodParameters = new List <IParameter>();
            ConvertVisitor.AddParameters(node.Parameters, amrt.MethodParameters, resolver.CallingMember, resolver.CallingClass ?? new DefaultClass(resolver.CompilationUnit, "__Dummy"));
            MakeResult(amrt);
        }
Exemple #4
0
 public IReturnType ConvertType(AST.TypeReference typeRef)
 {
     return(ConvertVisitor.CreateReturnType(typeRef, callingClass, callingMember,
                                            caretLine, caretColumn, pc));
 }
Exemple #5
0
        private ICompilationUnit Parse(IProjectContent projectContent, string fileName, int[] lineLength, BooCompiler compiler)
        {
            compiler.Parameters.OutputWriter = new StringWriter();
            compiler.Parameters.TraceLevel   = System.Diagnostics.TraceLevel.Off;

            // Compile pipeline as of Boo 0.9.4:
            // Boo.Lang.Compiler.Pipelines.Parse:
            //   Boo.Lang.Compiler.Steps.Parsing
            // Boo.Lang.Compiler.Pipelines.ExpandMacros:
            //   Boo.Lang.Compiler.Steps.PreErrorChecking
            //   Boo.Lang.Compiler.Steps.MergePartialClasses
            //   Boo.Lang.Compiler.Steps.InitializeNameResolutionService
            //   Boo.Lang.Compiler.Steps.IntroduceGlobalNamespaces
            //   Boo.Lang.Compiler.Steps.TransformCallableDefinitions
            //   Boo.Lang.Compiler.Steps.BindTypeDefinitions
            //   Boo.Lang.Compiler.Steps.BindGenericParameters
            //   Boo.Lang.Compiler.Steps.ResolveImports
            //   Boo.Lang.Compiler.Steps.BindBaseTypes
            //   Boo.Lang.Compiler.Steps.MacroAndAttributeExpansion
            // Boo.Lang.Compiler.Pipelines.ResolveExpressions:
            //   Boo.Lang.Compiler.Steps.ExpandAstLiterals
            //   Boo.Lang.Compiler.Steps.IntroduceModuleClasses
            //   Boo.Lang.Compiler.Steps.NormalizeStatementModifiers
            //   Boo.Lang.Compiler.Steps.NormalizeTypeAndMemberDefinitions
            //   Boo.Lang.Compiler.Steps.NormalizeExpressions
            //   Boo.Lang.Compiler.Steps.BindTypeDefinitions
            //   Boo.Lang.Compiler.Steps.BindGenericParameters
            //   Boo.Lang.Compiler.Steps.BindEnumMembers
            //   Boo.Lang.Compiler.Steps.BindBaseTypes
            //   Boo.Lang.Compiler.Steps.CheckMemberTypes
            //   Boo.Lang.Compiler.Steps.BindMethods
            //   Boo.Lang.Compiler.Steps.ResolveTypeReferences
            //   Boo.Lang.Compiler.Steps.BindTypeMembers
            //   Boo.Lang.Compiler.Steps.CheckGenericConstraints
            //   Boo.Lang.Compiler.Steps.ProcessInheritedAbstractMembers
            //   Boo.Lang.Compiler.Steps.CheckMemberNames
            //   Boo.Lang.Compiler.Steps.ProcessMethodBodiesWithDuckTyping
            //   Boo.Lang.Compiler.Steps.ReifyTypes
            //   Boo.Lang.Compiler.Steps.VerifyExtensionMethods
            //   Boo.Lang.Compiler.Steps.TypeInference
            // Boo.Lang.Compiler.Pipelines.Compile:
            //   Boo.Lang.Compiler.Steps.ConstantFolding
            //   Boo.Lang.Compiler.Steps.CheckLiteralValues
            //   Boo.Lang.Compiler.Steps.OptimizeIterationStatements
            //   Boo.Lang.Compiler.Steps.BranchChecking
            //   Boo.Lang.Compiler.Steps.CheckIdentifiers
            //   Boo.Lang.Compiler.Steps.StricterErrorChecking
            //   Boo.Lang.Compiler.Steps.CheckAttributesUsage
            //   Boo.Lang.Compiler.Steps.ExpandDuckTypedExpressions
            //   Boo.Lang.Compiler.Steps.ProcessAssignmentsToValueTypeMembers
            //   Boo.Lang.Compiler.Steps.ExpandPropertiesAndEvents
            //   Boo.Lang.Compiler.Steps.CheckMembersProtectionLevel
            //   Boo.Lang.Compiler.Steps.NormalizeIterationStatements
            //   Boo.Lang.Compiler.Steps.ProcessSharedLocals
            //   Boo.Lang.Compiler.Steps.ProcessClosures
            //   Boo.Lang.Compiler.Steps.ProcessGenerators
            //   Boo.Lang.Compiler.Steps.ExpandVarArgsMethodInvocations
            //   Boo.Lang.Compiler.Steps.InjectCallableConversions
            //   Boo.Lang.Compiler.Steps.ImplementICallableOnCallableDefinitions
            //   Boo.Lang.Compiler.Steps.RemoveDeadCode
            //   Boo.Lang.Compiler.Steps.CheckNeverUsedMembers
            // Boo.Lang.Compiler.Pipelines.CompileToMemory:
            //   Boo.Lang.Compiler.Steps.EmitAssembly
            // Boo.Lang.Compiler.Pipelines.CompileToFile:
            //   Boo.Lang.Compiler.Steps.SaveAssembly


            CompilerPipeline compilePipe = new Parse();

            compilePipe.Add(new PreErrorChecking());
            compilePipe.Add(new MergePartialClasses());
            compilePipe.Add(new InitializeNameResolutionService());
            compilePipe.Add(new IntroduceGlobalNamespaces());
            // TransformCallableDefinitions: not used for CC
            compilePipe.Add(new BindTypeDefinitions());
            compilePipe.Add(new BindGenericParameters());
            compilePipe.Add(new ResolveImports());
            compilePipe.Add(new BindBaseTypes());
            compilePipe.Add(new MacroAndAttributeExpansion());
            compilePipe.Add(new IntroduceModuleClasses());

            var parsingStep = compilePipe[0];
            //compiler.Parameters.Environment.Provide<ParserSettings>().TabSize = 1;

            ConvertVisitor visitor = new ConvertVisitor(lineLength, projectContent);

            visitor.Cu.FileName = fileName;
            compilePipe.Add(visitor);

            compilePipe.BreakOnErrors    = false;
            compiler.Parameters.Pipeline = compilePipe;
            compiler.Parameters.References.Add(typeof(Boo.Lang.Useful.Attributes.SingletonAttribute).Assembly);

            int errorCount = 0;

            compilePipe.AfterStep += delegate(object sender, CompilerStepEventArgs args) {
                if (args.Step == parsingStep)
                {
                    errorCount = args.Context.Errors.Count;
                }
            };
            try {
                compiler.Run();
                visitor.Cu.ErrorsDuringCompile = errorCount > 0;
            } catch (Exception ex) {
                MessageService.ShowException(ex);
            }
            return(visitor.Cu);
        }
Exemple #6
0
        public ConstructedReturnType ConstructTypeFromGenericReferenceExpression(GenericReferenceExpression node)
        {
            Stack <Expression> stack = new Stack <Expression>();
            Expression         expr  = node;

            while (expr != null)
            {
                stack.Push(expr);
                if (expr is MemberReferenceExpression)
                {
                    expr = ((MemberReferenceExpression)expr).Target;
                }
                else if (expr is GenericReferenceExpression)
                {
                    expr = ((GenericReferenceExpression)expr).Target;
                }
                else
                {
                    expr = null;
                }
            }
            StringBuilder      name          = new StringBuilder();
            List <IReturnType> typeArguments = new List <IReturnType>();

            while (stack.Count > 0)
            {
                expr = stack.Pop();
                if (expr is MemberReferenceExpression)
                {
                    name.Append('.');
                    name.Append(((MemberReferenceExpression)expr).Name);
                }
                else if (expr is GenericReferenceExpression)
                {
                    foreach (TypeReference tr in ((GenericReferenceExpression)expr).GenericArguments)
                    {
                        typeArguments.Add(ConvertVisitor.CreateReturnType(tr, callingClass,
                                                                          resolver.CallingMember,
                                                                          resolver.CaretLine,
                                                                          resolver.CaretColumn,
                                                                          projectContent));
                    }
                }
                else if (expr is ReferenceExpression)
                {
                    name.Append(((ReferenceExpression)expr).Name);
                }
                else
                {
                    LoggingService.Warn("Unknown expression in GenericReferenceExpression: " + expr);
                }
            }
            IReturnType rt = projectContent.SearchType(new SearchTypeRequest(name.ToString(), typeArguments.Count, callingClass,
                                                                             cu, resolver.CaretLine, resolver.CaretColumn)).Result;

            if (rt != null)
            {
                return(new ConstructedReturnType(rt, typeArguments));
            }
            else
            {
                return(null);
            }
        }
		private ICompilationUnit Parse(IProjectContent projectContent, string fileName, int[] lineLength, BooCompiler compiler)
		{
			compiler.Parameters.OutputWriter = new StringWriter();
			compiler.Parameters.TraceLevel = System.Diagnostics.TraceLevel.Off;
			
			// Compile pipeline as of Boo 0.9.2:
			// Boo.Lang.Compiler.Pipelines.Parse:
			//   Boo.Lang.Parser.BooParsingStep
			// Boo.Lang.Compiler.Pipelines.ExpandMacros:
			//   Boo.Lang.Compiler.Steps.InitializeTypeSystemServices
			//   Boo.Lang.Compiler.Steps.PreErrorChecking
			//   Boo.Lang.Compiler.Steps.MergePartialClasses
			//   Boo.Lang.Compiler.Steps.InitializeNameResolutionService
			//   Boo.Lang.Compiler.Steps.IntroduceGlobalNamespaces
			//   Boo.Lang.Compiler.Steps.TransformCallableDefinitions
			//   Boo.Lang.Compiler.Steps.BindTypeDefinitions
			//   Boo.Lang.Compiler.Steps.BindGenericParameters
			//   Boo.Lang.Compiler.Steps.BindNamespaces
			//   Boo.Lang.Compiler.Steps.BindBaseTypes
			//   Boo.Lang.Compiler.Steps.MacroAndAttributeExpansion
			// Boo.Lang.Compiler.Pipelines.ResolveExpressions:
			//   Boo.Lang.Compiler.Steps.ExpandAstLiterals
			//   Boo.Lang.Compiler.Steps.IntroduceModuleClasses
			//   Boo.Lang.Compiler.Steps.NormalizeStatementModifiers
			//   Boo.Lang.Compiler.Steps.NormalizeTypeAndMemberDefinitions
			//   Boo.Lang.Compiler.Steps.NormalizeOmittedExpressions
			//   Boo.Lang.Compiler.Steps.BindTypeDefinitions
			//   Boo.Lang.Compiler.Steps.BindGenericParameters
			//   Boo.Lang.Compiler.Steps.BindEnumMembers
			//   Boo.Lang.Compiler.Steps.BindBaseTypes
			//   Boo.Lang.Compiler.Steps.CheckMemberTypes
			//   Boo.Lang.Compiler.Steps.BindMethods
			//   Boo.Lang.Compiler.Steps.ResolveTypeReferences
			//   Boo.Lang.Compiler.Steps.BindTypeMembers
			//   Boo.Lang.Compiler.Steps.CheckGenericConstraints
			//   Boo.Lang.Compiler.Steps.ProcessInheritedAbstractMembers
			//   Boo.Lang.Compiler.Steps.CheckMemberNames
			//   Boo.Lang.Compiler.Steps.ProcessMethodBodiesWithDuckTyping
			//   Boo.Lang.Compiler.Steps.PreProcessExtensionMethods
			// Boo.Lang.Compiler.Pipelines.Compile:
			//   Boo.Lang.Compiler.Steps.ConstantFolding
			//   Boo.Lang.Compiler.Steps.NormalizeLiterals
			//   Boo.Lang.Compiler.Steps.OptimizeIterationStatements
			//   Boo.Lang.Compiler.Steps.BranchChecking
			//   Boo.Lang.Compiler.Steps.CheckIdentifiers
			//   Boo.Lang.Compiler.Steps.StricterErrorChecking
			//   Boo.Lang.Compiler.Steps.CheckAttributesUsage
			//   Boo.Lang.Compiler.Steps.ExpandDuckTypedExpressions
			//   Boo.Lang.Compiler.Steps.ProcessAssignmentsToValueTypeMembers
			//   Boo.Lang.Compiler.Steps.ExpandProperties
			//   Boo.Lang.Compiler.Steps.RemoveDeadCode
			//   Boo.Lang.Compiler.Steps.CheckMembersProtectionLevel
			//   Boo.Lang.Compiler.Steps.NormalizeIterationStatements
			//   Boo.Lang.Compiler.Steps.ProcessSharedLocals
			//   Boo.Lang.Compiler.Steps.ProcessClosures
			//   Boo.Lang.Compiler.Steps.ProcessGenerators
			//   Boo.Lang.Compiler.Steps.ExpandVarArgsMethodInvocations
			//   Boo.Lang.Compiler.Steps.InjectCallableConversions
			//   Boo.Lang.Compiler.Steps.ImplementICallableOnCallableDefinitions
			//   Boo.Lang.Compiler.Steps.CheckNeverUsedMembers
			// Boo.Lang.Compiler.Pipelines.CompileToMemory:
			//   Boo.Lang.Compiler.Steps.EmitAssembly
			// Boo.Lang.Compiler.Pipelines.CompileToFile:
			//   Boo.Lang.Compiler.Steps.SaveAssembly
			
			
			CompilerPipeline compilePipe = new Parse();
			compilePipe.Add(new InitializeTypeSystemServices());
			compilePipe.Add(new PreErrorChecking());
			compilePipe.Add(new MergePartialClasses());
			compilePipe.Add(new InitializeNameResolutionService());
			compilePipe.Add(new IntroduceGlobalNamespaces());
			// TransformCallableDefinitions: not used for CC
			compilePipe.Add(new BindTypeDefinitions());
			compilePipe.Add(new BindGenericParameters());
			compilePipe.Add(new BindNamespacesWithoutRemovingErrors());
			compilePipe.Add(new BindBaseTypes());
			compilePipe.Add(new MacroAndAttributeExpansion());
			compilePipe.Add(new IntroduceModuleClasses());
			
			BooParsingStep parsingStep = (BooParsingStep)compilePipe[0];
			parsingStep.TabSize = 1;
			
			ConvertVisitor visitor = new ConvertVisitor(lineLength, projectContent);
			visitor.Cu.FileName = fileName;
			compilePipe.Add(visitor);
			
			compilePipe.BreakOnErrors = false;
			compiler.Parameters.Pipeline = compilePipe;
			compiler.Parameters.References.Add(typeof(Boo.Lang.Useful.Attributes.SingletonAttribute).Assembly);
			
			int errorCount = 0;
			compilePipe.AfterStep += delegate(object sender, CompilerStepEventArgs args) {
				if (args.Step == parsingStep)
					errorCount = args.Context.Errors.Count;
			};
			try {
				compiler.Run();
				visitor.Cu.ErrorsDuringCompile = errorCount > 0;
			} catch (Exception ex) {
				MessageService.ShowError(ex);
			}
			return visitor.Cu;
		}