コード例 #1
0
        public BindingContext BuildContext(BindingContext callingContext,
                                           IUnboundDecl parameterType, IBoundDecl argType,
                                           ref IEnumerable <IBoundDecl> typeArgs, out bool canInferArgs)
        {
            // try to infer the args if not passed in
            IList <IBoundDecl> inferredTypeArgs = TypeArgInferrer.Infer(TypeParameters,
                                                                        parameterType, argType);

            canInferArgs = inferredTypeArgs != null;

            if (canInferArgs)
            {
                typeArgs = inferredTypeArgs;
            }

            if (typeArgs.IsEmpty())
            {
                typeArgs = null;
            }

            // include the open namespaces of the calling context. this was the instantiated
            // generic has access to everything that the instantiation call site has access
            // to
            var searchSpace = new NameSearchSpace(BaseType.SearchSpace, callingContext.SearchSpace);

            return(new BindingContext(callingContext.Compiler, searchSpace, TypeParameters, typeArgs));
        }
コード例 #2
0
ファイル: TypeTable.cs プロジェクト: stjordanis/magpie-csharp
        public INamedType Find(NameSearchSpace searchSpace, Position position,
                               string name, IEnumerable <IBoundDecl> typeArgs)
        {
            // look through the namespaces
            foreach (var potentialName in searchSpace.SearchFor(name))
            {
                // look for a concrete type
                var type = Find(potentialName, typeArgs);
                if (type != null)
                {
                    return(type);
                }

                // look for a generic
                foreach (var structure in mGenericStructs)
                {
                    // names must match
                    if (structure.Name != potentialName)
                    {
                        continue;
                    }

                    // number of type args must match
                    if (typeArgs.Count() != structure.TypeParameters.Count)
                    {
                        continue;
                    }

                    return(structure.Instantiate(mCompiler, typeArgs));
                }

                //### bob: gross copy/paste of above
                // look for a generic
                foreach (var union in mGenericUnions)
                {
                    // names must match
                    if (union.Name != potentialName)
                    {
                        continue;
                    }

                    // number of type args must match
                    if (typeArgs.Count() != union.TypeParameters.Count)
                    {
                        continue;
                    }

                    return(union.Instantiate(mCompiler, typeArgs));
                }
            }

            // not found
            throw new CompileException(position, "Could not find a type named " + name + ".");
        }
コード例 #3
0
ファイル: Compiler.cs プロジェクト: stjordanis/magpie-csharp
        private void AddNamespace(string parentName, SourceFile file, Namespace namespaceObj)
        {
            var searchSpace = new NameSearchSpace(parentName, file.UsingNamespaces);

            foreach (var function in namespaceObj.Functions)
            {
                function.BindSearchSpace(searchSpace);
                mFunctions.AddUnbound(function);
            }

            foreach (var structure in namespaceObj.Structs)
            {
                structure.BindSearchSpace(searchSpace);
                mTypes.Add(structure);
            }

            foreach (var union in namespaceObj.Unions)
            {
                union.BindSearchSpace(searchSpace);
                mTypes.Add(union);
            }

            foreach (var function in namespaceObj.GenericFunctions)
            {
                function.BaseType.BindSearchSpace(searchSpace);
                mFunctions.Add(function);
            }

            foreach (var structure in namespaceObj.GenericStructs)
            {
                structure.BaseType.BindSearchSpace(searchSpace);
                mTypes.Add(structure);
            }

            foreach (var union in namespaceObj.GenericUnions)
            {
                union.BaseType.BindSearchSpace(searchSpace);
                mTypes.Add(union);
            }

            foreach (var childNamespace in namespaceObj.Namespaces)
            {
                var name = NameSearchSpace.Qualify(parentName, childNamespace.Name);
                AddNamespace(name, file, childNamespace);
            }
        }
コード例 #4
0
        public BindingContext(Compiler compiler, NameSearchSpace searchSpace,
                              IEnumerable <string> typeParameters, IEnumerable <IBoundDecl> typeArguments)
        {
            Compiler    = compiler;
            SearchSpace = searchSpace;

            NameGenerator = new NameGenerator();

            // build the argument dictionary
            TypeArguments = new Dictionary <string, IBoundDecl>();

            if ((typeParameters != null) && (typeArguments != null))
            {
                foreach (var pair in typeParameters.Zip(typeArguments))
                {
                    TypeArguments[pair.Item1] = pair.Item2;
                }
            }
        }
コード例 #5
0
        /// <summary>
        /// Creates a new search space by combining the usings of one search
        /// space with another.
        /// </summary>
        public NameSearchSpace(NameSearchSpace main, NameSearchSpace additional)
        {
            // clone the main one
            Namespace       = main.Namespace;
            UsingNamespaces = new List <string>(main.UsingNamespaces);

            // include that additional namespaces
            if (!UsingNamespaces.Contains(additional.Namespace))
            {
                UsingNamespaces.Add(additional.Namespace);
            }

            foreach (var name in additional.UsingNamespaces)
            {
                if (!UsingNamespaces.Contains(name))
                {
                    UsingNamespaces.Add(name);
                }
            }
        }
コード例 #6
0
 /// <summary>
 /// Binds the definition to the name context in which it is defined.
 /// </summary>
 /// <param name="searchSpace"></param>
 public void BindSearchSpace(NameSearchSpace searchSpace)
 {
     SearchSpace = searchSpace;
 }
コード例 #7
0
 public BindingContext(Compiler compiler, NameSearchSpace searchSpace)
     : this(compiler, searchSpace, null, null)
 {
 }