public override TypeDefinitionBase BaseType()
    {
        if (resolvingBaseType)
        {
            return(null);
        }
        resolvingBaseType = true;

        if (baseTypeConstraint != null && (baseTypeConstraint.Definition == null || !baseTypeConstraint.Definition.IsValid()) ||
            interfacesConstraint != null && interfacesConstraint.Exists(x => x.Definition == null || !x.Definition.IsValid()))
        {
            baseTypeConstraint   = null;
            interfacesConstraint = null;
        }

        if (baseTypeConstraint == null && interfacesConstraint == null)
        {
            interfacesConstraint = new List <SymbolReference>();

            SyntaxTreeNode_Rule clauseNode = null;
            if (declarations != null)
            {
                for (var i = 0; i < declarations.Count; i++)
                {
                    var d = declarations[i];
                    if (d != null && d.IsValid())
                    {
                        SyntaxTreeNode_Rule constraintsNode = null;
                        var typeParameterListNode           = d.parseTreeNode.Parent;
                        var parentRuleName = typeParameterListNode.Parent.RuleName;
                        if (parentRuleName == "structDeclaration" ||
                            parentRuleName == "classDeclaration" ||
                            parentRuleName == "interfaceDeclaration" ||
                            parentRuleName == "delegateDeclaration" ||
                            parentRuleName == "interfaceMethodDeclaration")
                        {
                            constraintsNode = typeParameterListNode.Parent.FindChildByName("typeParameterConstraintsClauses") as SyntaxTreeNode_Rule;
                        }
                        else if (parentRuleName == "qidStart" || parentRuleName == "qidPart")
                        {
                            constraintsNode = typeParameterListNode.Parent
                                              .Parent // qid
                                              .Parent // memberName
                                              .Parent // methodHeader
                                              .FindChildByName("typeParameterConstraintsClauses") as SyntaxTreeNode_Rule;
                        }

                        if (constraintsNode != null)
                        {
                            for (var j = 0; j < constraintsNode.NumValidNodes; j++)
                            {
                                clauseNode = constraintsNode.NodeAt(j);
                                if (clauseNode != null && clauseNode.NumValidNodes == 4)
                                {
                                    var c = clauseNode.NodeAt(1);
                                    if (c != null && c.NumValidNodes == 1)
                                    {
                                        var id = DecodeId(c.LeafAt(0).token.text);
                                        if (id == name)
                                        {
                                            break;
                                        }
                                    }
                                }
                                clauseNode = null;
                            }
                        }
                        break;
                    }
                }
            }

            if (clauseNode != null)
            {
                var constrantListNode = clauseNode.NodeAt(3);
                if (constrantListNode != null)
                {
                    var secondaryList = constrantListNode.NodeAt(-1);
                    if (secondaryList != null && secondaryList.RuleName == "secondaryConstraintList")
                    {
                        for (int i = 0; i < secondaryList.NumValidNodes; i += 2)
                        {
                            var constraintNode = secondaryList.NodeAt(i);
                            if (constraintNode != null)
                            {
                                var typeNameNode = constraintNode.NodeAt(0);
                                if (typeNameNode != null)
                                {
                                    if (baseTypeConstraint == null && interfacesConstraint.Count == 0)
                                    {
                                        var resolvedType = ResolveNode(typeNameNode, null, null, 0, true) as TypeDefinitionBase;
                                        if (resolvedType != null && resolvedType.kind != SymbolKind.Error)
                                        {
                                            if (resolvedType.kind == SymbolKind.Interface)
                                            {
                                                interfacesConstraint.Add(new SymbolReference(typeNameNode));
                                            }
                                            else
                                            {
                                                baseTypeConstraint = new SymbolReference(typeNameNode);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        interfacesConstraint.Add(new SymbolReference(typeNameNode));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        var result = baseTypeConstraint != null ? baseTypeConstraint.Definition as TypeDefinitionBase : base.BaseType();

        if (result == this)
        {
            baseTypeConstraint = new SymbolReference(circularBaseType);
            result             = circularBaseType;
        }
        resolvingBaseType = false;
        return(result);
    }
    public SyntaxTreeNode_Base NameNode()
    {
        if (parseTreeNode == null || parseTreeNode.NumValidNodes == 0)
        {
            return(null);
        }

        SyntaxTreeNode_Base nameNode = null;

        switch (parseTreeNode.RuleName)
        {
        case "Declaration_Namespace":
            nameNode = parseTreeNode.ChildAt(1);
            var nameNodeAsNode = nameNode as SyntaxTreeNode_Rule;
            if (nameNodeAsNode != null && nameNodeAsNode.NumValidNodes != 0)
            {
                nameNode = nameNodeAsNode.ChildAt(-1) ?? nameNode;
            }
            break;

        case "Directive_UsingAlias":
            nameNode = parseTreeNode.ChildAt(0);
            break;

        case "interfaceDeclaration":
        case "structDeclaration":
        case "classDeclaration":
        case "enumDeclaration":
            nameNode = parseTreeNode.ChildAt(1);
            break;

        case "delegateDeclaration":
            nameNode = parseTreeNode.ChildAt(2);
            break;

        case "eventDeclarator":
        case "eventWithAccessorsDeclaration":
        case "propertyDeclaration":
        case "interfacePropertyDeclaration":
        case "variableDeclarator":
        case "localVariableDeclarator":
        case "constantDeclarator":
        case "interfaceMethodDeclaration":
        case "catchExceptionIdentifier":
            nameNode = parseTreeNode.ChildAt(0);
            break;

        case "methodDeclaration":
        case "constructorDeclaration":
            var methodHeaderNode = parseTreeNode.NodeAt(0);
            if (methodHeaderNode != null && methodHeaderNode.NumValidNodes > 0)
            {
                nameNode = methodHeaderNode.ChildAt(0);
            }
            break;

        case "methodHeader":
        case "constructorDeclarator":
            nameNode = parseTreeNode.ChildAt(0);
            break;

        case "destructorDeclarator":
            nameNode = parseTreeNode.FindChildByName("IDENTIFIER");
            break;

        case "fixedParameter":
        case "operatorParameter":
        case "parameterArray":
        case "explicitAnonymousFunctionParameter":
            nameNode = parseTreeNode.FindChildByName("NAME");
            break;

        case "implicitAnonymousFunctionParameter":
            nameNode = parseTreeNode.ChildAt(0);
            break;

        case "typeParameter":
            nameNode = parseTreeNode.ChildAt(0);
            break;

        case "enumMemberDeclaration":
            if (parseTreeNode.ChildAt(0) is SyntaxTreeNode_Rule)
            {
                nameNode = parseTreeNode.ChildAt(1);
            }
            else
            {
                nameNode = parseTreeNode.ChildAt(0);
            }
            break;

        case "statementList":
            return(null);

        case "lambdaExpression":
        case "anonymousMethodExpression":
            return(parseTreeNode);

        case "interfaceTypeList":
            nameNode = parseTreeNode.ChildAt(0);
            break;

        case "foreachStatement":
        case "fromClause":
            nameNode = parseTreeNode.FindChildByName("NAME");
            break;

        case "getAccessorDeclaration":
        case "interfaceGetAccessorDeclaration":
        case "setAccessorDeclaration":
        case "interfaceSetAccessorDeclaration":
        case "addAccessorDeclaration":
        case "removeAccessorDeclaration":
            nameNode = parseTreeNode.FindChildByName("IDENTIFIER");
            break;

        case "indexerDeclaration":
        case "interfaceIndexerDeclaration":
        case "labeledStatement":
            return(parseTreeNode.ChildAt(0));

        case "conversionOperatorDeclarator":
        case "operatorDeclarator":
        case "Directive_UsingNamespace":
        case "typeParameterConstraintsClause":
            return(null);

        default:
            Debug.LogWarning("Don't know how to extract symbol name from: " + parseTreeNode);
            return(null);
        }
        return(nameNode);
    }
예제 #3
0
    public override TypeDefinitionBase BaseType()
    {
        if (resolvingBaseType)
        {
            return(null);
        }
        resolvingBaseType = true;

        if (baseType != null && (baseType.Definition == null || !baseType.Definition.IsValid()) ||
            interfaces != null && interfaces.Exists(x => x.Definition == null || !x.Definition.IsValid()))
        {
            baseType   = null;
            interfaces = null;
        }

        if (baseType == null && interfaces == null)
        {
            interfaces = new List <SymbolReference>();

            SyntaxTreeNode_Rule baseNode          = null;
            SyntaxTreeNode_Rule interfaceListNode = null;
            SymbolDeclaration   decl = null;
            if (declarations != null)
            {
                foreach (var d in declarations)
                {
                    if (d != null)
                    {
                        baseNode = (SyntaxTreeNode_Rule)d.parseTreeNode.FindChildByName(
                            d.kind == SymbolKind.Class ? "classBase" :
                            d.kind == SymbolKind.Struct ? "structInterfaces" :
                            "interfaceBase");
                        interfaceListNode = baseNode != null?baseNode.NodeAt(1) : null;

                        if (baseNode != null)
                        {
                            decl = d;
                            break;
                        }
                    }
                }
            }

            if (decl != null)
            {
                switch (decl.kind)
                {
                case SymbolKind.Class:
                    if (interfaceListNode != null)
                    {
                        baseType = new SymbolReference(interfaceListNode.ChildAt(0));
                        if (baseType.Definition.kind == SymbolKind.Interface)
                        {
                            interfaces.Add(baseType);
                            baseType = this != builtInTypes_object?ReflectedTypeReference.ForType(typeof(object)) : null;
                        }

                        for (var i = 2; i < interfaceListNode.NumValidNodes; i += 2)
                        {
                            interfaces.Add(new SymbolReference(interfaceListNode.ChildAt(i)));
                        }
                    }
                    else
                    {
                        baseType = this != builtInTypes_object?ReflectedTypeReference.ForType(typeof(object)) : null;
                    }
                    break;

                case SymbolKind.Struct:
                case SymbolKind.Interface:
                    baseType = decl.kind == SymbolKind.Struct ?
                               ReflectedTypeReference.ForType(typeof(ValueType)) :
                               ReflectedTypeReference.ForType(typeof(object));
                    if (interfaceListNode != null)
                    {
                        for (var i = 0; i < interfaceListNode.NumValidNodes; i += 2)
                        {
                            interfaces.Add(new SymbolReference(interfaceListNode.ChildAt(i)));
                        }
                    }
                    break;

                case SymbolKind.Enum:
                    baseType = ReflectedTypeReference.ForType(typeof(Enum));
                    break;

                case SymbolKind.Delegate:
                    baseType = ReflectedTypeReference.ForType(typeof(MulticastDelegate));
                    break;
                }
            }
            //Debug.Log("BaseType() of " + this + " is " + (baseType != null ? baseType.definition.ToString() : "null"));
        }

        var result = baseType != null ? baseType.Definition as TypeDefinitionBase : base.BaseType();

        if (result == this)
        {
            baseType = new SymbolReference(circularBaseType);
            result   = circularBaseType;
        }
        resolvingBaseType = false;
        return(result);
    }
    public static int ProcessArgumentListNode(SyntaxTreeNode_Rule argumentListNode, out Modifiers[] modifiers, out List <TypeDefinitionBase> argumentTypes, TypeDefinitionBase extendedType)
    {
        var numArguments = argumentListNode == null ? 0 : (argumentListNode.NumValidNodes + 1) / 2;
        var thisOffest   = 0;

        if (extendedType != null)
        {
            thisOffest = 1;
            ++numArguments;
        }

        modifiers     = new Modifiers[numArguments];
        argumentTypes = new List <TypeDefinitionBase>();
        var resolvedArguments = new SymbolDefinition[numArguments];

        if (extendedType != null)
        {
            argumentTypes.Add(extendedType);
        }

        for (var i = thisOffest; i < numArguments; ++i)
        {
            var argumentNode = argumentListNode.NodeAt((i - thisOffest) * 2);
            if (argumentNode != null)
            {
                var argumentValueNode = argumentNode.FindChildByName("argumentValue") as SyntaxTreeNode_Rule;
                if (argumentValueNode != null)
                {
                    resolvedArguments[i] = ResolveNode(argumentValueNode);
                    if (resolvedArguments[i] != null)
                    {
                        argumentTypes.Add(resolvedArguments[i].TypeOf() as TypeDefinitionBase ?? unknownType);
                    }
                    else
                    {
                        argumentTypes.Add(unknownType);
                    }

                    var modifierLeaf = argumentValueNode.LeafAt(0);
                    if (modifierLeaf != null)
                    {
                        if (modifierLeaf.IsLit("ref"))
                        {
                            modifiers[i] = Modifiers.Ref;
                        }
                        else if (modifierLeaf.IsLit("out"))
                        {
                            modifiers[i] = Modifiers.Out;
                        }
                    }

                    continue;
                }
            }

            numArguments = i;
            break;
        }

        return(numArguments);
    }