@@ -616,7 +616,7 @@ int getIndirectTypeIndex(DwarfTypeProperties typeProperties) {
616616 }
617617
618618 void setLayoutIndex (ClassEntry classEntry , int idx ) {
619- assert idx >= 0 ;
619+ assert idx >= 0 || idx == - 1 ;
620620 DwarfClassProperties classProperties = lookupClassProperties (classEntry );
621621 assert classProperties .getTypeEntry () == classEntry ;
622622 assert classProperties .layoutIndex == -1 || classProperties .layoutIndex == idx ;
@@ -632,7 +632,19 @@ int getLayoutIndex(ClassEntry classEntry) {
632632 }
633633
634634 void setIndirectLayoutIndex (ClassEntry classEntry , int idx ) {
635- assert idx >= 0 ;
635+ // The layout index of a POINTER type is set to the type index of its referent.
636+ // If the pointer type is generated before its referent that means it can be set
637+ // with value -1 (unset) on the first sizing pass. The indirect layout will
638+ // be reset to a positive offset on the second pass before it is used to write
639+ // the referent of the pointer type. Hence the condition in the following assert.
640+ assert idx >= 0 || idx == -1 ;
641+ // Note however, that this possibility needs to be finessed when writing
642+ // a foreign struct ADDRESS field of POINTER type (i.e. an embedded field).
643+ // If the struct is generated before the POINTER type then the layout index will
644+ // still be -1 during the second write pass when the field type needs to be
645+ // written. This possibility is handled by typing the field using the typeIdx
646+ // of the referent. the latter is guaranteed to have been set during the first pass.
647+
636648 DwarfClassProperties classProperties = lookupClassProperties (classEntry );
637649 assert classProperties .getTypeEntry () == classEntry ;
638650 assert classProperties .indirectLayoutIndex == -1 || classProperties .indirectLayoutIndex == idx ;
0 commit comments