Esempio n. 1
0
 protected MethodExpression(ExpressionType type, Expression objectReference, MethodSymbol method, Collection<Expression> parameters) :
     base(type, (method.AssociatedType.Type == SymbolType.GenericParameter ? objectReference.EvaluatedType : method.AssociatedType),
          SymbolFilter.Public | SymbolFilter.InstanceMembers) {
     _method = method;
     _objectReference = objectReference;
     _parameters = (parameters == null) ? new Collection<Expression>() : parameters;
 }
Esempio n. 2
0
        private ParameterSymbol BuildParameter(ParameterNode parameterNode, MethodSymbol methodSymbol) {
            ParameterMode parameterMode = ParameterMode.In;

            if ((parameterNode.Flags == ParameterFlags.Out) ||
                (parameterNode.Flags == ParameterFlags.Ref)) {
                parameterMode = ParameterMode.InOut;
            }
            else if (parameterNode.Flags == ParameterFlags.Params) {
                parameterMode = ParameterMode.List;
            }

            TypeSymbol parameterType = methodSymbol.SymbolSet.ResolveType(parameterNode.Type, _symbolTable, methodSymbol);
            Debug.Assert(parameterType != null);

            if (parameterType != null) {
                return new ParameterSymbol(parameterNode.Name, methodSymbol, parameterType, parameterMode);
            }

            return null;
        }
Esempio n. 3
0
        private MethodSymbol BuildMethod(MethodDeclarationNode methodNode, TypeSymbol typeSymbol) {
            MethodSymbol method = null;
            if (methodNode.NodeType == ParseNodeType.ConstructorDeclaration) {
                method = new ConstructorSymbol(typeSymbol, (methodNode.Modifiers & Modifiers.Static) != 0);
            }
            else {
                TypeSymbol returnType = typeSymbol.SymbolSet.ResolveType(methodNode.Type, _symbolTable, typeSymbol);
                Debug.Assert(returnType != null);

                if (returnType != null) {
                    method = new MethodSymbol(methodNode.Name, typeSymbol, returnType);
                    BuildMemberDetails(method, typeSymbol, methodNode, methodNode.Attributes);

                    ICollection<string> conditions = null;
                    foreach (AttributeNode attrNode in methodNode.Attributes) {
                        if (attrNode.TypeName.Equals("Conditional", StringComparison.Ordinal)) {
                            if (conditions == null) {
                                conditions = new List<string>();
                            }

                            Debug.Assert(attrNode.Arguments[0] is LiteralNode);
                            Debug.Assert(((LiteralNode)attrNode.Arguments[0]).Value is string);

                            conditions.Add((string)((LiteralNode)attrNode.Arguments[0]).Value);
                        }
                    }

                    if (conditions != null) {
                        method.SetConditions(conditions);
                    }
                }
            }

            if (method != null) {
                if ((methodNode.Modifiers & Modifiers.Abstract) != 0) {
                    method.SetImplementationState(SymbolImplementationFlags.Abstract);
                }
                else if ((methodNode.Modifiers & Modifiers.Override) != 0) {
                    method.SetImplementationState(SymbolImplementationFlags.Override);
                }

                if ((methodNode.Parameters != null) && (methodNode.Parameters.Count != 0)) {
                    foreach (ParameterNode parameterNode in methodNode.Parameters) {
                        ParameterSymbol paramSymbol = BuildParameter(parameterNode, method);
                        if (paramSymbol != null) {
                            paramSymbol.SetParseContext(parameterNode);
                            method.AddParameter(paramSymbol);
                        }
                    }
                }

                if ((method.Visibility & MemberVisibility.Static) != 0) {
                    string scriptAlias = GetAttributeValue(methodNode.Attributes, "ScriptAlias");
                    if (scriptAlias != null) {
                        method.SetAlias(scriptAlias);
                    }
                }
            }

            return method;
        }
