lang: Evaluate generic layout intrinsics
06b2f463e59e8fbee9baca4759e24680495296c8901151103103129f51d84418
Validate the typed size and alignment declarations, fold direct calls with concrete layouts, and evaluate abstract queries after substitution. Emit a constant-returning function when a query is used as a value. Assisted-by: Codex:gpt-6 Change-Id: Ie9d54cdad5df447242729828815bb78ce364af63
1 parent
2ee3620a
compiler/radiance/radiance.rad
+26 -14
| 1089 | 1089 | let childId = module::childAt(entry, i); |
|
| 1090 | 1090 | try lowerModuleTreeInto(ctx, low, modules, childId, false, pkg, sink); |
|
| 1091 | 1091 | } |
|
| 1092 | 1092 | } |
|
| 1093 | 1093 | ||
| 1094 | + | /// Print a source prefix for a lowering diagnostic. |
|
| 1095 | + | fn printLowerLocation(modules: module::ModuleTable, site: il::SrcLoc) { |
|
| 1096 | + | if let entry = module::get(modules, site.moduleId) { |
|
| 1097 | + | io::print(entry.filePath); |
|
| 1098 | + | if let source = entry.source { |
|
| 1099 | + | if let loc = scanner::getLocation( |
|
| 1100 | + | scanner::SourceLoc::File(entry.filePath), source, site.offset |
|
| 1101 | + | ) { |
|
| 1102 | + | io::print(":"); |
|
| 1103 | + | io::printU32(loc.line as u32); |
|
| 1104 | + | io::print(":"); |
|
| 1105 | + | io::printU32(loc.col as u32); |
|
| 1106 | + | } |
|
| 1107 | + | } |
|
| 1108 | + | io::print(": "); |
|
| 1109 | + | } |
|
| 1110 | + | } |
|
| 1111 | + | ||
| 1094 | 1112 | /// Report a lowering failure and return the driver error. A sink output failure has |
|
| 1095 | 1113 | /// already been reported. Arena exhaustion is not a compiler bug, so it is not an |
|
| 1096 | 1114 | /// internal error. |
|
| 1097 | 1115 | fn lowerError(err: lower::LowerError, modules: module::ModuleTable) -> Error { |
|
| 1098 | 1116 | if let case lower::LowerError::SpecializationLimit(trace) = err { |
|
| 1099 | 1117 | for site, i in trace { |
|
| 1100 | - | if let entry = module::get(modules, site.moduleId) { |
|
| 1101 | - | io::print(entry.filePath); |
|
| 1102 | - | if let source = entry.source { |
|
| 1103 | - | if let loc = scanner::getLocation( |
|
| 1104 | - | scanner::SourceLoc::File(entry.filePath), source, site.offset |
|
| 1105 | - | ) { |
|
| 1106 | - | io::print(":"); |
|
| 1107 | - | io::printU32(loc.line as u32); |
|
| 1108 | - | io::print(":"); |
|
| 1109 | - | io::printU32(loc.col as u32); |
|
| 1110 | - | } |
|
| 1111 | - | } |
|
| 1112 | - | io::print(": "); |
|
| 1113 | - | } |
|
| 1118 | + | printLowerLocation(modules, site); |
|
| 1114 | 1119 | if i == 0 { |
|
| 1115 | 1120 | io::print("error: "); |
|
| 1116 | 1121 | io::print(lower::errorMessage(err)); |
|
| 1117 | 1122 | io::print("\n"); |
|
| 1118 | 1123 | } else { |
|
| 1119 | 1124 | io::print("note: specialization requested here\n"); |
|
| 1120 | 1125 | } |
|
| 1121 | 1126 | } |
|
| 1122 | 1127 | return Error::Other; |
|
| 1123 | 1128 | } |
|
| 1129 | + | if let case lower::LowerError::InvalidLayoutQuery(site) = err { |
|
| 1130 | + | printLowerLocation(modules, site); |
|
| 1131 | + | io::print("error: "); |
|
| 1132 | + | io::print(lower::errorMessage(err)); |
|
| 1133 | + | io::print("\n"); |
|
| 1134 | + | return Error::Other; |
|
| 1135 | + | } |
|
| 1124 | 1136 | if err == lower::LowerError::OutputFailed { |
|
| 1125 | 1137 | return Error::Other; |
|
| 1126 | 1138 | } |
|
| 1127 | 1139 | if err == lower::LowerError::AllocationFailed { |
|
| 1128 | 1140 | return error(&["lowering ran out of memory"]); |
lib/std/lang/lower.rad
+78 -18
| 176 | 176 | AllocationFailed, |
|
| 177 | 177 | /// Two different error types have the same tag. |
|
| 178 | 178 | ErrorTagCollision, |
|
| 179 | 179 | /// Generic requests exceed the package count or type nesting limit. |
|
| 180 | 180 | SpecializationLimit(*[il::SrcLoc]), |
|
| 181 | + | /// The concrete type in a specialized layout query has an invalid layout. |
|
| 182 | + | InvalidLayoutQuery(il::SrcLoc), |
|
| 181 | 183 | } |
|
| 182 | 184 | ||
| 183 | 185 | /// Describe a lowering error in one sentence. |
|
| 184 | 186 | /// |
|
| 185 | 187 | /// Each error has its own text, so a failure report names the failed check. |
|
| 186 | 188 | export fn errorMessage(err: LowerError) -> *[u8] { |
|
| 187 | 189 | match err { |
|
| 190 | + | case LowerError::InvalidLayoutQuery(_) => |
|
| 191 | + | return "type layout exceeds the supported byte size", |
|
| 188 | 192 | case LowerError::SpecializationLimit(_) => |
|
| 189 | 193 | return "generic specialization exceeds compiler limits", |
|
| 190 | 194 | case LowerError::OutputFailed => return "the function output failed", |
|
| 191 | 195 | case LowerError::MissingSymbol(_) => return "a node has no symbol", |
|
| 192 | 196 | case LowerError::MissingType(_) => return "a node has no type", |
| 1700 | 1704 | if not resolver::fitsTypeDepth(arg, resolver::MAX_APPLICATION_TYPE_DEPTH) { |
|
| 1701 | 1705 | throw specializationLimit(self, site); |
|
| 1702 | 1706 | } |
|
| 1703 | 1707 | } |
|
| 1704 | 1708 | let symbol = self.resolver.symbols[*app.template]; |
|
| 1705 | - | if let provider = try foreignPackage(self, symbol.moduleId) { |
|
| 1709 | + | let mut owner = symbol.moduleId; |
|
| 1710 | + | if let case resolver::NodeExtra::LayoutIntrinsic(_) = resolver::extraFor( |
|
| 1711 | + | &self.resolver, symbol.node |
|
| 1712 | + | ) { |
|
| 1713 | + | set owner = nil; |
|
| 1714 | + | } else if let provider = try foreignPackage(self, symbol.moduleId) { |
|
| 1706 | 1715 | throw LowerError::Unsupported; |
|
| 1707 | 1716 | } |
|
| 1708 | - | let base = try qualifyName(self, symbol.moduleId, symbol.name); |
|
| 1717 | + | let base = try qualifyName(self, owner, symbol.name); |
|
| 1709 | 1718 | let name = try numberedName(self, base, ordinal, "generic"); |
|
| 1710 | 1719 | let case resolver::Type::Fn(signature) = specializeType(self, resolver::Type::Fn(app.signature)) |
|
| 1711 | 1720 | else panic "specializedFunction: expected a function type"; |
|
| 1712 | 1721 | let request = FunctionSpecialization { |
|
| 1713 | 1722 | template: app.template, |
| 1731 | 1740 | let request = self.tables.specializations.queue[i]; |
|
| 1732 | 1741 | let symbol = self.resolver.symbols[*request.template]; |
|
| 1733 | 1742 | let case ast::NodeValue::FnDecl(decl) = symbol.node.value else { |
|
| 1734 | 1743 | panic "emitSpecializations: expected a template"; |
|
| 1735 | 1744 | }; |
|
| 1736 | - | if decl.body == nil { |
|
| 1737 | - | throw LowerError::MissingMetadata; |
|
| 1738 | - | } |
|
| 1739 | - | set self.currentMod = symbol.moduleId; |
|
| 1740 | - | set self.tables.specializations.active = i; |
|
| 1741 | - | let f = try lowerFunction( |
|
| 1742 | - | self, symbol.node, decl, request.signature, request.name |
|
| 1743 | - | ) catch e { |
|
| 1745 | + | let f = try lowerSpecialization(self, symbol, decl, request, i) catch e { |
|
| 1744 | 1746 | set self.tables.specializations.active = nil; |
|
| 1745 | 1747 | set self.currentMod = module; |
|
| 1746 | 1748 | throw e; |
|
| 1747 | 1749 | }; |
|
| 1748 | 1750 | set self.tables.specializations.active = nil; |
| 1750 | 1752 | try emitFunction(self, f, FnRole::Normal, Linkage::Local, sink); |
|
| 1751 | 1753 | set self.tables.specializations.emitted += 1; |
|
| 1752 | 1754 | } |
|
| 1753 | 1755 | } |
|
| 1754 | 1756 | ||
| 1757 | + | /// Lower one requested template body or intrinsic function value. |
|
| 1758 | + | fn lowerSpecialization( |
|
| 1759 | + | self: &mut Lowerer, |
|
| 1760 | + | symbol: resolver::Symbol, |
|
| 1761 | + | decl: ast::FnDecl, |
|
| 1762 | + | request: FunctionSpecialization, |
|
| 1763 | + | index: u32 |
|
| 1764 | + | ) -> *il::Fn throws (LowerError) { |
|
| 1765 | + | if let case resolver::NodeExtra::LayoutIntrinsic(query) = resolver::extraFor( |
|
| 1766 | + | &self.resolver, symbol.node |
|
| 1767 | + | ) { |
|
| 1768 | + | let value = resolver::layoutQueryValue(&self.resolver, request.substitution.args[0], query) |
|
| 1769 | + | else throw LowerError::InvalidLayoutQuery(request.site); |
|
| 1770 | + | set self.currentMod = request.site.moduleId; |
|
| 1771 | + | try beginFn(self, symbol.node, request.signature, request.name); |
|
| 1772 | + | set self.srcLoc.offset = request.site.offset; |
|
| 1773 | + | let entry = try createBlock(self, "entry"); |
|
| 1774 | + | set self.entryBlock = entry; |
|
| 1775 | + | switchToBlock(self, entry); |
|
| 1776 | + | try emit(self, il::Instr::Ret { val: il::Val::Imm(value as i64) }); |
|
| 1777 | + | let blocks = try finalizeBlocks(self); |
|
| 1778 | + | return try storeFn( |
|
| 1779 | + | self, request.name, &[], il::ValType::Data(il::Type::W32), false, blocks |
|
| 1780 | + | ); |
|
| 1781 | + | } |
|
| 1782 | + | if decl.body == nil { |
|
| 1783 | + | throw LowerError::MissingMetadata; |
|
| 1784 | + | } |
|
| 1785 | + | set self.currentMod = symbol.moduleId; |
|
| 1786 | + | set self.tables.specializations.active = index; |
|
| 1787 | + | return try lowerFunction(self, symbol.node, decl, request.signature, request.name); |
|
| 1788 | + | } |
|
| 1789 | + | ||
| 1755 | 1790 | /// Store a complete function in the function arena and return its address. |
|
| 1756 | 1791 | /// The arena keeps the function until its owner resets the arena. |
|
| 1757 | 1792 | fn allocFn(self: &mut Lowerer, func: il::Fn) -> *il::Fn throws (LowerError) { |
|
| 1758 | 1793 | let mut funcs: *mut [il::Fn] = &mut []; |
|
| 1759 | 1794 | try funcs.append(func, self.fnArena) catch { |
| 2628 | 2663 | } |
|
| 2629 | 2664 | else => {} |
|
| 2630 | 2665 | } |
|
| 2631 | 2666 | // Function pointer references in constant data. |
|
| 2632 | 2667 | if let case resolver::Type::Fn(fnType) = ty { |
|
| 2633 | - | let sym = resolver::symbolFor(&self.resolver, node) |
|
| 2634 | - | else throw LowerError::MissingSymbol(node); |
|
| 2635 | - | let modId = resolver::moduleIdForSymbol(&self.resolver, sym); |
|
| 2636 | - | let symName = self.resolver.symbols[*sym].name; |
|
| 2637 | - | let qualName = try qualifyName(self, modId, symName); |
|
| 2638 | - | if let provider = try foreignPackage(self, modId) { |
|
| 2639 | - | try recordFunctionReference(self, provider, qualName, fnType); |
|
| 2668 | + | let mut name: *[u8] = undefined; |
|
| 2669 | + | if let case resolver::NodeExtra::FunctionApplication(index) = resolver::extraFor( |
|
| 2670 | + | &self.resolver, node |
|
| 2671 | + | ) { |
|
| 2672 | + | let case il::Val::FnAddr(specialized) = try specializedFunction(self, node, index) |
|
| 2673 | + | else panic "lowerConstDataPayloadInto: expected a function address"; |
|
| 2674 | + | set name = specialized; |
|
| 2675 | + | } else { |
|
| 2676 | + | let sym = resolver::symbolFor(&self.resolver, node) |
|
| 2677 | + | else throw LowerError::MissingSymbol(node); |
|
| 2678 | + | let modId = resolver::moduleIdForSymbol(&self.resolver, sym); |
|
| 2679 | + | let symName = self.resolver.symbols[*sym].name; |
|
| 2680 | + | set name = try qualifyName(self, modId, symName); |
|
| 2681 | + | if let provider = try foreignPackage(self, modId) { |
|
| 2682 | + | try recordFunctionReference(self, provider, name, fnType); |
|
| 2683 | + | } |
|
| 2640 | 2684 | } |
|
| 2641 | 2685 | try dataBuilderPush( |
|
| 2642 | 2686 | b, |
|
| 2643 | 2687 | il::DataValue { |
|
| 2644 | - | item: il::DataItem::Fn(qualName), count: 1 |
|
| 2688 | + | item: il::DataItem::Fn(name), count: 1 |
|
| 2645 | 2689 | }, |
|
| 2646 | 2690 | self.resolver.arena |
|
| 2647 | 2691 | ); |
|
| 2648 | 2692 | return b.values.len - start; |
|
| 2649 | 2693 | } |
| 2687 | 2731 | case ast::NodeValue::ArrayRepeatLit(repeat) => |
|
| 2688 | 2732 | try lowerConstArrayRepeatInto(self, repeat, ty, dataPrefix, b), |
|
| 2689 | 2733 | case ast::NodeValue::RecordLit(recLit) => |
|
| 2690 | 2734 | try lowerConstRecordLitInto(self, node, recLit, ty, dataPrefix, b), |
|
| 2691 | 2735 | case ast::NodeValue::Call(call) => { |
|
| 2736 | + | if resolver::constValueEntry(&self.resolver, node) <> nil { |
|
| 2737 | + | try lowerConstScalarDataInto(self, node, ty, dataPrefix, b); |
|
| 2738 | + | return b.values.len - start; |
|
| 2739 | + | } |
|
| 2692 | 2740 | let calleeSym = resolver::symbolFor(&self.resolver, call.callee) |
|
| 2693 | 2741 | else throw LowerError::MissingSymbol(call.callee); |
|
| 2694 | 2742 | match self.resolver.symbols[*calleeSym].data { |
|
| 2695 | 2743 | case resolver::SymbolData::Variant { .. } => |
|
| 2696 | 2744 | try lowerConstUnionVariantInto( |
| 10652 | 10700 | node: *ast::Node, |
|
| 10653 | 10701 | call: &ast::Call |
|
| 10654 | 10702 | ) -> il::Val throws (LowerError) { |
|
| 10655 | 10703 | let nodeData = resolver::extraFor(&self.resolver, node); |
|
| 10656 | 10704 | ||
| 10705 | + | if let case resolver::NodeExtra::LayoutQuery { query, target } = nodeData { |
|
| 10706 | + | if let value = resolver::constValueEntry(&self.resolver, node) { |
|
| 10707 | + | return try constValueToVal(self, value, node); |
|
| 10708 | + | } |
|
| 10709 | + | let concrete = specializeType(self, *target); |
|
| 10710 | + | let moduleId = self.currentMod else 0; |
|
| 10711 | + | let value = resolver::layoutQueryValue(&self.resolver, concrete, query) |
|
| 10712 | + | else throw LowerError::InvalidLayoutQuery(il::SrcLoc { |
|
| 10713 | + | moduleId, offset: node.span.offset |
|
| 10714 | + | }); |
|
| 10715 | + | return il::Val::Imm(value as i64); |
|
| 10716 | + | } |
|
| 10657 | 10717 | // Check for slice method dispatch. |
|
| 10658 | 10718 | if let case resolver::NodeExtra::SliceAppend { elemType } = nodeData { |
|
| 10659 | 10719 | return try lowerSliceAppend(self, call, specializeTypePtr(self, elemType)); |
|
| 10660 | 10720 | } |
|
| 10661 | 10721 | if let case resolver::NodeExtra::SliceReserve { elemType } = nodeData { |
lib/std/lang/resolver.rad
+158 -13
| 679 | 679 | RequiresCopy, |
|
| 680 | 680 | /// Expected type syntax in an argument. |
|
| 681 | 681 | ExpectedType, |
|
| 682 | 682 | /// An operation needs a concrete layout. |
|
| 683 | 683 | LayoutDependsOnParameter, |
|
| 684 | + | /// A concrete layout cannot be represented as a u32 byte count. |
|
| 685 | + | InvalidLayout, |
|
| 684 | 686 | /// The compiler cannot resolve a generic declaration or application. |
|
| 685 | 687 | Generic, |
|
| 686 | 688 | } |
|
| 687 | 689 | ||
| 688 | 690 | /// High-level classification for semantic diagnostics. |
| 993 | 995 | record SuperAccessResult: Copy { |
|
| 994 | 996 | scope: ScopeId, |
|
| 995 | 997 | child: *ast::Node, |
|
| 996 | 998 | } |
|
| 997 | 999 | ||
| 1000 | + | /// Layout property returned by a compiler intrinsic. |
|
| 1001 | + | export union LayoutQuery: Copy { |
|
| 1002 | + | /// Byte size of one value, including padding. |
|
| 1003 | + | Size, |
|
| 1004 | + | /// Required alignment in bytes. |
|
| 1005 | + | Alignment, |
|
| 1006 | + | } |
|
| 1007 | + | ||
| 998 | 1008 | /// Node-specific resolver metadata. |
|
| 999 | 1009 | export union NodeExtra: Copy { |
|
| 1000 | 1010 | /// Byte capacity of the static heap produced by a constant call. |
|
| 1001 | 1011 | StaticHeap(u32), |
|
| 1002 | 1012 | /// No extra data for this node. |
|
| 1003 | 1013 | None, |
|
| 1004 | 1014 | /// Index of a checked generic function application. |
|
| 1005 | 1015 | FunctionApplication(u32), |
|
| 1016 | + | /// Validated layout intrinsic declaration. |
|
| 1017 | + | LayoutIntrinsic(LayoutQuery), |
|
| 1018 | + | /// A call to a layout intrinsic with its resolved type argument. |
|
| 1019 | + | LayoutQuery { |
|
| 1020 | + | /// Layout property to evaluate. |
|
| 1021 | + | query: LayoutQuery, |
|
| 1022 | + | /// Type whose layout is queried. |
|
| 1023 | + | target: *Type, |
|
| 1024 | + | }, |
|
| 1006 | 1025 | /// Resolved field index for record literal fields. |
|
| 1007 | 1026 | RecordField { index: u32 }, |
|
| 1008 | 1027 | /// Slice range metadata for subscript expressions with ranges. |
|
| 1009 | 1028 | SliceRange(SliceRangeInfo), |
|
| 1010 | 1029 | /// Cached union variant metadata for patterns/constructors. |
| 2136 | 2155 | case Type::Optional(inner) => return layoutDependsOnParameter(self, *inner), |
|
| 2137 | 2156 | else => return false, |
|
| 2138 | 2157 | } |
|
| 2139 | 2158 | } |
|
| 2140 | 2159 | ||
| 2160 | + | /// Evaluate a resolved layout query if its layout fits in a u32. |
|
| 2161 | + | export fn layoutQueryValue(self: &Resolver, target: Type, query: LayoutQuery) -> ?u32 { |
|
| 2162 | + | if not layoutFits(self, target) { |
|
| 2163 | + | return nil; |
|
| 2164 | + | } |
|
| 2165 | + | let layout = getTypeLayout(self, target); |
|
| 2166 | + | match query { |
|
| 2167 | + | case LayoutQuery::Size => return layout.size, |
|
| 2168 | + | case LayoutQuery::Alignment => return layout.alignment, |
|
| 2169 | + | } |
|
| 2170 | + | } |
|
| 2171 | + | ||
| 2172 | + | /// Check the sizes used by array and optional layout before u32 arithmetic. |
|
| 2173 | + | fn layoutFits(self: &Resolver, ty: Type) -> bool { |
|
| 2174 | + | match ty { |
|
| 2175 | + | case Type::Opaque => return false, |
|
| 2176 | + | case Type::Array(array) => { |
|
| 2177 | + | if not layoutFits(self, *array.item) { |
|
| 2178 | + | return false; |
|
| 2179 | + | } |
|
| 2180 | + | let size = getTypeLayout(self, *array.item).size as u64; |
|
| 2181 | + | return size * array.length as u64 <= mem::U32_MAX as u64; |
|
| 2182 | + | } |
|
| 2183 | + | case Type::Optional(inner) => { |
|
| 2184 | + | if not layoutFits(self, *inner) { |
|
| 2185 | + | return false; |
|
| 2186 | + | } |
|
| 2187 | + | if isNullableType(*inner) { |
|
| 2188 | + | return true; |
|
| 2189 | + | } |
|
| 2190 | + | let layout = getTypeLayout(self, *inner); |
|
| 2191 | + | let alignment = max(layout.alignment, 1) as u64; |
|
| 2192 | + | let unpadded = layout.size as u64 + alignment; |
|
| 2193 | + | let size = (unpadded + alignment - 1) & ~(alignment - 1); |
|
| 2194 | + | return size <= mem::U32_MAX as u64; |
|
| 2195 | + | } |
|
| 2196 | + | case Type::Nominal(id) => { |
|
| 2197 | + | match self.nominals[*id] { |
|
| 2198 | + | case NominalType::Parameter { .. }, NominalType::Placeholder(_) => return false, |
|
| 2199 | + | else => return true, |
|
| 2200 | + | } |
|
| 2201 | + | } |
|
| 2202 | + | else => return true, |
|
| 2203 | + | } |
|
| 2204 | + | } |
|
| 2205 | + | ||
| 2141 | 2206 | /// Get the layout of a type. |
|
| 2142 | 2207 | export fn getTypeLayout(self: &Resolver, ty: Type) -> Layout { |
|
| 2143 | 2208 | match ty { |
|
| 2144 | 2209 | case Type::Pointer { .. } => return Layout { size: PTR_SIZE, alignment: PTR_SIZE }, |
|
| 2145 | 2210 | case Type::Slice { .. }, Type::TraitObject { .. } => |
| 4434 | 4499 | else => {} |
|
| 4435 | 4500 | } |
|
| 4436 | 4501 | } |
|
| 4437 | 4502 | return false; |
|
| 4438 | 4503 | } |
|
| 4439 | - | case ast::NodeValue::FieldAccess(_), ast::NodeValue::Bracket { .. } => |
|
| 4440 | - | return self.nodeData.get(node.id).constValue |
|
| 4441 | - | <> nil or self.constantNode(node) <> nil, |
|
| 4504 | + | case ast::NodeValue::FieldAccess(_), ast::NodeValue::Bracket { .. } => { |
|
| 4505 | + | let metadata = nodeData(self, node); |
|
| 4506 | + | if let case NodeExtra::FunctionApplication(_) = metadata.extra { |
|
| 4507 | + | return true; |
|
| 4508 | + | } |
|
| 4509 | + | return metadata.constValue <> nil or self.constantNode(node) <> nil; |
|
| 4510 | + | } |
|
| 4442 | 4511 | case ast::NodeValue::BuiltinCall { .. } => |
|
| 4443 | 4512 | return self.nodeData.get(node.id).constValue |
|
| 4444 | 4513 | <> nil, |
|
| 4445 | 4514 | case ast::NodeValue::Call(call) => { |
|
| 4446 | - | if let case NodeExtra::StaticHeap(_) = self.nodeData.get(node.id).extra { |
|
| 4515 | + | let metadata = nodeData(self, node); |
|
| 4516 | + | if metadata.constValue <> nil { |
|
| 4517 | + | return true; |
|
| 4518 | + | } |
|
| 4519 | + | if let case NodeExtra::StaticHeap(_) = metadata.extra { |
|
| 4447 | 4520 | return true; |
|
| 4448 | 4521 | } |
|
| 4449 | 4522 | // Constructor calls (union variants, unlabeled records) are constant |
|
| 4450 | 4523 | // if all payload args are themselves constant. |
|
| 4451 | 4524 | if let sym = symbolFor(self, call.callee) { |
| 5302 | 5375 | let id = allocNominalType(self, NominalType::Parameter { name: text, copy }); |
|
| 5303 | 5376 | try bindTypeIdent(self, name, binder, id, 0); |
|
| 5304 | 5377 | } |
|
| 5305 | 5378 | } |
|
| 5306 | 5379 | ||
| 5380 | + | /// Identify a generic layout intrinsic by its compiler-defined name. |
|
| 5381 | + | fn layoutIntrinsic(name: *[u8]) -> ?LayoutQuery { |
|
| 5382 | + | if mem::eq(name, "sizeOf") { |
|
| 5383 | + | return LayoutQuery::Size; |
|
| 5384 | + | } |
|
| 5385 | + | if mem::eq(name, "alignOf") { |
|
| 5386 | + | return LayoutQuery::Alignment; |
|
| 5387 | + | } |
|
| 5388 | + | return nil; |
|
| 5389 | + | } |
|
| 5390 | + | ||
| 5391 | + | /// Verify the declaration contract of a layout intrinsic. |
|
| 5392 | + | fn validateLayoutIntrinsic( |
|
| 5393 | + | self: &mut Resolver, node: *ast::Node, decl: &ast::FnDecl, sig: &FnType, attrs: u32 |
|
| 5394 | + | ) throws (ResolveError) { |
|
| 5395 | + | if not ast::hasAttribute(attrs, ast::Attribute::Constant) |
|
| 5396 | + | or decl.binders.len <> 1 |
|
| 5397 | + | or sig.paramTypes.len <> 0 |
|
| 5398 | + | or *sig.returnType <> Type::U32 |
|
| 5399 | + | or sig.throwList.len <> 0 |
|
| 5400 | + | or sig.isUnsafe |
|
| 5401 | + | or decl.body <> nil |
|
| 5402 | + | { |
|
| 5403 | + | throw emitError(self, node, ErrorKind::ConstantFunction(ConstantError::InvalidIntrinsic)); |
|
| 5404 | + | } |
|
| 5405 | + | let case ast::NodeValue::TypeBinder { bound: nil, .. } = decl.binders[0].value |
|
| 5406 | + | else throw emitError(self, node, ErrorKind::ConstantFunction( |
|
| 5407 | + | ConstantError::InvalidIntrinsic |
|
| 5408 | + | )); |
|
| 5409 | + | } |
|
| 5410 | + | ||
| 5307 | 5411 | /// Analyze a function declaration signature and bind the function name. |
|
| 5308 | 5412 | fn resolveFnDecl(self: &mut Resolver, node: *ast::Node, decl: ast::FnDecl) -> Type |
|
| 5309 | 5413 | throws (ResolveError) |
|
| 5310 | 5414 | { |
|
| 5311 | 5415 | let attrMask = resolveAttributes(self, decl.attrs); |
|
| 5312 | - | if decl.binders.len > 0 |
|
| 5313 | - | and (ast::hasAttribute(attrMask, ast::Attribute::Default) |
|
| 5416 | + | let mut layoutQuery: ?LayoutQuery = nil; |
|
| 5417 | + | if ast::hasAttribute(attrMask, ast::Attribute::Intrinsic) { |
|
| 5418 | + | set layoutQuery = layoutIntrinsic(try nodeName(self, decl.name)); |
|
| 5419 | + | if layoutQuery == nil and decl.binders.len > 0 { |
|
| 5420 | + | throw emitError(self, node, ErrorKind::ConstantFunction( |
|
| 5421 | + | ConstantError::InvalidIntrinsic |
|
| 5422 | + | )); |
|
| 5423 | + | } |
|
| 5424 | + | } |
|
| 5425 | + | if decl.binders.len > 0 { |
|
| 5426 | + | if ast::hasAttribute(attrMask, ast::Attribute::Default) |
|
| 5314 | 5427 | or ast::hasAttribute(attrMask, ast::Attribute::Test) |
|
| 5315 | - | or ast::hasAttribute(attrMask, ast::Attribute::Extern) |
|
| 5316 | - | or ast::hasAttribute(attrMask, ast::Attribute::Intrinsic)) |
|
| 5317 | - | { |
|
| 5318 | - | throw emitError(self, decl.binders[0], ErrorKind::Bracket(BracketError::Generic)); |
|
| 5428 | + | or (ast::hasAttribute(attrMask, ast::Attribute::Extern) and layoutQuery == nil) |
|
| 5429 | + | { |
|
| 5430 | + | throw emitError(self, decl.binders[0], ErrorKind::Bracket(BracketError::Generic)); |
|
| 5431 | + | } |
|
| 5319 | 5432 | } |
|
| 5320 | 5433 | let mut fnType = FnType { |
|
| 5321 | 5434 | paramTypes: &[], |
|
| 5322 | 5435 | returnType: allocType(self, Type::Void), |
|
| 5323 | 5436 | throwList: &[], |
| 5333 | 5446 | exitFn(self); |
|
| 5334 | 5447 | throw e; |
|
| 5335 | 5448 | }; |
|
| 5336 | 5449 | exitFn(self); |
|
| 5337 | 5450 | ||
| 5338 | - | if ast::hasAttribute(attrMask, ast::Attribute::Constant) { |
|
| 5451 | + | if let query = layoutQuery { |
|
| 5452 | + | try validateLayoutIntrinsic(self, node, &decl, &fnType, attrMask); |
|
| 5453 | + | set self.nodeData.getMut(node.id).extra = NodeExtra::LayoutIntrinsic(query); |
|
| 5454 | + | } else if ast::hasAttribute(attrMask, ast::Attribute::Constant) { |
|
| 5339 | 5455 | if not ast::hasAttribute(attrMask, ast::Attribute::Intrinsic) { |
|
| 5340 | 5456 | throw emitError(self, node, ErrorKind::ConstantFunction( |
|
| 5341 | 5457 | ConstantError::RequiresIntrinsic |
|
| 5342 | 5458 | )); |
|
| 5343 | 5459 | } |
| 5444 | 5560 | } |
|
| 5445 | 5561 | if isExtern { |
|
| 5446 | 5562 | throw emitError(self, node, ErrorKind::FnUnexpectedBody); |
|
| 5447 | 5563 | } |
|
| 5448 | 5564 | try resolveExecutableBody(self, node, fnType, nil, decl.sig.params, body); |
|
| 5449 | - | } else if not isExtern { |
|
| 5565 | + | } else if not isExtern and not isIntrinsic { |
|
| 5450 | 5566 | let entry = module::get(self.moduleTable, self.currentMod); |
|
| 5451 | 5567 | let mut provider = false; |
|
| 5452 | 5568 | if let entry = entry { |
|
| 5453 | 5569 | set provider = entry.role == module::Role::Interface; |
|
| 5454 | 5570 | } |
| 8386 | 8502 | expected: Type::Unknown, |
|
| 8387 | 8503 | actual: calleeTy, |
|
| 8388 | 8504 | })); |
|
| 8389 | 8505 | }; |
|
| 8390 | 8506 | try checkCallArgs(self, node, call, info, ctx); |
|
| 8507 | + | if let case NodeExtra::FunctionApplication(index) = nodeData(self, call.callee).extra { |
|
| 8508 | + | let application = self.typeTables.functionApplications[index]; |
|
| 8509 | + | let decl = self.symbols[*application.template].node; |
|
| 8510 | + | if let case NodeExtra::LayoutIntrinsic(query) = nodeData(self, decl).extra { |
|
| 8511 | + | let target = application.substitution.args[0]; |
|
| 8512 | + | if not layoutDependsOnParameter(self, target) { |
|
| 8513 | + | let value = layoutQueryValue(self, target, query) else throw emitError( |
|
| 8514 | + | self, |
|
| 8515 | + | call.callee, |
|
| 8516 | + | ErrorKind::Bracket( |
|
| 8517 | + | BracketError::InvalidLayout |
|
| 8518 | + | ) |
|
| 8519 | + | ); |
|
| 8520 | + | setNodeConstValue(self, node, constInt(value as u64, 32, false, false)); |
|
| 8521 | + | } |
|
| 8522 | + | set self.nodeData.getMut(node.id).extra = NodeExtra::LayoutQuery { |
|
| 8523 | + | query, target: allocType(self, target) |
|
| 8524 | + | }; |
|
| 8525 | + | return setNodeType(self, node, Type::U32); |
|
| 8526 | + | } |
|
| 8527 | + | } |
|
| 8391 | 8528 | if let sym = |
|
| 8392 | 8529 | calleeSymbol; |
|
| 8393 | 8530 | ast::hasAttribute(self.symbols[*sym].attrs, ast::Attribute::Constant) |
|
| 8394 | 8531 | { |
|
| 8395 | 8532 | if self.constantInitializer == nil { |
| 9109 | 9246 | if items.len <> decl.binders.len { |
|
| 9110 | 9247 | throw emitError(self, node, ErrorKind::Bracket(BracketError::ArgumentCount( |
|
| 9111 | 9248 | CountMismatch { expected: decl.binders.len, actual: items.len } |
|
| 9112 | 9249 | ))); |
|
| 9113 | 9250 | } |
|
| 9251 | + | let declaration = self.symbols[*template].node; |
|
| 9252 | + | let intrinsic = extraFor(self, declaration); |
|
| 9114 | 9253 | let mut params: *mut [NominalId] = &mut []; |
|
| 9115 | 9254 | let mut args: *mut [Type] = &mut []; |
|
| 9116 | 9255 | for item, i in items { |
|
| 9117 | 9256 | let binder = decl.binders[i]; |
|
| 9118 | 9257 | let sym = symbolFor(self, binder) |
| 9124 | 9263 | "generic type argument exceeds the nesting limit" |
|
| 9125 | 9264 | )); |
|
| 9126 | 9265 | } |
|
| 9127 | 9266 | try ensureNoHeapValue(self, item, arg); |
|
| 9128 | 9267 | // Call contracts must account for regions carried by a replacement. |
|
| 9129 | - | if refs::containsRef(self, arg) { |
|
| 9268 | + | if let case NodeExtra::LayoutIntrinsic(query) = intrinsic { |
|
| 9269 | + | try ensureTypeResolved(self, arg, item); |
|
| 9270 | + | if not layoutDependsOnParameter(self, arg) and layoutQueryValue(self, arg, query) == nil |
|
| 9271 | + | { |
|
| 9272 | + | throw emitError(self, item, ErrorKind::Bracket(BracketError::InvalidLayout)); |
|
| 9273 | + | } |
|
| 9274 | + | } else if refs::containsRef(self, arg) { |
|
| 9130 | 9275 | throw emitError(self, item, ErrorKind::Bracket(BracketError::Generic)); |
|
| 9131 | 9276 | } |
|
| 9132 | 9277 | if isCopy(self, Type::Nominal(id)) and not isCopy(self, arg) { |
|
| 9133 | 9278 | throw emitError(self, item, ErrorKind::Bracket(BracketError::RequiresCopy)); |
|
| 9134 | 9279 | } |
lib/std/lang/resolver/printer.rad
+2 -0
| 537 | 537 | } |
|
| 538 | 538 | case super::BracketError::RequiresCopy => |
|
| 539 | 539 | io::print("type argument must satisfy the Copy bound"), |
|
| 540 | 540 | case super::BracketError::ExpectedType => |
|
| 541 | 541 | io::print("expected a type argument"), |
|
| 542 | + | case super::BracketError::InvalidLayout => |
|
| 543 | + | io::print("type layout exceeds the supported byte size"), |
|
| 542 | 544 | case super::BracketError::Generic => |
|
| 543 | 545 | io::print("generic declarations and types cannot be resolved"), |
|
| 544 | 546 | } |
|
| 545 | 547 | } |
|
| 546 | 548 | case super::ErrorKind::InvalidAlignmentValue(val) => { |
test/tests/generic.layout.overflow.reject.rad
added
+11 -0
| 1 | + | //! rejects: 6:36: type layout exceeds the supported byte size |
|
| 2 | + | ||
| 3 | + | @intrinsic export constant fn sizeOf[T]() -> u32; |
|
| 4 | + | ||
| 5 | + | fn oversized[T]() -> u32 { |
|
| 6 | + | return sizeOf[[T; 1073741824]](); |
|
| 7 | + | } |
|
| 8 | + | ||
| 9 | + | @default fn main() -> u32 { |
|
| 10 | + | return oversized[u64](); |
|
| 11 | + | } |
test/tests/generic.layout.rad
added
+39 -0
| 1 | + | //! returns: 0 |
|
| 2 | + | ||
| 3 | + | /// Return the size of a value of T. |
|
| 4 | + | @intrinsic export constant fn sizeOf[T]() -> u32; |
|
| 5 | + | ||
| 6 | + | /// Return the alignment of a value of T. |
|
| 7 | + | @intrinsic export constant fn alignOf[T]() -> u32; |
|
| 8 | + | ||
| 9 | + | /// Check padding and stride. |
|
| 10 | + | record Padded { |
|
| 11 | + | /// Byte before padding. |
|
| 12 | + | first: u8, |
|
| 13 | + | /// Aligned field. |
|
| 14 | + | second: u32, |
|
| 15 | + | } |
|
| 16 | + | ||
| 17 | + | /// Byte size of four words. |
|
| 18 | + | constant WORDS_BYTES: u32 = sizeOf[[u32; 4]](); |
|
| 19 | + | ||
| 20 | + | /// Callable layout query stored in constant data. |
|
| 21 | + | constant WORD_ALIGNMENT: fn() -> u32 = alignOf[u32]; |
|
| 22 | + | ||
| 23 | + | /// Return the stride after substituting the argument. |
|
| 24 | + | fn stride[T]() -> u32 { |
|
| 25 | + | return sizeOf[[T; 2]](); |
|
| 26 | + | } |
|
| 27 | + | ||
| 28 | + | /// Exercise direct queries, constants, and function values. |
|
| 29 | + | @default fn main() -> u32 { |
|
| 30 | + | assert WORDS_BYTES == 16; |
|
| 31 | + | assert WORD_ALIGNMENT() == 4; |
|
| 32 | + | assert sizeOf[Padded]() == 8; |
|
| 33 | + | assert alignOf[Padded]() == 4; |
|
| 34 | + | assert stride[Padded]() == 16; |
|
| 35 | + | let getAlign: fn() -> u32 = alignOf[u64]; |
|
| 36 | + | assert getAlign() == 8; |
|
| 37 | + | assert sizeOf[*opaque]() == 8; |
|
| 38 | + | return 0; |
|
| 39 | + | } |