summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-12-04 18:21:09 -0800
committerCliff Click <cliffc@acm.org>2026-12-04 18:21:09 -0800
commit236e343e995a18129ca6c666ff0e7bfaa500d5e4 (patch)
tree91b61852a4c8a8535ebe37bf9673af8bc53d79ac
parenta71fcc14da7d80f54cc3f956740163ae41f7c7bf (diff)
downloadSimple-236e343e995a18129ca6c666ff0e7bfaa500d5e4.tar.gz
Simple-236e343e995a18129ca6c666ff0e7bfaa500d5e4.zip
Resolve required nested types through their owning compilation unit
Search qualified-name prefixes when the nested type has no separate object symbol, then validate the requested type after queued declarations close. Cover source and object-file owners and reject missing nested declarations. Full make -k -j8 tests passes.
-rw-r--r--chapter33/src/main/java/com/seaofnodes/simple/codegen/ParseAll.java23
-rw-r--r--chapter33/src/test/java/com/seaofnodes/simple/SeparateCompilationTest.java41
2 files changed, 61 insertions, 3 deletions
diff --git a/chapter33/src/main/java/com/seaofnodes/simple/codegen/ParseAll.java b/chapter33/src/main/java/com/seaofnodes/simple/codegen/ParseAll.java
index 882a0f8a..79655b40 100644
--- a/chapter33/src/main/java/com/seaofnodes/simple/codegen/ParseAll.java
+++ b/chapter33/src/main/java/com/seaofnodes/simple/codegen/ParseAll.java
@@ -1,6 +1,7 @@
package com.seaofnodes.simple.codegen;
import com.seaofnodes.simple.parser.Parser;
+import com.seaofnodes.simple.parser.Lexer;
import com.seaofnodes.simple.node.*;
import com.seaofnodes.simple.print.*;
import com.seaofnodes.simple.type.*;
@@ -28,6 +29,7 @@ public abstract class ParseAll {
private static final Ary<CompUnit> WORK = new Ary<>(CompUnit.class);
private static final Ary<CompUnit> NEEDS_LOAD = new Ary<>(CompUnit.class);
+ private static final HashMap<String,Lexer> REQUIRED_TYPES = new HashMap<>();
// Return a compilation unit (cached or new). If new, it does not necessarily
// need to be compiled.
@@ -88,6 +90,7 @@ public abstract class ParseAll {
private static void parseAll(CodeGen code, CompUnit cunit) {
WORK.clear(); // Cleanup from prior tests
NEEDS_LOAD.clear();
+ REQUIRED_TYPES.clear();
// Prime the pump with the first CompUnit
enqueue(cunit);
// Work through all unparsed compilation units and parse
@@ -115,6 +118,9 @@ public abstract class ParseAll {
TypeStruct[] tss = Type.closeOver(ary.asAry(),Parser.TYPES);
for( TypeStruct ts : tss )
Parser.TYPES.put(ts._name,ts);
+ for( var required : REQUIRED_TYPES.entrySet() )
+ if( !(Parser.TYPES.get(required.getKey()) instanceof TypeStruct ts) || ts._fref )
+ throw Parser.error("Unknown type '"+required.getKey()+"'",required.getValue());
for( CompUnit cu : code._compunits.values() )
if( cu._ext==null )
cu._clz = (TypeStruct)Parser.TYPES.get(TypeStruct.addClzPrefix(cu._cname));
@@ -156,9 +162,11 @@ public abstract class ParseAll {
if( Parser.TYPES.get(typeName) instanceof TypeStruct ts && !ts._fref ) continue;
try {
CompUnit xcunit = findRequiredType(code,cunit,typeName);
- if( xcunit != null )
+ if( xcunit != null ) {
cunit.addDep(xcunit);
- else
+ if( !typeName.equals(xcunit._cname) || (xcunit._obj!=null && xcunit._src==null) )
+ REQUIRED_TYPES.putIfAbsent(typeName,required.getValue());
+ } else
throw Parser.error("Unknown type '"+typeName+"'",required.getValue());
} catch( IOException ioe ) {
throw new RuntimeException(ioe);
@@ -175,7 +183,16 @@ public abstract class ParseAll {
if( xcunit != null )
return xcunit;
}
- return findCUnitExternalSimple(code,typeName);
+ // A nested struct is declared inside its owning object; it need not
+ // have its own compilation-unit symbol. Validate the complete name
+ // after the owner's declarations have been loaded and closed.
+ for( String name=typeName; ; ) {
+ CompUnit cu = findCUnitExternalSimple(code,name);
+ if( cu!=null ) return cu;
+ int dot = name.lastIndexOf('.');
+ if( dot<0 ) return null;
+ name = name.substring(0,dot);
+ }
}
// Load the public symbols for this CompUnit, and add dependent CompUnits to the NEEDS_LOAD list.
diff --git a/chapter33/src/test/java/com/seaofnodes/simple/SeparateCompilationTest.java b/chapter33/src/test/java/com/seaofnodes/simple/SeparateCompilationTest.java
index 72e26a2e..db8e696c 100644
--- a/chapter33/src/test/java/com/seaofnodes/simple/SeparateCompilationTest.java
+++ b/chapter33/src/test/java/com/seaofnodes/simple/SeparateCompilationTest.java
@@ -10,6 +10,47 @@ import org.junit.Test;
import static org.junit.Assert.*;
public class SeparateCompilationTest {
+ @Test public void testImportedNestedTypeOnlyReference() throws Exception {
+ Path lib=Files.createTempDirectory(Files.createDirectories(Path.of("build/objs/separate")),"nested-type-");
+ new CodeGen(null,lib.toString(),null,"Library",
+ "struct Box { i64 x=42; };",123,true,TypeInteger.BOT)
+ .driver(TestC.CPU_PORT,TestC.CALL_CONVENTION,false,false);
+ var paths=new Ary<>(new String[]{lib.toString()});
+ // No expression mentions Library: only this parameter requests its type table.
+ CodeGen code=new CodeGen(null,lib.toString(),paths,"Client",
+ "val read_noInline={ Library.Box b -> b.x; }; return 0;",123,true,TypeInteger.BOT)
+ .driver(CodeGen.Phase.TypeCheck);
+ assertEquals("0",Eval2.eval(code,0));
+ for( String name : new String[]{"Library.Missing"} ) {
+ try {
+ new CodeGen(null,lib.toString(),paths,"Client",
+ "val read_noInline={ "+name+" b -> b.x; }; return 0;",123,true,TypeInteger.BOT)
+ .driver(CodeGen.Phase.TypeCheck);
+ fail("Accepted nonexistent nested type "+name);
+ } catch( com.seaofnodes.simple.parser.Parser.ParseException expected ) {
+ assertTrue(expected.getMessage(),expected.getMessage().contains("Unknown type"));
+ }
+ }
+ }
+
+ @Test public void testNestedTypeInAnotherSourceUnit() throws Exception {
+ Path root=Files.createTempDirectory(Files.createDirectories(Path.of("build/objs/separate")),"nested-source-");
+ Files.createDirectories(root.resolve("Main"));
+ Files.writeString(root.resolve("Main/Library.smp"),"struct Box { i64 x=42; };");
+ CodeGen code=new CodeGen(root.toString(),root.resolve("obj").toString(),null,"Main",
+ "val read_noInline={ Library.Box b -> b.x; }; return 0;",123,true,TypeInteger.BOT)
+ .driver(CodeGen.Phase.TypeCheck);
+ assertEquals("0",Eval2.eval(code,0));
+ try {
+ new CodeGen(root.toString(),root.resolve("obj").toString(),null,"Main",
+ "val read_noInline={ Library.Missing b -> b.x; }; return 0;",123,true,TypeInteger.BOT)
+ .driver(CodeGen.Phase.TypeCheck);
+ fail("The owning file exists, but it does not define Missing");
+ } catch( com.seaofnodes.simple.parser.Parser.ParseException expected ) {
+ assertTrue(expected.getMessage(),expected.getMessage().contains("Unknown type"));
+ }
+ }
+
@Test public void testSparseWideMemoryImport() throws Exception {
Path build=Path.of("build/objs/separate");
Files.createDirectories(build);