Esempio n. 4
0
        private void BuildMembers(TypeSymbol typeSymbol) {
            if (typeSymbol.Type == SymbolType.Delegate) {
                DelegateTypeNode delegateNode = (DelegateTypeNode)typeSymbol.ParseContext;

                TypeSymbol returnType = typeSymbol.SymbolSet.ResolveType(delegateNode.ReturnType, _symbolTable, typeSymbol);
                Debug.Assert(returnType != null);

                if (returnType != null) {
                    MethodSymbol invokeMethod = new MethodSymbol("Invoke", typeSymbol, returnType, MemberVisibility.Public);
                    invokeMethod.SetTransformedName(String.Empty);

                    // Mark the method as abstract, as there is no actual implementation of the method
                    // to be generated
                    invokeMethod.SetImplementationState(SymbolImplementationFlags.Abstract);

                    typeSymbol.AddMember(invokeMethod);
                }
                return;
            }

            CustomTypeNode typeNode = (CustomTypeNode)typeSymbol.ParseContext;

            foreach (MemberNode member in typeNode.Members) {
                MemberSymbol memberSymbol = null;
                switch (member.NodeType) {
                    case ParseNodeType.FieldDeclaration:
                    case ParseNodeType.ConstFieldDeclaration:
                        memberSymbol = BuildField((FieldDeclarationNode)member, typeSymbol);
                        break;
                    case ParseNodeType.PropertyDeclaration:
                        memberSymbol = BuildPropertyAsField((PropertyDeclarationNode)member, typeSymbol);
                        if (memberSymbol == null) {
                            memberSymbol = BuildProperty((PropertyDeclarationNode)member, typeSymbol);
                        }
                        break;
                    case ParseNodeType.IndexerDeclaration:
                        memberSymbol = BuildIndexer((IndexerDeclarationNode)member, typeSymbol);
                        break;
                    case ParseNodeType.ConstructorDeclaration:
                    case ParseNodeType.MethodDeclaration:
                        if ((member.Modifiers & Modifiers.Extern) != 0) {
                            // Extern methods are there for defining overload signatures, so
                            // we just skip them as far as metadata goes. The validator has
                            // taken care of the requirements/constraints around use of extern methods.
                            continue;
                        }
                        memberSymbol = BuildMethod((MethodDeclarationNode)member, typeSymbol);
                        break;
                    case ParseNodeType.EventDeclaration:
                        memberSymbol = BuildEvent((EventDeclarationNode)member, typeSymbol);
                        break;
                    case ParseNodeType.EnumerationFieldDeclaration:
                        memberSymbol = BuildEnumField((EnumerationFieldNode)member, typeSymbol);
                        break;
                }

                if (memberSymbol != null) {
                    memberSymbol.SetParseContext(member);

                    if ((typeSymbol.IsApplicationType == false) &&
                        ((memberSymbol.Type == SymbolType.Constructor) ||
                         (typeSymbol.GetMember(memberSymbol.Name) != null))) {
                        // If the type is an imported type, then it is allowed to contain
                        // overloads, and we're simply going to ignore its existence, as long
                        // as one overload has been added to the member table.
                        continue;
                    }

                    typeSymbol.AddMember(memberSymbol);

                    if ((typeSymbol.Type == SymbolType.Class) && (memberSymbol.Type == SymbolType.Event)) {
                        EventSymbol eventSymbol = (EventSymbol)memberSymbol;
                        if (eventSymbol.DefaultImplementation) {
                            // Add a private field that will serve as the backing member
                            // later on in the conversion (eg. in non-event expressions)
                            MemberVisibility visibility = MemberVisibility.PrivateInstance;
                            if ((eventSymbol.Visibility & MemberVisibility.Static) != 0) {
                                visibility |= MemberVisibility.Static;
                            }

                            FieldSymbol fieldSymbol =
                                new FieldSymbol("__" + Utility.CreateCamelCaseName(eventSymbol.Name), typeSymbol,
                                                eventSymbol.AssociatedType);
                            fieldSymbol.SetVisibility(visibility);
                            fieldSymbol.SetParseContext(((EventDeclarationNode)eventSymbol.ParseContext).Field);

                            typeSymbol.AddMember(fieldSymbol);
                        }
                    }
                }
            }
        }
