Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Generic Types Support — Phase 1: Classes & Functions
Adds comprehensive support for generic classes and functions with type erasure-based implementation.
Overview
Implements Phase 1 of generics support:
class Stack<T>,class Box<T>,class Pair<A,B>with type parameter substitutioninterface<T>andclass<T>type argumentsHow It Works
Generic Classes
Uses type erasure: Generic type parameters (
T) are compiled toi8*(opaque pointers), and generic arrays (T[]) to%ObjectArray*. No monomorphization — single compiled code for all type instantiations.Generic Functions
Type parameters are inferred from return type annotations and usage context.
Type Arguments on Interfaces & Classes
Detects and rejects invalid type arguments at compile time:
Changes
Core Codegen:
llvm-generator.ts: Generic class/function handling, type parameter substitutionvariable-allocator.ts: Allocate correct types for generic parameters based on contextmethod-calls.ts: Guard class instances from array method dispatchParser:
parser-ts/handlers/declarations.ts,parser-native/transformer.ts: Parse/transform generic syntaxast/types.ts: Add generic metadata to class/interface/function nodesCollections:
array/mutators.ts,array/reorder.ts: AddgenerateObjectArrayPop,generateObjectArrayShiftfor generic array handlingTests:
tests/fixtures/generics/:stack.ts,queue.ts,box.ts,pair.ts— generic class examplesgeneric-functions.ts— function type inferencegeneric-class-arg.ts,generic-extends.ts,generic-interface.ts— type argument validationLimitations & Future Work
Current (Phase 1):
string/interfacetype parametersT→i8*Not Yet (Phase 2-3):
new Stack<number>())swap(): Pair<B,A>)<T extends Comparable>)...(T[]))Testing
All existing tests pass. Self-hosting verified (Stage 0-2 pass).