def fini_expression( ir: irast.Base, *, ctx: context.ContextLevel, ) -> irast.Command: if (isinstance(ir, irast.Set) and pathctx.get_set_scope(ir, ctx=ctx) is None): ir = setgen.scoped_set(ir, ctx=ctx) cardinality = qltypes.Cardinality.AT_MOST_ONE if ctx.path_scope is not None: # The inference context object will be shared between # cardinality and multiplicity inferrers. inf_ctx = inference.make_ctx(env=ctx.env) # Simple expressions have no scope. cardinality = inference.infer_cardinality( ir, scope_tree=ctx.path_scope, ctx=inf_ctx, ) multiplicity: Optional[qltypes.Multiplicity] = None if ctx.env.options.validate_multiplicity: multiplicity = inference.infer_multiplicity( ir, scope_tree=ctx.path_scope, ctx=inf_ctx, ) # Fix up weak namespaces _rewrite_weak_namespaces(ir, ctx) if ctx.path_scope is not None: ctx.path_scope.validate_unique_ids() if isinstance(ir, irast.Command): if isinstance(ir, irast.ConfigCommand): ir.scope_tree = ctx.path_scope # IR is already a Command return ir volatility = inference.infer_volatility(ir, env=ctx.env) if ctx.env.options.schema_view_mode: for view in ctx.view_nodes.values(): if view.is_collection(): continue assert isinstance(view, s_types.InheritingType) _elide_derived_ancestors(view, ctx=ctx) if not isinstance(view, s_sources.Source): continue view_own_pointers = view.get_pointers(ctx.env.schema) for vptr in view_own_pointers.objects(ctx.env.schema): _elide_derived_ancestors(vptr, ctx=ctx) ctx.env.schema = vptr.set_field_value( ctx.env.schema, 'from_alias', True, ) tgt = vptr.get_target(ctx.env.schema) assert tgt is not None if (tgt.is_union_type(ctx.env.schema) and tgt.get_is_opaque_union(ctx.env.schema)): # Opaque unions should manifest as std::BaseObject # in schema views. ctx.env.schema = vptr.set_target( ctx.env.schema, ctx.env.schema.get('std::BaseObject', type=s_types.Type), ) if not isinstance(vptr, s_sources.Source): continue vptr_own_pointers = vptr.get_pointers(ctx.env.schema) for vlprop in vptr_own_pointers.objects(ctx.env.schema): _elide_derived_ancestors(vlprop, ctx=ctx) ctx.env.schema = vlprop.set_field_value( ctx.env.schema, 'from_alias', True, ) expr_type = inference.infer_type(ir, ctx.env) in_polymorphic_func = (ctx.env.options.func_params is not None and ctx.env.options.func_params.has_polymorphic( ctx.env.schema)) if (not in_polymorphic_func and not ctx.env.options.allow_generic_type_output): anytype = expr_type.find_any(ctx.env.schema) if anytype is not None: raise errors.QueryError( 'expression returns value of indeterminate type', hint='Consider using an explicit type cast.', context=ctx.env.type_origins.get(anytype)) if ctx.must_use_views: alias, srcctx = next(iter(ctx.must_use_views.values())) raise errors.QueryError( f'unused alias definition: {str(alias)!r}', context=srcctx, ) result = irast.Statement( expr=ir, params=list(ctx.env.query_parameters.values()), views=ctx.view_nodes, source_map=ctx.source_map, scope_tree=ctx.env.path_scope, cardinality=cardinality, volatility=volatility, multiplicity=multiplicity, stype=expr_type, view_shapes={ src: [ptr for ptr, _ in ptrs] for src, ptrs in ctx.env.view_shapes.items() }, view_shapes_metadata=ctx.env.view_shapes_metadata, schema=ctx.env.schema, schema_refs=frozenset(ctx.env.schema_refs - ctx.env.created_schema_objects), schema_ref_exprs=ctx.env.schema_ref_exprs, new_coll_types=frozenset( t for t in (ctx.env.schema_refs | ctx.env.created_schema_objects) if isinstance(t, s_types.Collection) and t != expr_type), type_rewrites={s.typeref.id: s for s in ctx.type_rewrites.values()}, ) return result
def fini_expression( ir: irast.Base, *, ctx: context.ContextLevel, ) -> irast.Command: # Run delayed work callbacks. for cb in ctx.completion_work: cb(ctx=ctx) ctx.completion_work.clear() for ir_set in ctx.env.set_types: if ir_set.path_id.namespace: ir_set.path_id = ir_set.path_id.strip_weak_namespaces() if isinstance(ir, irast.Command): if isinstance(ir, irast.ConfigCommand): ir.scope_tree = ctx.path_scope # IR is already a Command return ir if ctx.path_scope is not None: # Simple expressions have no scope. for node in ctx.path_scope.path_descendants: if node.path_id.namespace: node.path_id = node.path_id.strip_weak_namespaces() cardinality = inference.infer_cardinality( ir, scope_tree=ctx.path_scope, env=ctx.env) else: cardinality = qltypes.Cardinality.ONE if ctx.env.schema_view_mode: for view in ctx.view_nodes.values(): if view.is_collection(): continue _elide_derived_ancestors(view, ctx=ctx) if not isinstance(view, s_sources.Source): continue view_own_pointers = view.get_pointers(ctx.env.schema) for vptr in view_own_pointers.objects(ctx.env.schema): _elide_derived_ancestors(vptr, ctx=ctx) tgt = vptr.get_target(ctx.env.schema) if (tgt.is_union_type(ctx.env.schema) and tgt.get_is_opaque_union(ctx.env.schema)): # Opaque unions should manifest as std::Object # in schema views. ctx.env.schema = vptr.set_target( ctx.env.schema, ctx.env.schema.get('std::Object'), ) if not hasattr(vptr, 'get_pointers'): continue vptr_own_pointers = vptr.get_pointers(ctx.env.schema) for vlprop in vptr_own_pointers.objects(ctx.env.schema): _elide_derived_ancestors(vlprop, ctx=ctx) expr_type = inference.infer_type(ir, ctx.env) in_polymorphic_func = ( ctx.env.func_params is not None and ctx.env.func_params.has_polymorphic(ctx.env.schema) ) if not in_polymorphic_func and not ctx.env.allow_generic_type_output: anytype = expr_type.find_any(ctx.env.schema) if anytype is not None: raise errors.QueryError( 'expression returns value of indeterminate type', hint='Consider using an explicit type cast.', context=ctx.env.type_origins.get(anytype)) if ctx.must_use_views: alias, srcctx = next(iter(ctx.must_use_views.values())) raise errors.QueryError( f'unused alias definition: {alias!r}', context=srcctx, ) result = irast.Statement( expr=ir, params=ctx.env.query_parameters, views=ctx.view_nodes, source_map=ctx.source_map, scope_tree=ctx.path_scope, cardinality=cardinality, stype=expr_type, view_shapes=ctx.env.view_shapes, view_shapes_metadata=ctx.env.view_shapes_metadata, schema=ctx.env.schema, schema_refs=frozenset( ctx.env.schema_refs - ctx.env.created_schema_objects), new_coll_types=frozenset( t for t in ctx.env.created_schema_objects if isinstance(t, s_types.Collection) and t != expr_type ), ) return result
def fini_expression( ir: irast.Base, *, ctx: context.ContextLevel) -> irast.Command: # Run delayed work callbacks. for cb in ctx.completion_work: cb(ctx=ctx) ctx.completion_work.clear() for ir_set in ctx.env.set_types: if ir_set.path_id.namespace: ir_set.path_id = ir_set.path_id.strip_weak_namespaces() if isinstance(ir, irast.Command): if isinstance(ir, irast.ConfigCommand): ir.scope_tree = ctx.path_scope # IR is already a Command return ir if ctx.path_scope is not None: # Simple expressions have no scope. for node in ctx.path_scope.get_all_path_nodes(include_subpaths=True): if node.path_id.namespace: node.path_id = node.path_id.strip_weak_namespaces() cardinality = inference.infer_cardinality( ir, scope_tree=ctx.path_scope, env=ctx.env) else: cardinality = qltypes.Cardinality.ONE if ctx.env.schema_view_mode: for view in ctx.view_nodes.values(): if not hasattr(view, 'get_own_pointers'): # duck check continue view_own_pointers = view.get_own_pointers(ctx.env.schema) for vptr in view_own_pointers.objects(ctx.env.schema): ctx.env.schema = vptr.set_field_value( ctx.env.schema, 'target', vptr.get_target(ctx.env.schema).material_type( ctx.env.schema)) derived_from = vptr.get_derived_from(ctx.env.schema) if (derived_from is not None and derived_from.get_derived_from(ctx.env.schema) is not None): ctx.env.schema = vptr.set_field_value( ctx.env.schema, 'derived_from', derived_from.get_nearest_non_derived_parent( ctx.env.schema, ) ) if not hasattr(vptr, 'get_own_pointers'): continue vptr_own_pointers = vptr.get_own_pointers(ctx.env.schema) for vlprop in vptr_own_pointers.objects(ctx.env.schema): vlprop_target = vlprop.get_target(ctx.env.schema) ctx.env.schema = vlprop.set_field_value( ctx.env.schema, 'target', vlprop_target.material_type(ctx.env.schema)) expr_type = inference.infer_type(ir, ctx.env) if ctx.func is None: anytype = expr_type.find_any(ctx.env.schema) if anytype is not None: raise errors.QueryError( 'expression returns value of indeterminate type', hint='Consider using an explicit type cast.', context=ctx.env.type_origins.get(anytype)) result = irast.Statement( expr=ir, params=ctx.env.query_parameters, views=ctx.view_nodes, source_map=ctx.source_map, scope_tree=ctx.path_scope, cardinality=cardinality, stype=expr_type, view_shapes=ctx.env.view_shapes, view_shapes_metadata=ctx.env.view_shapes_metadata, schema=ctx.env.schema, ) return result
def fini_expression(ir: irast.Base, *, ctx: context.ContextLevel) -> irast.Command: # Run delayed work callbacks. for cb in ctx.completion_work: cb(ctx=ctx) ctx.completion_work.clear() for ir_set in ctx.env.set_types: if ir_set.path_id.namespace: ir_set.path_id = ir_set.path_id.strip_weak_namespaces() if isinstance(ir, irast.Command): if isinstance(ir, irast.ConfigCommand): ir.scope_tree = ctx.path_scope # IR is already a Command return ir if ctx.path_scope is not None: # Simple expressions have no scope. for node in ctx.path_scope.get_all_path_nodes(include_subpaths=True): if node.path_id.namespace: node.path_id = node.path_id.strip_weak_namespaces() cardinality = inference.infer_cardinality(ir, scope_tree=ctx.path_scope, env=ctx.env) else: cardinality = qltypes.Cardinality.ONE if ctx.env.schema_view_mode: for view in ctx.view_nodes.values(): if view.is_collection(): continue _elide_derived_ancestors(view, ctx=ctx) if not isinstance(view, s_sources.Source): continue view_own_pointers = view.get_pointers(ctx.env.schema) for vptr in view_own_pointers.objects(ctx.env.schema): _elide_derived_ancestors(vptr, ctx=ctx) if not hasattr(vptr, 'get_pointers'): continue vptr_own_pointers = vptr.get_pointers(ctx.env.schema) for vlprop in vptr_own_pointers.objects(ctx.env.schema): _elide_derived_ancestors(vlprop, ctx=ctx) expr_type = inference.infer_type(ir, ctx.env) in_polymorphic_func = (ctx.env.func_params is not None and ctx.env.func_params.has_polymorphic(ctx.env.schema)) if not in_polymorphic_func and not ctx.env.allow_generic_type_output: anytype = expr_type.find_any(ctx.env.schema) if anytype is not None: raise errors.QueryError( 'expression returns value of indeterminate type', hint='Consider using an explicit type cast.', context=ctx.env.type_origins.get(anytype)) result = irast.Statement( expr=ir, params=ctx.env.query_parameters, views=ctx.view_nodes, source_map=ctx.source_map, scope_tree=ctx.path_scope, cardinality=cardinality, stype=expr_type, view_shapes=ctx.env.view_shapes, view_shapes_metadata=ctx.env.view_shapes_metadata, schema=ctx.env.schema, schema_refs=frozenset(ctx.env.schema_refs), ) return result