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

Method alloc

chapter19/src/main/java/com/seaofnodes/simple/Parser.java:1130–1176  ·  view source on GitHub ↗

Parse an allocation

()

Source from the content-addressed store, hash-verified

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