Esempio n. 5
0
 public MethodExpression(Expression objectReference, MethodSymbol method)
     : this(ExpressionType.MethodInvoke, objectReference, method, null) {
 }
        private MemberSymbol CreateGenericMember(MemberSymbol templateMember, IList <TypeSymbol> typeArguments)
        {
            TypeSymbol parentType = (TypeSymbol)templateMember.Parent;
            TypeSymbol instanceAssociatedType;

            if (templateMember.AssociatedType.Type == SymbolType.GenericParameter)
            {
                GenericParameterSymbol genericParameter = (GenericParameterSymbol)templateMember.AssociatedType;
                instanceAssociatedType = typeArguments[genericParameter.Index];
            }
            else
            {
                instanceAssociatedType = typeArguments[0];
            }

            if (templateMember.Type == SymbolType.Indexer)
            {
                IndexerSymbol templateIndexer = (IndexerSymbol)templateMember;
                IndexerSymbol instanceIndexer = new IndexerSymbol(parentType, instanceAssociatedType);

                if (templateIndexer.IsIntrinsic)
                {
                    instanceIndexer.SetIntrinsic();
                }
                instanceIndexer.SetVisibility(templateIndexer.Visibility);

                return(instanceIndexer);
            }
            else if (templateMember.Type == SymbolType.Property)
            {
                PropertySymbol templateProperty = (PropertySymbol)templateMember;
                PropertySymbol instanceProperty = new PropertySymbol(templateProperty.Name, parentType, instanceAssociatedType);

                if (templateProperty.IsTransformed)
                {
                    instanceProperty.SetTransformedName(templateProperty.GeneratedName);
                }
                instanceProperty.SetNameCasing(templateProperty.IsCasePreserved);
                instanceProperty.SetVisibility(templateProperty.Visibility);

                return(instanceProperty);
            }
            else if (templateMember.Type == SymbolType.Field)
            {
                FieldSymbol templateField = (FieldSymbol)templateMember;
                FieldSymbol instanceField = new FieldSymbol(templateField.Name, parentType, instanceAssociatedType);

                if (templateField.IsTransformed)
                {
                    instanceField.SetTransformedName(templateField.GeneratedName);
                }
                instanceField.SetNameCasing(templateField.IsCasePreserved);
                instanceField.SetVisibility(templateField.Visibility);

                return(instanceField);
            }
            else if (templateMember.Type == SymbolType.Method)
            {
                MethodSymbol templateMethod = (MethodSymbol)templateMember;
                MethodSymbol instanceMethod = new MethodSymbol(templateMethod.Name, parentType, instanceAssociatedType);

                if (templateMethod.IsTransformed)
                {
                    instanceMethod.SetTransformedName(templateMethod.GeneratedName);
                }
                if (templateMethod.InterfaceMember != null)
                {
                    instanceMethod.SetInterfaceMember(templateMethod.InterfaceMember);
                }
                instanceMethod.SetNameCasing(templateMethod.IsCasePreserved);
                instanceMethod.SetVisibility(templateMethod.Visibility);

                return(instanceMethod);
            }

            Debug.Fail("Unexpected generic member '" + templateMember.Name + " on type '" + ((TypeSymbol)templateMember.Parent).FullName + "'.");
            return(null);
        }
Esempio n. 7
0
        private static void GenerateMethod(ScriptGenerator generator, string typeName, MethodSymbol methodSymbol) {
            if (methodSymbol.IsAbstract) {
                return;
            }

            ScriptTextWriter writer = generator.Writer;

            bool instanceMember = ((methodSymbol.Visibility & MemberVisibility.Static) == 0);
            if (instanceMember == false) {
                if (methodSymbol.IsGlobalMethod) {
                    string mixinRoot = null;
                    if (methodSymbol.Parent.Type == SymbolType.Class) {
                        mixinRoot = ((ClassSymbol)methodSymbol.Parent).MixinRoot;
                    }
                    if (String.IsNullOrEmpty(mixinRoot)) {
                        mixinRoot = "window";
                    }

                    writer.Write(mixinRoot);
                }
                else {
                    writer.Write(typeName);
                }

                writer.Write(".");
            }

            writer.Write(methodSymbol.GeneratedName);
            if (instanceMember) {
                writer.WriteTrimmed(": ");
            }
            else {
                writer.WriteTrimmed(" = ");
            }

            writer.Write("function");
            if (generator.Options.DebugFlavor) {
                writer.Write(" ");
                writer.Write(typeName.Replace(".", "_"));
                writer.Write("$");
                writer.Write(methodSymbol.GeneratedName);
            }
            writer.Write("(");
            if (methodSymbol.Parameters != null) {
                bool obfuscateParams = false;
                if (generator.Options.Minimize) {
                    obfuscateParams = !methodSymbol.IsPublic;
                }

                int paramIndex = 0;
                foreach (ParameterSymbol parameterSymbol in methodSymbol.Parameters) {
                    if (paramIndex > 0) {
                        writer.WriteTrimmed(", ");
                    }
                    if (obfuscateParams) {
                        parameterSymbol.SetTransformedName("$p" + paramIndex);
                    }
                    writer.Write(parameterSymbol.GeneratedName);

                    paramIndex++;
                }
            }
            writer.WriteTrimmed(") ");
            writer.Write("{");
            writer.WriteNewLine();
            writer.Indent++;

            if (generator.Options.EnableDocComments) {
                DocCommentGenerator.GenerateComment(generator, methodSymbol);
            }

            CodeGenerator.GenerateScript(generator, methodSymbol);
            writer.Indent--;
            writer.Write("}");

            if (instanceMember == false) {
                writer.WriteSignificantNewLine();
            }
        }
Esempio n. 8
0
 public static void GenerateScript(ScriptGenerator generator, MethodSymbol symbol) {
     GenerateImplementationScript(generator, symbol, symbol.Implementation);
 }
