MCPcopy Create free account
hub / github.com/SeaOfNodes/Simple / type

Method type

chapter23/src/main/java/com/seaofnodes/simple/Parser.java:898–958  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

896
897 // Parse and return a type or null. Valid types always are followed by an
898 // 'id' which the caller must parse. This lets us distinguish forward ref
899 // types (which ARE valid here) from local vars in an (optional) forward
900 // ref type position.
901
902 // t = int|i8|i16|i32|i64|u8|u16|u32|u64|byte|bool | flt|f32|f64 | val | var | struct[?]
903 private Type type() {
904 int old1 = pos();
905 boolean writable = match("!");
906 boolean readonly = !writable && match("~");
907 // Only type with a leading `{` is a function pointer...
908 if( peek('{') ) return typeFunPtr();
909
910 // Otherwise you get a type name
911 String tname = _lexer.matchId();
912 if( tname==null ) return posT(old1);
913
914 // Convert the type name to a type.
915 Type t0 = TYPES.get(tname);
916 // No new types as keywords
917 if( t0 == null && KEYWORDS.contains(tname) )
918 return posT(old1);
919 if( t0 == Type.BOTTOM || t0 == Type.TOP ) return t0; // var/val type inference
920
921 // Check for subtype.
922 while( true ) {
923 int old2 = pos();
924 if( !match(".") ) break;
925 String sname = _lexer.matchId();
926 if( sname==null ) { pos(old2); break; }
927 String tsname = tname+"."+sname;
928 t0 = TYPES.get(tsname);
929 if( t0==null ) { pos(old2); t0 = TYPES.get(tname); break; }
930 tname = tsname;
931 }
932
933 // Still no type found? Assume forward reference
934 Type t1 = t0 == null ? TypeMemPtr.make(TypeStruct.open(tname)) :t0; // Null: assume a forward ref type
935 // A qualifier before the name applies to the struct reference, not
936 // to an enclosing array. Arrays have their own [] / [~] qualifier.
937 if( t1 instanceof TypeMemPtr tmp ) {
938 if( readonly || !writable ) t1 = tmp.makeRO();
939 } else if( writable || readonly )
940 return posT(old1);
941
942 // Nest arrays and '?' as needed
943 Type t2 = t1;
944 while( true ) {
945 if( match("?") ) {
946 if( !(t2 instanceof TypeMemPtr tmp) )
947 throw error("Type "+t0+" cannot be null");
948 if( tmp.nullable() ) throw error("Type "+t2+" already allows null");
949 t2 = tmp.makeNullable();
950 } else if( match("[~]") ) {
951 t2 = typeAry(t2,true).makeRO();
952 } else if( match("[]") ) {
953 t2 = typeAry(t2,false);
954 } else
955 break;

Callers 5

typeFunPtrMethod · 0.95
parsePrimaryMethod · 0.95
allocMethod · 0.95
funcMethod · 0.95

Calls 15

posMethod · 0.95
peekMethod · 0.95
typeFunPtrMethod · 0.95
posTMethod · 0.95
matchMethod · 0.95
makeMethod · 0.95
openMethod · 0.95
errorMethod · 0.95
typeAryMethod · 0.95
matchIdMethod · 0.45
getMethod · 0.45
containsMethod · 0.45

Tested by

no test coverage detected