<commit_before>// MetadataImporter.cs
// Script#/Core/Compiler
// This source code is subject to terms and conditions of the Apache License, Version 2.0.
//

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using ScriptSharp;
using ScriptSharp.Importer.IL;
using ScriptSharp.ScriptModel;
using ICustomAttributeProvider = ScriptSharp.Importer.IL.ICustomAttributeProvider;

namespace ScriptSharp.Importer {

    internal sealed class MetadataImporter {

        private SymbolSet _symbols;
        private CompilerOptions _options;
        private IErrorHandler _errorHandler;

        private List<TypeSymbol> _importedTypes;
        private bool _resolveError;

        public MetadataImporter(CompilerOptions options, IErrorHandler errorHandler) {
            Debug.Assert(options != null);
            Debug.Assert(errorHandler != null);

            _options = options;
            _errorHandler = errorHandler;
        }

        private ICollection<TypeSymbol> ImportAssemblies(MetadataSource mdSource) {
            _importedTypes = new List<TypeSymbol>();

            ImportScriptAssembly(mdSource, mdSource.CoreAssemblyPath, /* coreAssembly */ true);

            foreach (TypeSymbol typeSymbol in _importedTypes) {
                if ((typeSymbol.Type == SymbolType.Class) &&
                    (typeSymbol.Name.Equals("Array", StringComparison.Ordinal))) {
                    // Array is special - it is used to build other Arrays of more
                    // specific types we load members for in the second pass...
                    ImportMembers(typeSymbol);
                }
            }

            foreach (TypeSymbol typeSymbol in _importedTypes) {
                if (typeSymbol.IsGeneric) {
                    // Generics are also special - they are used to build other generic instances
                    // with specific types for generic arguments as we load members for other
                    // types subsequently...
                    ImportMembers(typeSymbol);
                }
            }

            foreach (TypeSymbol typeSymbol in _importedTypes) {
                if ((typeSymbol.IsGeneric == false) &&
                    ((typeSymbol.Type != SymbolType.Class) ||
                     (typeSymbol.Name.Equals("Array", StringComparison.Ordinal) == false))) {
                    ImportMembers(typeSymbol);
                }

                // There is some special-case logic to be performed on some members of the
                // global namespace.

                if (typeSymbol.Type == SymbolType.Class) {
                    if (typeSymbol.Name.Equals("Script", StringComparison.Ordinal)) {
                        // The Script class contains additional pseudo global methods that cannot
                        // be referenced at compile-time by the app author, but can be
                        // referenced by generated code during compilation.
                        ImportPseudoMembers(PseudoClassMembers.Script, (ClassSymbol)typeSymbol);
                    }
                    else if (typeSymbol.Name.Equals("Object", StringComparison.Ordinal)) {
                        // We need to add a static GetType method

                        ImportPseudoMembers(PseudoClassMembers.Object, (ClassSymbol)typeSymbol);
                    }
                    else if (typeSymbol.Name.Equals("Dictionary", StringComparison.Ordinal)) {
                        // The Dictionary class contains static methods at runtime, rather
                        // than instance methods.

                        ImportPseudoMembers(PseudoClassMembers.Dictionary, (ClassSymbol)typeSymbol);
                    }
                    else if (typeSymbol.Name.Equals("Arguments", StringComparison.Ordinal)) {
                        // We need to add a static indexer, which isn't allowed in C#

                        ImportPseudoMembers(PseudoClassMembers.Arguments, (ClassSymbol)typeSymbol);
                    }
                }
            }

            // Import all the types first.
            // Types need to be loaded upfront so that they can be used in resolving types associated
            // with members.
            foreach (string assemblyPath in mdSource.Assemblies) {
                ImportScriptAssembly(mdSource, assemblyPath, /* coreAssembly */ false);
            }

            // Resolve Base Types
            foreach (TypeSymbol typeSymbol in _importedTypes) {
                if (typeSymbol.Type == SymbolType.Class) {
                    ImportBaseType((ClassSymbol)typeSymbol);
                }
            }

            // Import members
            foreach (TypeSymbol typeSymbol in _importedTypes) {
                if (typeSymbol.IsCoreType) {
                    // already processed above
                    continue;
                }

                ImportMembers(typeSymbol);
            }

            return _importedTypes;
        }

