Exemplo n.º 1
0
        private Statement ProcessForeachStatement(ForeachNode node)
        {
            TypeSymbol type = _symbolSet.ResolveType(node.Type, _symbolTable, _memberContext);

            Debug.Assert(type != null);

            bool dictionaryContainer = (type.Name == "DictionaryEntry") || (type.Name == "KeyValuePair`2");

            Expression collectionExpression = _expressionBuilder.BuildExpression(node.Container);

            if (collectionExpression is MemberExpression)
            {
                collectionExpression = _expressionBuilder.TransformMemberExpression((MemberExpression)collectionExpression);
            }

            ForInStatement statement;

            if (dictionaryContainer)
            {
                VariableSymbol dictionaryVariable = null;

                if (collectionExpression.Type != ExpressionType.Local)
                {
                    string dictionaryVariableName = _symbolTable.CreateSymbolName("dict");
                    dictionaryVariable = new VariableSymbol(dictionaryVariableName, _memberContext,
                                                            collectionExpression.EvaluatedType);
                }

                statement = new ForInStatement(collectionExpression, dictionaryVariable);

                string         keyVariableName = _symbolTable.CreateSymbolName("key");
                VariableSymbol keyVariable     = new VariableSymbol(keyVariableName, _memberContext,
                                                                    _symbolSet.ResolveIntrinsicType(IntrinsicType.String));
                statement.SetLoopVariable(keyVariable);
            }
            else
            {
                statement = new ForInStatement(collectionExpression);

                string         enumeratorVariableName = _symbolTable.CreateSymbolName("enum");
                VariableSymbol enumVariable           = new VariableSymbol(enumeratorVariableName, _memberContext,
                                                                           _symbolSet.ResolveIntrinsicType(IntrinsicType.IEnumerator));
                statement.SetLoopVariable(enumVariable);
            }

            _symbolTable.PushScope();

            VariableSymbol itemVariable = new VariableSymbol(node.Name.Name, _memberContext, type);

            _symbolTable.AddSymbol(itemVariable);
            statement.SetItemVariable(itemVariable);

            Statement body = BuildStatement((StatementNode)node.Body);

            statement.AddBody(body);

            _symbolTable.PopScope();

            return(statement);
        }
Exemplo n.º 2
0
        private void ImportEnumFields(TypeSymbol enumTypeSymbol)
        {
            TypeDefinition type = (TypeDefinition)enumTypeSymbol.MetadataReference;

            foreach (FieldDefinition field in type.Fields)
            {
                if (field.IsSpecialName)
                {
                    continue;
                }

                Debug.Assert(enumTypeSymbol is EnumerationSymbol);
                EnumerationSymbol enumSymbol = (EnumerationSymbol)enumTypeSymbol;

                TypeSymbol fieldType;

                if (enumSymbol.UseNamedValues)
                {
                    fieldType = symbols.ResolveIntrinsicType(IntrinsicType.String);
                }
                else
                {
                    fieldType = symbols.ResolveIntrinsicType(IntrinsicType.Integer);
                }

                string fieldName = field.Name;

                EnumerationFieldSymbol fieldSymbol =
                    new EnumerationFieldSymbol(fieldName, enumTypeSymbol, field.Constant, fieldType);
                ImportMemberDetails(fieldSymbol, null, field);

                enumTypeSymbol.AddMember(fieldSymbol);
            }
        }
Exemplo n.º 3
0
        private EnumerationFieldSymbol BuildEnumField(EnumerationFieldNode fieldNode, TypeSymbol typeSymbol)
        {
            Debug.Assert(typeSymbol is EnumerationSymbol);
            EnumerationSymbol enumSymbol = (EnumerationSymbol)typeSymbol;

            TypeSymbol fieldTypeSymbol;

            if (enumSymbol.UseNamedValues)
            {
                fieldTypeSymbol = _symbols.ResolveIntrinsicType(IntrinsicType.String);
            }
            else
            {
                fieldTypeSymbol = _symbols.ResolveIntrinsicType(IntrinsicType.Integer);
            }

            EnumerationFieldSymbol fieldSymbol = new EnumerationFieldSymbol(fieldNode.Name, typeSymbol, fieldNode.Value, fieldTypeSymbol);

            BuildMemberDetails(fieldSymbol, typeSymbol, fieldNode, fieldNode.Attributes);

            return(fieldSymbol);
        }
