Parse an allocation
()
| 767 | if( !(t instanceof TypeMemPtr tmp) ) |
| 768 | throw error("Cannot allocate a "+t.str()); |
| 769 | |
| 770 | // Parse new struct { default_initialization } |
| 771 | StructNode s = INITS.get(tmp._obj._name); |
| 772 | if( s==null ) throw error("Unknown struct type '" + tmp._obj._name + "'"); |
| 773 | |
| 774 | Field[] fs = s._ts._fields; |
| 775 | // if the object is fully initialized, we can skip a block here. |
| 776 | // Check for constructor block: |
| 777 | boolean hasConstructor = match("{"); |
| 778 | Ary<Node> init=s._inputs; int idx=0; |
| 779 | if( hasConstructor ) { |
| 780 | idx = _scope.nIns(); |
| 781 | // Push a scope, and pre-assign all struct fields. |
| 782 | _scope.push(); |
| 783 | for( int i=0; i<fs.length; i++ ) |
| 784 | _scope.define(fs[i]._fname, fs[i]._type, fs[i]._final, s.in(i)); |
| 785 | // Parse the constructor body |
| 786 | parseBlock(); |
| 787 | require("}"); |
| 788 | init = _scope._inputs; |
| 789 | } |
| 790 | // Check that all fields are initialized |
| 791 | for( int i=idx; i<init.size(); i++ ) |
| 792 | if( init.get(i)._type == Type.TOP ) |
| 793 | throw error("'"+tmp._obj._name+"' is not fully initialized, field '" + fs[i-idx]._fname + "' needs to be set in a constructor"); |
| 794 | Node ptr = newStruct(tmp._obj, con(tmp._obj.offset(fs.length)), idx, init ); |
| 795 | if( hasConstructor ) |
| 796 | _scope.pop(); |
| 797 | return ptr; |
| 798 | } |
| 799 | |
| 800 | |
| 801 | /** |
| 802 | * Return a NewNode initialized memory. |
| 803 | * @param obj is the declared type, with GLB fields |
| 804 | * @param init is a collection of initialized fields |
| 805 | */ |
| 806 | private Node newStruct( TypeStruct obj, Node size, int idx, Ary<Node> init ) { |
| 807 | Field[] fs = obj._fields; |
| 808 | if( fs==null ) |
| 809 | throw error("Unknown struct type '" + obj._name + "'"); |
| 810 | Node prior = mem().keep(); |
| 811 | MemMergeNode input = new MemMergeNode(null); |
| 812 | for( Field f : fs ) input.alias(f._alias,prior); |
| 813 | // Constructor values follow control, memory, and allocation size. |
| 814 | Node[] ns = new Node[3+fs.length]; |
| 815 | ns[0] = ctrl(); |
| 816 | ns[1] = input.peephole(); |
no test coverage detected