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 Parse an allocation
1147 */
1148 private Node alloc() {
1149 Type t = type();
1150 if( t==null ) throw error("Expected a type");
1151 // Parse ary[ length_expr ]
1152 if( match("[") ) {
1153 if( !t.makeZero().isa(t) )
1154 throw error("Cannot allocate a non-nullable, since arrays always zero/null fill");
1155 Node len = parseAsgn();
1156 if( !(len._type instanceof TypeInteger) )
1157 throw error("Cannot allocate an array with length "+len._type);
1158 require("]");
1159 TypeMemPtr tmp = typeAry(t);
1160 return newArray(tmp._obj,len);
1161 }
1162
1163 if( !(t instanceof TypeMemPtr tmp) )
1164 throw error("Cannot allocate a "+t.str());
1165
1166 // Parse new struct { default_initialization }
1167 StructNode s = INITS.get(tmp._obj._name);
1168 if( s==null ) throw error("Unknown struct type '" + tmp._obj._name + "'");
1169
1170 Field[] fs = s._ts._fields;
1171 // if the object is fully initialized, we can skip a block here.
1172 // Check for constructor block:
1173 boolean hasConstructor = match("{");
1174 Ary<Node> init=s._inputs; int idx=0;
1175 if( hasConstructor ) {
1176 idx = _scope.nIns();
1177 // Push a scope, and pre-assign all struct fields.
1178 _scope.push(ScopeNode.Kind.Block);
1179 Lexer loc = loc();
1180 for( int i=0; i<fs.length; i++ )
1181 _scope.define(fs[i]._fname, fs[i]._type, fs[i]._final, s.in(i)._type==Type.TOP ? con(Type.BOTTOM) : s.in(i), loc);
1182 // Parse the constructor body
1183 require(parseBlock(ScopeNode.Kind.Constructor),"}");
1184 init = _scope._inputs;
1185 }
1186 // Check that all fields are initialized
1187 for( int i=idx; i<init.size(); i++ )
1188 if( init.at(i)._type == Type.TOP || init.at(i)._type == Type.BOTTOM )
1189 throw error("'"+tmp._obj._name+"' is not fully initialized, field '" + fs[i-idx]._fname + "' needs to be set in a constructor");
1190 Node ptr = newStruct(tmp._obj, con(tmp._obj.offset(fs.length)), idx, init );
1191 if( hasConstructor )
1192 _scope.pop();
1193 return ptr;
1194 }
1195
1196
1197 /**

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