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

Method alloc

chapter18/src/main/java/com/seaofnodes/simple/Parser.java:1148–1194  ·  view source on GitHub ↗

Parse an allocation

()

Source from the content-addressed store, hash-verified

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

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