sun::semantic_analysis::GenericSpecializer

sun::semantic_analysis::GenericSpecializer

class · Source (opens in a new tab)

class sun::semantic_analysis::GenericSpecializer

Builds and caches the specializations a program asks for.

Bodies are analyzed in the scope the template was declared in, so names inside a template resolve as written at the definition site rather than at the call site that triggered the instantiation. Instantiation returns prepared signatures immediately; bodies are always queued until analyzePendingBodies.

Types

Public Functions

GenericSpecializer

public · function · Source (opens in a new tab)

sun::semantic_analysis::GenericSpecializer::GenericSpecializer(SemanticContext &ctx, SemanticAnalyzer &sema)

Share scope state and checking helpers.

Related: SemanticContext, SemanticAnalyzer

analyzePendingBodies

public · function · Source (opens in a new tab)

void sun::semantic_analysis::GenericSpecializer::analyzePendingBodies()

Drain prepared specialization bodies in FIFO order, including new jobs.

Direct instantiation callers must drain before consuming analyzed bodies. On failure, discard remaining work and preserve the original diagnostic.

applyVariadicParamTypes

public · function · Source (opens in a new tab)

void sun::semantic_analysis::GenericSpecializer::applyVariadicParamTypes(PrototypeAST &clonedProto, const PrototypeAST &proto, const std::vector< sun::types::TypePtr > &variadicArgTypes, std::optional< sun::support::Position > loc)

Record a specialization's pack element types on its cloned prototype and check them against the pack's declared type annotation.

_params_of<C> for a class C means C must have a matching init overload; for a lambda, that lambda's parameters must match. Any other annotation is recorded and left unchecked. Call inside the type parameter scope, so the T in _params_of<T> resolves.

Related: PrototypeAST, sun::types::TypePtr, sun::support::Position

checkTypeParameterConstraints

public · function · Source (opens in a new tab)

void sun::semantic_analysis::GenericSpecializer::checkTypeParameterConstraints(const std::vector< sun::ast::TypeParameter > &typeParams, const std::vector< sun::types::TypePtr > &typeArgs, const std::string &what, const std::string &name, std::optional< sun::support::Position > loc=std::nullopt)

Check each type argument against its parameter's constraint, if it has one, and report the first violation.

Call it at every instantiation point, right after the arity check.

Defer arguments containing unresolved type parameters, such as Box while resolving a generic signature. Check them when specialization supplies concrete types; deferral does not prove constraint satisfaction.

what and name name the thing being instantiated, e.g. ("generic function", "spawn").

Related: sun::ast::TypeParameter, sun::types::TypePtr, sun::support::Position

classDefinitionScope

public · function · Source (opens in a new tab)

SemanticScope * sun::semantic_analysis::GenericSpecializer::classDefinitionScope(const sun::types::ClassType &classType) const

The scope a class's template was declared in: for a specialization, the generic's; for a plain class with generic methods, its own registration.

Null when the class has no template.

Related: SemanticScope, sun::types::ClassType

declareVariadicPack

public · function · Source (opens in a new tab)

void sun::semantic_analysis::GenericSpecializer::declareVariadicPack(const PrototypeAST &proto)

Bring a specialization's args... pack into scope for body analysis: the pack itself, so args... can be expanded, and one variable per element under the name codegen gives that parameter.

Related: PrototypeAST

discardPendingBodies

public · function · Source (opens in a new tab)

void sun::semantic_analysis::GenericSpecializer::discardPendingBodies()

Discard pending work without analyzing it after a failed compilation.

findGenericMethodAST

public · function · Source (opens in a new tab)

FunctionAST * sun::semantic_analysis::GenericSpecializer::findGenericMethodAST(const sun::types::ClassType *classType, const std::string &methodName)

Find a generic method's FunctionAST on a class by name (nullptr if none).

Related: sun::ast::FunctionAST, sun::types::ClassType, FunctionAST

genericFunctionSignature

public · function · Source (opens in a new tab)

TypePtr sun::semantic_analysis::GenericSpecializer::genericFunctionSignature(const GenericFunctionInfo &genericInfo, const std::vector< sun::types::TypePtr > &typeArgs)

Type-argument inference itself is sun::semantic_analysis (generic_type_arguments.h).

The signature a generic function has under the given type arguments, without instantiating it: what a call in a template body resolves to until the enclosing generic is specialized.