Esempio n. 9
0
        private void BuildCode(MethodSymbol methodSymbol)
        {
            if (methodSymbol.IsAbstract) {
                return;
            }

            ImplementationBuilder implBuilder = new ImplementationBuilder(_options, _errorHandler);
            methodSymbol.AddImplementation(implBuilder.BuildMethod(methodSymbol));

            _implementations.Add(methodSymbol.Implementation);

            if (methodSymbol.AnonymousMethods != null) {
                foreach (AnonymousMethodSymbol anonymousMethod in methodSymbol.AnonymousMethods) {
                    Debug.Assert(anonymousMethod.Implementation != null);

                    _implementations.Add(anonymousMethod.Implementation);
                }
            }
        }
Esempio n. 10
0
        private Expression ProcessDotExpressionNode(BinaryExpressionNode node) {
            SymbolFilter filter = SymbolFilter.All;
            MemberSymbol memberSymbol = null;

            Expression objectExpression = ProcessDotExpressionNode(node, filter, out memberSymbol);
            if (objectExpression == null) {
                // We didn't successfully create an expression. The first pass attempted to
                // process the right child as an instance member of the left child expression.
                // We need to process the left child again as a type so we can process the
                // right child as a static member this time around.
                filter &= ~SymbolFilter.Members;
                objectExpression = ProcessDotExpressionNode(node, filter, out memberSymbol);
            }
            Debug.Assert(objectExpression != null);

            TypeSymbol dictionaryType = _symbolSet.ResolveIntrinsicType(IntrinsicType.Dictionary);
            TypeSymbol genericDictionaryType = _symbolSet.ResolveIntrinsicType(IntrinsicType.GenericDictionary);
            TypeSymbol nullableType = _symbolSet.ResolveIntrinsicType(IntrinsicType.Nullable);

            if (memberSymbol.Type == SymbolType.Property) {
                if ((memberSymbol.Parent == dictionaryType) ||
                    (memberSymbol.Parent == genericDictionaryType)) {
                    MethodSymbol methodSymbol = null;

                    if (String.CompareOrdinal(memberSymbol.Name, "Count") == 0) {
                        methodSymbol = (MethodSymbol)dictionaryType.GetMember("GetKeyCount");
                        Debug.Assert(methodSymbol != null);
                    }
                    else if (String.CompareOrdinal(memberSymbol.Name, "Keys") == 0) {
                        methodSymbol = (MethodSymbol)dictionaryType.GetMember("GetKeys");
                        Debug.Assert(methodSymbol != null);
                    }

                    if (methodSymbol != null) {
                        MethodExpression methodExpression =
                            new MethodExpression(new TypeExpression(dictionaryType, SymbolFilter.Public | SymbolFilter.StaticMembers),
                                                 methodSymbol);
                        methodExpression.AddParameterValue(objectExpression);
                        return methodExpression;
                    }
                }
                else if (memberSymbol.Parent == nullableType) {
                    if (String.CompareOrdinal(memberSymbol.Name, "Value") == 0) {
                        // Nullable<T>.Value becomes Nullable<T>

                        TypeSymbol underlyingType = objectExpression.EvaluatedType.GenericArguments.First();
                        objectExpression.Reevaluate(underlyingType);

                        return objectExpression;
                    }
                    else if (String.CompareOrdinal(memberSymbol.Name, "HasValue") == 0) {
                        // Nullable<T>.Value becomes Script.IsValue(Nullable<T>)

                        TypeSymbol scriptType = _symbolSet.ResolveIntrinsicType(IntrinsicType.Script);
                        MethodSymbol isValueMethod = (MethodSymbol)scriptType.GetMember("IsValue");

                        MethodExpression methodExpression
                            = new MethodExpression(new TypeExpression(scriptType, SymbolFilter.Public | SymbolFilter.StaticMembers),
                                                   isValueMethod);
                        methodExpression.AddParameterValue(objectExpression);

                        return methodExpression;
                    }
                }
            }
            else if (memberSymbol.Type == SymbolType.Method) {
                if (memberSymbol.Parent == nullableType) {
                    // Nullable<T>.GetValueOrDefault() becomes Nullable<T> || 0|false

                    TypeSymbol underlyingType = objectExpression.EvaluatedType.GenericArguments.First();

                    object defaultValue = 0;
                    if (underlyingType == _symbolSet.ResolveIntrinsicType(IntrinsicType.Boolean)) {
                        defaultValue = false;
                    }
                    else if (underlyingType == _symbolSet.ResolveIntrinsicType(IntrinsicType.String)) {
                        defaultValue = String.Empty;
                    }

                    LiteralExpression literalExpression = new LiteralExpression(underlyingType, defaultValue);

                    BinaryExpression logicalOrExpression = new BinaryExpression(Operator.LogicalOr, objectExpression, literalExpression);
                    logicalOrExpression.Reevaluate(underlyingType);
                    logicalOrExpression.AddParenthesisHint();

                    return logicalOrExpression;
                }
            }

            string dependency = ((TypeSymbol)memberSymbol.Parent).DependencyName;
            if (String.IsNullOrEmpty(dependency) == false) {
                _options.AddExecutionDependency(dependency);
            }

            MemberExpression expression = new MemberExpression(objectExpression, memberSymbol);
            if ((memberSymbol.Type == SymbolType.Method) &&
                memberSymbol.AssociatedType.IsGeneric && (memberSymbol.AssociatedType.GenericArguments == null)) {
                Debug.Assert(node.RightChild.NodeType == ParseNodeType.GenericName);
                Debug.Assert(((GenericNameNode)node.RightChild).TypeArguments != null);

                List<TypeSymbol> typeArgs = new List<TypeSymbol>();
                foreach (ParseNode typeArgNode in ((GenericNameNode)node.RightChild).TypeArguments) {
                    typeArgs.Add(_symbolSet.ResolveType(typeArgNode, _symbolTable, _symbolContext));
                }

                TypeSymbol returnType = _symbolSet.CreateGenericTypeSymbol(memberSymbol.AssociatedType, typeArgs);
                if (returnType != null) {
                    MethodSymbol genericMethod = (MethodSymbol)memberSymbol;
                    MethodSymbol instanceMethod = new MethodSymbol(genericMethod.Name, (TypeSymbol)genericMethod.Parent, returnType);

                    if (genericMethod.IsTransformed) {
                        instanceMethod.SetTransformedName(genericMethod.GeneratedName);
                    }
                    instanceMethod.SetNameCasing(genericMethod.IsCasePreserved);

                    expression = new MemberExpression(objectExpression, instanceMethod);
                }
            }

            return expression;
        }
