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

Method alloc

chapter22/src/main/java/com/seaofnodes/simple/Parser.java:1214–1260  ·  view source on GitHub ↗

Parse an allocation

()

Source from the content-addressed store, hash-verified

1212
1213
1214 // Expect an ID here. If not found, assume a forward reference function
1215 Var requireLookupId() {
1216 // Try in the normal variable namespace
1217 String id = _lexer.matchId();
1218 if( id == null || KEYWORDS.contains(id) )
1219 throw errorSyntax("an identifier or expression");
1220 Var n = _scope.lookup(id);
1221 // If missing, assume a forward reference
1222 if( n==null ) {
1223 _scope.define(id, FRefNode.FREF_TYPE, true, XCTRL, loc());
1224 n = _scope.lookup(id);
1225 } else {
1226 // Lookup worked on an out-of-function value.
1227 if( _scope.outOfFunction(n) ) {
1228 // No closures, so this has to be a final constant (which
1229 // includes forward refs)
1230 Node def = _scope.in(n._idx);
1231 if( !(def instanceof FRefNode) && !(n._final && def._type.isConstant()) )
1232 throw error("Variable '"+n._name+"' is out of function scope and must be a final constant");
1233 }
1234 }
1235 return n;
1236 }
1237
1238 /**
1239 Parse an allocation
1240 */
1241 private Node alloc() {
1242 Type t = type();
1243 if( t==null ) throw error("Expected a type");
1244 // Parse ary[ length_expr ]
1245 if( match("[") ) {
1246 if( !t.makeZero().isa(t) )
1247 throw error("Cannot allocate a non-nullable, since arrays always zero/null fill");
1248 Node len = parseAsgn();
1249 if( !(len._type instanceof TypeInteger) )
1250 throw error("Cannot allocate an array with length "+len._type);
1251 require("]");
1252 TypeMemPtr tmp = typeAry(t);
1253 return newArray(tmp._obj,len);
1254 }
1255
1256 if( !(t instanceof TypeMemPtr tmp) )
1257 throw error("Cannot allocate a "+t.str());
1258
1259 // Parse new struct { default_initialization }
1260 StructNode s = INITS.get(tmp._obj._name);
1261 if( s==null ) throw error("Unknown struct type '" + tmp._obj._name + "'");
1262
1263 Field[] fs = s._ts._fields;

Callers 1

parsePrimaryMethod · 0.95

Calls 15

typeMethod · 0.95
errorMethod · 0.95
matchMethod · 0.95
makeZeroMethod · 0.95
parseAsgnMethod · 0.95
requireMethod · 0.95
typeAryMethod · 0.95
newArrayMethod · 0.95
strMethod · 0.95
locMethod · 0.95
conMethod · 0.95
parseBlockMethod · 0.95

Tested by

no test coverage detected