Source file src/cmd/compile/internal/contraire/gen.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package contraire
     6  
     7  import "fmt"
     8  
     9  // newGenerator returns a new generator.
    10  func newGenerator(cfg *config) *generator {
    11  	return &generator{cfg: cfg}
    12  }
    13  
    14  // A generator generates Contraire ASTs.
    15  type generator struct {
    16  	cfg *config // immutable
    17  	s   *state  // mutable
    18  }
    19  
    20  type state struct {
    21  	constDecls []*constDecl
    22  	typeDecls  []*typeDecl
    23  }
    24  
    25  func (g *generator) genProg() *prog {
    26  	g.s = &state{}
    27  
    28  	// Generate all of the LHS information for the constant and type declarations.
    29  	// This way, names are available for both forward and backward references
    30  	// when generating the RHS information. This allows for cycles.
    31  	g.s.constDecls = g.declareConstLHSs()
    32  	g.s.typeDecls = g.declareTypeLHSs()
    33  
    34  	g.declareConstRHSs()
    35  	g.declareTypeRHSs()
    36  
    37  	// Package declarations into a program.
    38  	var decls []decl
    39  	for _, d := range g.s.constDecls {
    40  		decls = append(decls, d)
    41  	}
    42  	for _, d := range g.s.typeDecls {
    43  		decls = append(decls, d)
    44  	}
    45  	return &prog{decls: decls}
    46  }
    47  
    48  func (g *generator) declareConstLHSs() []*constDecl {
    49  	decls := make([]*constDecl, oneToN(g.cfg.ConstDeclMaxCount))
    50  	for i := range decls {
    51  		decls[i] = &constDecl{name: fmt.Sprintf("c%d", i)}
    52  	}
    53  	return decls
    54  }
    55  
    56  func (g *generator) declareConstRHSs() {
    57  	for _, d := range g.s.constDecls {
    58  		d.expr = g.genConstExpr()
    59  	}
    60  }
    61  
    62  func (g *generator) declareTypeLHSs() []*typeDecl {
    63  	decls := make([]*typeDecl, oneToN(g.cfg.TypeDeclMaxCount))
    64  	for i := range decls {
    65  		decls[i] = &typeDecl{name: fmt.Sprintf("T%d", i)}
    66  	}
    67  	return decls
    68  }
    69  
    70  func (g *generator) declareTypeRHSs() {
    71  	for _, d := range g.s.typeDecls {
    72  		d.expr = g.genTypeExpr()
    73  	}
    74  }
    75  
    76  func (g *generator) genConstExpr() constExpr {
    77  	return chooseCDF(
    78  		alt[constExpr]{g.cfg.PConstName, g.genConstName},
    79  		alt[constExpr]{g.cfg.PConstLtrl, g.genConstLtrl},
    80  	)
    81  }
    82  
    83  func (g *generator) genConstName() constExpr {
    84  	return &constName{name: chooseUniform(g.s.constDecls).name}
    85  }
    86  
    87  var constLtrlSingleton = &constLtrl{}
    88  
    89  func (g *generator) genConstLtrl() constExpr {
    90  	return constLtrlSingleton
    91  }
    92  
    93  func (g *generator) genTypeExpr() typeExpr {
    94  	return chooseCDF(
    95  		alt[typeExpr]{g.cfg.PTypeName, g.genTypeDeclName},
    96  		alt[typeExpr]{g.cfg.PTypeLtrl, g.genTypeLtrl},
    97  	)
    98  }
    99  
   100  func (g *generator) genTypeDeclName() typeExpr {
   101  	return &typeName{name: chooseUniform(g.s.typeDecls).name}
   102  }
   103  
   104  func (g *generator) genTypeLtrl() typeExpr {
   105  	return chooseCDF(
   106  		alt[typeExpr]{g.cfg.PTypeBasic, g.genTypeBasic},
   107  		alt[typeExpr]{g.cfg.PTypePointer, g.genTypePointer},
   108  	)
   109  }
   110  
   111  var typeBasicSingleton = &typeBasic{}
   112  
   113  func (g *generator) genTypeBasic() typeExpr {
   114  	return typeBasicSingleton
   115  }
   116  
   117  func (g *generator) genTypePointer() typeExpr {
   118  	return &typePointer{expr: g.genTypeExpr()}
   119  }
   120  

View as plain text