Esempio n. 11
0
        private MemberSymbol CreateGenericMember(MemberSymbol templateMember, IList<TypeSymbol> typeArguments)
        {
            TypeSymbol parentType = (TypeSymbol)templateMember.Parent;
            TypeSymbol instanceAssociatedType;

            if (templateMember.AssociatedType.Type == SymbolType.GenericParameter) {
                GenericParameterSymbol genericParameter = (GenericParameterSymbol)templateMember.AssociatedType;
                instanceAssociatedType = typeArguments[genericParameter.Index];
            }
            else {
                instanceAssociatedType = typeArguments[0];
            }

            if (templateMember.Type == SymbolType.Indexer) {
                IndexerSymbol templateIndexer = (IndexerSymbol)templateMember;
                IndexerSymbol instanceIndexer = new IndexerSymbol(parentType, instanceAssociatedType);

                if (templateIndexer.UseScriptIndexer) {
                    instanceIndexer.SetScriptIndexer();
                }
                instanceIndexer.SetVisibility(templateIndexer.Visibility);

                return instanceIndexer;
            }
            else if (templateMember.Type == SymbolType.Property) {
                PropertySymbol templateProperty = (PropertySymbol)templateMember;
                PropertySymbol instanceProperty = new PropertySymbol(templateProperty.Name, parentType, instanceAssociatedType);

                if (templateProperty.IsTransformed) {
                    instanceProperty.SetTransformedName(templateProperty.GeneratedName);
                }
                instanceProperty.SetNameCasing(templateProperty.IsCasePreserved);
                instanceProperty.SetVisibility(templateProperty.Visibility);

                return instanceProperty;
            }
            else if (templateMember.Type == SymbolType.Field) {
                FieldSymbol templateField = (FieldSymbol)templateMember;
                FieldSymbol instanceField = new FieldSymbol(templateField.Name, parentType, instanceAssociatedType);

                if (templateField.IsTransformed) {
                    instanceField.SetTransformedName(templateField.GeneratedName);
                }
                instanceField.SetNameCasing(templateField.IsCasePreserved);
                instanceField.SetVisibility(templateField.Visibility);

                return instanceField;
            }
            else if (templateMember.Type == SymbolType.Method) {
                MethodSymbol templateMethod = (MethodSymbol)templateMember;
                MethodSymbol instanceMethod = new MethodSymbol(templateMethod.Name, parentType, instanceAssociatedType);

                if (templateMethod.IsAliased) {
                    instanceMethod.SetAlias(templateMethod.Alias);
                }
                else if (templateMethod.IsTransformed) {
                    instanceMethod.SetTransformedName(templateMethod.GeneratedName);
                }
                if (templateMethod.SkipGeneration) {
                    instanceMethod.SetSkipGeneration();
                }
                if (templateMethod.InterfaceMember != null) {
                    instanceMethod.SetInterfaceMember(templateMethod.InterfaceMember);
                }
                instanceMethod.SetNameCasing(templateMethod.IsCasePreserved);
                instanceMethod.SetVisibility(templateMethod.Visibility);

                return instanceMethod;
            }

            Debug.Fail("Unexpected generic member '" + templateMember.Name + " on type '" + ((TypeSymbol)templateMember.Parent).FullName + "'.");
            return null;
        }