Exemplo n.º 4
0
        public void GenerateScriptMetadata(SymbolSet symbolSet)
        {
            Debug.Assert(symbolSet != null);
            TypeSymbol nullableType = symbolSet.ResolveIntrinsicType(IntrinsicType.Nullable);

            var types = CollectEmittableTypes(symbolSet)
                        .OrderBy(t => t, typeComparer);

            List <string> dependencies = GenerateDependenciesList(symbolSet);

            Writer.Write("ss.registerMetadataImporter(function(){");
            Writer.Write("(function(");
            Writer.Write(string.Join(",", dependencies));
            Writer.WriteLine(") {");
            Writer.Indent++;
            Writer.WriteLine("\"use strict\"");
            Writer.WriteLine("var Void = null;");
            Writer.WriteLine("var Type = Function;");
            Writer.WriteLine($"var module = {DSharpStringResources.DSHARP_SCRIPT_NAME}.modules['{symbols.ScriptName}'];");

            Writer.WriteLine("function setTypeMetadata(typeName, members) {");
            Writer.Indent++;

            Writer.WriteLine("if(module[typeName])");
            Writer.Indent++;
            Writer.WriteLine("module[typeName].$members = members");
            Writer.Indent--;
            Writer.WriteLine("}");
            Writer.Indent--;

            foreach (TypeSymbol type in types)
            {
                if (GetMembers(type) is IEnumerable <MemberSymbol> members && members.Any())
                {
                    Writer.WriteLine($"setTypeMetadata('{type.GeneratedName}', [");
                    Writer.Indent++;
                    WriteMembers(members, nullableType);
                    Writer.WriteLine("]");
                    Writer.Indent--;
                    Writer.WriteLine(");");
                }
            }

            Writer.Indent--;
            Writer.Write("})(");
            Writer.Write(string.Join(",", dependencies.Select(d => d != DSharpStringResources.DSHARP_SCRIPT_NAME
                ? $"ss.dependency('{d}')"
                : DSharpStringResources.DSHARP_SCRIPT_NAME)));
            Writer.Write(")});");
        }
Exemplo n.º 5
0
        private static string GetParameterTypeName(TypeSymbol parameterType)
        {
            SymbolSet  symbolSet    = parameterType.SymbolSet;
            TypeSymbol nullableType = symbolSet.ResolveIntrinsicType(IntrinsicType.Nullable);

            if (parameterType.FullName == nullableType.FullName)
            {
                parameterType = parameterType.GenericArguments.First();
            }

            TypeSymbol typeType     = symbolSet.ResolveIntrinsicType(IntrinsicType.Type);
            TypeSymbol functionType = symbolSet.ResolveIntrinsicType(IntrinsicType.Function);

            if (parameterType.FullName == typeType.FullName || parameterType.Type == SymbolType.Delegate)
            {
                parameterType = functionType;
            }

            if (parameterType.Type == SymbolType.Enumeration)
            {
                EnumerationSymbol enumType = (EnumerationSymbol)parameterType;

                if (enumType.UseNamedValues)
                {
                    TypeSymbol stringType = symbolSet.ResolveIntrinsicType(IntrinsicType.String);
                    parameterType = stringType;
                }
                else
                {
                    TypeSymbol numberType = symbolSet.ResolveIntrinsicType(IntrinsicType.Number);
                    parameterType = numberType;
                }
            }

            return(parameterType.FullGeneratedName);
        }
Exemplo n.º 6
0
        public SymbolImplementation BuildField(FieldSymbol fieldSymbol)
        {
            rootScope    = new SymbolScope((ISymbolTable)fieldSymbol.Parent);
            currentScope = rootScope;

            Expression initializerExpression = null;

            FieldDeclarationNode fieldDeclarationNode = (FieldDeclarationNode)fieldSymbol.ParseContext;

            Debug.Assert(fieldDeclarationNode != null);

            VariableInitializerNode initializerNode = (VariableInitializerNode)fieldDeclarationNode.Initializers[0];

            if (initializerNode.Value != null)
            {
                ExpressionBuilder expressionBuilder = new ExpressionBuilder(this, fieldSymbol, errorHandler, options);
                initializerExpression = expressionBuilder.BuildExpression(initializerNode.Value);

                if (initializerExpression is MemberExpression)
                {
                    initializerExpression =
                        expressionBuilder.TransformMemberExpression((MemberExpression)initializerExpression);
                }
            }
            else
            {
                TypeSymbol fieldType = fieldSymbol.AssociatedType;
                SymbolSet  symbolSet = fieldSymbol.SymbolSet;

                if (GetDefaultValue(fieldType, symbolSet) is object defaultValue)
                {
                    initializerExpression =
                        new LiteralExpression(symbolSet.ResolveIntrinsicType(IntrinsicType.Object),
                                              defaultValue);
                    fieldSymbol.SetImplementationState(hasInitializer: true);
                }
            }

            if (initializerExpression != null)
            {
                List <Statement> statements = new List <Statement>();
                statements.Add(new ExpressionStatement(initializerExpression, /* isFragment */ true));

                return(new SymbolImplementation(statements, null, "this"));
            }

            return(null);
        }
