Пример #1
0
        private bool AddType(TypeDefinitionSyntax pDefinition)
        {
            //Check for duplicate type definitions
            pDefinition.EmitOrder = _order++;
            var name = SyntaxHelper.GetFullTypeName(pDefinition.DeclaredType);

            if (_unit.IsTypeDefined(name))
            {
                CompilerErrors.DuplicateType(name, pDefinition.Span);
                return(true);
            }

            //Ensure types on base struct
            for (int i = 0; i < pDefinition.Fields.Count; i++)
            {
                //Ensure all types on the field have been found
                var f        = pDefinition.Fields[i];
                var typeName = SyntaxHelper.GetFullTypeName(f.TypeNode);
                if (!pDefinition.TypeParameters.Contains(typeName))
                {
                    var result = _unit.FromString(f.TypeNode, out SmallType fieldType);
                    if (result != Compiler.FindResult.Found)
                    {
                        return(false);
                    }

                    f.TypeNode.SetType(fieldType);
                }
            }

            //Ensure types on any implements
            List <TypeDefinitionSyntax> implements = null;

            if (_implements.ContainsKey(pDefinition.Name))
            {
                implements = _implements[pDefinition.Name];
                foreach (var impl in implements)
                {
                    for (int i = 0; i < impl.Fields.Count; i++)
                    {
                        //Ensure all types on the field have been found
                        var f        = impl.Fields[i];
                        var typeName = SyntaxHelper.GetFullTypeName(f.TypeNode);
                        if (!pDefinition.TypeParameters.Contains(typeName))
                        {
                            var result = _unit.FromString(f.TypeNode, out SmallType fieldType);
                            if (result != Compiler.FindResult.Found)
                            {
                                return(false);
                            }

                            f.TypeNode.SetType(fieldType);
                        }
                    }
                }
            }

            //Duplicate fields get checked in AddType
            //Other validate like ensuring fields/methods are present is done later after all types have been found
            //See ValidateImplementation
            _unit.AddType(pDefinition, implements);
            return(true);
        }