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

Method type

chapter22/src/main/java/com/seaofnodes/simple/Parser.java:835–896  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

833 // Parse and return a type or null. Valid types always are followed by an
834 // 'id' which the caller must parse. This lets us distinguish forward ref
835 // types (which ARE valid here) from local vars in an (optional) forward
836 // ref type position.
837
838 // t = int|i8|i16|i32|i64|u8|u16|u32|u64|byte|bool | flt|f32|f64 | val | var | struct[?]
839 private Type type() {
840 int old1 = pos();
841 boolean writable = match("!");
842 boolean readonly = !writable && match("~");
843 // Only type with a leading `{` is a function pointer...
844 if( peek('{') ) return typeFunPtr();
845
846 // Otherwise you get a type name
847 String tname = _lexer.matchId();
848 if( tname==null ) return posT(old1);
849
850 // Convert the type name to a type.
851 Type t0 = TYPES.get(tname);
852 // No new types as keywords
853 if( t0 == null && KEYWORDS.contains(tname) )
854 return posT(old1);
855 if( t0 == Type.BOTTOM || t0 == Type.TOP ) return t0; // var/val type inference
856
857 // Check for subtype.
858 while( true ) {
859 int old2 = pos();
860 if( !match(".") ) break;
861 String sname = _lexer.matchId();
862 if( sname==null ) { pos(old2); break; }
863 String tsname = tname+"."+sname;
864 t0 = TYPES.get(tsname);
865 if( t0==null ) { pos(old2); t0 = TYPES.get(tname); break; }
866 tname = tsname;
867 }
868
869 // Still no type found? Assume forward reference
870 Type t1 = t0 == null ? TypeMemPtr.make(TypeStruct.makeFRef(tname)) :t0; // Null: assume a forward ref type
871 // A qualifier before the name applies to the struct reference, not
872 // to an enclosing array. Arrays have their own [] / [~] qualifier.
873 if( t1 instanceof TypeMemPtr tmp ) {
874 if( readonly || !writable ) t1 = tmp.makeRO();
875 } else if( writable || readonly )
876 return posT(old1);
877
878 // Nest arrays and '?' as needed
879 Type t2 = t1;
880 while( true ) {
881 if( match("?") ) {
882 if( !(t2 instanceof TypeMemPtr tmp) )
883 throw error("Type "+t0+" cannot be null");
884 if( tmp.nullable() ) throw error("Type "+t2+" already allows null");
885 t2 = tmp.makeNullable();
886 } else if( match("[~]") ) {
887 TypeMemPtr ary = typeAry(t2);
888 t2 = ary.makeFrom(ary._obj.makeRO()).makeRO();
889 } else if( match("[]") ) {
890 t2 = typeAry(t2);
891 } else
892 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
makeFRefMethod · 0.95
errorMethod · 0.95
makeNullableMethod · 0.95
typeAryMethod · 0.95
makeFromMethod · 0.95
matchIdMethod · 0.45

Tested by

no test coverage detected