<commit_before>import 'package:code_builder/code_builder.dart' as dart;
import 'package:compiler/compiler.dart';

import 'constants.dart';
import 'declarations/module.dart';

final Query<CandyType, dart.Reference> compileType =
    Query<CandyType, dart.Reference>(
  'dart.compileType',
  evaluateAlways: true,
  provider: (context, type) {
    dart.Reference compile(CandyType type) => compileType(context, type);

    return type.map(
      this_: (_) {
        throw CompilerError.unsupportedFeature(
          'Compiling the `This`-type to Dart is not yet supported.',
        );
      },
      user: (type) {
        if (type == CandyType.any) return _createDartType('Object');
        if (type == CandyType.unit) return _createDartType('void', url: null);
        if (type == CandyType.never) return _createDartType('dynamic');
        if (type == CandyType.bool) return _createDartType('bool');
        if (type == CandyType.number) return _createDartType('Num');
        if (type == CandyType.int) return _createDartType('int');
        if (type == CandyType.float) return _createDartType('double');
        if (type == CandyType.string) return _createDartType('String');

        return _createDartType(
          type.name,
          url: moduleIdToImportUrl(context, type.parentModuleId),
        );
      },
      tuple: (type) {
        final url = moduleIdToImportUrl(context, ModuleId.corePrimitives);
        return dart.TypeReference((b) => b
          ..symbol = 'Tuple${type.items.length}'
          ..url = url
          ..types.addAll(type.items.map((i) => compileType(context, i)))
          ..isNullable = false);
      },
      function: (type) {
        return dart.FunctionType((b) {
          if (type.receiverType != null) {
            b.requiredParameters.add(compile(type.receiverType));
          }
          b
            ..requiredParameters.addAll(type.parameterTypes.map(compile))
            ..returnType = compile(type.returnType);
        });
      },
      union: (_) => dart.refer('dynamic', dartCoreUrl),
      intersection: (_) => dart.refer('dynamic', dartCoreUrl),
      parameter: (type) => dart.refer(type.name),
      reflection: (type) {
        final url = moduleIdToImportUrl(context, ModuleId.coreReflection);
        final id = type.declarationId;
        if (id.isModule) {
          return dart.refer('Module', url);
        } else if (id.isTrait || id.isClass) {
          return dart.refer('Type', url);
        } else if (id.isProperty) {
          final propertyHir = getPropertyDeclarationHir(context, id);
          assert(!propertyHir.isStatic);
          return compileType(
            context,
            CandyType.function(
              receiverType:
                  getPropertyDeclarationParentAsType(context, id).value,
              returnType: propertyHir.type,
            ),
          );
        } else if (id.isFunction) {
          final functionHir = getFunctionDeclarationHir(context, id);
          assert(!functionHir.isStatic);
          return compileType(
            context,
            functionHir.functionType.copyWith(
              receiverType:
                  getPropertyDeclarationParentAsType(context, id).value,
            ),
          );
        } else {
          throw CompilerError.internalError(
            'Invalid reflection target for compiling type: `$id`.',
          );
        }
      },
    );
  },
);

dart.TypeReference _createDartType(
  String name, {
  String url = dartCoreUrl,
  List<dart.TypeReference> typeArguments = const [],
}) {
  return dart.TypeReference((b) => b
    ..symbol = name
    ..url = url
    ..types.addAll(typeArguments)
    ..isNullable = false);
}<commit_msg>refactor(packages/compiler_dart): unify type reference generation<commit_after>import 'package:code_builder/code_builder.dart' as dart;
import 'package:compiler/compiler.dart';

import 'constants.dart';
import 'declarations/module.dart';

final Query<CandyType, dart.Reference> compileType =
    Query<CandyType, dart.Reference>(
  'dart.compileType',
  evaluateAlways: true,
  provider: (context, type) {
    dart.Reference compile(CandyType type) => compileType(context, type);

    return type.map(
      this_: (_) {
        throw CompilerError.unsupportedFeature(
          'Compiling the `This`-type to Dart is not yet supported.',
        );
      },
      user: (type) {
        if (type == CandyType.any) return _createType('Object');
        if (type == CandyType.unit) return _createType('void', url: null);
        if (type == CandyType.never) return _createType('dynamic');
        if (type == CandyType.bool) return _createType('bool');
        if (type == CandyType.number) return _createType('Num');
        if (type == CandyType.int) return _createType('int');
        if (type == CandyType.float) return _createType('double');
        if (type == CandyType.string) return _createType('String');

        return _createDartType(
          type.name,
          url: moduleIdToImportUrl(context, type.parentModuleId),
        );
      },
      tuple: (type) {
        final url = moduleIdToImportUrl(context, ModuleId.corePrimitives);
        return dart.TypeReference((b) => b
          ..symbol = 'Tuple${type.items.length}'
          ..url = url
          ..types.addAll(type.items.map((i) => compileType(context, i)))
          ..isNullable = false);
      },
      function: (type) {
        return dart.FunctionType((b) {
          if (type.receiverType != null) {
            b.requiredParameters.add(compile(type.receiverType));
          }
          b
            ..requiredParameters.addAll(type.parameterTypes.map(compile))
            ..returnType = compile(type.returnType);
        });
      },
      union: (_) => _createType('dynamic'),
      intersection: (_) => _createType('dynamic'),
      parameter: (type) => _createType(type.name, url: null),
      reflection: (type) {
        final url = moduleIdToImportUrl(context, ModuleId.coreReflection);
        final id = type.declarationId;
        if (id.isModule) {
          return dart.refer('Module', url);
        } else if (id.isTrait || id.isClass) {
          return dart.refer('Type', url);
        } else if (id.isProperty) {
          final propertyHir = getPropertyDeclarationHir(context, id);
          assert(!propertyHir.isStatic);
          return compileType(
            context,
            CandyType.function(
              receiverType:
                  getPropertyDeclarationParentAsType(context, id).value,
              returnType: propertyHir.type,
            ),
          );
        } else if (id.isFunction) {
          final functionHir = getFunctionDeclarationHir(context, id);
          assert(!functionHir.isStatic);
          return compileType(
            context,
            functionHir.functionType.copyWith(
              receiverType:
                  getPropertyDeclarationParentAsType(context, id).value,
            ),
          );
        } else {
          throw CompilerError.internalError(
            'Invalid reflection target for compiling type: `$id`.',
          );
        }
      },
    );
  },
);

dart.Reference _createType(
  String name, {
  String url = dartCoreUrl,
  List<dart.Reference> typeArguments = const [],
}) {
  return dart.TypeReference((b) => b
    ..symbol = name
    ..url = url
    ..types.addAll(typeArguments)
    ..isNullable = false);
}
{'fraction_non_alphanumeric': 0.12604701432045393, 'fraction_numerical': 0.0, 'mean_word_length': 2.312304250559284, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 23, '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': '7544893', 'n_tokens_mistral': 2128, 'n_tokens_neox': 2100, 'n_words': 430}