Represents a file within the mudule as a CLR type.
namespace [DIR]{ class [FNAME] { object [Main](){ ... } } }
Inheritance: NamedTypeSymbol
Esempio n. 1
0
        public SourceNamedTypeSymbol(SourceFileSymbol file, TypeDecl syntax)
        {
            Contract.ThrowIfNull(file);

            _syntax = syntax;
            _file   = file;
        }
        public SourceGlobalMethodSymbol(SourceFileSymbol file)
        {
            Contract.ThrowIfNull(file);

            _file = file;
            _params = BuildParameters().ToImmutableArray();
        }
Esempio n. 3
0
        public SourceFunctionSymbol(SourceFileSymbol file, FunctionDecl syntax)
        {
            Contract.ThrowIfNull(file);

            _file   = file;
            _syntax = syntax;
        }
Esempio n. 4
0
        public SourceGlobalMethodSymbol(SourceFileSymbol file)
        {
            Contract.ThrowIfNull(file);

            _file   = file;
            _params = BuildParameters().ToImmutableArray();
        }
 private SourceTypeSymbol(SourceFileSymbol file, TypeDecl syntax, NamedTypeSymbol baseType, ImmutableArray <NamedTypeSymbol> ifacesType, int version)
     : this(file, syntax)
 {
     _lazyBaseType       = baseType;
     _lazyInterfacesType = ifacesType;
     _version            = version;
 }
Esempio n. 6
0
        internal void Bind(Symbol symbol, SourceFileSymbol file)
        {
            Debug.Assert(symbol != null);

            if (_type == null)
            {
                var type = (NamedTypeSymbol)symbol.DeclaringCompilation.GetTypeFromTypeRef(_tref);

                if (type.IsErrorTypeOrNull() || type.SpecialType == SpecialType.System_Object)
                {
                    symbol.DeclaringCompilation.DeclarationDiagnostics.Add(
                        Location.Create(file.SyntaxTree, _tref.Span.ToTextSpan()),
                        Errors.ErrorCode.ERR_TypeNameCannotBeResolved,
                        _tref.ToString());

                    type = new MissingMetadataTypeSymbol(_tref.ToString(), 0, false);
                }

                // bind arguments
                if (!TryResolveCtor(type, symbol.DeclaringCompilation, out _ctor, out _ctorArgs) && type.IsValidType())
                {
                    symbol.DeclaringCompilation.DeclarationDiagnostics.Add(
                        Location.Create(file.SyntaxTree, _tref.Span.ToTextSpan()),
                        Errors.ErrorCode.ERR_NoMatchingOverload,
                        type.Name + "..ctor");
                }

                // bind named parameters
                if (type.IsErrorTypeOrNull() || _properties.IsDefaultOrEmpty)
                {
                    _namedArgs = ImmutableArray <KeyValuePair <string, TypedConstant> > .Empty;
                }
                else
                {
                    var namedArgs = new KeyValuePair <string, TypedConstant> [_properties.Length];
                    for (int i = 0; i < namedArgs.Length; i++)
                    {
                        var prop   = _properties[i];
                        var member =
                            (Symbol)type.LookupMember <PropertySymbol>(prop.Key.Value) ??
                            (Symbol)type.LookupMember <FieldSymbol>(prop.Key.Value);

                        if (member != null && TryBindTypedConstant(member.GetTypeOrReturnType(), prop.Value, symbol.DeclaringCompilation, out var arg))
                        {
                            namedArgs[i] = new KeyValuePair <string, TypedConstant>(prop.Key.Value, arg);
                        }
                        else
                        {
                            throw new InvalidOperationException();
                        }
                    }

                    _namedArgs = namedArgs.AsImmutable();
                }

                //
                _type = type;
            }
        }