Esempio n. 12
0
        private void ImportPseudoMembers(PseudoClassMembers memberSet, ClassSymbol classSymbol)
        {
            // Import pseudo members that go on the class but aren't defined in mscorlib.dll
            // These are meant to be used by internal compiler-generated transformations etc.
            // and aren't meant to be referenced directly in C# code.

            if (memberSet == PseudoClassMembers.Script) {
                TypeSymbol boolType = (TypeSymbol)((ISymbolTable)_symbols.SystemNamespace).FindSymbol("Boolean", null, SymbolFilter.Types);
                Debug.Assert(boolType != null);

                TypeSymbol intType = (TypeSymbol)((ISymbolTable)_symbols.SystemNamespace).FindSymbol("Int32", null, SymbolFilter.Types);
                Debug.Assert(intType != null);

                TypeSymbol floatType = (TypeSymbol)((ISymbolTable)_symbols.SystemNamespace).FindSymbol("Single", null, SymbolFilter.Types);
                Debug.Assert(floatType != null);

                TypeSymbol stringType = (TypeSymbol)((ISymbolTable)_symbols.SystemNamespace).FindSymbol("String", null, SymbolFilter.Types);
                Debug.Assert(stringType != null);

                // Define the Escape, Unescape, encodeURI, decodeURI, encodeURIComponent, decodeURIComponent methods
                MethodSymbol escapeMethod = new MethodSymbol("Escape", classSymbol, stringType, MemberVisibility.Public | MemberVisibility.Static);
                classSymbol.AddMember(escapeMethod);

                MethodSymbol unescapeMethod = new MethodSymbol("Unescape", classSymbol, stringType, MemberVisibility.Public | MemberVisibility.Static);
                classSymbol.AddMember(unescapeMethod);

                MethodSymbol encodeURIMethod = new MethodSymbol("EncodeUri", classSymbol, stringType, MemberVisibility.Public | MemberVisibility.Static);
                encodeURIMethod.SetTransformedName("encodeURI");
                classSymbol.AddMember(encodeURIMethod);

                MethodSymbol decodeURIMethod = new MethodSymbol("DecodeUri", classSymbol, stringType, MemberVisibility.Public | MemberVisibility.Static);
                decodeURIMethod.SetTransformedName("decodeURI");
                classSymbol.AddMember(decodeURIMethod);

                MethodSymbol encodeURIComponentMethod = new MethodSymbol("EncodeUriComponent", classSymbol, stringType, MemberVisibility.Public | MemberVisibility.Static);
                encodeURIComponentMethod.SetTransformedName("encodeURIComponent");
                classSymbol.AddMember(encodeURIComponentMethod);

                MethodSymbol decodeURIComponentMethod = new MethodSymbol("DecodeUriComponent", classSymbol, stringType, MemberVisibility.Public | MemberVisibility.Static);
                decodeURIComponentMethod.SetTransformedName("decodeURIComponent");
                classSymbol.AddMember(decodeURIComponentMethod);

                return;
            }

            if (memberSet == PseudoClassMembers.Arguments) {
                TypeSymbol objectType = (TypeSymbol)((ISymbolTable)_symbols.SystemNamespace).FindSymbol("Object", null, SymbolFilter.Types);
                Debug.Assert(objectType != null);

                IndexerSymbol indexer = new IndexerSymbol(classSymbol, objectType, MemberVisibility.Public | MemberVisibility.Static);
                indexer.SetIntrinsic();
                classSymbol.AddMember(indexer);

                return;
            }

            if (memberSet == PseudoClassMembers.Type) {
                // Define the Type.GetInstanceType static method which provides the functionality of
                // Object.GetType instance method. We don't extend Object.prototype in script to add
                // GetType, since we want to keep Object's protoype clean of any extensions.
                //
                // We create this symbol here, so that later the ExpressionBuilder can transform
                // calls to Object.GetType to this.
                TypeSymbol objectType = (TypeSymbol)((ISymbolTable)_symbols.SystemNamespace).FindSymbol("Object", null, SymbolFilter.Types);
                Debug.Assert(objectType != null);

                TypeSymbol typeType = (TypeSymbol)((ISymbolTable)_symbols.SystemNamespace).FindSymbol("Type", null, SymbolFilter.Types);
                Debug.Assert(objectType != null);

                MethodSymbol getTypeMethod = new MethodSymbol("GetInstanceType", classSymbol, typeType, MemberVisibility.Public | MemberVisibility.Static);
                getTypeMethod.AddParameter(new ParameterSymbol("instance", getTypeMethod, objectType, ParameterMode.In));
                classSymbol.AddMember(getTypeMethod);

                return;
            }

            if (memberSet == PseudoClassMembers.Dictionary) {
                TypeSymbol intType = (TypeSymbol)((ISymbolTable)_symbols.SystemNamespace).FindSymbol("Int32", null, SymbolFilter.Types);
                Debug.Assert(intType != null);

                TypeSymbol boolType = (TypeSymbol)((ISymbolTable)_symbols.SystemNamespace).FindSymbol("Boolean", null, SymbolFilter.Types);
                Debug.Assert(boolType != null);

                TypeSymbol voidType = (TypeSymbol)((ISymbolTable)_symbols.SystemNamespace).FindSymbol("Void", null, SymbolFilter.Types);
                Debug.Assert(boolType != null);

                TypeSymbol stringType = (TypeSymbol)((ISymbolTable)_symbols.SystemNamespace).FindSymbol("String", null, SymbolFilter.Types);
                Debug.Assert(boolType != null);

                // Define Dictionary.Keys
                MethodSymbol getKeysMethod = new MethodSymbol("GetKeys", classSymbol, _symbols.CreateArrayTypeSymbol(stringType), MemberVisibility.Public | MemberVisibility.Static);
                getKeysMethod.SetTransformedName("keys");
                classSymbol.AddMember(getKeysMethod);

                // Define Dictionary.GetCount
                MethodSymbol countMethod = new MethodSymbol("GetKeyCount", classSymbol, intType, MemberVisibility.Public | MemberVisibility.Static);
                classSymbol.AddMember(countMethod);

                // Define Dictionary.ClearKeys
                MethodSymbol clearMethod = new MethodSymbol("ClearKeys", classSymbol, voidType, MemberVisibility.Public | MemberVisibility.Static);
                classSymbol.AddMember(clearMethod);

                // Define Dictionary.DeleteKey
                MethodSymbol deleteMethod = new MethodSymbol("DeleteKey", classSymbol, voidType, MemberVisibility.Public | MemberVisibility.Static);
                classSymbol.AddMember(deleteMethod);

                // Define Dictionary.KeyExists
                MethodSymbol existsMethod = new MethodSymbol("KeyExists", classSymbol, boolType, MemberVisibility.Public | MemberVisibility.Static);
                classSymbol.AddMember(existsMethod);

                return;
            }

            if (memberSet == PseudoClassMembers.String) {
                // In script, String.replace replaces only the first occurrence of a string
                // whereas in C# all occurrences are replaced.
                // Replace becomes replaceAll (a method we add) in generated script
                // ReplaceFirst becomes replace in generated script.
                // ReplaceRegex also becomes replace in generated script. (We added ReplaceRegex so
                //   it could be mapped to the native replace method, rather than out replaceAll
                //   extension)

                MethodSymbol replaceFirstMethod = (MethodSymbol)classSymbol.GetMember("ReplaceFirst");
                Debug.Assert(replaceFirstMethod != null);
                replaceFirstMethod.SetTransformedName("replace");

                MethodSymbol replaceMethod = (MethodSymbol)classSymbol.GetMember("Replace");
                Debug.Assert(replaceMethod != null);
                replaceMethod.SetTransformedName("replaceAll");

                MethodSymbol replaceRegexMethod = (MethodSymbol)classSymbol.GetMember("ReplaceRegex");
                Debug.Assert(replaceRegexMethod != null);
                replaceRegexMethod.SetTransformedName("replace");
            }
        }
