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

Method alloc

chapter17/src/main/java/com/seaofnodes/simple/Parser.java:951–995  ·  view source on GitHub ↗

Parse an allocation

()

Source from the content-addressed store, hash-verified

949 Parse an allocation
950 */
951 private Node alloc() {
952 Type t = type();
953 if( t==null ) throw error("Expected a type");
954 // Parse ary[ length_expr ]
955 if( match("[") ) {
956 Node len = parseAsgn();
957 if( !(len._type instanceof TypeInteger) )
958 throw error("Cannot allocate an array with length "+len._type);
959 require("]");
960 TypeMemPtr tmp = typeAry(t);
961 return newArray(tmp._obj,len);
962 }
963
964 if( !(t instanceof TypeMemPtr tmp) )
965 throw error("Cannot allocate a "+t.str());
966
967 // Parse new struct { default_initialization }
968 StructNode s = INITS.get(tmp._obj._name);
969 if( s==null ) throw error("Unknown struct type '" + tmp._obj._name + "'");
970
971 Field[] fs = s._ts._fields;
972 // if the object is fully initialized, we can skip a block here.
973 // Check for constructor block:
974 boolean hasConstructor = match("{");
975 Ary<Node> init=s._inputs; int idx=0;
976 if( hasConstructor ) {
977 idx = _scope.nIns();
978 // Push a scope, and pre-assign all struct fields.
979 _scope.push();
980 for( int i=0; i<fs.length; i++ )
981 _scope.define(fs[i]._fname, fs[i]._type, fs[i]._final, s.in(i)._type==Type.TOP ? con(Type.BOTTOM) : s.in(i));
982 // Parse the constructor body
983 parseBlock(true);
984 require("}");
985 init = _scope._inputs;
986 }
987 // Check that all fields are initialized
988 for( int i=idx; i<init.size(); i++ )
989 if( /*init.at(i)._type == Type.TOP ||*/ init.at(i)._type == Type.BOTTOM )
990 throw error("'"+tmp._obj._name+"' is not fully initialized, field '" + fs[i-idx]._fname + "' needs to be set in a constructor");
991 Node ptr = newStruct(tmp._obj, con(tmp._obj.offset(fs.length)), idx, init );
992 if( hasConstructor )
993 _scope.pop();
994 return ptr;
995 }
996
997
998 /**

Callers 1

parsePrimaryMethod · 0.95

Calls 15

typeMethod · 0.95
errorMethod · 0.95
matchMethod · 0.95
parseAsgnMethod · 0.95
requireMethod · 0.95
typeAryMethod · 0.95
newArrayMethod · 0.95
strMethod · 0.95
conMethod · 0.95
parseBlockMethod · 0.95
newStructMethod · 0.95
getMethod · 0.45

Tested by

no test coverage detected