Esempio n. 7
0
        public override bool IsSealed => true;  // traits cannot be extended

        public SourceTraitTypeSymbol(SourceFileSymbol file, TypeDecl syntax)
            : base(file, syntax)
        {
            Debug.Assert(syntax.MemberAttributes.IsTrait());
            Debug.Assert(syntax.BaseClass == null); // not expecting trait can extend another class

            _typeParameters = ImmutableArray.Create <TypeParameterSymbol>(new AnonymousTypeParameterSymbol(this, 0, "TSelf", hasReferenceTypeConstraint: true));
        }
Esempio n. 8
0
        public SourceFunctionSymbol(SourceFileSymbol file, FunctionDecl syntax)
        {
            Contract.ThrowIfNull(file);

            _file = file;
            _syntax = syntax;
            _params = BuildParameters(syntax.Signature, syntax.PHPDoc).AsImmutable();
        }
Esempio n. 9
0
        public SourceFunctionSymbol(SourceFileSymbol file, FunctionDecl syntax)
        {
            Contract.ThrowIfNull(file);

            _file   = file;
            _syntax = syntax;
            _params = BuildParameters(syntax.Signature, syntax.PHPDoc).AsImmutable();
        }
Esempio n. 10
0
 /// <summary>
 /// Gets file symbol containing given symbol.
 /// </summary>
 public static SourceFileSymbol GetContainingFileSymbol(this Symbol s)
 {
     return(s?.OriginalDefinition switch
     {
         SourceFileSymbol file => file,
         SourceRoutineSymbol routine => routine.ContainingFile,
         SourceTypeSymbol type => type.ContainingFile,
         _ => s != null?GetContainingFileSymbol(s.ContainingSymbol) : null,
     });
Esempio n. 11
0
        public SourceTypeSymbol(SourceFileSymbol file, TypeDecl syntax)
        {
            Contract.ThrowIfNull(file);

            _syntax = syntax;
            _file   = file;

            //
            _staticsContainer = new SynthesizedStaticFieldsHolder(this);
        }
        /// <summary>
        /// Gets enumeration of all routines (global code, functions and methods) within the file.
        /// </summary>
        internal static IEnumerable <SourceRoutineSymbol> GetAllRoutines(this SourceFileSymbol file)
        {
            // all functions + global code + methods + lambdas
            var funcs = file.Functions.Cast <SourceRoutineSymbol>();
            var main  = file.MainMethod;

            var types   = file.ContainedTypes.SelectMany(t => t.AllVersions());
            var methods = types.SelectMany(f => f.GetMembers().OfType <SourceRoutineSymbol>());
            var lambdas = ((ILambdaContainerSymbol)file).Lambdas;

            return(funcs.Concat(main).Concat(methods).Concat(lambdas));
        }
Esempio n. 13
0
            public void VisitSourceUnit(SourceUnit unit)
            {
                if (unit != null && unit.Ast != null)
                {
                    var file = new SourceFileSymbol(_compilation, unit.Ast);
                    _currentFile = file;
                    _tables._files.Add(file.RelativeFilePath, _currentFile);

                    VisitGlobalCode(unit.Ast);

                    //
                    if (_tables.FirstScript == null)
                        _tables.FirstScript = _currentFile;

                    //
                    _currentFile = null;
                }
            }
Esempio n. 14
0
            public void VisitSourceUnit(SourceUnit unit)
            {
                if (unit != null && unit.Ast != null)
                {
                    var file = new SourceFileSymbol(_compilation, unit.Ast);
                    _currentFile = file;
                    _tables._files.Add(file.RelativeFilePath, _currentFile);

                    VisitGlobalCode(unit.Ast);

                    //
                    if (_tables.FirstScript == null)
                    {
                        _tables.FirstScript = _currentFile;
                    }

                    //
                    _currentFile = null;
                }
            }
Esempio n. 15
0
            public override void VisitGlobalCode(GlobalCode x)
            {
                Debug.Assert(_syntaxTree.Source.Ast == x);

                var fsymbol = new SourceFileSymbol(_compilation, _syntaxTree);

                _currentFile = fsymbol;
                _tables._files.Add(fsymbol.RelativeFilePath, fsymbol);
                _tables._ordinalMap.Add(_syntaxTree, _tables._ordinalMap.Count);

                if (_tables.FirstScript == null)
                {
                    _tables.FirstScript = _currentFile;
                }

                _containerStack.Push(fsymbol);

                base.VisitGlobalCode(x);

                _containerStack.Pop();

                _currentFile = null;
            }
Esempio n. 16
0
        public SourceGlobalMethodSymbol(SourceFileSymbol file)
        {
            Contract.ThrowIfNull(file);

            _file = file;
        }
Esempio n. 17
0
        public void AddSyntaxTree(PhpSyntaxTree tree)
        {
            Contract.ThrowIfNull(tree);

            Debug.Assert(tree.Root != null);

            // create file symbol (~ php script containing type)
            var fsymbol = new SourceFileSymbol(_compilation, tree);

            if (FirstScript == null)
            {
                FirstScript = fsymbol;
            }

            // collect type declarations
            foreach (var t in tree.Types)
            {
                var typesymbol = (t is AnonymousTypeDecl)
                    ? new SourceAnonymousTypeSymbol(fsymbol, (AnonymousTypeDecl)t)
                    : new SourceTypeSymbol(fsymbol, t);

                t.SetProperty(typesymbol);    // remember bound type symbol
                fsymbol.ContainedTypes.Add(typesymbol);
            }

            // annotate routines that contain yield
            if (!tree.YieldNodes.IsDefaultOrEmpty)
            {
                var yieldsInRoutines = new Dictionary <LangElement, List <IYieldLikeEx> >();
                foreach (var y in tree.YieldNodes)
                {
                    Debug.Assert(y is IYieldLikeEx);
                    var yield = y as IYieldLikeEx;

                    var containingRoutine = y.GetContainingRoutine();
                    Debug.Assert(containingRoutine != null);

                    if (!yieldsInRoutines.ContainsKey(containingRoutine))
                    {
                        yieldsInRoutines.Add(containingRoutine, new List <IYieldLikeEx>());
                    }
                    yieldsInRoutines[containingRoutine].Add(yield);
                }

                foreach (var yieldsInRoutine in yieldsInRoutines)
                {
                    var routine = yieldsInRoutine.Key;
                    var yields  = yieldsInRoutine.Value;

                    routine.Properties.SetProperty(typeof(ImmutableArray <IYieldLikeEx>), yields.ToImmutableArray());
                }
            }

            //
            foreach (var f in tree.Functions)
            {
                var routine = new SourceFunctionSymbol(fsymbol, f);

                f.SetProperty(routine); // remember bound function symbol
                fsymbol.AddFunction(routine);
            }

            //
            foreach (var l in tree.Lambdas)
            {
                var containingtype = l.ContainingType;
                var container      = (containingtype != null) ? (NamedTypeSymbol)containingtype.GetProperty <SourceTypeSymbol>() : fsymbol;

                var lambdasymbol = new SourceLambdaSymbol(l, container, !l.IsStatic);
                Debug.Assert(container is ILambdaContainerSymbol);
                ((ILambdaContainerSymbol)container).AddLambda(lambdasymbol);
            }

            //
            _files.Add(fsymbol.RelativeFilePath, fsymbol);
            _ordinalMap.Add(tree, _ordinalMap.Count);
            _version++;
        }
Esempio n. 18
0
        public SourceTypeSymbol(SourceFileSymbol file, TypeDecl syntax)
        {
            Contract.ThrowIfNull(file);

            _syntax = syntax;
            _file = file;

            //
            _staticsContainer = new SynthesizedStaticFieldsHolder(this);
        }
        public void AddSyntaxTree(PhpSyntaxTree tree)
        {
            Contract.ThrowIfNull(tree);

            Debug.Assert(tree.Root != null);

            // create file symbol (~ php script containing type)
            var fsymbol = new SourceFileSymbol(_compilation, tree);

            if (FirstScript == null)
            {
                FirstScript = fsymbol;
            }

            // collect type declarations
            foreach (var t in tree.Types)
            {
                var typesymbol = (t is AnonymousTypeDecl)
                    ? new SourceAnonymousTypeSymbol(fsymbol, (AnonymousTypeDecl)t)
                    : new SourceTypeSymbol(fsymbol, t);

                t.SetProperty(typesymbol);    // remember bound type symbol
                fsymbol.ContainedTypes.Add(typesymbol);
            }

            // annotate routines that contain yield
            if (!tree.YieldNodes.IsDefaultOrEmpty)
            {
                foreach (var y in tree.YieldNodes)
                {
                    var containingroutine = y.GetContainingRoutine();
                    Debug.Assert(containingroutine != null);
                    // TODO: annotate routine as generator ! so it can be bound as generator already
                }
            }

            //
            foreach (var f in tree.Functions)
            {
                var routine = new SourceFunctionSymbol(fsymbol, f);

                f.SetProperty(routine); // remember bound function symbol
                fsymbol.AddFunction(routine);
            }

            //
            foreach (var l in tree.Lambdas)
            {
                var containingtype = l.ContainingType;
                var container      = (containingtype != null) ? (NamedTypeSymbol)containingtype.GetProperty <SourceTypeSymbol>() : fsymbol;

                var lambdasymbol = new SourceLambdaSymbol(l, container, !l.IsStatic);
                Debug.Assert(container is ILambdaContainerSymbol);
                ((ILambdaContainerSymbol)container).AddLambda(lambdasymbol);
            }

            //
            _files.Add(fsymbol.RelativeFilePath, fsymbol);
            _ordinalMap.Add(tree, _ordinalMap.Count);
            _version++;
        }
Esempio n. 20
0
 public SourceAnonymousTypeSymbol(SourceFileSymbol file, AnonymousTypeDecl syntax)
     : base(file, syntax)
 {
 }
Esempio n. 21
0
        static IEnumerable <ImmutableArray <T> > Variations <T>(ImmutableArray <T> types, SourceFileSymbol containingFile) where T : NamedTypeSymbol
        {
            if (types.Length == 0)
            {
                return(new ImmutableArray <T>[] { ImmutableArray <T> .Empty });
            }

            // skip non ambiguous types
            int i = 0;

            while (i < types.Length && (types[i] == null || types[i].IsErrorType() == false))
            {
                i++;
            }

            if (i == types.Length)
            {
                return(new ImmutableArray <T>[] { types });
            }

            // [prefix, {types[i]}, Variations(types[i+1..])

            var prefix = (i == 0)
                ? ImmutableArray <T> .Empty
                : ImmutableArray.CreateRange(types.Take(i));

            var suffixvariations = Variations(types.RemoveRange(0, i + 1), containingFile);

            var ambiguity = (types[i] as ErrorTypeSymbol).CandidateSymbols.Cast <T>().ToList();

            // in case there is an ambiguity that is declared in current scope unconditionally, pick this one and ignore the others
            var best = ambiguity.FirstOrDefault(x => ReferenceEquals((x as SourceTypeSymbol)?.ContainingFile, containingFile) && !(x as SourceTypeSymbol)._syntax.IsConditional);

            if (best != null)
            {
                ambiguity = new List <T>(1)
                {
                    best
                };
            }

            return(ambiguity.SelectMany(t =>
            {
                var list = new List <ImmutableArray <T> >();

                // prefix + t + |suffixvariations|
                foreach (var v in suffixvariations)
                {
                    var bldr = ImmutableArray.CreateBuilder <T>(types.Length);
                    bldr.AddRange(prefix);
                    bldr.Add(t);
                    bldr.AddRange(v);
                    // prefix + t + v
                    list.Add(bldr.ToImmutable());
                }

                //
                return list;
            }));
        }