protected virtual void GetCodeType()
        {
            if (_canInferType && (_contextHandler.GetCodeType() == null || _contextHandler.GetCodeType().Infer) && ComponentCollection.IsComponent <InitialValueComponent>())
            {
                _varInfo.InferType = true;
            }
            else
            {
                CodeType type;

                if (_contextHandler.GetCodeType() == null)
                {
                    type = _parseInfo.TranslateInfo.Types.Any();
                }
                else
                {
                    // Get the type.
                    type = TypeFromContext.GetCodeTypeFromContext(
                        _parseInfo,
                        _scope,
                        _contextHandler.GetCodeType()
                        );
                }

                ApplyCodeType(type);
            }
        }
        public VariableApply(ParseInfo parseInfo, Scope scope, Scope getter, IVariableInstance variable, Identifier variableContext)
        {
            Variable   = variable;
            _parseInfo = parseInfo;
            _name      = variableContext.Token.Text;
            CallRange  = variableContext.Token.Range;
            getter     = getter ?? scope;

            // Get the index.
            if (variableContext.Index != null)
            {
                _index = new IExpression[variableContext.Index.Count];
                for (int i = 0; i < _index.Length; i++)
                {
                    _index[i] = parseInfo.GetExpression(scope, variableContext.Index[i].Expression, getter: getter);
                }
            }

            // Get the generics.
            if (variableContext.TypeArgs != null)
            {
                _generics = new CodeType[variableContext.TypeArgs.Count];
                for (int i = 0; i < _generics.Length; i++)
                {
                    _generics[i] = TypeFromContext.GetCodeTypeFromContext(parseInfo, getter, variableContext.TypeArgs[i]);
                }
            }

            VariableCall = Variable.GetExpression(_parseInfo, CallRange, _index, _generics);
        }