Related: sun::types::TypePtr, GenericFunctionInfo, sun::semantic_analysis

hasPendingBodies

public · function · Source (opens in a new tab)

bool sun::semantic_analysis::GenericSpecializer::hasPendingBodies() const

Whether prepared specializations still require body checking.

instantiateGenericClass

instantiateGenericClass(const std::string &baseName, const std::vector< sun::types::TypePtr > &typeArgs)

public · function · Source (opens in a new tab)

std::shared_ptr< sun::types::ClassType > sun::semantic_analysis::GenericSpecializer::instantiateGenericClass(const std::string &baseName, const std::vector< sun::types::TypePtr > &typeArgs)

Monomorphize a generic class for the given type arguments, reusing the specialization if it already exists.

Related: sun::types::ClassType, sun::types::TypePtr

instantiateGenericClass(const GenericClassInfo &genericClassInfo, const std::vector< sun::types::TypePtr > &typeArgs)

public · function · Source (opens in a new tab)

std::shared_ptr< sun::types::ClassType > sun::semantic_analysis::GenericSpecializer::instantiateGenericClass(const GenericClassInfo &genericClassInfo, const std::vector< sun::types::TypePtr > &typeArgs)

The same when the template has already been looked up.

Related: sun::types::ClassType, GenericClassInfo, sun::types::TypePtr

instantiateGenericEnum

instantiateGenericEnum(const std::string &baseName, const std::vector< sun::types::TypePtr > &typeArgs)

public · function · Source (opens in a new tab)

std::shared_ptr< sun::types::EnumType > sun::semantic_analysis::GenericSpecializer::instantiateGenericEnum(const std::string &baseName, const std::vector< sun::types::TypePtr > &typeArgs)

Instantiate Option<i32> from a generic enum template (monomorphization).

Related: sun::types::EnumType, sun::types::TypePtr

instantiateGenericEnum(const GenericEnumInfo &genericInfo, const std::vector< sun::types::TypePtr > &typeArgs)

public · function · Source (opens in a new tab)

std::shared_ptr< sun::types::EnumType > sun::semantic_analysis::GenericSpecializer::instantiateGenericEnum(const GenericEnumInfo &genericInfo, const std::vector< sun::types::TypePtr > &typeArgs)

Instantiate an already selected template without repeating name lookup.

Related: sun::types::EnumType, GenericEnumInfo, sun::types::TypePtr

instantiateGenericFunction

public · function · Source (opens in a new tab)

std::optional< SpecializedFunctionInfo > sun::semantic_analysis::GenericSpecializer::instantiateGenericFunction(const GenericFunctionInfo &genericInfo, const std::vector< sun::types::TypePtr > &typeArgs, const std::optional< std::vector< sun::types::TypePtr > > &variadicArgTypes=std::nullopt)

Monomorphize a generic function for the given type arguments, reusing the cached specialization when there is one.

Empty when it cannot be built. variadicArgTypes carries the types filling an args... pack at the call site; like the method path, they drive the specialization's arity and its specialization key, and std::nullopt defers a pack-bearing template until a call site supplies them.

Related: SpecializedFunctionInfo, GenericFunctionInfo, sun::types::TypePtr

instantiateGenericInterface

instantiateGenericInterface(const GenericInterfaceInfo &genericInfo, const std::vector< sun::types::TypePtr > &typeArgs)

public · function · Source (opens in a new tab)

std::shared_ptr< sun::types::InterfaceType > sun::semantic_analysis::GenericSpecializer::instantiateGenericInterface(const GenericInterfaceInfo &genericInfo, const std::vector< sun::types::TypePtr > &typeArgs)

Instantiate an already selected template without repeating name lookup.

Related: sun::types::InterfaceType, GenericInterfaceInfo, sun::types::TypePtr

instantiateGenericInterface(const std::string &baseName, const std::vector< sun::types::TypePtr > &typeArgs)

public · function · Source (opens in a new tab)

std::shared_ptr< sun::types::InterfaceType > sun::semantic_analysis::GenericSpecializer::instantiateGenericInterface(const std::string &baseName, const std::vector< sun::types::TypePtr > &typeArgs)

Monomorphize a generic interface for the given type arguments, reusing the specialization if it already exists.

Related: sun::types::InterfaceType, sun::types::TypePtr

