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

Method alloc

chapter21/src/main/java/com/seaofnodes/simple/Parser.java:1150–1196  ·  view source on GitHub ↗

Parse an allocation

()

Source from the content-addressed store, hash-verified

1148 throw errorSyntax("an identifier or expression");
1149 Var n = _scope.lookup(id);
1150 // If missing, assume a forward reference
1151 if( n==null ) {
1152 _scope.define(id, FRefNode.FREF_TYPE, true, XCTRL, loc());
1153 n = _scope.lookup(id);
1154 } else {
1155 // Lookup worked on an out-of-function value.
1156 if( _scope.outOfFunction(n) ) {
1157 // No closures, so this has to be a final constant (which
1158 // includes forward refs)
1159 Node def = _scope.in(n._idx);
1160 if( !(def instanceof FRefNode) && !(n._final && def._type.isConstant()) )
1161 throw error("Variable '"+n._name+"' is out of function scope and must be a final constant");
1162 }
1163 }
1164 return n;
1165 }
1166
1167 /**
1168 Parse an allocation
1169 */
1170 private Node alloc() {
1171 Type t = type();
1172 if( t==null ) throw error("Expected a type");
1173 // Parse ary[ length_expr ]
1174 if( match("[") ) {
1175 if( !t.makeZero().isa(t) )
1176 throw error("Cannot allocate a non-nullable, since arrays always zero/null fill");
1177 Node len = parseAsgn();
1178 if( !(len._type instanceof TypeInteger) )
1179 throw error("Cannot allocate an array with length "+len._type);
1180 require("]");
1181 TypeMemPtr tmp = typeAry(t);
1182 return newArray(tmp._obj,len);
1183 }
1184
1185 if( !(t instanceof TypeMemPtr tmp) )
1186 throw error("Cannot allocate a "+t.str());
1187
1188 // Parse new struct { default_initialization }
1189 StructNode s = INITS.get(tmp._obj._name);
1190 if( s==null ) throw error("Unknown struct type '" + tmp._obj._name + "'");
1191
1192 Field[] fs = s._ts._fields;
1193 // if the object is fully initialized, we can skip a block here.
1194 // Check for constructor block:
1195 boolean hasConstructor = match("{");
1196 Ary<Node> init=s._inputs; int idx=0;
1197 if( hasConstructor ) {
1198 idx = _scope.nIns();
1199 // Push a scope, and pre-assign all struct 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