Exemplo n.º 7
0
        public SymbolImplementation BuildField(FieldSymbol fieldSymbol)
        {
            rootScope    = new SymbolScope((ISymbolTable)fieldSymbol.Parent);
            currentScope = rootScope;

            Expression initializerExpression = null;

            FieldDeclarationNode fieldDeclarationNode = (FieldDeclarationNode)fieldSymbol.ParseContext;

            Debug.Assert(fieldDeclarationNode != null);

            VariableInitializerNode initializerNode = (VariableInitializerNode)fieldDeclarationNode.Initializers[0];

            if (initializerNode.Value != null)
            {
                ExpressionBuilder expressionBuilder = new ExpressionBuilder(this, fieldSymbol, errorHandler, options);
                initializerExpression = expressionBuilder.BuildExpression(initializerNode.Value);

                if (initializerExpression is MemberExpression)
                {
                    initializerExpression =
                        expressionBuilder.TransformMemberExpression((MemberExpression)initializerExpression);
                }
            }
            else
            {
                object defaultValue = null;

                TypeSymbol fieldType = fieldSymbol.AssociatedType;
                SymbolSet  symbolSet = fieldSymbol.SymbolSet;

                if (fieldType.Type == SymbolType.Enumeration)
                {
                    // The default for named values is null, so this only applies to
                    // regular enum types

                    EnumerationSymbol enumType = (EnumerationSymbol)fieldType;

                    if (enumType.UseNamedValues == false)
                    {
                        defaultValue = 0;
                    }
                }
                else if (fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.Integer) ||
                         fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.UnsignedInteger) ||
                         fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.Long) ||
                         fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.UnsignedLong) ||
                         fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.Short) ||
                         fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.UnsignedShort) ||
                         fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.Byte) ||
                         fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.SignedByte) ||
                         fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.Double) ||
                         fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.Single) ||
                         fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.Decimal))
                {
                    defaultValue = 0;
                }
                else if (fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.Boolean))
                {
                    defaultValue = false;
                }

                if (defaultValue != null)
                {
                    initializerExpression =
                        new LiteralExpression(symbolSet.ResolveIntrinsicType(IntrinsicType.Object),
                                              defaultValue);
                    fieldSymbol.SetImplementationState(/* hasInitializer */ true);
                }
            }

            if (initializerExpression != null)
            {
                List <Statement> statements = new List <Statement>();
                statements.Add(new ExpressionStatement(initializerExpression, /* isFragment */ true));

                return(new SymbolImplementation(statements, null, "this"));
            }

            return(null);
        }
Exemplo n.º 8
0
        public static object GetDefaultValue(TypeSymbol type, SymbolSet symbolSet)
        {
            if (type.Type == SymbolType.Enumeration)
            {
                // The default for named values is null, so this only applies to
                // regular enum types

                EnumerationSymbol enumType = (EnumerationSymbol)type;

                if (enumType.UseNamedValues == false)
                {
                    return(0);
                }
            }
            else if (type == symbolSet.ResolveIntrinsicType(IntrinsicType.Integer) ||
                     type == symbolSet.ResolveIntrinsicType(IntrinsicType.UnsignedInteger) ||
                     type == symbolSet.ResolveIntrinsicType(IntrinsicType.Long) ||
                     type == symbolSet.ResolveIntrinsicType(IntrinsicType.UnsignedLong) ||
                     type == symbolSet.ResolveIntrinsicType(IntrinsicType.Short) ||
                     type == symbolSet.ResolveIntrinsicType(IntrinsicType.UnsignedShort) ||
                     type == symbolSet.ResolveIntrinsicType(IntrinsicType.Byte) ||
                     type == symbolSet.ResolveIntrinsicType(IntrinsicType.SignedByte) ||
                     type == symbolSet.ResolveIntrinsicType(IntrinsicType.Double) ||
                     type == symbolSet.ResolveIntrinsicType(IntrinsicType.Single) ||
                     type == symbolSet.ResolveIntrinsicType(IntrinsicType.Decimal))
            {
                return(0);
            }
            else if (type == symbolSet.ResolveIntrinsicType(IntrinsicType.Boolean))
            {
                return(false);
            }

            return(null);
        }
Exemplo n.º 9
0
 public static LiteralExpression GetDefaultValueExpression(TypeSymbol type, SymbolSet symbolSet)
 {
     return(new LiteralExpression(symbolSet.ResolveIntrinsicType(IntrinsicType.Object),
                                  GetDefaultValue(type, symbolSet)));
 }