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

Method alloc

chapter20/src/main/java/com/seaofnodes/simple/Parser.java:1132–1178  ·  view source on GitHub ↗

Parse an allocation

()

Source from the content-addressed store, hash-verified

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