Esempio n. 13
0
        private void ImportMethods(TypeSymbol typeSymbol)
        {
            // NOTE: We do not import parameters for imported members.
            //       Parameters are used in the script model generation phase to populate
            //       symbol tables, which is not done for imported methods.

            TypeDefinition type = (TypeDefinition)typeSymbol.MetadataReference;

            foreach (MethodDefinition method in type.Methods) {
                if (method.IsSpecialName) {
                    continue;
                }
                if (method.IsPrivate || method.IsAssembly || method.IsFamilyAndAssembly) {
                    continue;
                }

                string methodName = method.Name;
                if (typeSymbol.GetMember(methodName) != null) {
                    // Ignore if its an overload since we don't care about parameters
                    // for imported methods, overloaded ctors don't matter.
                    // We just care about return values pretty much, and existence of the
                    // method.
                    continue;
                }

                TypeSymbol returnType = ResolveType(method.MethodReturnType.ReturnType);
                if (returnType == null) {
                    continue;
                }

                MethodSymbol methodSymbol = new MethodSymbol(methodName, typeSymbol, returnType);
                ImportMemberDetails(methodSymbol, method, method);

                if (method.HasGenericParameters) {
                    List<GenericParameterSymbol> genericArguments = new List<GenericParameterSymbol>();
                    foreach (GenericParameter genericParameter in method.GenericParameters) {
                        GenericParameterSymbol arg =
                            new GenericParameterSymbol(genericParameter.Position, genericParameter.Name,
                                                      /* typeArgument */ false,
                                                      _symbols.GlobalNamespace);
                        genericArguments.Add(arg);
                    }

                    methodSymbol.AddGenericArguments(genericArguments);
                }

                if (method.IsAbstract) {
                    // NOTE: We're ignoring the override scenario - it doesn't matter in terms
                    //       of the compilation and code generation
                    methodSymbol.SetImplementationState(SymbolImplementationFlags.Abstract);
                }

                if (MetadataHelpers.ShouldSkipFromScript(method)) {
                    methodSymbol.SetSkipGeneration();
                }

                if ((methodSymbol.Visibility & MemberVisibility.Static) != 0) {
                    string alias = MetadataHelpers.GetScriptAlias(method);
                    if (String.IsNullOrEmpty(alias) == false) {
                        methodSymbol.SetAlias(alias);
                    }
                }

                ICollection<string> conditions;
                if (MetadataHelpers.ShouldTreatAsConditionalMethod(method, out conditions)) {
                    methodSymbol.SetConditions(conditions);
                }

                typeSymbol.AddMember(methodSymbol);
            }
        }
