visitExtensionDeclaration method

  1. @override
Object? visitExtensionDeclaration(
  1. SExtensionDeclaration node
)
override

Implementation

@override
Object? visitExtensionDeclaration(SExtensionDeclaration node) {
  final extensionName = node.name?.name;
  if (Logger.isDebug) {
    Logger.debug(
      "[visitExtensionDeclaration] Declaring extension: ${extensionName ?? '<unnamed>'}",
    );
  }

  // 1. Resolve the 'on' type
  final onTypeNode = node.extendedType;
  if (onTypeNode == null) {
    // This might happen for extension types, which are different.
    // For now, assume classic extensions always have an 'on' clause.
    Logger.warn(
      "[visitExtensionDeclaration] Extension '${extensionName ?? '<unnamed>'}' has no 'on' clause, skipping.",
    );
    return null;
  }

  // Resolve the type name from the AST node
  String onTypeName;
  bool isOnNullableType = false; // G-DOV-10/11: Track nullable on-type
  if (onTypeNode is SNamedType) {
    onTypeName = onTypeNode.name!.name;
    isOnNullableType = onTypeNode.isNullable; // Check for 'T?' syntax
  } else {
    Logger.warn(
      "[visitExtensionDeclaration] Unsupported 'on' type node for resolution: ${onTypeNode.runtimeType}. Skipping extension.",
    );
    return null;
  }

  // Look up the RuntimeType in the environment. PR #8: a prefixed type
  // (`on l.Base`) is looked up through its prefix.
  final onTypeLookupName = _relationshipLookupName(onTypeNode);
  RuntimeType resolveOnType() {
    RuntimeType onRuntimeType;
    try {
      final typeValue = environment.get(onTypeLookupName);

      // Handle Resolution of Native/Bridged Types
      if (typeValue is RuntimeType) {
        // Standard case: Found an InterpretedClass/Mixin or existing BridgedClass
        onRuntimeType = typeValue;
        if (Logger.isDebug) {
          Logger.debug(
            "[visitExtensionDeclaration] Resolved 'on' type '$onTypeName' to RuntimeType: ${onRuntimeType.name}",
          );
        }
      } else if (typeValue is NativeFunction) {
        // Heuristic: If environment.get returns a NativeFunction for a common type name,
        // assume it represents the native type and find/create a corresponding BridgedClass.
        // This relies on the global environment being populated correctly with type bridges.
        BridgedClass? bridgedType = _getBridgedClassForNativeType(onTypeName);
        if (bridgedType != null) {
          onRuntimeType = bridgedType;
          if (Logger.isDebug) {
            Logger.debug(
              "[visitExtensionDeclaration] Resolved native 'on' type '$onTypeName' to BridgedClass: ${onRuntimeType.name}",
            );
          }
        } else {
          // We found something, but couldn't map it to a known bridged type
          throw RuntimeD4rtException(
            "Symbol '$onTypeName' resolved to NativeFunction but could not map to a known BridgedClass.",
          );
        }
      } else {
        // Resolved to something unexpected (e.g., an instance, null, etc.)
        throw RuntimeD4rtException(
          "Symbol '$onTypeName' resolved to non-type: ${typeValue?.runtimeType}",
        );
      }
    } on RuntimeD4rtException catch (e) {
      // SCC31: the type named in `on T` resolved to nothing at all, which is a
      // question here rather than an answer — a core type may still be reachable
      // as a bridge. Asked by type and by exact name: the old
      // `e.message.contains(...)` made a formatted diagnostic load-bearing, and
      // also took this branch when the name merely *appeared* in an unrelated
      // failure's message.
      if (e is UndefinedNameD4rtException && e.name == onTypeName) {
        // Special handling for core types that might not be explicitly defined if stdlib wasn't fully loaded?
        // Or maybe they are always NativeFunctions?
        // ghissues77: `dart:core`'s function typedef `Comparator` (package
        // collection's `extension ComparatorExtension<T> on Comparator<T>`)
        // names a function type; the extension applies to functions.
        BridgedClass? coreBridgedType = _getBridgedClassForNativeType(
          onTypeName == 'Comparator' ? 'Function' : onTypeName,
        );
        if (coreBridgedType != null) {
          onRuntimeType = coreBridgedType;
          if (Logger.isDebug) {
            Logger.debug(
              "[visitExtensionDeclaration] Resolved unfound core 'on' type '$onTypeName' to BridgedClass: ${onRuntimeType.name}",
            );
          }
        } else {
          // Type genuinely not found or not a recognized core type
          throw RuntimeD4rtException(
            "Could not resolve 'on' type '$onTypeName' for extension '${extensionName ?? '<unnamed>'}': Type not found or not a recognized core type.",
          );
        }
      } else {
        // Propagate other RuntimeErrors (like the non-type error from above)
        //
        // SCE70: the sibling throw above is NOT given the payload, and the
        // difference is the branch condition rather than an oversight. That one
        // fires when `e is UndefinedNameD4rtException` — a name that resolved to
        // nothing, which scc31 keeps deliberately uncatchable and which carries
        // no preserved native exception. There is nothing there to forward.
        throw RuntimeD4rtException(
          "Could not resolve 'on' type '$onTypeName' for extension '${extensionName ?? '<unnamed>'}': ${e.message}",
          originalException: e.originalException,
          originalStackTrace: e.originalStackTrace,
        );
      }
    }
    return onRuntimeType;
  }

  // GitHub #9 — the `on` type may be declared in a library that is still
  // loading (an import cycle, or a barrel that has not reached the export
  // declaring it). The extension is then created and registered below as
  // usual, so importers copy it with the module's exports, and its type is
  // resolved once the outermost load returns
  // ([InterpretedExtension.resolveOnType]); a type still undefined then is
  // reported. Until then no lookup applies the extension.
  final onTypeDeferred =
      moduleContext.isLoadingModules &&
      !environment.isDefined(onTypeLookupName) &&
      _getBridgedClassForNativeType(onTypeName) == null;
  final RuntimeType? onRuntimeType = onTypeDeferred ? null : resolveOnType();

  // 2. Process members (methods, getters, setters, operators) - both instance and static
  final members = <String, Callable>{};
  // ghissues56: setters apart, so a getter and a setter of one name are
  // both kept.
  final setters = <String, Callable>{};
  final staticMethods = <String, Callable>{};
  final staticGetters = <String, Callable>{};
  final staticSetters = <String, Callable>{};
  final staticFields = <String, Object?>{};

  for (final member in node.body.members) {
    if (member is SMethodDeclaration) {
      final methodName =
          member.name!.name; // Operator names like '+', '[]' are also lexemes

      if (member.isStatic) {
        // Handle static methods, getters, and setters
        final function = InterpretedFunction.method(
          member,
          environment,
          null,
        );

        if (member.isGetter) {
          staticGetters[methodName] = function;
          if (Logger.isDebug) {
            Logger.debug(
              "[visitExtensionDeclaration]   Added static getter: $methodName",
            );
          }
        } else if (member.isSetter) {
          staticSetters[methodName] = function;
          if (Logger.isDebug) {
            Logger.debug(
              "[visitExtensionDeclaration]   Added static setter: $methodName",
            );
          }
        } else {
          staticMethods[methodName] = function;
          if (Logger.isDebug) {
            Logger.debug(
              "[visitExtensionDeclaration]   Added static method: $methodName",
            );
          }
        }
      } else {
        // Create InterpretedExtensionMethod for instance method-like declarations
        final function = InterpretedExtensionMethod(
          member,
          environment,
          onRuntimeType,
          extensionTypeParameters: [
            for (final typeParameter
                in node.typeParameters?.typeParameters ?? const [])
              ?typeParameter.name?.name,
          ],
          onTypeAnnotation: onTypeNode,
        );
        if (member.isSetter) {
          setters[methodName] = function;
        } else {
          members[methodName] = function;
        }
        String memberType = "method";
        if (member.isGetter) memberType = "getter";
        if (member.isSetter) memberType = "setter";
        if (member.isOperator) memberType = "operator";
        if (Logger.isDebug) {
          Logger.debug(
            "[visitExtensionDeclaration]   Added extension $memberType: $methodName",
          );
        }
      }
    } else if (member is SFieldDeclaration) {
      // Only static fields are allowed in extensions.
      if (!member.isStatic) {
        Logger.warn(
          "[visitExtensionDeclaration] Instance fields are not allowed in extensions. Skipping field '$member'.",
        );
        continue; // Skip instance fields
      }
      // Handle static fields - store in the InterpretedExtension object
      for (final variable in member.fields!.variables) {
        final fieldName = variable.name!.name;
        Object? value;
        if (variable.initializer != null) {
          // Evaluate static initializer immediately in the current environment
          try {
            value = variable.initializer!.accept<Object?>(this);
          } catch (e) {
            throw RuntimeD4rtException(
              "Error evaluating static initializer for extension field '$fieldName': $e",
            );
          }
        }
        // Store in staticFields map instead of environment
        staticFields[fieldName] = value;
        if (Logger.isDebug) {
          Logger.debug(
            "[visitExtensionDeclaration]   Stored static field: $fieldName",
          );
        }
      }
    } else {
      Logger.warn(
        "[visitExtensionDeclaration] Unsupported extension member type: ${member.runtimeType}. Skipping.",
      );
    }
  }

  // 3. Create and store the InterpretedExtension
  final interpretedExtension = InterpretedExtension(
    name: extensionName,
    onType: onRuntimeType,
    isOnNullableType: isOnNullableType, // G-DOV-10/11: Pass nullable flag
    members: members,
    setters: setters,
    staticMethods: staticMethods,
    staticGetters: staticGetters,
    staticSetters: staticSetters,
    staticFields: staticFields,
  );

  // How to store it? In the environment associated with its name?
  // Or in a separate list in the environment?
  // Let's try defining it by name if it has one, otherwise maybe a special list.
  if (extensionName != null) {
    environment.define(extensionName, interpretedExtension);
    if (Logger.isDebug) {
      Logger.debug(
        "[visitExtensionDeclaration] Defined named extension '$extensionName' in environment.",
      );
    }
  } else {
    // Store unnamed extensions in a special list in the environment
    environment.addUnnamedExtension(interpretedExtension);
    Logger.debug(
      "[visitExtensionDeclaration] Added unnamed extension to environment list.",
    );
  }

  if (onTypeDeferred) {
    final onTypeEnv = environment;
    moduleContext.deferUntilModulesLoaded(() {
      final savedEnv = environment;
      try {
        environment = onTypeEnv;
        interpretedExtension.resolveOnType(resolveOnType());
      } finally {
        environment = savedEnv;
      }
    });
  }

  return null; // Declarations typically return null
}