Esempio n. 3
0
        public CreateObjectAction(ParseInfo parseInfo, Scope scope, NewExpression context)
        {
            if (!context.Type.Valid)
            {
                CreatingObjectOf = parseInfo.Types.Any();
                return;
            }

            // Get the type. Syntax error if there is no type name.
            CreatingObjectOf = TypeFromContext.GetCodeTypeFromContext(parseInfo, scope, context.Type);

            DocRange nameRange = context.Type.GenericToken.Range;

            // Get the constructor to use.
            OverloadChooser = new OverloadChooser(
                CreatingObjectOf.Constructors.Select(c => new ConstructorOverload(c)).ToArray(),
                parseInfo,
                CreatingObjectOf.ReturningScope(),
                scope,
                nameRange,
                context.Range,
                context.Range,
                new OverloadError("type " + CreatingObjectOf.Name)
                );
            OverloadChooser.Apply(context.Parameters, false, null);

            Constructor = (Constructor)OverloadChooser.Overload;

            if (Constructor != null)
            {
                Constructor.Call(parseInfo, nameRange);
                parseInfo.Script.AddHover(context.Range, Constructor.GetLabel(parseInfo.TranslateInfo, LabelInfo.Hover));

                // Default restricted parameter values.
                OverloadChooser.Match.CheckOptionalsRestrictedCalls(parseInfo, nameRange);

                // Bridge other restricted values.
                if (Constructor.CallInfo != null)
                {
                    RestrictedCall.BridgeMethodCall(parseInfo, Constructor.CallInfo, nameRange, context.Type.GenericToken.Text, Constructor.RestrictedValuesAreFatal);
                }
            }
        }
        private DefinedMethodProvider(ParseInfo parseInfo, IScopeHandler scopeProvider, FunctionContext context, IDefinedTypeInitializer containingType)
        {
            _parseInfo     = parseInfo;
            Context        = context;
            ContainingType = containingType;
            CallInfo       = new CallInfo(new RecursiveCallHandler(this, context.Subroutine || context.Attributes.Recursive), parseInfo.Script, ContentReady);

            DocRange nameRange = context.Identifier.Range;

            // Get the attributes.
            var attributes = new GenericAttributeAppender(AttributeType.Ref, AttributeType.In, AttributeType.GlobalVar, AttributeType.PlayerVar);

            AttributesGetter.GetAttributes(parseInfo.Script.Diagnostics, context.Attributes, attributes);

            // Set the attributes.
            Static      = attributes.IsStatic;
            Recursive   = attributes.IsRecursive;
            Virtual     = attributes.IsVirtual;
            AccessLevel = attributes.Accessor;
            Recursive   = attributes.IsRecursive;

            // Get subroutine info.
            if (context.Subroutine)
            {
                IsSubroutine            = true;
                SubroutineName          = context.Subroutine.Text.RemoveQuotes();
                SubroutineDefaultGlobal = !context.PlayerVar;
            }

            // Setup the scope.
            var containingScope = scopeProvider.GetScope(Static);

            containingScope.MethodContainer = true;
            _methodScope = containingScope.Child(true);

            // Get the generics.
            GenericTypes = AnonymousType.GetGenerics(parseInfo, context.TypeArguments, this);
            foreach (var type in GenericTypes)
            {
                _methodScope.AddType(new GenericCodeTypeInitializer(type));
            }

            // Get the type.
            if (!context.Type.IsVoid)
            {
                ReturnType = TypeFromContext.GetCodeTypeFromContext(parseInfo, _methodScope, context.Type);
            }

            // Setup the parameters.
            ParameterProviders = ParameterProvider.GetParameterProviders(parseInfo, _methodScope, context.Parameters, IsSubroutine);
            ParameterTypes     = ParameterProviders.Select(p => p.Type).ToArray();

            // Override
            if (attributes.IsOverride)
            {
                OverridingFunction = (DefinedMethodInstance)scopeProvider.GetOverridenFunction(parseInfo.TranslateInfo, new FunctionOverrideInfo(Name, ParameterTypes));
                if (OverridingFunction == null)
                {
                    SemanticsHelper.CouldNotOverride(parseInfo, nameRange, "method");
                }
            }

            // Check conflicts and add to scope.
            scopeProvider.CheckConflict(parseInfo, new(Name, ParameterTypes), nameRange);
            scopeProvider.Add(GetDefaultInstance(scopeProvider.DefinedIn()), Static);

            // Add LSP elements
            // Hover
            parseInfo.Script.AddHover(nameRange, GetLabel(parseInfo.TranslateInfo, LabelInfo.Hover));
            // 'references' code lens
            parseInfo.Script.AddCodeLensRange(new ReferenceCodeLensRange(this, parseInfo, CodeLensSourceType.Function, DefinedAt.range));
            // Rename & go-to-definition
            parseInfo.Script.Elements.AddDeclarationCall(this, new DeclarationCall(nameRange, true));
            // todo: 'override' code lens
            // if (Attributes.IsOverrideable)
            //     parseInfo.Script.AddCodeLensRange(new ImplementsCodeLensRange(this, parseInfo.Script, CodeLensSourceType.Function, nameRange));

            // Add the CallInfo to the recursion check.
            parseInfo.TranslateInfo.GetComponent <RecursionCheckComponent>().AddCheck(CallInfo);

            // Queue content for staged initiation.
            parseInfo.TranslateInfo.StagedInitiation.On(this);
        }
        public void GetMeta()
        {
            // Setup scopes.
            _operationalStaticScope = _parseInfo.TranslateInfo.RulesetScope.Child(Name);
            _operationalObjectScope = _parseInfo.TranslateInfo.RulesetScope.Child(Name);

            // Add typeargs to scopes.
            foreach (var type in GenericTypes)
            {
                _operationalStaticScope.AddType(new GenericCodeTypeInitializer(type));
                _operationalObjectScope.AddType(new GenericCodeTypeInitializer(type));
            }

            // Get the type being extended.
            // This is an array for future interface support.
            if (_typeContext.Inheriting.Count > 0)
            {
                var inheritContext = _typeContext.Inheriting[0];

                // Get the type being inherited.
                var inheriting = TypeFromContext.GetCodeTypeFromContext(_parseInfo, _operationalStaticScope, inheritContext);

                // GetCodeType will return null if the type is not found.
                if (inheriting != null)
                {
                    inheriting.Call(_parseInfo, inheritContext.Range);
                    TryToExtend(inheriting, inheritContext.GenericToken.GetRange(inheritContext.Range));
                }
            }

            WorkingInstance = GetInstance();

            // Get declarations.
            foreach (var declaration in _typeContext.Declarations)
            {
                DeclaredElements.Add(((IDefinedTypeInitializer)this).ApplyDeclaration(declaration, _parseInfo));
            }

            // Get the constructors.
            if (_typeContext.Constructors.Count > 0)
            {
                Constructors = new IConstructorProvider <Constructor> [_typeContext.Constructors.Count];
                for (int i = 0; i < Constructors.Length; i++)
                {
                    Constructors[i] = new DefinedConstructorProvider(this, _parseInfo, _operationalObjectScope, _typeContext.Constructors[i]);
                }
            }
            else
            {
                // If there are no constructors, create a default constructor.
                Constructors = new IConstructorProvider <Constructor>[] {
                    new EmptyConstructorProvider(DefinedAt)
                };
            }

            (Extends as ClassType)?.Elements.AddToScope(_parseInfo.TranslateInfo, _operationalStaticScope, false);
            (Extends as ClassType)?.Elements.AddToScope(_parseInfo.TranslateInfo, _operationalObjectScope, true);

            _onReady.Set();

            // TODO: update these
            // If the extend token exists, add completion that only contains all extendable classes.
            // if (_typeContext.InheritToken != null)
            //     _parseInfo.Script.AddCompletionRange(new CompletionRange(
            //         // Get the completion items of all types.
            //         _parseInfo.TranslateInfo.Types.AllTypes
            //             .Where(t => t is ClassType ct && ct.CanBeExtended)
            //             .Select(t => t.GetCompletion())
            //             .ToArray(),
            //         // Get the completion range.
            //         _typeContext.InheritToken.Range.End + _parseInfo.Script.NextToken(_typeContext.InheritToken).Range.Start,
            //         // This completion takes priority.
            //         CompletionRangeKind.ClearRest
            //     ));
            // _parseInfo.Script.AddCodeLensRange(new ReferenceCodeLensRange(this, _parseInfo, CodeLensSourceType.Type, _definedAt.range));
        }