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

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