        private void ImportBaseType(ClassSymbol classSymbol) {
            TypeDefinition type = (TypeDefinition)classSymbol.MetadataReference;
            TypeReference baseType = type.BaseType;

            if (baseType != null) {
                ClassSymbol baseClassSymbol = ResolveType(baseType) as ClassSymbol;
                if ((baseClassSymbol != null) &&
                    (String.CompareOrdinal(baseClassSymbol.FullName, "Object") != 0)) {
                    classSymbol.SetInheritance(baseClassSymbol, /* interfaces */ null);
                }
            }
        }

        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);
            }
        }

        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;
                int fieldValue = (int)field.Constant;

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

                enumTypeSymbol.AddMember(fieldSymbol);
            }
        }

        private void ImportEvents(TypeSymbol typeSymbol) {
            TypeDefinition type = (TypeDefinition)typeSymbol.MetadataReference;

            foreach (EventDefinition eventDef in type.Events) {
                if (eventDef.IsSpecialName) {
                    continue;
                }
                if ((eventDef.AddMethod == null) || (eventDef.RemoveMethod == null)) {
                    continue;
                }
                if (eventDef.AddMethod.IsPrivate || eventDef.AddMethod.IsAssembly || eventDef.AddMethod.IsFamilyAndAssembly) {
                    continue;
                }

                string eventName = eventDef.Name;

                TypeSymbol eventHandlerType = ResolveType(eventDef.EventType);
                if (eventHandlerType == null) {
                    continue;
                }

                EventSymbol eventSymbol = new EventSymbol(eventName, typeSymbol, eventHandlerType);
                ImportMemberDetails(eventSymbol, eventDef.AddMethod, eventDef);

                string addAccessor;<commit_msg>Fix crash when referencing non-int enums<commit_after>// MetadataImporter.cs
// Script#/Core/Compiler
// This source code is subject to terms and conditions of the Apache License, Version 2.0.
//

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using ScriptSharp;
using ScriptSharp.Importer.IL;
using ScriptSharp.ScriptModel;
using ICustomAttributeProvider = ScriptSharp.Importer.IL.ICustomAttributeProvider;

namespace ScriptSharp.Importer {

    internal sealed class MetadataImporter {

        private SymbolSet _symbols;
        private CompilerOptions _options;
        private IErrorHandler _errorHandler;

        private List<TypeSymbol> _importedTypes;
        private bool _resolveError;

        public MetadataImporter(CompilerOptions options, IErrorHandler errorHandler) {
            Debug.Assert(options != null);
            Debug.Assert(errorHandler != null);

            _options = options;
            _errorHandler = errorHandler;
        }

        private ICollection<TypeSymbol> ImportAssemblies(MetadataSource mdSource) {
            _importedTypes = new List<TypeSymbol>();

            ImportScriptAssembly(mdSource, mdSource.CoreAssemblyPath, /* coreAssembly */ true);

            foreach (TypeSymbol typeSymbol in _importedTypes) {
                if ((typeSymbol.Type == SymbolType.Class) &&
                    (typeSymbol.Name.Equals("Array", StringComparison.Ordinal))) {
                    // Array is special - it is used to build other Arrays of more
                    // specific types we load members for in the second pass...
                    ImportMembers(typeSymbol);
                }
            }

            foreach (TypeSymbol typeSymbol in _importedTypes) {
                if (typeSymbol.IsGeneric) {
                    // Generics are also special - they are used to build other generic instances
                    // with specific types for generic arguments as we load members for other
                    // types subsequently...
                    ImportMembers(typeSymbol);
                }
            }

            foreach (TypeSymbol typeSymbol in _importedTypes) {
                if ((typeSymbol.IsGeneric == false) &&
                    ((typeSymbol.Type != SymbolType.Class) ||
                     (typeSymbol.Name.Equals("Array", StringComparison.Ordinal) == false))) {
                    ImportMembers(typeSymbol);
                }

                // There is some special-case logic to be performed on some members of the
                // global namespace.

                if (typeSymbol.Type == SymbolType.Class) {
                    if (typeSymbol.Name.Equals("Script", StringComparison.Ordinal)) {
                        // The Script class contains additional pseudo global methods that cannot
                        // be referenced at compile-time by the app author, but can be
                        // referenced by generated code during compilation.
                        ImportPseudoMembers(PseudoClassMembers.Script, (ClassSymbol)typeSymbol);
                    }
                    else if (typeSymbol.Name.Equals("Object", StringComparison.Ordinal)) {
                        // We need to add a static GetType method

                        ImportPseudoMembers(PseudoClassMembers.Object, (ClassSymbol)typeSymbol);
                    }
                    else if (typeSymbol.Name.Equals("Dictionary", StringComparison.Ordinal)) {
                        // The Dictionary class contains static methods at runtime, rather
                        // than instance methods.

                        ImportPseudoMembers(PseudoClassMembers.Dictionary, (ClassSymbol)typeSymbol);
                    }
                    else if (typeSymbol.Name.Equals("Arguments", StringComparison.Ordinal)) {
                        // We need to add a static indexer, which isn't allowed in C#

                        ImportPseudoMembers(PseudoClassMembers.Arguments, (ClassSymbol)typeSymbol);
                    }
                }
            }

            // Import all the types first.
            // Types need to be loaded upfront so that they can be used in resolving types associated
            // with members.
            foreach (string assemblyPath in mdSource.Assemblies) {
                ImportScriptAssembly(mdSource, assemblyPath, /* coreAssembly */ false);
            }

            // Resolve Base Types
            foreach (TypeSymbol typeSymbol in _importedTypes) {
                if (typeSymbol.Type == SymbolType.Class) {
                    ImportBaseType((ClassSymbol)typeSymbol);
                }
            }

            // Import members
            foreach (TypeSymbol typeSymbol in _importedTypes) {
                if (typeSymbol.IsCoreType) {
                    // already processed above
                    continue;
                }

                ImportMembers(typeSymbol);
            }

            return _importedTypes;
        }

        private void ImportBaseType(ClassSymbol classSymbol) {
            TypeDefinition type = (TypeDefinition)classSymbol.MetadataReference;
            TypeReference baseType = type.BaseType;

            if (baseType != null) {
                ClassSymbol baseClassSymbol = ResolveType(baseType) as ClassSymbol;
                if ((baseClassSymbol != null) &&
                    (String.CompareOrdinal(baseClassSymbol.FullName, "Object") != 0)) {
                    classSymbol.SetInheritance(baseClassSymbol, /* interfaces */ null);
                }
            }
        }

        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);
            }
        }

        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);
            }
        }

        private void ImportEvents(TypeSymbol typeSymbol) {
            TypeDefinition type = (TypeDefinition)typeSymbol.MetadataReference;

            foreach (EventDefinition eventDef in type.Events) {
                if (eventDef.IsSpecialName) {
                    continue;
                }
                if ((eventDef.AddMethod == null) || (eventDef.RemoveMethod == null)) {
                    continue;
                }
                if (eventDef.AddMethod.IsPrivate || eventDef.AddMethod.IsAssembly || eventDef.AddMethod.IsFamilyAndAssembly) {
                    continue;
                }

                string eventName = eventDef.Name;

                TypeSymbol eventHandlerType = ResolveType(eventDef.EventType);
                if (eventHandlerType == null) {
                    continue;
                }

                EventSymbol eventSymbol = new EventSymbol(eventName, typeSymbol, eventHandlerType);
                ImportMemberDetails(eventSymbol, eventDef.AddMethod, eventDef);

                string addAccessor;
{'fraction_non_alphanumeric': 0.07242607564878159, 'fraction_numerical': 0.00045230960592525585, 'mean_word_length': 1.7258437355524734, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3425867', 'n_tokens_mistral': 4083, 'n_tokens_neox': 3761, 'n_words': 975}