Package org.apache.flex.compiler.internal.definitions

Examples of org.apache.flex.compiler.internal.definitions.FunctionDefinition


        verifyFunctionModifiers(f);

        final MethodInfo mi = startFunctionGeneration(f);
        if ( mi != null )
        {
            FunctionDefinition funcDef = f.getDefinition();
            Name funcName = funcDef.getMName(this.currentScope.getProject());
           
            SemanticUtils.checkScopedToDefaultNamespaceProblem(this.currentScope, f, funcDef, null);
           

            boolean conflictsWithOtherDefinition = false;
            if ( funcName == null )
            {
                //  getMName() emitted a diagnostic,
                //  repair and continue.
                funcName = new Name("<invalid>");
            }
            else
            {
                conflictsWithOtherDefinition = currentScope.getMethodBodySemanticChecker().checkFunctionForConflictingDefinitions(f, funcDef);
            }
      
            ITraitVisitor tv = null;

            int traitKind = this.processingPackage?
                DirectiveProcessor.functionTraitKind(f, ABCConstants.TRAIT_Method):
                DirectiveProcessor.functionTraitKind(f, ABCConstants.TRAIT_Var);

            if (! this.currentScope.traitsVisitor.getTraits().containsTrait(traitKind, funcName) )
            {
                this.currentScope.declareVariableName(funcName);

                if ( ! this.processingPackage )
                {
                    if ( f.isGetter() || f.isSetter() )
                    {
                        tv = this.currentScope.traitsVisitor.visitMethodTrait(
                            traitKind,
                            funcName,
                            ITraitsVisitor.RUNTIME_DISP_ID,
                            mi);
                        assert tv != null : "visitMethodTrait should never return null!";
                    }
                    else
                    {
                        tv = this.currentScope.traitsVisitor.visitSlotTrait(
                            traitKind,
                            funcName,
                            ITraitsVisitor.RUNTIME_SLOT,
                            LexicalScope.anyType,
                            LexicalScope.noInitializer);
                        assert tv != null : "visitSlotTrait should never return null!";
   
                        this.currentScope.getInitInstructions().addInstruction(ABCConstants.OP_getglobalscope);
                        this.currentScope.getInitInstructions().addInstruction(ABCConstants.OP_newfunction, mi);
                        this.currentScope.getInitInstructions().addInstruction(ABCConstants.OP_setproperty, funcName);
                    }
                   
   
                }
                else
                {
                    tv = this.currentScope.traitsVisitor.visitMethodTrait(traitKind, funcName, 0, mi);
                    assert tv != null : "visitMethodTrait should never return null!";
                }
               
                if ( tv != null )
                {
                    this.currentScope.processMetadata(tv, funcDef.getAllMetaTags());
                    tv.visitEnd();
                }
            }
            else if (!conflictsWithOtherDefinition)
            {
View Full Code Here


    @Override
    public ITraitVisitor visitMethodTrait(int kind, Name name, int disp_id, MethodInfo method)
    {
        final String definitionName = getDefinitionName(name);

        FunctionDefinition methodDef;
       
        kind &= ABCConstants.TRAIT_KIND_MASK;
        switch (kind)
        {
            case ABCConstants.KIND_METHOD:
                methodDef = new FunctionDefinition(definitionName);
                break;
            case ABCConstants.KIND_GETTER:
                methodDef = new GetterDefinition(definitionName);
                break;
            case ABCConstants.KIND_SETTER:
                methodDef = new SetterDefinition(definitionName);
                break;
            case ABCConstants.KIND_FUNCTION:
                methodDef = new FunctionDefinition(definitionName);
                break;
            default:
                throw new IllegalStateException("Invalid method kind:" + kind);
        }
       
        final INamespaceReference namespaceReference = getNamespaceReference(name);
        methodDef.setNamespaceReference(namespaceReference);

        int paramTypesSize = method.getParamTypes().size();
        final ParameterDefinition params[] = new ParameterDefinition[paramTypesSize + (method.needsRest() ? 1 : 0)];
        if (params.length > 0)
        {
            ASScope methodScope = new FunctionScope(scope);
            methodScope.setContainingDefinition(methodDef);
            methodDef.setContainedScope(methodScope);
            Vector<PooledValue> defaultValues = method.getDefaultValues();
            int firstOptionalParam = paramTypesSize - defaultValues.size();
            for (int i = 0; i < paramTypesSize; i++)
            {
                final Name paramType = method.getParamTypes().get(i);
                final String paramName = i < method.getParamNames().size() ? method.getParamNames().get(i) : MethodInfo.UNKNOWN_PARAM_NAME;
                params[i] = new ParameterDefinition(paramName);
                params[i].setTypeReference(paramType == null ? TYPE_ANY : scopeBuilder.getReference(paramType));
                if (i >= firstOptionalParam)
                {
                    Object defaultValue = defaultValues.get(i - firstOptionalParam).getValue();
                    params[i].setDefaultValue(defaultValue);
                }
                methodScope.addDefinition(params[i]);
            }
            if( method.needsRest() )
            {
                ParameterDefinition rest = new ParameterDefinition(MethodInfo.UNKNOWN_PARAM_NAME);
                rest.setRest();
                rest.setTypeReference(ReferenceFactory.builtinReference(IASLanguageConstants.BuiltinType.ARRAY));
                params[paramTypesSize] = rest;
            }
        }
        methodDef.setParameters(params);

        Name returnType = method.getReturnType();
        methodDef.setReturnTypeReference(returnType == null ? TYPE_ANY : scopeBuilder.getReference(returnType));
       
        // The type of a getter or setter is its property type
        // (i.e., the getter's return type or the setter's parameter type).
        // The type of a method or function is "Function".
        switch (kind)
        {
            case ABCConstants.KIND_GETTER:
                methodDef.setTypeReference(methodDef.getReturnTypeReference());
                break;
            case ABCConstants.KIND_SETTER:
                methodDef.setTypeReference(methodDef.getParameters()[0].getTypeReference());
                break;
            case ABCConstants.KIND_METHOD:
            case ABCConstants.KIND_FUNCTION:
                methodDef.setTypeReference(TYPE_FUNCTION);
                break;
            default:
                throw new IllegalStateException("Invalid method kind:" + kind);
        }

        if (isStatic)
            methodDef.setStatic();

        scope.addDefinition(methodDef);

        return new CollectMetadataTraitVisitor(methodDef);
    }
View Full Code Here

        // Need to setup the scopes for the constructor and any params
        // here instead of ABCScopeBuilder, as we need to have a handle to the
        // class scope which isn't set until here.
        if (classDef instanceof ClassDefinition)
        {
            FunctionDefinition ctor = (FunctionDefinition)((ClassDefinition)classDef).getConstructor();
            classDef.getContainedScope().addDefinition(ctor);

            IParameterDefinition[] params = ctor.getParameters();
            if (params.length > 0)
            {
                ASScope ctorScope = new FunctionScope(scope);
                ctorScope.setContainingDefinition(ctor);
                ctor.setContainedScope(ctorScope);

                for (IParameterDefinition param : params)
                {
                    ctorScope.addDefinition(param);
                }
View Full Code Here

    private void setupConstructor(InstanceInfo iinfo, ClassDefinition classDefinition)
    {
        String ctorName = ScopedDefinitionTraitsVisitor.getDefinitionName(iinfo.name);

        FunctionDefinition ctor = new FunctionDefinition(ctorName);
        ctor.setNamespaceReference(NamespaceDefinition.getCodeModelImplicitDefinitionNamespace());
        ctor.setTypeReference(TYPE_FUNCTION);
        // NOTE: don't set a return type for constructors
        ctor.setReturnTypeReference(null);

        MethodInfo mInfo = iinfo.iInit;
        int paramTypesSize = mInfo.getParamTypes().size();
        final ParameterDefinition params[] = new ParameterDefinition[paramTypesSize + (mInfo.needsRest() ? 1 : 0)];
        if (params.length > 0)
        {
            Vector<PooledValue> defaultValues = mInfo.getDefaultValues();
            int firstOptionalParam = paramTypesSize - defaultValues.size();
            for (int i = 0; i < paramTypesSize; i++)
            {
                final Name paramType = mInfo.getParamTypes().get(i);
                final String paramName = i < mInfo.getParamNames().size() ? mInfo.getParamNames().get(i) : MethodInfo.UNKNOWN_PARAM_NAME;
                params[i] = new ParameterDefinition(paramName);
                params[i].setTypeReference(paramType == null ? TYPE_ANY : getReference(paramType));
                if (i >= firstOptionalParam)
                {
                    Object defaultValue = defaultValues.get(i - firstOptionalParam).getValue();
                    params[i].setDefaultValue(defaultValue);
                }
            }

            if (mInfo.needsRest())
            {
                ParameterDefinition rest = new ParameterDefinition(MethodInfo.UNKNOWN_PARAM_NAME);
                rest.setRest();
                rest.setTypeReference(ReferenceFactory.builtinReference(IASLanguageConstants.BuiltinType.ARRAY));
                params[paramTypesSize] = rest;
            }
        }

        ctor.setParameters(params);
        ctor.setAsConstructor(classDefinition);
        ctor.setImplicit();
    }
View Full Code Here

    private void setupCastFunction(InstanceInfo iinfo, InterfaceDefinition interfaceDefinition)
    {
        String castName = ScopedDefinitionTraitsVisitor.getDefinitionName(iinfo.name);

        FunctionDefinition castFunc = new FunctionDefinition(castName);
        castFunc.setNamespaceReference(NamespaceDefinition.getCodeModelImplicitDefinitionNamespace());
        castFunc.setReturnTypeReference(ReferenceFactory.resolvedReference(interfaceDefinition));
        castFunc.setCastFunction();
        castFunc.setImplicit();
    }
View Full Code Here

        IMetaInfo[] metaTags = getAllMetaTags(classDefinition);

        // Add "goto definition help" metadata for the constructor.
        if (this.ctorFunction != null)
        {
            FunctionDefinition ctorDef = this.ctorFunction.getDefinition();
            MetaTag metaTag = MetaTag.createGotoDefinitionHelp(classDefinition,
                    classDefinition.getContainingFilePath(),
                    Integer.toString(ctorDef.getNameStart()), true);
            if (metaTag != null)
                metaTags = MetaTag.addMetaTag(metaTags, metaTag);
        }       

        this.classScope.processMetadata(tv, metaTags);
View Full Code Here

    private void setupCastFunction(EnumSet<PostProcessStep> set, InterfaceDefinition def, ASScope scope)
    {
        // Unlike ClassNode, InterfaceNode doesn't create implicit definitions
        // for "this" or "super" for interfaces; but it does create an implicit
        // function that helps us resolve casting expressions such as IFoo(foo).
        FunctionDefinition castFunc = new FunctionDefinition(getName());
        castFunc.setNamespaceReference(NamespaceDefinition.getCodeModelImplicitDefinitionNamespace());
        castFunc.setReturnTypeReference(ReferenceFactory.resolvedReference(def));
        castFunc.setCastFunction();
        castFunc.setImplicit();

        // Add this definition to the interface scope.
        scope.addDefinition(castFunc);
    }
View Full Code Here

    @Override
    void declareFunction(FunctionNode func)
    {  
        func.parseFunctionBody(classScope.getProblems());

        final FunctionDefinition funcDef = func.getDefinition();

        final boolean is_constructor = func.isConstructor();
       
        ICompilerProject project = classScope.getProject();
       
        boolean isBindable = false;
        if (funcDef instanceof AccessorDefinition)
        {
            IMetaTag[] metaTags = funcDef.getAllMetaTags();
            for (IMetaTag metaTag : metaTags)
            {
                if (metaTag.getTagName().equals(BindableHelper.BINDABLE))
                {
                    IMetaTagAttribute[] attrs = metaTag.getAllAttributes();
                    isBindable = attrs.length == 0;
                }
            }
            if (!isBindable)
            {
                AccessorDefinition otherDef =
                    ((AccessorDefinition)funcDef).resolveCorrespondingAccessor(classScope.getProject());
                // ignore if not in your class def
                if (otherDef != null && otherDef.getContainingScope().equals(funcDef.getContainingScope()))
                {
                    metaTags = otherDef.getAllMetaTags();
                    for (IMetaTag metaTag : metaTags)
                    {
                        if (metaTag.getTagName().equals(BindableHelper.BINDABLE))
                        {
                            IMetaTagAttribute[] attrs = metaTag.getAllAttributes();
                            isBindable = attrs.length == 0;
                        }
                    }
                }
            }
        }
       
        functionSemanticChecks(func);

        Name funcName = funcDef.getMName(classScope.getProject());
        Name bindableName = null;
        boolean wasOverride = false;
        if (isBindable)
        {
            // move function into bindable namespace
            bindableName = BindableHelper.getBackingPropertyName(funcName, "_" + this.classDefinition.getQualifiedName());
            wasOverride = funcDef.isOverride();
            funcDef.unsetOverride();
        }
      
        //  Save the constructor function until
        //  we've seen all the instance variables
        //  that might need initialization.
        if ( is_constructor )
        {
            if (this.ctorFunction == null)
                this.ctorFunction = func;
            else
            {
                // If we already have a ctor, must be multiply defined. Ignore it and generate problem
                String name = this.className.getBaseName();
                classScope.addProblem( new MultipleContructorDefinitionsProblem(func, name));
            }
        }
        else
        {
            LexicalScope ls = funcDef.isStatic()? classStaticScope: classScope;

            MethodInfo mi = classScope.getGenerator().generateFunction(func, ls, null, bindableName);
           
            if ( mi != null )
            {
                ITraitVisitor tv = ls.traitsVisitor.visitMethodTrait(functionTraitKind(func, TRAIT_Method),
                        bindableName != null ? bindableName : funcName, 0, mi);
               
                if (funcName != null && bindableName == null)
                    classScope.getMethodBodySemanticChecker().checkFunctionForConflictingDefinitions(func, funcDef);

                if ( ! funcDef.isStatic() && bindableName == null)
                    if (funcDef.getNamespaceReference() instanceof NamespaceDefinition.IProtectedNamespaceDefinition)
                        this.iinfo.flags |= ABCConstants.CLASS_FLAG_protected;

                ls.processMetadata(tv, getAllMetaTags(funcDef));
               
                if ( func.hasModifier(ASModifier.FINAL))
                    tv.visitAttribute(Trait.TRAIT_FINAL, Boolean.TRUE);
                // don't set override if we've moved it to the bindable namespace
                if (!wasOverride && (func.hasModifier(ASModifier.OVERRIDE) || funcDef.isOverride()))
                    tv.visitAttribute(Trait.TRAIT_OVERRIDE, Boolean.TRUE);
                tv.visitEnd();
            }
        }
        if (isBindable)
        {
            if (wasOverride)
                funcDef.setOverride();
            if (funcDef instanceof GetterDefinition)
            {
                DefinitionBase bindableGetter = func.buildBindableGetter(funcName.getBaseName());
                ASScope funcScope = (ASScope)funcDef.getContainingScope();
                bindableGetter.setContainingScope(funcScope);
                LexicalScope ls = funcDef.isStatic()? classStaticScope: classScope;
                ls.generateBindableGetter(bindableGetter, funcName, bindableName,
                                        funcDef.resolveType(project).getMName(project), getAllMetaTags(funcDef));
            }
            else
            {
                TypeDefinitionBase typeDef = funcDef.resolveType(project);
                ASScope funcScope = (ASScope)funcDef.getContainingScope();
                DefinitionBase bindableSetter = func.buildBindableSetter(funcName.getBaseName(),
                        funcScope,
                        funcDef.getTypeReference());
                bindableSetter.setContainingScope(funcScope);
                LexicalScope ls = funcDef.isStatic()? classStaticScope: classScope;
                ls.generateBindableSetter(bindableSetter, funcName, bindableName,
                        typeDef.getMName(project), getAllMetaTags(funcDef))
            }
        }
    }
View Full Code Here

     */
    void functionSemanticChecks(FunctionNode node)
    {
        verifyFunctionModifiers(node);

        FunctionDefinition func = node.getDefinition();

        Collection<ICompilerProblem> problems = classScope.getProblems();

        // code model has some peculiar ideas about what makes a function a constructor or not
        boolean looks_like_ctor = func.isConstructor();
        looks_like_ctor |= func.getBaseName() != null && this.className != null && func.getBaseName().equals(this.className.getBaseName());

        if (! looks_like_ctor && (func.getBaseName() != null))
        {
            SemanticUtils.checkScopedToDefaultNamespaceProblem(classScope, node, func, this.classDefinition.getQualifiedName());
        }
        if( looks_like_ctor )
        {
            // If a constructor has a namespace as part of it's declaration, it must be declared public.
            // It is ok to omit the namespace
            // We must check the AST, as CM treats all ctors as public no matter what the user typed in
            // so the FunctionDefinition will always be in the public namespace
            if( node.getActualNamespaceNode() != null &&
                    node.getActualNamespaceNode().getName() != IASKeywordConstants.PUBLIC)
                problems.add(new ConstructorMustBePublicProblem(node.getActualNamespaceNode()));

            // A constructor cannot be static
            if( func.isStatic() )
                problems.add(new ConstructorIsStaticProblem(node));

            // A constructor cannot declare a return type, other than void.
            IExpressionNode returnTypeExpression = node.getReturnTypeNode();
            if (returnTypeExpression != null)
            {
                // We cannot check whether node.resolveReturnType() returns the definition
                // for the void type, because  the return type of a constructor is considered
                // to be the class of the object being constructed, rather than void.
                // So instead we simply check whether the type annotation was void.
                boolean returnTypeIsVoid = false;
                if (returnTypeExpression instanceof ILanguageIdentifierNode)
                {
                    LanguageIdentifierKind kind = ((ILanguageIdentifierNode)returnTypeExpression).getKind();
                    if (kind == LanguageIdentifierKind.VOID)
                        returnTypeIsVoid = true;
                }
                if (!returnTypeIsVoid)
                {
                    ICompilerProblem problem = new ConstructorCannotHaveReturnTypeProblem(returnTypeExpression);
                    problems.add(problem);
                }
            }

            // Is it a getter or setter that appears to be the constructor?
            if( func instanceof IAccessorDefinition )
                problems.add(new ConstructorIsGetterSetterProblem(node.getNameExpressionNode()));
        }
        else if( !func.isStatic() )
        {
            // We have to find the (potentially) overriden function whether we are an override or
            // not/
            FunctionDefinition override = func.resolveOverriddenFunction(classScope.getProject());
            if( func.isOverride() )
            {
                if( override == null )
                {
                    // Didn't find the function we are supposed to be overriding
                    problems.add(new OverrideNotFoundProblem(node.getNameExpressionNode()));
                }
                else
                {
                    if( !func.hasCompatibleSignature(override, classScope.getProject()) )
                    {
                        // Signatures didn't match
                        problems.add(new IncompatibleOverrideProblem(node.getNameExpressionNode()));
                    }
                    if( override.isFinal() )
                    {
                        // overriding final
                        problems.add(new OverrideFinalProblem(node.getNameExpressionNode()));
                    }
                }
View Full Code Here

    @Override
    protected void analyze(EnumSet<PostProcessStep> set, ASScope scope, Collection<ICompilerProblem> problems)
    {
        super.analyze(set, scope, problems);

        FunctionDefinition definition = getDefinition();
        if (definition != null)
        {
            ParameterDefinition[] parameters = definition.getParameters();
            if (parameters != null && parameters.length > 0)
                definition.setTypeReference(parameters[0].getTypeReference());
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.flex.compiler.internal.definitions.FunctionDefinition

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.