instantiateGenericMethod

public · function · Source (opens in a new tab)

std::shared_ptr< FunctionAST > sun::semantic_analysis::GenericSpecializer::instantiateGenericMethod(std::shared_ptr< sun::types::ClassType > classType, const std::string &methodName, const std::vector< sun::types::TypePtr > &methodTypeArgs, const std::optional< std::vector< sun::types::TypePtr > > &variadicArgTypes=std::nullopt)

Instantiates a generic method on a class with specific type arguments.

Stores the specialization on the generic method's FunctionAST. Returns the specialized FunctionAST for codegen lookup. variadicArgTypes carries the resolved types of the actual variadic arguments at the call site (for a method ending in a pack). When the method is variadic, these drive the specialization's arity, its init overload selection, and its specialization key. std::nullopt means "no call info available" (e.g. from type inference): a variadic method is then not specialized here and the call-site trigger, which supplies the types (possibly an empty vector for a zero-arg call), does the real work.

Related: sun::ast::FunctionAST, sun::types::ClassType, sun::types::TypePtr, FunctionAST

lookupGenericClassOf

public · function · Source (opens in a new tab)

const GenericClassInfo * sun::semantic_analysis::GenericSpecializer::lookupGenericClassOf(const sun::types::ClassType &specialized) const

Retrieve the template or generic-method definition by declaration ID.

Related: GenericClassInfo, sun::types::ClassType

requireGenericSpecialization

public · function · Source (opens in a new tab)

SpecializedFunctionInfo sun::semantic_analysis::GenericSpecializer::requireGenericSpecialization(const GenericFunctionInfo &genericInfo, const std::vector< sun::types::TypePtr > &typeArgs, const std::string &displayName, std::optional< sun::support::Position > loc, const std::optional< std::vector< sun::types::TypePtr > > &variadicArgTypes=std::nullopt)

Instantiate for a call site: same as instantiateGenericFunction, but a failure is the call's error rather than an empty optional to unpack.

Related: SpecializedFunctionInfo, GenericFunctionInfo, sun::types::TypePtr, sun::support::Position

templateStillAbstract

public · function · Source (opens in a new tab)

bool sun::semantic_analysis::GenericSpecializer::templateStillAbstract(const GenericFunctionInfo &genericInfo, const std::vector< sun::types::TypePtr > &typeArgs)

True when a call cannot be specialized yet because the template's type arguments are still type parameters.

Usually that shows in typeArgs, but a pack-only template has none of its own and may still borrow a type parameter from an enclosing generic through args...: _params_of<T>.

Related: GenericFunctionInfo, sun::types::TypePtr

Private Functions

analyzeCallableBody

private · function · Source (opens in a new tab)

void sun::semantic_analysis::GenericSpecializer::analyzeCallableBody(sun::ast::FunctionAST &ast, std::shared_ptr< sun::types::ClassType > classType, bool isMethod)

Bind prepared parameters, captures and packs before checking a callable.

Related: sun::ast::FunctionAST, sun::types::ClassType

analyzeClassBody

private · function · Source (opens in a new tab)

void sun::semantic_analysis::GenericSpecializer::analyzeClassBody(sun::ast::ClassDefinitionAST &ast, std::shared_ptr< sun::types::ClassType > classType)

Check ordinary specialized methods, then constructor initialization.

Related: sun::ast::ClassDefinitionAST, sun::types::ClassType

Private Fields

ctx_

private · variable · Source (opens in a new tab)

SemanticContext& sun::semantic_analysis::GenericSpecializer::ctx_

No documentation comment.

Related: SemanticContext

pendingBodies_

private · variable · Source (opens in a new tab)

std::deque<PendingBody> sun::semantic_analysis::GenericSpecializer::pendingBodies_

No documentation comment.

Related: PendingBody

sema_

private · variable · Source (opens in a new tab)

SemanticAnalyzer& sun::semantic_analysis::GenericSpecializer::sema_

No documentation comment.

Related: SemanticAnalyzer

specializedFunctionCache_

private · variable · Source (opens in a new tab)

std::map<sun::semantic_analysis::DeclarationId, SpecializedFunctionInfo> sun::semantic_analysis::GenericSpecializer::specializedFunctionCache_

No documentation comment.

Related: sun::semantic_analysis::DeclarationId, SpecializedFunctionInfo