Esempio n. 14
0
        private void ImportDelegateInvoke(TypeSymbol delegateTypeSymbol)
        {
            TypeDefinition type = (TypeDefinition)delegateTypeSymbol.MetadataReference;

            foreach (MethodDefinition method in type.Methods) {
                if (String.CompareOrdinal(method.Name, "Invoke") != 0) {
                    continue;
                }

                TypeSymbol returnType = ResolveType(method.MethodReturnType.ReturnType);
                Debug.Assert(returnType != null);
                if (returnType == null) {
                    continue;
                }

                MethodSymbol methodSymbol = new MethodSymbol("Invoke", delegateTypeSymbol, returnType, MemberVisibility.Public);
                methodSymbol.SetImplementationState(SymbolImplementationFlags.Abstract);
                methodSymbol.SetTransformedName(String.Empty);

                delegateTypeSymbol.AddMember(methodSymbol);
            }
        }
Esempio n. 15
0
 public DelegateExpression(Expression objectReference, MethodSymbol method)
     : base(ExpressionType.Delegate, method.AssociatedType, SymbolFilter.Public | SymbolFilter.InstanceMembers)
 {
     _method = method;
     _objectReference = objectReference;
 }
Esempio n. 16
0
        private static void GenerateMethodComment(ScriptTextWriter writer, MethodSymbol methodSymbol) {
            GenerateSummaryComment(writer, methodSymbol);

            if (methodSymbol.Parameters != null) {
                foreach (ParameterSymbol parameterSymbol in methodSymbol.Parameters) {
                    GenerateParameterComment(writer, parameterSymbol);
                }
            }

            GenerateReturnsComment(writer, methodSymbol.AssociatedType);
        }
 public SymbolImplementation BuildMethod(MethodSymbol methodSymbol)
 {
     BlockStatementNode methodBody = ((MethodDeclarationNode)methodSymbol.ParseContext).Implementation;
     return BuildImplementation((ISymbolTable)methodSymbol.Parent,
                                methodSymbol, methodBody, /* addAllParameters */ true);
 }
Esempio n. 18
0
        private void DumpMethod(MethodSymbol methodSymbol)
        {
            _writer.Write("Abstract: ");
            _writer.WriteLine(methodSymbol.IsAbstract);

            if (methodSymbol.Conditions != null) {
                _writer.WriteLine("Conditions:");
                _writer.Indent++;
                foreach (string condition in methodSymbol.Conditions) {
                    _writer.WriteLine(condition);
                }
                _writer.Indent--;
            }

            if (methodSymbol.Parameters != null) {
                _writer.WriteLine("Parameters:");
                _writer.Indent++;
                foreach (ParameterSymbol paramSymbol in methodSymbol.Parameters) {
                    DumpSymbol(paramSymbol);
                }
                _writer.Indent--;
            }
        }