modernc.org/cc@v1.0.1/ast2.go (about)

     1  // Copyright 2016 The CC 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 cc // import "modernc.org/cc"
     6  
     7  import (
     8  	"fmt"
     9  	"go/token"
    10  	"strconv"
    11  	"strings"
    12  
    13  	"modernc.org/golex/lex"
    14  	"modernc.org/mathutil"
    15  	"modernc.org/xc"
    16  )
    17  
    18  type operand interface {
    19  	eval(*lexer) (value interface{}, typ Type)
    20  	Node
    21  }
    22  
    23  // Node represents an AST node.
    24  type Node interface {
    25  	Pos() token.Pos
    26  }
    27  
    28  // ---------------------------------------------------------- CompoundStatement
    29  
    30  // Scope returns n's scope.
    31  func (n *CompoundStatement) Scope() *Bindings { return n.scope }
    32  
    33  // ---------------------------------------------------------------- Declaration
    34  
    35  // Declarator returns a synthetic Declarator when n.InitDeclaratorListOpt is
    36  // nil.
    37  func (n *Declaration) Declarator() *Declarator { return n.declarator }
    38  
    39  // ------------------------------------------------------ DeclarationSpecifiers
    40  
    41  // IsInline implements specifier.
    42  func (n *DeclarationSpecifiers) IsInline() bool {
    43  	return n.attr&saInline != 0
    44  }
    45  
    46  // IsTypedef implements specifier.
    47  func (n *DeclarationSpecifiers) IsTypedef() bool {
    48  	return n.attr&saTypedef != 0
    49  }
    50  
    51  // IsExtern implements specifier.
    52  func (n *DeclarationSpecifiers) IsExtern() bool {
    53  	return n.attr&saExtern != 0
    54  }
    55  
    56  // IsStatic implements specifier.
    57  func (n *DeclarationSpecifiers) IsStatic() bool {
    58  	return n.attr&saStatic != 0
    59  }
    60  
    61  // IsAuto implements specifier.
    62  func (n *DeclarationSpecifiers) IsAuto() bool {
    63  	return n.attr&saAuto != 0
    64  }
    65  
    66  // IsRegister implements specifier.
    67  func (n *DeclarationSpecifiers) IsRegister() bool {
    68  	return n.attr&saRegister != 0
    69  }
    70  
    71  // IsConst returns whether n includes the 'const' type qualifier.
    72  func (n *DeclarationSpecifiers) IsConst() bool {
    73  	return n.attr&saConst != 0
    74  }
    75  
    76  // IsRestrict implements specifier.
    77  func (n *DeclarationSpecifiers) IsRestrict() bool {
    78  	return n.attr&saRestrict != 0
    79  }
    80  
    81  // IsVolatile implements specifier.
    82  func (n *DeclarationSpecifiers) IsVolatile() bool {
    83  	return n.attr&saVolatile != 0
    84  }
    85  
    86  // kind implements specifier.
    87  func (n *DeclarationSpecifiers) kind() Kind { return tsValid[n.typeSpecifiers()] }
    88  
    89  // typeSpecifiers implements specifier.
    90  func (n *DeclarationSpecifiers) typeSpecifiers() int {
    91  	return n.typeSpecifier
    92  }
    93  
    94  // firstTypeSpecifier implements specifier.
    95  func (n *DeclarationSpecifiers) firstTypeSpecifier() *TypeSpecifier {
    96  	for n.Case != 1 { // TypeSpecifier DeclarationSpecifiersOpt
    97  		o := n.DeclarationSpecifiersOpt
    98  		if o == nil {
    99  			return nil
   100  		}
   101  
   102  		n = o.DeclarationSpecifiers
   103  	}
   104  	return n.TypeSpecifier
   105  }
   106  
   107  // attrs implements specifier.
   108  func (n *DeclarationSpecifiers) attrs() int { return n.attr }
   109  
   110  // member implements specifier.
   111  func (n *DeclarationSpecifiers) member(nm int) (*Member, error) {
   112  	return n.firstTypeSpecifier().member(nm)
   113  }
   114  
   115  // str implements specifier.
   116  func (n *DeclarationSpecifiers) str() string {
   117  	return specifierString(n)
   118  }
   119  
   120  // TypedefName implements Specifier.
   121  func (n *DeclarationSpecifiers) TypedefName() int {
   122  	if n.kind() == TypedefName {
   123  		return n.firstTypeSpecifier().Token.Val
   124  	}
   125  	return 0
   126  }
   127  
   128  // ----------------------------------------------------------------- Declarator
   129  
   130  // Identifier returns the ID of the name declared by n and the scope the name
   131  // is declared in.
   132  func (n *Declarator) Identifier() (int, *Bindings) {
   133  	dd := n.DirectDeclarator.bottom()
   134  	if dd != nil {
   135  		return dd.Token.Val, dd.DeclarationScope()
   136  	}
   137  
   138  	return 0, nil
   139  }
   140  
   141  // RawSpecifier returns the raw Specifier associated with n before expanding
   142  // typedefs. The effective Specifier is accessible via the Type field of n.
   143  func (n *Declarator) RawSpecifier() Specifier { return n.specifier }
   144  
   145  func (n *Declarator) clone() *Declarator {
   146  	m := *n
   147  	return &m
   148  }
   149  
   150  func (n *Declarator) stars() int { return n.PointerOpt.stars() }
   151  
   152  func (n *Declarator) isCompatible(m *Declarator) (r bool) {
   153  	return n == m || n.Type.(*ctype).isCompatible(m.Type.(*ctype))
   154  }
   155  
   156  func (n *Declarator) unsigednEnum(lx *lexer, s Specifier) bool {
   157  	switch x := s.(type) {
   158  	case *DeclarationSpecifiers:
   159  		o := x.DeclarationSpecifiersOpt
   160  		if o == nil {
   161  			return false
   162  		}
   163  
   164  		switch ds := o.DeclarationSpecifiers; ds.Case {
   165  		case 1: // TypeSpecifier DeclarationSpecifiersOpt          // Case 1
   166  			switch ts := ds.TypeSpecifier; ts.Case {
   167  			case 12: // EnumSpecifier                // Case 12
   168  				return ts.EnumSpecifier.isUnsigned(lx)
   169  			}
   170  		}
   171  	case *SpecifierQualifierList:
   172  		ts := x.TypeSpecifier
   173  		if ts == nil {
   174  			return false
   175  		}
   176  
   177  		switch ts.Case {
   178  		case 12: // EnumSpecifier                // Case 12
   179  			return ts.EnumSpecifier.isUnsigned(lx)
   180  		}
   181  	}
   182  	return false
   183  }
   184  
   185  func (n *Declarator) setFull(lx *lexer) Type {
   186  	d := n
   187  	var dds0, dds []*DirectDeclarator
   188  	for dd := d.DirectDeclarator; dd != nil; dd = dd.directDeclarator() {
   189  		dds = append(dds, dd)
   190  	}
   191  	for i, j := 0, len(dds)-1; i < j; i, j = i+1, j-1 { // reverse
   192  		dds[i], dds[j] = dds[j], dds[i]
   193  	}
   194  
   195  	resultAttr := 0
   196  	mask := 0
   197  	if d.specifier != nil {
   198  		if d.specifier.IsTypedef() {
   199  			dds0 = append([]*DirectDeclarator(nil), dds...)
   200  		}
   201  		if d.specifier.typeSpecifiers() == 0 && lx.tweaks.enableImplicitIntType {
   202  			switch x := d.specifier.(type) {
   203  			case *DeclarationSpecifiers:
   204  				x.typeSpecifier = tsEncode(tsInt)
   205  			default:
   206  				panic(fmt.Errorf("%s: TODO %T", position(n.Pos()), x))
   207  			}
   208  		}
   209  	}
   210  loop0:
   211  	for d.specifier != nil {
   212  		switch d.specifier.kind() {
   213  		case TypedefName:
   214  			resultAttr |= d.specifier.attrs()
   215  			ts := d.specifier.firstTypeSpecifier()
   216  			dd := ts.scope.Lookup(NSIdentifiers, ts.Token.Val).Node.(*DirectDeclarator) // eg. typedef T dd, (*dd), dd(int), ...
   217  			if dd.Case != 0 {                                                           // IDENTIFIER
   218  				panic("internal error")
   219  			}
   220  
   221  			nd := dd.top().declarator
   222  			mask = saTypedef // nd.specifier.IsTypedef() == true
   223  			dds2 := nd.Type.(*ctype).dds0
   224  			d2 := d.clone()
   225  			d2.specifier = nil
   226  			dd2 := &DirectDeclarator{
   227  				Case:       1, //  '(' Declarator ')'
   228  				Declarator: d2,
   229  			}
   230  			dds = append(dds, dd2)
   231  			dds = append(dds, dds2[1:]...)
   232  			d = nd
   233  		case typeof:
   234  			resultAttr |= d.specifier.attrs()
   235  			ts := d.specifier.firstTypeSpecifier()
   236  			nd := ts.Type.Declarator()
   237  			dds2 := ts.Type.(*ctype).dds0
   238  			d2 := d.clone()
   239  			d2.specifier = nil
   240  			dd2 := &DirectDeclarator{
   241  				Case:       1, //  '(' Declarator ')'
   242  				Declarator: d2,
   243  			}
   244  			dds = append(dds, dd2)
   245  			dds = append(dds, dds2...)
   246  			d = nd
   247  		default:
   248  			break loop0
   249  		}
   250  	}
   251  
   252  	// Inner ((...)) -> (...)
   253  	for {
   254  		changed := false
   255  		w := 0
   256  		for r := 0; r < len(dds); {
   257  			dd := dds[r]
   258  			if r == len(dds)-1 || dd.Case != 1 { // '(' Declarator ')'
   259  				dds[w] = dd
   260  				w++
   261  				r++
   262  				continue
   263  			}
   264  
   265  			dd2 := dds[r+1]
   266  			if dd2.Case != 1 {
   267  				dds[w] = dd
   268  				w++
   269  				r++
   270  				continue
   271  			}
   272  
   273  			d := dd.Declarator
   274  			d2 := dd2.Declarator
   275  			switch s, s2 := d.stars(), d2.stars(); {
   276  			case s == 0 && s2 == 0:
   277  				dds[w] = dd
   278  				w++
   279  				r += 2
   280  				changed = true
   281  			case s == 0 && s2 != 0:
   282  				dds[w] = dd2
   283  				w++
   284  				r += 2
   285  				changed = true
   286  			case s != 0 && s2 == 0:
   287  				dds[w] = dd
   288  				w++
   289  				r += 2
   290  				changed = true
   291  			case s != 0 && s2 != 0:
   292  				d2 := d2.clone()
   293  				var p *Pointer
   294  				for i := 0; i < s+s2; i++ {
   295  					p = &Pointer{Pointer: p}
   296  				}
   297  				d2.PointerOpt = &PointerOpt{Pointer: p}
   298  				dd2 := dd2.clone()
   299  				dd2.Declarator = d2
   300  				dds[w] = dd2
   301  				w++
   302  				r += 2
   303  				changed = true
   304  			}
   305  
   306  		}
   307  		dds = dds[:w]
   308  		if !changed {
   309  			break
   310  		}
   311  	}
   312  
   313  	// Outer (...) -> ...
   314  	for {
   315  		i := len(dds) - 1
   316  		if dd := dds[i]; dd.Case == 1 /* '(' Declarator ')' */ && dd.Declarator.stars() == 0 {
   317  			dds = dds[:i:i]
   318  			continue
   319  		}
   320  
   321  		break
   322  	}
   323  	resultStars := 0
   324  	i := len(dds) - 1
   325  	if dd := dds[i]; dd.Case == 1 /* '(' Declarator ')' */ {
   326  		resultStars = dd.Declarator.stars()
   327  		dds = dds[:i:i]
   328  	}
   329  
   330  	stars := 0
   331  	resultStars += d.stars()
   332  	switch {
   333  	case len(dds) == 1:
   334  		if dds[0].Case != 0 { // IDENTIFIER
   335  			panic("internal error")
   336  		}
   337  
   338  		stars, resultStars = resultStars, 0
   339  	default:
   340  	again:
   341  		i := 1
   342  	loop:
   343  		for {
   344  			switch dd := dds[i]; dd.Case {
   345  			case 1: // '(' Declarator ')'
   346  				if dds[i-1].Case == 0 { // IDENTIFIER
   347  					stars = dd.Declarator.stars()
   348  					if stars == 0 {
   349  						copy(dds[i:], dds[i+1:])
   350  						dds = dds[: len(dds)-1 : len(dds)-1]
   351  						goto again
   352  					}
   353  				} else {
   354  					//dbg("", resultStars, stars, d.specifier.str(), ddsStr(dds))
   355  					panic("TODO")
   356  				}
   357  				i++
   358  			case
   359  				2, // DirectDeclarator '[' TypeQualifierListOpt ExpressionOpt ']'
   360  				6, // DirectDeclarator '(' ParameterTypeList ')'
   361  				7: // DirectDeclarator '(' IdentifierListOpt ')'
   362  				break loop
   363  			default:
   364  				//dbg("", position(n.Pos()), resultStars, stars, d.specifier.str(), ddsStr(dds))
   365  				panic(dd.Case)
   366  			}
   367  		}
   368  	}
   369  
   370  	resultSpecifier := d.specifier
   371  	resultAttr |= resultSpecifier.attrs()
   372  	resultAttr &^= mask
   373  	t := &ctype{
   374  		dds0:            dds0,
   375  		dds:             dds,
   376  		model:           lx.model,
   377  		resultAttr:      resultAttr,
   378  		resultSpecifier: resultSpecifier,
   379  		resultStars:     resultStars,
   380  		stars:           stars,
   381  	}
   382  	//fmt.Printf("%s: 343\n%s", position(d.Pos()), PrettyString(resultSpecifier))
   383  	if lx.tweaks.enableUnsignedEnums && n.unsigednEnum(lx, resultSpecifier) {
   384  		t.resultSpecifier = &spec{resultAttr, tsEncode(tsUnsigned)}
   385  	}
   386  	n.Type = t
   387  	//dbg("@@@@ %v: %s", position(n.Pos()), t.dds[0].Token.S())
   388  	//dbg("setFull %v: %v, %v %v", t, t.Kind(), t.resultStars, t.stars)
   389  	//dbg("", t.str())
   390  	//dbg("----> %v", t)
   391  
   392  	if lx.scope == nil {
   393  		return t
   394  	}
   395  
   396  	// Determine linkage
   397  
   398  	dd := dds[0]
   399  	scs := resultAttr & (saTypedef | saExtern | saStatic | saAuto | saRegister)
   400  	sk := lx.scope.kind
   401  	var prev, prevVisible *Declarator
   402  	var prevVisibleBinding *Binding
   403  	id := dd.Token.Val
   404  	if p := lx.scope.Parent; p != nil {
   405  		b := p.Lookup(NSIdentifiers, id)
   406  		if dd, ok := b.Node.(*DirectDeclarator); ok {
   407  			prevVisible = dd.TopDeclarator()
   408  			prevVisibleBinding = &b
   409  		}
   410  	}
   411  	if b := dd.prev; b != nil {
   412  		prev = b.Node.(*DirectDeclarator).TopDeclarator()
   413  	}
   414  
   415  	switch {
   416  	case
   417  		// [0]6.2.2, 6: The following identifiers have no linkage: an
   418  		// identifier declared to be anything other than an object or a
   419  		// function; an identifier declared to be a function parameter;
   420  		// a block scope identifier for an object declared without the
   421  		// storage-class specifier extern.
   422  		resultAttr&saTypedef != 0,
   423  		sk == ScopeParams,
   424  		(sk == ScopeBlock || sk == ScopeMembers) && resultAttr&saExtern == 0:
   425  
   426  		n.Linkage = None
   427  	case
   428  		// [0]6.2.2, 3: If the declaration of a file scope identifier
   429  		// for an object or a function contains the storage-class
   430  		// specifier static, the identifier has internal linkage.
   431  		sk == ScopeFile && resultAttr&saStatic != 0:
   432  
   433  		n.Linkage = Internal
   434  	case
   435  		// [0]6.2.2, 4: For an identifier declared with the
   436  		// storage-class specifier extern in a scope in which a prior
   437  		// declaration of that identifier is visible, if the prior
   438  		// declaration specifies internal or external linkage, the
   439  		// linkage of the identifier at the later declaration is the
   440  		// same as the linkage specified at the prior declaration.
   441  
   442  		resultAttr&saExtern != 0 &&
   443  			(prev != nil && (prev.Linkage == Internal || prev.Linkage == External) ||
   444  				prevVisible != nil && (prevVisible.Linkage == Internal || prevVisible.Linkage == External)):
   445  		switch {
   446  		case prev != nil && (prev.Linkage == Internal || prev.Linkage == External):
   447  			n.Linkage = prev.Linkage
   448  		default:
   449  			n.Linkage = prevVisible.Linkage
   450  			dd.visible = prevVisibleBinding
   451  		}
   452  	case
   453  		// [0]6.2.2, 4: If no prior declaration is visible, or if the
   454  		// prior declaration specifies no linkage, then the identifier
   455  		// has external linkage.
   456  		resultAttr&saExtern != 0 && (prev == nil || prev.Linkage == None):
   457  
   458  		n.Linkage = External
   459  	case
   460  		// [0]6.2.2, 5: If the declaration of an identifier for a
   461  		// function has no storage-class specifier, its linkage is
   462  		// determined exactly as if it were declared with the
   463  		// storage-class specifier extern.
   464  		t.Kind() == Function && scs == 0,
   465  		// [0]6.2.2, 5: If the declaration of an identifier for an
   466  		// object has file scope and no storage-class specifier, its
   467  		// linkage is external.
   468  		t.Kind() != Function && sk == ScopeFile && scs == 0:
   469  
   470  		n.Linkage = External
   471  	}
   472  
   473  	if isGenerating || id == 0 {
   474  		//dbg("setFull done (A)(%p): %s: %s\n%v", lx.scope, position(n.Pos()), n, resultSpecifier)
   475  		return t
   476  	}
   477  
   478  	if prev != nil && prev.specifier.IsTypedef() != n.specifier.IsTypedef() {
   479  		lx.report.Err(n.Pos(),
   480  			"redeclaration of %s as different kind of symbol, previous declaration at %v",
   481  			xc.Dict.S(id), position(prev.Pos()))
   482  		return t
   483  	}
   484  
   485  	switch n.Linkage {
   486  	case External:
   487  		// [0]6.2.2, 2: In the set of translation units and libraries
   488  		// that constitutes an entire program, each declaration of a
   489  		// particular identifier with external linkage denotes the same
   490  		// object or function.
   491  		if prev, ok := lx.externs[id]; ok && !n.isCompatible(prev) {
   492  			t, isA := compositeType(prev.Type, n.Type)
   493  			if t == nil {
   494  				lx.report.Err(n.Pos(),
   495  					"conflicting types for %s '%s' with external linkage, previous declaration at %s '%s'",
   496  					xc.Dict.S(id), n.Type, position(prev.Pos()), prev.Type)
   497  				break
   498  			}
   499  
   500  			if !isA {
   501  				dd.prev.Node = n.DirectDeclarator.bottom()
   502  			}
   503  		}
   504  
   505  		lx.externs[id] = n
   506  	case Internal:
   507  		// [0]6.2.2, 2: Within one translation unit, each declaration
   508  		// of an identifier with internal linkage denotes the same
   509  		// object or function.
   510  		if prev != nil && !n.isCompatible(prev) {
   511  			t, isA := compositeType(prev.Type, n.Type)
   512  			if t == nil {
   513  				lx.report.Err(n.Pos(),
   514  					"conflicting types for %s '%s' with internal linkage, previous declaration at %s '%s'",
   515  					xc.Dict.S(id), n.Type, position(prev.Pos()), prev.Type)
   516  				break
   517  			}
   518  
   519  			if !isA {
   520  				dd.prev.Node = n.DirectDeclarator.bottom()
   521  			}
   522  		}
   523  	case None:
   524  		// [0]6.2.2, 2: Each declaration of an identifier with no
   525  		// linkage denotes a unique entity.
   526  		if prev != nil {
   527  			if lx.tweaks.allowCompatibleTypedefRedefinitions &&
   528  				n.RawSpecifier().IsTypedef() && prev.RawSpecifier().IsTypedef() &&
   529  				strings.TrimPrefix(n.Type.String(), "typedef ") == strings.TrimPrefix(prev.Type.String(), "typedef ") {
   530  				break
   531  			}
   532  
   533  			lx.report.Err(n.Pos(),
   534  				"redeclaration of %s '%s' with no linkage, previous declaration at %v '%s'",
   535  				xc.Dict.S(id), n.Type, position(prev.Pos()), prev.Type)
   536  		}
   537  	default:
   538  		panic("internal error")
   539  	}
   540  
   541  	//dbg("setFull done: %s: %s", position(n.Pos()), n)
   542  	return t
   543  }
   544  
   545  // ----------------------------------------------------------- DeclaratorOpt
   546  func (n *DeclaratorOpt) isCompatible(m *DeclaratorOpt) bool {
   547  	return n == m || (n != nil && m != nil && n.Declarator.isCompatible(m.Declarator))
   548  }
   549  
   550  // ----------------------------------------------------------- DirectDeclarator
   551  
   552  // DeclarationScope returns the scope a name declared by n is in. If n does not
   553  // declare a name or n declares a name of a built in type, DeclarationScope
   554  // returns nil.
   555  func (n *DirectDeclarator) DeclarationScope() *Bindings {
   556  	return n.idScope
   557  }
   558  
   559  // TopDeclarator returns the top level Declarator associated with n.
   560  func (n *DirectDeclarator) TopDeclarator() *Declarator {
   561  	return n.top().declarator
   562  }
   563  
   564  func (n *DirectDeclarator) top() *DirectDeclarator {
   565  	for n.parent != nil {
   566  		n = n.parent
   567  	}
   568  	return n
   569  }
   570  
   571  func (n *DirectDeclarator) bottom() *DirectDeclarator {
   572  	for n.Case != 0 { // IDENTIFIER
   573  		n = n.directDeclarator()
   574  	}
   575  	return n
   576  }
   577  
   578  func (n *DirectDeclarator) clone() *DirectDeclarator {
   579  	m := *n
   580  	return &m
   581  }
   582  
   583  func (n *DirectDeclarator) isCompatible(m *DirectDeclarator) (r bool) {
   584  	if n == m {
   585  		return true
   586  	}
   587  
   588  	if n.Case > m.Case {
   589  		n, m = m, n
   590  	}
   591  
   592  	if n.Case != m.Case {
   593  		if n.Case == 6 && m.Case == 7 {
   594  			var b []Parameter
   595  			if o := m.IdentifierListOpt; o != nil {
   596  				b = o.params
   597  			}
   598  			return isCompatibleParameters(
   599  				n.ParameterTypeList.params,
   600  				b,
   601  				n.ParameterTypeList.Case == 1, // ParameterList ',' "..."
   602  				false,
   603  			)
   604  		}
   605  	}
   606  
   607  	switch n.Case {
   608  	case 0: // IDENTIFIER
   609  		return true
   610  	case 1: // '(' Declarator ')'
   611  		return true // Declarator checked before
   612  	case 2: // DirectDeclarator '[' TypeQualifierListOpt ExpressionOpt ']'
   613  		// [0]6.7.5.3 6: For two array types to be compatible, both
   614  		// shall have compatible element types, and if both size
   615  		// specifiers are present, and are integer constant
   616  		// expressions, then both size specifiers shall have the same
   617  		// constant value. If the two array types are used in a context
   618  		// which requires them to be compatible, it is undefined
   619  		// behavior if the two size specifiers evaluate to unequal
   620  		// values.
   621  		var nv, mv interface{}
   622  		if o := n.ExpressionOpt; o != nil {
   623  			nv = o.Expression.Value
   624  		}
   625  		if o := m.ExpressionOpt; o != nil {
   626  			mv = o.Expression.Value
   627  		}
   628  		if nv != nil && mv != nil && nv != mv {
   629  			return false
   630  		}
   631  
   632  		return true
   633  	case 6: // DirectDeclarator '(' ParameterTypeList ')'
   634  		return isCompatibleParameters(
   635  			n.ParameterTypeList.params,
   636  			m.ParameterTypeList.params,
   637  			n.ParameterTypeList.Case == 1, // ParameterList ',' "..."
   638  			m.ParameterTypeList.Case == 1, // ParameterList ',' "..."
   639  		)
   640  	case 7: // DirectDeclarator '(' IdentifierListOpt ')'
   641  		var a, b []Parameter
   642  		if o := n.IdentifierListOpt; o != nil {
   643  			a = o.params
   644  		}
   645  		if o := m.IdentifierListOpt; o != nil {
   646  			b = o.params
   647  		}
   648  
   649  		return isCompatibleParameters(a, b, false, false)
   650  	default:
   651  		panic(n.Case)
   652  	}
   653  }
   654  
   655  func (n *DirectDeclarator) directDeclarator() *DirectDeclarator {
   656  	switch n.Case {
   657  	case 0: // IDENTIFIER
   658  		return nil
   659  	case 1: // '(' Declarator ')'
   660  		return n.Declarator.DirectDeclarator
   661  	case
   662  		2, // DirectDeclarator '[' TypeQualifierListOpt ExpressionOpt ']'
   663  		3, // DirectDeclarator '[' "static" TypeQualifierListOpt Expression ']'
   664  		4, // DirectDeclarator '[' TypeQualifierList "static" Expression ']'
   665  		5, // DirectDeclarator '[' TypeQualifierListOpt '*' ']'
   666  		6, // DirectDeclarator '(' ParameterTypeList ')'
   667  		7: // DirectDeclarator '(' IdentifierListOpt ')'
   668  		return n.DirectDeclarator
   669  	default:
   670  		panic(n.Case)
   671  	}
   672  }
   673  
   674  func (n *DirectDeclarator) isArray() bool {
   675  	switch n.Case {
   676  	case
   677  		0, // IDENTIFIER
   678  		1, // '(' Declarator ')'                                                 // Case 1
   679  		6, // DirectDeclarator '(' ParameterTypeList ')'                         // Case 6
   680  		7: // DirectDeclarator '(' IdentifierListOpt ')'                         // Case 7
   681  		return false
   682  	case
   683  		2, // DirectDeclarator '[' TypeQualifierListOpt ExpressionOpt ']'        // Case 2
   684  		3, // DirectDeclarator '[' "static" TypeQualifierListOpt Expression ']'  // Case 3
   685  		4, // DirectDeclarator '[' TypeQualifierList "static" Expression ']'     // Case 4
   686  		5: // DirectDeclarator '[' TypeQualifierListOpt '*' ']'                  // Case 5
   687  		return true
   688  	default:
   689  		panic(n.Case)
   690  	}
   691  }
   692  
   693  func (n *DirectDeclarator) isVLA() *Expression {
   694  	switch dd := n.DirectDeclarator; dd.Case {
   695  	case 0: // IDENTIFIER
   696  		return nil
   697  	case 1: // '(' Declarator ')'                                                 // Case 1
   698  		//dbg("", n.TopDeclarator().Type, n.TopDeclarator().Type.Element(), n.TopDeclarator().Type.Element().Elements())
   699  		d := n.TopDeclarator()
   700  		if d.Type.Kind() == Ptr && d.Type.Element().Elements() >= 0 {
   701  			return nil
   702  		}
   703  
   704  		panic("TODO")
   705  	case 2: // DirectDeclarator '[' TypeQualifierListOpt ExpressionOpt ']'        // Case 2
   706  		o := n.ExpressionOpt
   707  		if o == nil || o.Expression.Value != nil {
   708  			return nil
   709  		}
   710  
   711  		return o.Expression
   712  	case 3: // DirectDeclarator '[' "static" TypeQualifierListOpt Expression ']'  // Case 3
   713  		panic("TODO")
   714  	case 4: // DirectDeclarator '[' TypeQualifierList "static" Expression ']'     // Case 4
   715  		panic("TODO")
   716  	case 5: // DirectDeclarator '[' TypeQualifierListOpt '*' ']'                  // Case 5
   717  		panic("TODO")
   718  	case 6: // DirectDeclarator '(' ParameterTypeList ')'                         // Case 6
   719  		return nil
   720  	case 7: // DirectDeclarator '(' IdentifierListOpt ')'                         // Case 7
   721  		panic("TODO")
   722  	default:
   723  		panic("internal error")
   724  	}
   725  }
   726  
   727  // -------------------------------------------------------------  EnumSpecifier
   728  
   729  func (n *EnumSpecifier) isUnsigned(lx *lexer) bool {
   730  	switch n.Case {
   731  	case 0: // "enum" IdentifierOpt '{' EnumeratorList CommaOpt '}'
   732  		return n.unsigned
   733  	case 1: // "enum" IDENTIFIER                                     // Case 1
   734  		switch b := lx.scope.Lookup(NSTags, n.Token2.Val); x := b.Node.(type) {
   735  		case *EnumSpecifier:
   736  			switch n := x; n.Case {
   737  			case 0: // "enum" IdentifierOpt '{' EnumeratorList CommaOpt '}'
   738  				return n.unsigned
   739  			}
   740  		}
   741  	}
   742  	return false
   743  }
   744  
   745  // ----------------------------------------------------------------- Expression
   746  
   747  func (n *Expression) cond(lx *lexer, op operand) {
   748  	m := lx.model
   749  	lv, _ := n.Expression.eval(lx)
   750  	if lv == nil {
   751  		_, at := op.eval(lx)
   752  		_, bt := n.Expression2.eval(lx)
   753  		if eqTypes(at, bt) {
   754  			n.Type = at
   755  			return
   756  		}
   757  
   758  		if IsArithmeticType(at) && IsArithmeticType(bt) {
   759  			n.Type = m.BinOpType(at, bt)
   760  			return
   761  		}
   762  
   763  		ak := at.Kind()
   764  		bk := bt.Kind()
   765  
   766  		if ak == Function && bk == Ptr {
   767  			if e := bt.Element(); e.Kind() == Function && eqTypes(at, e) {
   768  				n.Type = bt
   769  				return
   770  			}
   771  		}
   772  
   773  		if bk == Function && ak == Ptr {
   774  			if e := at.Element(); e.Kind() == Function && eqTypes(bt, e) {
   775  				n.Type = at
   776  				return
   777  			}
   778  		}
   779  
   780  		if (ak == Enum || ak == Bool) && IsIntType(bt) {
   781  			n.Type = at
   782  			return
   783  		}
   784  
   785  		if (bk == Enum || bk == Bool) && IsIntType(at) {
   786  			n.Type = bt
   787  			return
   788  		}
   789  
   790  		if ak == Struct && bk == Struct ||
   791  			ak == Union && bk == Union {
   792  			if at.CanAssignTo(bt) {
   793  				n.Type = at
   794  				return
   795  			}
   796  		}
   797  
   798  		if ak == Void && bk == Void {
   799  			n.Type = at
   800  			return
   801  		}
   802  
   803  		if ak == Array && bk == Array {
   804  			if at.(*ctype).isCompatible(bt.(*ctype)) {
   805  				n.Type = at
   806  				return
   807  			}
   808  
   809  			at = at.(*ctype).arrayDecay()
   810  			ak = at.Kind()
   811  			bt = bt.(*ctype).arrayDecay()
   812  			bk = bt.Kind()
   813  		}
   814  
   815  		if ak == Array && bk == Ptr && at.CanAssignTo(bt) {
   816  			n.Type = bt
   817  			return
   818  		}
   819  
   820  		if ak == Ptr && bk == Array && bt.CanAssignTo(at) {
   821  			n.Type = at
   822  			return
   823  		}
   824  
   825  		if ak == Ptr && bk == Ptr {
   826  			if at.CanAssignTo(bt) {
   827  				n.Type = at
   828  				return
   829  			}
   830  		}
   831  
   832  		if (ak == Ptr || ak == Array || ak == Function) && IsIntType(bt) {
   833  			n.Type = at
   834  			return
   835  		}
   836  
   837  		if (bk == Ptr || bk == Array || bk == Function) && IsIntType(at) {
   838  			n.Type = bt
   839  			return
   840  		}
   841  
   842  		if ak == Ptr && at.Element().Kind() == Void && bk == Ptr {
   843  			n.Type = bt
   844  			return
   845  		}
   846  
   847  		if bk == Ptr && bt.Element().Kind() == Void && ak == Ptr {
   848  			n.Type = at
   849  			return
   850  		}
   851  
   852  		lx.report.ErrTok(n.Token2, "'%s'/'%s' mismatch in conditional expression", at, bt)
   853  		return
   854  	}
   855  
   856  	if isNonZero(lv) {
   857  		n.Value, n.Type = op.eval(lx)
   858  		return
   859  	}
   860  
   861  	n.Value, n.Type = n.Expression2.eval(lx)
   862  }
   863  
   864  func (n *Expression) eval(lx *lexer) (interface{}, Type) {
   865  	m := lx.model
   866  	if n.Type != nil {
   867  		return n.Value, n.Type
   868  	}
   869  
   870  	n.Type = undefined
   871  outer:
   872  	switch n.Case {
   873  	case 0: // IDENTIFIER
   874  		b := n.scope.Lookup(NSIdentifiers, n.Token.Val)
   875  		if b.Node == nil {
   876  			lx.report.ErrTok(n.Token, "undefined: %s", n.Token.S())
   877  			break
   878  		}
   879  
   880  		dd := b.Node.(*DirectDeclarator)
   881  		t := dd.top().declarator.Type
   882  		if (t.Kind() == Ptr || t.Kind() == Array) && n.Type.Elements() == -1 {
   883  			found := false
   884  			dd := dd
   885  		more:
   886  			for dd.prev != nil {
   887  				dd = dd.prev.Node.(*DirectDeclarator)
   888  				if t2 := dd.TopDeclarator().Type; t2.Elements() >= 0 {
   889  					t = t2
   890  					found = true
   891  					break
   892  				}
   893  			}
   894  			if !found && dd.visible != nil {
   895  				dd = dd.visible.Node.(*DirectDeclarator)
   896  				if t2 := dd.TopDeclarator().Type; t2.Elements() >= 0 {
   897  					t = t2
   898  				} else {
   899  					goto more
   900  				}
   901  			}
   902  		}
   903  		n.Type = t
   904  		if v := dd.EnumVal; v != nil {
   905  			n.Value = v
   906  		}
   907  	case 1: // CHARCONST
   908  		n.Value, n.Type = m.charConst(lx, n.Token)
   909  	case 2: // FLOATCONST
   910  		n.Value, n.Type = m.floatConst(lx, n.Token)
   911  	case 3: // INTCONST
   912  		n.Value, n.Type = m.intConst(lx, n.Token)
   913  	case 4: // LONGCHARCONST
   914  		n.Value, n.Type = m.charConst(lx, n.Token)
   915  	case 5: // LONGSTRINGLITERAL
   916  		n.Value, n.Type = m.strConst(lx, n.Token)
   917  	case 6: // STRINGLITERAL
   918  		n.Value, n.Type = m.strConst(lx, n.Token)
   919  	case 7: //  '(' ExpressionList ')'
   920  		n.Value, n.Type = n.ExpressionList.eval(lx)
   921  	case 8: // Expression '[' ExpressionList ']'
   922  		_, t := n.Expression.eval(lx)
   923  		_, t2 := n.ExpressionList.eval(lx)
   924  		switch t.Kind() {
   925  		case Ptr, Array:
   926  			n.Type = t.Element()
   927  			if !IsIntType(t2) && t2.Kind() != Bool {
   928  				lx.report.Err(n.ExpressionList.Pos(), "array subscript is not an integer or bool (have '%s')", t2)
   929  				break
   930  			}
   931  
   932  			if p, x := n.Expression.Value, n.ExpressionList.Value; p != nil && x != nil {
   933  				sz := uintptr(n.Type.SizeOf())
   934  				switch pv := p.(type) {
   935  				case uintptr:
   936  					switch xv := x.(type) {
   937  					case int32:
   938  						pv += sz * uintptr(xv)
   939  					case uint32:
   940  						pv += sz * uintptr(xv)
   941  					case int64:
   942  						pv += sz * uintptr(xv)
   943  					case uint64:
   944  						pv += sz * uintptr(xv)
   945  					case uintptr:
   946  						pv += sz * xv
   947  					default:
   948  						panic("TODO")
   949  					}
   950  					n.Value = pv
   951  				case StringLitID, LongStringLitID:
   952  					// ok, but not a constant expression.
   953  				default:
   954  					panic("internal error")
   955  				}
   956  			}
   957  			break outer
   958  		}
   959  
   960  		if !IsIntType(t) && t.Kind() != Bool || t2.Kind() != Ptr && t2.Kind() != Array {
   961  			lx.report.ErrTok(n.Token, "invalid index expression types (%s[%t])", t, n.ExpressionList.Type)
   962  			break
   963  		}
   964  
   965  		n.Type = t2.Element()
   966  		if p, x := n.ExpressionList.Value, n.Expression.Value; p != nil && x != nil {
   967  			panic(fmt.Errorf("%s: TODO", position(n.Pos())))
   968  		}
   969  	case 9: // Expression '(' ArgumentExpressionListOpt ')'
   970  		if n.Expression.Case == 0 { // IDENTIFIER
   971  			if lx.tweaks.enableBuiltinConstantP && n.Expression.Token.Val == idBuiltinConstantP {
   972  				o := n.ArgumentExpressionListOpt
   973  				if o == nil {
   974  					lx.report.Err(n.Expression.Pos(), "missing argument of __builtin_constant_p")
   975  					break
   976  				}
   977  
   978  				args := o.ArgumentExpressionList
   979  				if args.ArgumentExpressionList != nil {
   980  					lx.report.Err(n.Expression.Pos(), "too many arguments of __builtin_constant_p")
   981  					break
   982  				}
   983  
   984  				n.Case = 3 // INTCONST
   985  				n.Type = lx.model.IntType
   986  				switch v, _ := args.Expression.eval(lx); {
   987  				case v != nil:
   988  					n.Value = int32(1)
   989  				default:
   990  					n.Value = int32(0)
   991  				}
   992  				break
   993  			}
   994  
   995  			if lx.tweaks.enableBuiltinClassifyType && n.Expression.Token.Val == idBuiltinClasifyType {
   996  				o := n.ArgumentExpressionListOpt
   997  				if o == nil {
   998  					lx.report.Err(n.Expression.Pos(), "missing argument of __builtin_classify_type")
   999  					break
  1000  				}
  1001  
  1002  				args := o.ArgumentExpressionList
  1003  				if args.ArgumentExpressionList != nil {
  1004  					lx.report.Err(n.Expression.Pos(), "too many arguments of __builtin_classify_type")
  1005  					break
  1006  				}
  1007  
  1008  				n.Case = 3 // INTCONST
  1009  				n.Type = lx.model.IntType
  1010  				v := noTypeClass
  1011  				if _, t := args.Expression.eval(lx); t != nil {
  1012  					v = classifyType[t.Kind()]
  1013  				}
  1014  				n.Value = int32(v)
  1015  				break
  1016  			}
  1017  
  1018  			if n.Expression.Token.Val == idBuiltinTypesCompatible {
  1019  				// using #define __builtin_types_compatible_p(type1, type2) __builtin_types_compatible__((type1){}, (type2){})
  1020  				o := n.ArgumentExpressionListOpt
  1021  				if o == nil {
  1022  					lx.report.Err(n.Expression.Pos(), "missing arguments of __builtin_types_compatible_p")
  1023  					break
  1024  				}
  1025  
  1026  				args := o.ArgumentExpressionList
  1027  				arg1 := args.Expression
  1028  				if arg1.Case != 14 { // '(' TypeName ')' '{' InitializerList CommaOpt '}'
  1029  					lx.report.Err(arg1.Pos(), "invalid argument of __builtin_types_compatible__")
  1030  					break
  1031  				}
  1032  
  1033  				args = args.ArgumentExpressionList
  1034  				if args == nil {
  1035  					lx.report.Err(n.Expression.Pos(), "missing argument of __builtin_types_compatible_p")
  1036  					break
  1037  				}
  1038  
  1039  				arg2 := args.Expression
  1040  				if arg2.Case != 14 { // '(' TypeName ')' '{' InitializerList CommaOpt '}'
  1041  					lx.report.Err(arg1.Pos(), "invalid argument of __builtin_types_compatible__")
  1042  					break
  1043  				}
  1044  
  1045  				if args.ArgumentExpressionList != nil {
  1046  					lx.report.Err(n.Expression.Pos(), "too many arguments of __builtin_types_compatible_p")
  1047  					break
  1048  				}
  1049  
  1050  				t := arg1.Type
  1051  				u := arg2.Type
  1052  				var v int32
  1053  				if !isEnum(arg1.TypeName, arg2.TypeName) && t.(*ctype).isCompatible(u.(*ctype)) {
  1054  					v = 1
  1055  					if t.Kind() == Ptr && u.Kind() == Ptr && t.Specifier().IsConst() != u.Specifier().IsConst() {
  1056  						v = 0
  1057  					}
  1058  				}
  1059  				n.Type = lx.model.IntType
  1060  				n.Value = v
  1061  				break
  1062  			}
  1063  
  1064  			b := n.Expression.scope.Lookup(NSIdentifiers, n.Expression.Token.Val)
  1065  			if b.Node == nil && lx.tweaks.enableImplicitFuncDef {
  1066  				n.Type = lx.model.IntType
  1067  				break
  1068  			}
  1069  		}
  1070  
  1071  		_, t := n.Expression.eval(lx)
  1072  		if t.Kind() == Ptr {
  1073  			t = t.Element()
  1074  		}
  1075  		if t.Kind() != Function {
  1076  			lx.report.Err(n.Expression.Pos(), "called object is not a function or function pointer (have '%s')", t)
  1077  			break
  1078  		}
  1079  
  1080  		n.Type = t.Result()
  1081  		params, isVariadic := t.Parameters()
  1082  		if params == nil {
  1083  			break // [0], 6.5.2.2/8
  1084  		}
  1085  
  1086  		var args []*Expression
  1087  		var types []Type
  1088  		if o := n.ArgumentExpressionListOpt; o != nil {
  1089  			for l := o.ArgumentExpressionList; l != nil; l = l.ArgumentExpressionList {
  1090  				ex := l.Expression
  1091  				args = append(args, ex)
  1092  				_, t := ex.eval(lx)
  1093  				types = append(types, t)
  1094  			}
  1095  		}
  1096  
  1097  		if g, e := len(args), len(params); g < e {
  1098  			lx.report.ErrTok(n.Token, "too few arguments to function (have %v, want %v)", g, e)
  1099  			break
  1100  		}
  1101  
  1102  		if !isVariadic {
  1103  			if len(args) > len(params) && len(params) != 0 /* composite type */ {
  1104  				lx.report.Err(n.ArgumentExpressionListOpt.Pos(), "too many arguments to function")
  1105  				break
  1106  			}
  1107  		}
  1108  
  1109  		for i, param := range params {
  1110  			pt := param.Type
  1111  			if pt.Kind() == Array {
  1112  				pt = pt.(*ctype).arrayDecay()
  1113  			}
  1114  			typ := types[i]
  1115  			if pt.Kind() == Function && typ.Kind() == Ptr && typ.Element().Kind() == Function {
  1116  				typ = typ.Element()
  1117  			}
  1118  			if !typ.CanAssignTo(pt) {
  1119  				lx.report.Err(args[i].Pos(), "expected '%s' but argument is of type '%s'", pt, typ)
  1120  			}
  1121  		}
  1122  	case 10: // Expression '.' IDENTIFIER
  1123  		_, t := n.Expression.eval(lx)
  1124  		mb, err := t.Member(n.Token2.Val)
  1125  		if err == nil {
  1126  			n.Type = mb.Type
  1127  		} else {
  1128  			// support AnonymousStructs() by doing some emulating... (todo check if enabled)
  1129  			offset, ty, err2 := memberOffsetRecursive(t, n.Token2.Val)
  1130  			if err2 == nil {
  1131  				// This is kindof a simple workaround... should work good enough though
  1132  				// and might be the easiest implementation possible
  1133  				// transform a.b into (*(ty*)((char*)(&a))+offset))
  1134  				ptr := &Expression{
  1135  					Case:       17, // &Expression
  1136  					Token:      xc.Token{lex.Char{Rune: '&'}, 0},
  1137  					Expression: n.Expression,
  1138  				}
  1139  				// sneak in a char pointer so that the offset is correct
  1140  				charTy := lx.model.CharType.Pointer()
  1141  				charTyDeclarator := &Declarator{Type: charTy}
  1142  				ptr = &Expression{
  1143  					Case:       25,
  1144  					Token:      xc.Token{lex.Char{Rune: '('}, 0},
  1145  					TypeName:   &TypeName{Type: charTy, declarator: charTyDeclarator},
  1146  					Token2:     xc.Token{lex.Char{Rune: ')'}, 0},
  1147  					Expression: ptr,
  1148  				}
  1149  				sid := dict.SID(strconv.Itoa(offset))
  1150  				offset := &Expression{
  1151  					Case:  3, // INTCONST
  1152  					Token: xc.Token{lex.Char{Rune: INTCONST}, sid},
  1153  				}
  1154  				fieldPtr := &Expression{
  1155  					Case:        29, // +
  1156  					Expression:  ptr,
  1157  					Token:       xc.Token{lex.Char{Rune: '+'}, 0},
  1158  					Expression2: offset,
  1159  				}
  1160  				ptrTy := (*ty).Pointer()
  1161  				declarator := &Declarator{Type: ptrTy}
  1162  				cast := &Expression{
  1163  					Case:       25, // cast to ty *
  1164  					Token:      xc.Token{lex.Char{Rune: '('}, 0},
  1165  					TypeName:   &TypeName{Type: ptrTy, declarator: declarator},
  1166  					Token2:     xc.Token{lex.Char{Rune: ')'}, 0},
  1167  					Expression: fieldPtr,
  1168  				}
  1169  				*n = Expression{
  1170  					Case:       18, // * (dereference)
  1171  					Token:      xc.Token{lex.Char{Rune: '*'}, 0},
  1172  					Expression: cast,
  1173  				}
  1174  				n.Value, n.Type = n.eval(lx)
  1175  			} else {
  1176  				lx.report.Err(n.Token2.Pos(), "%v (OR %v)", err, err2)
  1177  				break
  1178  			}
  1179  		}
  1180  	case 11: // Expression "->" IDENTIFIER
  1181  		v, t := n.Expression.eval(lx)
  1182  		if t.Kind() != Ptr && t.Kind() != Array {
  1183  			lx.report.ErrTok(n.Token2, "invalid type argument of -> (have '%v')", t)
  1184  			break
  1185  		}
  1186  
  1187  		t = t.Element()
  1188  		mb, err := t.Member(n.Token2.Val)
  1189  		if err != nil {
  1190  			lx.report.Err(n.Token2.Pos(), "%v", err)
  1191  			break
  1192  		}
  1193  
  1194  		n.Type = mb.Type
  1195  		switch x := v.(type) {
  1196  		case nil:
  1197  			// nop
  1198  		case uintptr:
  1199  			n.Value = x + uintptr(mb.OffsetOf)
  1200  		default:
  1201  			panic("internal error")
  1202  		}
  1203  	case 12: // Expression "++"
  1204  		n.Value, n.Type = n.Expression.eval(lx)
  1205  	case 13: // Expression "--"
  1206  		n.Value, n.Type = n.Expression.eval(lx)
  1207  	case 14: // '(' TypeName ')' '{' InitializerList CommaOpt '}'
  1208  		n.Type = n.TypeName.Type
  1209  		n.InitializerList.typeCheck(&n.Type, n.Type, false, lx)
  1210  	case 15: // "++" Expression
  1211  		n.Value, n.Type = n.Expression.eval(lx)
  1212  	case 16: // "--" Expression
  1213  		n.Value, n.Type = n.Expression.eval(lx)
  1214  	case 17: // '&' Expression
  1215  		var t Type
  1216  		n.Value, t = n.Expression.eval(lx)
  1217  		n.Type = t.Pointer()
  1218  	case 18: // '*' Expression
  1219  		_, t := n.Expression.eval(lx)
  1220  		if t.Kind() == Function {
  1221  			n.Type = t
  1222  			break
  1223  		}
  1224  
  1225  		if k := t.Kind(); k != Ptr && k != Array {
  1226  			lx.report.ErrTok(n.Token, "invalid argument type of unary * (have '%v')", t)
  1227  			break
  1228  		}
  1229  
  1230  		n.Type = t.Element()
  1231  	case 19: // '+' Expression
  1232  		v, t := n.Expression.eval(lx)
  1233  		n.Type = lx.model.promote(t)
  1234  		if v == nil {
  1235  			break
  1236  		}
  1237  
  1238  		n.Value = lx.model.MustConvert(v, n.Type)
  1239  	case 20: // '-' Expression
  1240  		v, t := n.Expression.eval(lx)
  1241  		n.Type = lx.model.promote(t)
  1242  		if v == nil {
  1243  			break
  1244  		}
  1245  
  1246  		v = lx.model.MustConvert(v, n.Type)
  1247  		switch x := v.(type) {
  1248  		case int16:
  1249  			n.Value = -x
  1250  		case uint16:
  1251  			n.Value = -x
  1252  		case int32:
  1253  			n.Value = -x
  1254  		case uint32:
  1255  			n.Value = -x
  1256  		case uint64:
  1257  			n.Value = -x
  1258  		case int64:
  1259  			n.Value = -x
  1260  		case float32:
  1261  			n.Value = -x
  1262  		case float64:
  1263  			n.Value = -x
  1264  		default:
  1265  			panic(fmt.Errorf("internal error: %T", x))
  1266  		}
  1267  	case 21: // '~' Expression
  1268  		v, t := n.Expression.eval(lx)
  1269  		n.Type = lx.model.promote(t)
  1270  		if v == nil {
  1271  			break
  1272  		}
  1273  
  1274  		v = lx.model.MustConvert(v, n.Type)
  1275  		switch x := v.(type) {
  1276  		case int32:
  1277  			n.Value = ^x
  1278  		case uint32:
  1279  			n.Value = ^x
  1280  		case int64:
  1281  			n.Value = ^x
  1282  		case uint64:
  1283  			n.Value = ^x
  1284  		default:
  1285  			panic(fmt.Errorf("internal error: %T", x))
  1286  		}
  1287  	case 22: // '!' Expression
  1288  		v, _ := n.Expression.eval(lx)
  1289  		n.Type = m.IntType
  1290  		if v == nil {
  1291  			break
  1292  		}
  1293  
  1294  		n.Value = m.cBool(isZero(v))
  1295  	case 23: // "sizeof" Expression
  1296  		n.Type = m.getSizeType(lx)
  1297  		switch v, t := n.Expression.eval(lx); x := v.(type) {
  1298  		case StringLitID:
  1299  			n.Value = m.MustConvert(int32(len(dict.S(int(x)))+1), n.Type)
  1300  		default:
  1301  			n.Value = m.MustConvert(uint64(t.SizeOf()), n.Type)
  1302  		}
  1303  	case 24: // "sizeof" '(' TypeName ')'
  1304  		n.Type = m.getSizeType(lx)
  1305  		n.Value = m.MustConvert(uint64(n.TypeName.declarator.Type.SizeOf()), n.Type)
  1306  	case 25: // '(' TypeName ')' Expression
  1307  		v, _ := n.Expression.eval(lx)
  1308  		n.Type = n.TypeName.declarator.Type
  1309  		n.Value = v
  1310  		if v != nil && n.Type.Kind() != Struct && n.Type.Kind() != Union && !isStrLitID(v) {
  1311  			n.Value = m.MustConvert(v, n.Type)
  1312  		}
  1313  	case 26: // Expression '*' Expression
  1314  		var a, b interface{}
  1315  		a, b, n.Type = m.binOp(lx, n.Expression, n.Expression2)
  1316  		n.BinOpType = n.Type
  1317  
  1318  		switch x := a.(type) {
  1319  		case nil:
  1320  			// nop
  1321  		case int32:
  1322  			n.Value = x * b.(int32)
  1323  		case uint32:
  1324  			n.Value = x * b.(uint32)
  1325  		case int64:
  1326  			n.Value = x * b.(int64)
  1327  		case uint64:
  1328  			n.Value = x * b.(uint64)
  1329  		case float32:
  1330  			n.Value = x * b.(float32)
  1331  		case float64:
  1332  			n.Value = x * b.(float64)
  1333  		case complex64:
  1334  			n.Value = x * b.(complex64)
  1335  		case complex128:
  1336  			n.Value = x * b.(complex128)
  1337  		default:
  1338  			panic(fmt.Errorf("internal error: %T", x))
  1339  		}
  1340  	case 27: // Expression '/' Expression
  1341  		var a, b interface{}
  1342  		a, b, n.Type = m.binOp(lx, n.Expression, n.Expression2)
  1343  		n.BinOpType = n.Type
  1344  		if b != nil && isZero(b) && IsIntType(n.Type) {
  1345  			lx.report.Err(n.Expression2.Pos(), "division by zero")
  1346  			break
  1347  		}
  1348  
  1349  		switch x := a.(type) {
  1350  		case nil:
  1351  			// nop
  1352  		case int32:
  1353  			n.Value = x / b.(int32)
  1354  		case uint32:
  1355  			n.Value = x / b.(uint32)
  1356  		case int64:
  1357  			n.Value = x / b.(int64)
  1358  		case uint64:
  1359  			n.Value = x / b.(uint64)
  1360  		case float32:
  1361  			n.Value = x / b.(float32)
  1362  		case float64:
  1363  			n.Value = x / b.(float64)
  1364  		default:
  1365  			panic(fmt.Errorf("internal error: %T", x))
  1366  		}
  1367  	case 28: // Expression '%' Expression
  1368  		var a, b interface{}
  1369  		a, b, n.Type = m.binOp(lx, n.Expression, n.Expression2)
  1370  		n.BinOpType = n.Type
  1371  		if b != nil && isZero(b) && IsIntType(n.Type) {
  1372  			lx.report.Err(n.Expression2.Pos(), "division by zero")
  1373  			break
  1374  		}
  1375  
  1376  		switch x := a.(type) {
  1377  		case nil:
  1378  			// nop
  1379  		case int32:
  1380  			n.Value = x % b.(int32)
  1381  		case uint32:
  1382  			n.Value = x % b.(uint32)
  1383  		case int64:
  1384  			n.Value = x % b.(int64)
  1385  		case uint64:
  1386  			n.Value = x % b.(uint64)
  1387  		default:
  1388  			panic(fmt.Errorf("internal error: %T", x))
  1389  		}
  1390  	case 29: // Expression '+' Expression
  1391  		_, at := n.Expression.eval(lx)
  1392  		_, bt := n.Expression2.eval(lx)
  1393  		if at.Kind() == Array {
  1394  			at = at.Element().Pointer()
  1395  		}
  1396  		if bt.Kind() == Array {
  1397  			bt = bt.Element().Pointer()
  1398  		}
  1399  		if at.Kind() > bt.Kind() {
  1400  			at, bt = bt, at
  1401  		}
  1402  		switch {
  1403  		case at.Kind() == Ptr:
  1404  			if IsIntType(bt) || bt.Kind() == Bool {
  1405  				n.Type = at
  1406  				break
  1407  			}
  1408  
  1409  			lx.report.ErrTok(n.Token, "incompatible types ('%s' + '%s')", at, bt)
  1410  		case IsArithmeticType(at):
  1411  			fallthrough
  1412  		default:
  1413  			var a, b interface{}
  1414  			a, b, n.Type = m.binOp(lx, n.Expression, n.Expression2)
  1415  			n.BinOpType = n.Type
  1416  			switch x := a.(type) {
  1417  			case nil:
  1418  				// nop
  1419  			case int32:
  1420  				n.Value = x + b.(int32)
  1421  			case uint32:
  1422  				n.Value = x + b.(uint32)
  1423  			case int64:
  1424  				n.Value = x + b.(int64)
  1425  			case uint64:
  1426  				n.Value = x + b.(uint64)
  1427  			case float32:
  1428  				n.Value = x + b.(float32)
  1429  			case float64:
  1430  				n.Value = x + b.(float64)
  1431  			case complex64:
  1432  				n.Value = x + b.(complex64)
  1433  			case complex128:
  1434  				n.Value = x + b.(complex128)
  1435  			default:
  1436  				panic(fmt.Errorf("internal error: %T", x))
  1437  			}
  1438  		}
  1439  	case 30: // Expression '-' Expression
  1440  		av, at := n.Expression.eval(lx)
  1441  		bv, bt := n.Expression2.eval(lx)
  1442  		if at.Kind() == Array {
  1443  			at = at.Element().Pointer()
  1444  		}
  1445  		if bt.Kind() == Array {
  1446  			bt = bt.Element().Pointer()
  1447  		}
  1448  		if at.Kind() == Ptr && bt.Kind() == Ptr {
  1449  			if !at.CanAssignTo(bt) {
  1450  				n.Type = undefined
  1451  				lx.report.Err(n.Expression2.Pos(), "incompatible types ('%s' - '%s')", at, bt)
  1452  				break
  1453  			}
  1454  
  1455  			n.Type = m.getPtrDiffType(lx)
  1456  			if av != nil && bv != nil {
  1457  				n.Value = lx.model.MustConvert((av.(uintptr)-bv.(uintptr))/uintptr(n.Type.SizeOf()), n.Type)
  1458  			}
  1459  			break
  1460  		}
  1461  
  1462  		if at.Kind() == Ptr && IsIntType(bt) {
  1463  			n.Type = at
  1464  			break
  1465  		}
  1466  
  1467  		var a, b interface{}
  1468  		a, b, n.Type = m.binOp(lx, n.Expression, n.Expression2)
  1469  		n.BinOpType = n.Type
  1470  		switch x := a.(type) {
  1471  		case nil:
  1472  			// nop
  1473  		case int32:
  1474  			n.Value = x - b.(int32)
  1475  		case uint32:
  1476  			n.Value = x - b.(uint32)
  1477  		case int64:
  1478  			n.Value = x - b.(int64)
  1479  		case uint64:
  1480  			n.Value = x - b.(uint64)
  1481  		case float32:
  1482  			n.Value = x - b.(float32)
  1483  		case float64:
  1484  			n.Value = x - b.(float64)
  1485  		case complex64:
  1486  			n.Value = x - b.(complex64)
  1487  		case complex128:
  1488  			n.Value = x - b.(complex128)
  1489  		default:
  1490  			panic(fmt.Errorf("internal error: %T", x))
  1491  		}
  1492  	case 31: // Expression "<<" Expression
  1493  		av, at := n.Expression.eval(lx)
  1494  		bv, bt := n.Expression2.eval(lx)
  1495  		n.Type = lx.model.promote(at)
  1496  		if av == nil || bv == nil {
  1497  			break
  1498  		}
  1499  
  1500  		av = lx.model.MustConvert(av, n.Type)
  1501  		bv = lx.model.MustConvert(bv, lx.model.promote(bt))
  1502  		switch x := av.(type) {
  1503  		case int8:
  1504  			switch y := bv.(type) {
  1505  			case int32:
  1506  				switch {
  1507  				case y > 0:
  1508  					n.Value = x << uint(y)
  1509  				case y < 0:
  1510  					n.Value = x >> uint(-y)
  1511  				default:
  1512  					n.Value = x
  1513  				}
  1514  			case uint32:
  1515  				switch {
  1516  				case y > 0:
  1517  					n.Value = x << uint(y)
  1518  				default:
  1519  					n.Value = x
  1520  				}
  1521  			case int64:
  1522  				switch {
  1523  				case y > 0:
  1524  					n.Value = x << uint(y)
  1525  				case y < 0:
  1526  					n.Value = x >> uint(-y)
  1527  				default:
  1528  					n.Value = x
  1529  				}
  1530  			case uint64:
  1531  				switch {
  1532  				case y > 0:
  1533  					n.Value = x << uint(y)
  1534  				default:
  1535  					n.Value = x
  1536  				}
  1537  			default:
  1538  				panic(fmt.Errorf("internal error: %T", y))
  1539  			}
  1540  		case uint8:
  1541  			switch y := bv.(type) {
  1542  			case int32:
  1543  				switch {
  1544  				case y > 0:
  1545  					n.Value = x << uint(y)
  1546  				case y < 0:
  1547  					n.Value = x >> uint(-y)
  1548  				default:
  1549  					n.Value = x
  1550  				}
  1551  			case uint32:
  1552  				switch {
  1553  				case y > 0:
  1554  					n.Value = x << uint(y)
  1555  				default:
  1556  					n.Value = x
  1557  				}
  1558  			case int64:
  1559  				switch {
  1560  				case y > 0:
  1561  					n.Value = x << uint(y)
  1562  				case y < 0:
  1563  					n.Value = x >> uint(-y)
  1564  				default:
  1565  					n.Value = x
  1566  				}
  1567  			case uint64:
  1568  				switch {
  1569  				case y > 0:
  1570  					n.Value = x << uint(y)
  1571  				default:
  1572  					n.Value = x
  1573  				}
  1574  			default:
  1575  				panic(fmt.Errorf("internal error: %T", y))
  1576  			}
  1577  		case int16:
  1578  			switch y := bv.(type) {
  1579  			case int16:
  1580  				switch {
  1581  				case y > 0:
  1582  					n.Value = x << uint(y)
  1583  				case y < 0:
  1584  					n.Value = x >> uint(-y)
  1585  				default:
  1586  					n.Value = x
  1587  				}
  1588  			case uint16:
  1589  				switch {
  1590  				case y > 0:
  1591  					n.Value = x << uint(y)
  1592  				default:
  1593  					n.Value = x
  1594  				}
  1595  			case int32:
  1596  				switch {
  1597  				case y > 0:
  1598  					n.Value = x << uint(y)
  1599  				case y < 0:
  1600  					n.Value = x >> uint(-y)
  1601  				default:
  1602  					n.Value = x
  1603  				}
  1604  			case uint32:
  1605  				switch {
  1606  				case y > 0:
  1607  					n.Value = x << uint(y)
  1608  				default:
  1609  					n.Value = x
  1610  				}
  1611  			case int64:
  1612  				switch {
  1613  				case y > 0:
  1614  					n.Value = x << uint(y)
  1615  				case y < 0:
  1616  					n.Value = x >> uint(-y)
  1617  				default:
  1618  					n.Value = x
  1619  				}
  1620  			case uint64:
  1621  				switch {
  1622  				case y > 0:
  1623  					n.Value = x << uint(y)
  1624  				default:
  1625  					n.Value = x
  1626  				}
  1627  			default:
  1628  				panic(fmt.Errorf("internal error: %T", y))
  1629  			}
  1630  		case uint16:
  1631  			switch y := bv.(type) {
  1632  			case int16:
  1633  				switch {
  1634  				case y > 0:
  1635  					n.Value = x << uint(y)
  1636  				case y < 0:
  1637  					n.Value = x >> uint(-y)
  1638  				default:
  1639  					n.Value = x
  1640  				}
  1641  			case uint16:
  1642  				switch {
  1643  				case y > 0:
  1644  					n.Value = x << uint(y)
  1645  				default:
  1646  					n.Value = x
  1647  				}
  1648  			case int32:
  1649  				switch {
  1650  				case y > 0:
  1651  					n.Value = x << uint(y)
  1652  				case y < 0:
  1653  					n.Value = x >> uint(-y)
  1654  				default:
  1655  					n.Value = x
  1656  				}
  1657  			case uint32:
  1658  				switch {
  1659  				case y > 0:
  1660  					n.Value = x << uint(y)
  1661  				default:
  1662  					n.Value = x
  1663  				}
  1664  			case int64:
  1665  				switch {
  1666  				case y > 0:
  1667  					n.Value = x << uint(y)
  1668  				case y < 0:
  1669  					n.Value = x >> uint(-y)
  1670  				default:
  1671  					n.Value = x
  1672  				}
  1673  			case uint64:
  1674  				switch {
  1675  				case y > 0:
  1676  					n.Value = x << uint(y)
  1677  				default:
  1678  					n.Value = x
  1679  				}
  1680  			default:
  1681  				panic(fmt.Errorf("internal error: %T", y))
  1682  			}
  1683  		case int32:
  1684  			switch y := bv.(type) {
  1685  			case int16:
  1686  				switch {
  1687  				case y > 0:
  1688  					n.Value = x << uint(y)
  1689  				case y < 0:
  1690  					n.Value = x >> uint(-y)
  1691  				default:
  1692  					n.Value = x
  1693  				}
  1694  			case uint16:
  1695  				switch {
  1696  				case y > 0:
  1697  					n.Value = x << uint(y)
  1698  				default:
  1699  					n.Value = x
  1700  				}
  1701  			case int32:
  1702  				switch {
  1703  				case y > 0:
  1704  					n.Value = x << uint(y)
  1705  				case y < 0:
  1706  					n.Value = x >> uint(-y)
  1707  				default:
  1708  					n.Value = x
  1709  				}
  1710  			case uint32:
  1711  				switch {
  1712  				case y > 0:
  1713  					n.Value = x << uint(y)
  1714  				default:
  1715  					n.Value = x
  1716  				}
  1717  			case int64:
  1718  				switch {
  1719  				case y > 0:
  1720  					n.Value = x << uint(y)
  1721  				case y < 0:
  1722  					n.Value = x >> uint(-y)
  1723  				default:
  1724  					n.Value = x
  1725  				}
  1726  			case uint64:
  1727  				switch {
  1728  				case y > 0:
  1729  					n.Value = x << uint(y)
  1730  				default:
  1731  					n.Value = x
  1732  				}
  1733  			default:
  1734  				panic(fmt.Errorf("internal error: %T", y))
  1735  			}
  1736  		case uint32:
  1737  			switch y := bv.(type) {
  1738  			case int16:
  1739  				switch {
  1740  				case y > 0:
  1741  					n.Value = x << uint(y)
  1742  				case y < 0:
  1743  					n.Value = x >> uint(-y)
  1744  				default:
  1745  					n.Value = x
  1746  				}
  1747  			case uint16:
  1748  				switch {
  1749  				case y > 0:
  1750  					n.Value = x << uint(y)
  1751  				default:
  1752  					n.Value = x
  1753  				}
  1754  			case int32:
  1755  				switch {
  1756  				case y > 0:
  1757  					n.Value = x << uint(y)
  1758  				case y < 0:
  1759  					n.Value = x >> uint(-y)
  1760  				default:
  1761  					n.Value = x
  1762  				}
  1763  			case uint32:
  1764  				switch {
  1765  				case y > 0:
  1766  					n.Value = x << uint(y)
  1767  				default:
  1768  					n.Value = x
  1769  				}
  1770  			case int64:
  1771  				switch {
  1772  				case y > 0:
  1773  					n.Value = x << uint(y)
  1774  				case y < 0:
  1775  					n.Value = x >> uint(-y)
  1776  				default:
  1777  					n.Value = x
  1778  				}
  1779  			case uint64:
  1780  				switch {
  1781  				case y > 0:
  1782  					n.Value = x << uint(y)
  1783  				default:
  1784  					n.Value = x
  1785  				}
  1786  			default:
  1787  				panic(fmt.Errorf("internal error: %T", y))
  1788  			}
  1789  		case int64:
  1790  			switch y := bv.(type) {
  1791  			case int16:
  1792  				switch {
  1793  				case y > 0:
  1794  					n.Value = x << uint(y)
  1795  				case y < 0:
  1796  					n.Value = x >> uint(-y)
  1797  				default:
  1798  					n.Value = x
  1799  				}
  1800  			case uint16:
  1801  				switch {
  1802  				case y > 0:
  1803  					n.Value = x << uint(y)
  1804  				default:
  1805  					n.Value = x
  1806  				}
  1807  			case int32:
  1808  				switch {
  1809  				case y > 0:
  1810  					n.Value = x << uint(y)
  1811  				case y < 0:
  1812  					n.Value = x >> uint(-y)
  1813  				default:
  1814  					n.Value = x
  1815  				}
  1816  			case uint32:
  1817  				switch {
  1818  				case y > 0:
  1819  					n.Value = x << uint(y)
  1820  				default:
  1821  					n.Value = x
  1822  				}
  1823  			case int64:
  1824  				switch {
  1825  				case y > 0:
  1826  					n.Value = x << uint(y)
  1827  				case y < 0:
  1828  					n.Value = x >> uint(-y)
  1829  				default:
  1830  					n.Value = x
  1831  				}
  1832  			case uint64:
  1833  				switch {
  1834  				case y > 0:
  1835  					n.Value = x << uint(y)
  1836  				default:
  1837  					n.Value = x
  1838  				}
  1839  			default:
  1840  				panic(fmt.Errorf("internal error: %T", y))
  1841  			}
  1842  		case uint64:
  1843  			switch y := bv.(type) {
  1844  			case int16:
  1845  				switch {
  1846  				case y > 0:
  1847  					n.Value = x << uint(y)
  1848  				case y < 0:
  1849  					n.Value = x >> uint(-y)
  1850  				default:
  1851  					n.Value = x
  1852  				}
  1853  			case uint16:
  1854  				switch {
  1855  				case y > 0:
  1856  					n.Value = x << uint(y)
  1857  				default:
  1858  					n.Value = x
  1859  				}
  1860  			case int32:
  1861  				switch {
  1862  				case y > 0:
  1863  					n.Value = x << uint(y)
  1864  				case y < 0:
  1865  					n.Value = x >> uint(-y)
  1866  				default:
  1867  					n.Value = x
  1868  				}
  1869  			case uint32:
  1870  				switch {
  1871  				case y > 0:
  1872  					n.Value = x << uint(y)
  1873  				default:
  1874  					n.Value = x
  1875  				}
  1876  			case int64:
  1877  				switch {
  1878  				case y > 0:
  1879  					n.Value = x << uint(y)
  1880  				case y < 0:
  1881  					n.Value = x >> uint(-y)
  1882  				default:
  1883  					n.Value = x
  1884  				}
  1885  			case uint64:
  1886  				switch {
  1887  				case y > 0:
  1888  					n.Value = x << uint(y)
  1889  				default:
  1890  					n.Value = x
  1891  				}
  1892  			default:
  1893  				panic(fmt.Errorf("internal error: %T", y))
  1894  			}
  1895  		default:
  1896  			panic(fmt.Errorf("internal error: %T", x))
  1897  		}
  1898  	case 32: // Expression ">>" Expression
  1899  		av, at := n.Expression.eval(lx)
  1900  		bv, bt := n.Expression2.eval(lx)
  1901  		n.Type = lx.model.promote(at)
  1902  		if av == nil || bv == nil {
  1903  			break
  1904  		}
  1905  
  1906  		av = lx.model.MustConvert(av, n.Type)
  1907  		bv = lx.model.MustConvert(bv, lx.model.promote(bt))
  1908  		switch x := av.(type) {
  1909  		case int8:
  1910  			switch y := bv.(type) {
  1911  			case int32:
  1912  				switch {
  1913  				case y > 0:
  1914  					n.Value = x >> uint(y)
  1915  				case y < 0:
  1916  					n.Value = x << uint(-y)
  1917  				default:
  1918  					n.Value = x
  1919  				}
  1920  			case uint32:
  1921  				switch {
  1922  				case y > 0:
  1923  					n.Value = x >> uint(y)
  1924  				default:
  1925  					n.Value = x
  1926  				}
  1927  			case int64:
  1928  				switch {
  1929  				case y > 0:
  1930  					n.Value = x >> uint(y)
  1931  				case y < 0:
  1932  					n.Value = x << uint(-y)
  1933  				default:
  1934  					n.Value = x
  1935  				}
  1936  			case uint64:
  1937  				switch {
  1938  				case y > 0:
  1939  					n.Value = x >> uint(y)
  1940  				default:
  1941  					n.Value = x
  1942  				}
  1943  			default:
  1944  				panic(fmt.Errorf("internal error: %T", y))
  1945  			}
  1946  		case uint8:
  1947  			switch y := bv.(type) {
  1948  			case int32:
  1949  				switch {
  1950  				case y > 0:
  1951  					n.Value = x >> uint(y)
  1952  				case y < 0:
  1953  					n.Value = x << uint(-y)
  1954  				default:
  1955  					n.Value = x
  1956  				}
  1957  			case uint32:
  1958  				switch {
  1959  				case y > 0:
  1960  					n.Value = x >> uint(y)
  1961  				default:
  1962  					n.Value = x
  1963  				}
  1964  			case int64:
  1965  				switch {
  1966  				case y > 0:
  1967  					n.Value = x >> uint(y)
  1968  				case y < 0:
  1969  					n.Value = x << uint(-y)
  1970  				default:
  1971  					n.Value = x
  1972  				}
  1973  			case uint64:
  1974  				switch {
  1975  				case y > 0:
  1976  					n.Value = x >> uint(y)
  1977  				default:
  1978  					n.Value = x
  1979  				}
  1980  			default:
  1981  				panic(fmt.Errorf("internal error: %T", y))
  1982  			}
  1983  		case int16:
  1984  			switch y := bv.(type) {
  1985  			case int32:
  1986  				switch {
  1987  				case y > 0:
  1988  					n.Value = x >> uint(y)
  1989  				case y < 0:
  1990  					n.Value = x << uint(-y)
  1991  				default:
  1992  					n.Value = x
  1993  				}
  1994  			case uint32:
  1995  				switch {
  1996  				case y > 0:
  1997  					n.Value = x >> uint(y)
  1998  				default:
  1999  					n.Value = x
  2000  				}
  2001  			case int64:
  2002  				switch {
  2003  				case y > 0:
  2004  					n.Value = x >> uint(y)
  2005  				case y < 0:
  2006  					n.Value = x << uint(-y)
  2007  				default:
  2008  					n.Value = x
  2009  				}
  2010  			case uint64:
  2011  				switch {
  2012  				case y > 0:
  2013  					n.Value = x >> uint(y)
  2014  				default:
  2015  					n.Value = x
  2016  				}
  2017  			default:
  2018  				panic(fmt.Errorf("internal error: %T", y))
  2019  			}
  2020  		case uint16:
  2021  			switch y := bv.(type) {
  2022  			case int32:
  2023  				switch {
  2024  				case y > 0:
  2025  					n.Value = x >> uint(y)
  2026  				case y < 0:
  2027  					n.Value = x << uint(-y)
  2028  				default:
  2029  					n.Value = x
  2030  				}
  2031  			case uint32:
  2032  				switch {
  2033  				case y > 0:
  2034  					n.Value = x >> uint(y)
  2035  				default:
  2036  					n.Value = x
  2037  				}
  2038  			case int64:
  2039  				switch {
  2040  				case y > 0:
  2041  					n.Value = x >> uint(y)
  2042  				case y < 0:
  2043  					n.Value = x << uint(-y)
  2044  				default:
  2045  					n.Value = x
  2046  				}
  2047  			case uint64:
  2048  				switch {
  2049  				case y > 0:
  2050  					n.Value = x >> uint(y)
  2051  				default:
  2052  					n.Value = x
  2053  				}
  2054  			default:
  2055  				panic(fmt.Errorf("internal error: %T", y))
  2056  			}
  2057  		case int32:
  2058  			switch y := bv.(type) {
  2059  			case int32:
  2060  				switch {
  2061  				case y > 0:
  2062  					n.Value = x >> uint(y)
  2063  				case y < 0:
  2064  					n.Value = x << uint(-y)
  2065  				default:
  2066  					n.Value = x
  2067  				}
  2068  			case uint32:
  2069  				switch {
  2070  				case y > 0:
  2071  					n.Value = x >> uint(y)
  2072  				default:
  2073  					n.Value = x
  2074  				}
  2075  			case int64:
  2076  				switch {
  2077  				case y > 0:
  2078  					n.Value = x >> uint(y)
  2079  				case y < 0:
  2080  					n.Value = x << uint(-y)
  2081  				default:
  2082  					n.Value = x
  2083  				}
  2084  			case uint64:
  2085  				switch {
  2086  				case y > 0:
  2087  					n.Value = x >> uint(y)
  2088  				default:
  2089  					n.Value = x
  2090  				}
  2091  			default:
  2092  				panic(fmt.Errorf("internal error: %T", y))
  2093  			}
  2094  		case uint32:
  2095  			switch y := bv.(type) {
  2096  			case int32:
  2097  				switch {
  2098  				case y > 0:
  2099  					n.Value = x >> uint(y)
  2100  				case y < 0:
  2101  					n.Value = x << uint(-y)
  2102  				default:
  2103  					n.Value = x
  2104  				}
  2105  			case uint32:
  2106  				switch {
  2107  				case y > 0:
  2108  					n.Value = x >> uint(y)
  2109  				default:
  2110  					n.Value = x
  2111  				}
  2112  			case int64:
  2113  				switch {
  2114  				case y > 0:
  2115  					n.Value = x >> uint(y)
  2116  				case y < 0:
  2117  					n.Value = x << uint(-y)
  2118  				default:
  2119  					n.Value = x
  2120  				}
  2121  			case uint64:
  2122  				switch {
  2123  				case y > 0:
  2124  					n.Value = x >> uint(y)
  2125  				default:
  2126  					n.Value = x
  2127  				}
  2128  			default:
  2129  				panic(fmt.Errorf("internal error: %T", y))
  2130  			}
  2131  		case int64:
  2132  			switch y := bv.(type) {
  2133  			case int32:
  2134  				switch {
  2135  				case y > 0:
  2136  					n.Value = x >> uint(y)
  2137  				case y < 0:
  2138  					n.Value = x << uint(-y)
  2139  				default:
  2140  					n.Value = x
  2141  				}
  2142  			case uint32:
  2143  				switch {
  2144  				case y > 0:
  2145  					n.Value = x >> uint(y)
  2146  				default:
  2147  					n.Value = x
  2148  				}
  2149  			case int64:
  2150  				switch {
  2151  				case y > 0:
  2152  					n.Value = x >> uint(y)
  2153  				case y < 0:
  2154  					n.Value = x << uint(-y)
  2155  				default:
  2156  					n.Value = x
  2157  				}
  2158  			case uint64:
  2159  				switch {
  2160  				case y > 0:
  2161  					n.Value = x >> uint(y)
  2162  				default:
  2163  					n.Value = x
  2164  				}
  2165  			default:
  2166  				panic(fmt.Errorf("internal error: %T", y))
  2167  			}
  2168  		case uint64:
  2169  			switch y := bv.(type) {
  2170  			case int32:
  2171  				switch {
  2172  				case y > 0:
  2173  					n.Value = x >> uint(y)
  2174  				case y < 0:
  2175  					n.Value = x << uint(-y)
  2176  				default:
  2177  					n.Value = x
  2178  				}
  2179  			case uint32:
  2180  				switch {
  2181  				case y > 0:
  2182  					n.Value = x >> uint(y)
  2183  				default:
  2184  					n.Value = x
  2185  				}
  2186  			case int64:
  2187  				switch {
  2188  				case y > 0:
  2189  					n.Value = x >> uint(y)
  2190  				case y < 0:
  2191  					n.Value = x << uint(-y)
  2192  				default:
  2193  					n.Value = x
  2194  				}
  2195  			case uint64:
  2196  				switch {
  2197  				case y > 0:
  2198  					n.Value = x >> uint(y)
  2199  				default:
  2200  					n.Value = x
  2201  				}
  2202  			default:
  2203  				panic(fmt.Errorf("internal error: %T", y))
  2204  			}
  2205  		default:
  2206  			panic(fmt.Errorf("internal error: %T", x))
  2207  		}
  2208  	case 33: // Expression '<' Expression
  2209  		n.Type = m.IntType
  2210  		_, at := n.Expression.eval(lx)
  2211  		_, bt := n.Expression2.eval(lx)
  2212  		a0, b0 := at, bt
  2213  		if at.Kind() > bt.Kind() {
  2214  			at, bt = bt, at
  2215  		}
  2216  		switch {
  2217  		case at.Kind() == Ptr:
  2218  			if bt.Kind() == Array {
  2219  				bt = bt.Element().Pointer()
  2220  			}
  2221  			if !at.CanAssignTo(bt) {
  2222  				lx.report.ErrTok(n.Token, "incompatible types ('%s' < '%s')", a0, b0)
  2223  			}
  2224  			break
  2225  		case IsArithmeticType(at):
  2226  			fallthrough
  2227  		default:
  2228  			n.Type = m.IntType
  2229  			var a, b interface{}
  2230  			a, b, n.BinOpType = m.binOp(lx, n.Expression, n.Expression2)
  2231  			switch x := a.(type) {
  2232  			case nil:
  2233  				// nop
  2234  			case int32:
  2235  				n.Value = m.cBool(x < b.(int32))
  2236  			case uint32:
  2237  				n.Value = m.cBool(x < b.(uint32))
  2238  			case int64:
  2239  				n.Value = m.cBool(x < b.(int64))
  2240  			case uint64:
  2241  				n.Value = m.cBool(x < b.(uint64))
  2242  			case float32:
  2243  				n.Value = m.cBool(x < b.(float32))
  2244  			case float64:
  2245  				n.Value = m.cBool(x < b.(float64))
  2246  			default:
  2247  				panic(fmt.Errorf("internal error: %T", x))
  2248  			}
  2249  		}
  2250  	case 34: // Expression '>' Expression
  2251  		n.Type = m.IntType
  2252  		_, at := n.Expression.eval(lx)
  2253  		_, bt := n.Expression2.eval(lx)
  2254  		a0, b0 := at, bt
  2255  		if at.Kind() > bt.Kind() {
  2256  			at, bt = bt, at
  2257  		}
  2258  		switch {
  2259  		case at.Kind() == Ptr:
  2260  			if bt.Kind() == Array {
  2261  				bt = bt.Element().Pointer()
  2262  			}
  2263  			if !at.CanAssignTo(bt) {
  2264  				lx.report.ErrTok(n.Token, "incompatible types ('%s' > '%s')", a0, b0)
  2265  			}
  2266  			break
  2267  		case IsArithmeticType(at):
  2268  			fallthrough
  2269  		default:
  2270  			n.Type = m.IntType
  2271  			var a, b interface{}
  2272  			a, b, n.BinOpType = m.binOp(lx, n.Expression, n.Expression2)
  2273  			switch x := a.(type) {
  2274  			case nil:
  2275  				// nop
  2276  			case int32:
  2277  				n.Value = m.cBool(x > b.(int32))
  2278  			case int64:
  2279  				n.Value = m.cBool(x > b.(int64))
  2280  			case uint32:
  2281  				n.Value = m.cBool(x > b.(uint32))
  2282  			case uint64:
  2283  				n.Value = m.cBool(x > b.(uint64))
  2284  			case float32:
  2285  				n.Value = m.cBool(x > b.(float32))
  2286  			case float64:
  2287  				n.Value = m.cBool(x > b.(float64))
  2288  			default:
  2289  				panic(fmt.Errorf("internal error: %T", x))
  2290  			}
  2291  		}
  2292  	case 35: // Expression "<=" Expression
  2293  		n.Type = m.IntType
  2294  		_, at := n.Expression.eval(lx)
  2295  		_, bt := n.Expression2.eval(lx)
  2296  		a0, b0 := at, bt
  2297  		if at.Kind() > bt.Kind() {
  2298  			at, bt = bt, at
  2299  		}
  2300  		switch {
  2301  		case at.Kind() == Ptr:
  2302  			if !at.CanAssignTo(bt) {
  2303  				lx.report.ErrTok(n.Token, "incompatible types ('%s' <= '%s')", a0, b0)
  2304  			}
  2305  			break
  2306  		case IsArithmeticType(at):
  2307  			fallthrough
  2308  		default:
  2309  			n.Type = m.IntType
  2310  			var a, b interface{}
  2311  			a, b, n.BinOpType = m.binOp(lx, n.Expression, n.Expression2)
  2312  			switch x := a.(type) {
  2313  			case nil:
  2314  				// nop
  2315  			case int32:
  2316  				n.Value = m.cBool(x <= b.(int32))
  2317  			case uint32:
  2318  				n.Value = m.cBool(x <= b.(uint32))
  2319  			case int64:
  2320  				n.Value = m.cBool(x <= b.(int64))
  2321  			case uint64:
  2322  				n.Value = m.cBool(x <= b.(uint64))
  2323  			case float32:
  2324  				n.Value = m.cBool(x <= b.(float32))
  2325  			case float64:
  2326  				n.Value = m.cBool(x <= b.(float64))
  2327  			default:
  2328  				panic(fmt.Errorf("internal error: %T", x))
  2329  			}
  2330  		}
  2331  	case 36: // Expression ">=" Expression
  2332  		n.Type = m.IntType
  2333  		_, at := n.Expression.eval(lx)
  2334  		_, bt := n.Expression2.eval(lx)
  2335  		a0, b0 := at, bt
  2336  		if at.Kind() > bt.Kind() {
  2337  			at, bt = bt, at
  2338  		}
  2339  		switch {
  2340  		case at.Kind() == Ptr:
  2341  			if bt.Kind() == Array {
  2342  				bt = bt.Element().Pointer()
  2343  			}
  2344  			if !at.CanAssignTo(bt) {
  2345  				lx.report.ErrTok(n.Token, "incompatible types ('%s' >= '%s')", a0, b0)
  2346  			}
  2347  			break
  2348  		case IsArithmeticType(at):
  2349  			fallthrough
  2350  		default:
  2351  			var a, b interface{}
  2352  			a, b, n.BinOpType = m.binOp(lx, n.Expression, n.Expression2)
  2353  			switch x := a.(type) {
  2354  			case nil:
  2355  				// nop
  2356  			case int32:
  2357  				n.Value = m.cBool(x >= b.(int32))
  2358  			case uint32:
  2359  				n.Value = m.cBool(x >= b.(uint32))
  2360  			case int64:
  2361  				n.Value = m.cBool(x >= b.(int64))
  2362  			case uint64:
  2363  				n.Value = m.cBool(x >= b.(uint64))
  2364  			case float32:
  2365  				n.Value = m.cBool(x >= b.(float32))
  2366  			case float64:
  2367  				n.Value = m.cBool(x >= b.(float64))
  2368  			default:
  2369  				panic(fmt.Errorf("internal error: %T", x))
  2370  			}
  2371  		}
  2372  	case 37: // Expression "==" Expression
  2373  		n.Type = m.IntType
  2374  		_, at := n.Expression.eval(lx)
  2375  		_, bt := n.Expression2.eval(lx)
  2376  		a0, b0 := at, bt
  2377  		if at.Kind() > bt.Kind() {
  2378  			at, bt = bt, at
  2379  		}
  2380  		switch {
  2381  		case at.Kind() == Ptr:
  2382  			if IsIntType(bt) {
  2383  				break
  2384  			}
  2385  
  2386  			if bt.Kind() == Array {
  2387  				bt = bt.(*ctype).arrayDecay()
  2388  			}
  2389  			if bt.Kind() == Function && at.Element().Kind() == Function {
  2390  				bt = bt.Pointer()
  2391  			}
  2392  			if !at.CanAssignTo(bt) {
  2393  				lx.report.ErrTok(n.Token, "incompatible types ('%s' == '%s')", a0, b0)
  2394  			}
  2395  			break
  2396  		case at.Kind() == Function && bt.Kind() == Function:
  2397  			// nop
  2398  		case IsArithmeticType(at):
  2399  			fallthrough
  2400  		default:
  2401  			var a, b interface{}
  2402  			a, b, n.BinOpType = m.binOp(lx, n.Expression, n.Expression2)
  2403  			if a == nil {
  2404  				break
  2405  			}
  2406  
  2407  			n.Value = m.cBool(a == b)
  2408  		}
  2409  	case 38: // Expression "!=" Expression
  2410  		n.Type = m.IntType
  2411  		av, at := n.Expression.eval(lx)
  2412  		bv, bt := n.Expression2.eval(lx)
  2413  		if at.Kind() > bt.Kind() {
  2414  			at, bt = bt, at
  2415  		}
  2416  	outer38:
  2417  		switch {
  2418  		case at.Kind() == Ptr:
  2419  			if av != nil && bv != nil {
  2420  				x := av.(uintptr)
  2421  				switch y := bv.(type) {
  2422  				case int32:
  2423  					n.Value = m.cBool(x != uintptr(y))
  2424  					break outer38
  2425  				default:
  2426  					panic(fmt.Errorf("TODO %s: %T %T", position(n.Pos()), av, bv))
  2427  				}
  2428  			}
  2429  
  2430  			if IsIntType(bt) {
  2431  				break
  2432  			}
  2433  
  2434  			if bt.Kind() == Function && at.Element().Kind() == Function {
  2435  				bt = bt.Pointer()
  2436  			}
  2437  			if bt.Kind() == Array {
  2438  				bt = bt.(*ctype).arrayDecay()
  2439  			}
  2440  			if !at.CanAssignTo(bt) {
  2441  				lx.report.ErrTok(n.Token, "incompatible types ('%s' != '%s')", at, bt)
  2442  			}
  2443  			break
  2444  		case IsArithmeticType(at):
  2445  			fallthrough
  2446  		default:
  2447  			var a, b interface{}
  2448  			a, b, n.BinOpType = m.binOp(lx, n.Expression, n.Expression2)
  2449  			if a == nil {
  2450  				break
  2451  			}
  2452  
  2453  			n.Value = m.cBool(a != b)
  2454  		}
  2455  	case 39: // Expression '&' Expression
  2456  		var a, b interface{}
  2457  		a, b, n.Type = m.binOp(lx, n.Expression, n.Expression2)
  2458  		n.BinOpType = n.Type
  2459  		switch x := a.(type) {
  2460  		case nil:
  2461  			// nop
  2462  		case int32:
  2463  			n.Value = x & b.(int32)
  2464  		case uint32:
  2465  			n.Value = x & b.(uint32)
  2466  		case int64:
  2467  			n.Value = x & b.(int64)
  2468  		case uint64:
  2469  			n.Value = x & b.(uint64)
  2470  		default:
  2471  			panic(fmt.Errorf("internal error: %T", x))
  2472  		}
  2473  	case 40: // Expression '^' Expression
  2474  		var a, b interface{}
  2475  		a, b, n.Type = m.binOp(lx, n.Expression, n.Expression2)
  2476  		n.BinOpType = n.Type
  2477  		switch x := a.(type) {
  2478  		case nil:
  2479  			// nop
  2480  		case int32:
  2481  			n.Value = x ^ b.(int32)
  2482  		case uint32:
  2483  			n.Value = x ^ b.(uint32)
  2484  		case int64:
  2485  			n.Value = x ^ b.(int64)
  2486  		case uint64:
  2487  			n.Value = x ^ b.(uint64)
  2488  		default:
  2489  			panic(fmt.Errorf("internal error: %T", x))
  2490  		}
  2491  	case 41: // Expression '|' Expression
  2492  		var a, b interface{}
  2493  		a, b, n.Type = m.binOp(lx, n.Expression, n.Expression2)
  2494  		n.BinOpType = n.Type
  2495  		switch x := a.(type) {
  2496  		case nil:
  2497  			// nop
  2498  		case int32:
  2499  			n.Value = x | b.(int32)
  2500  		case uint32:
  2501  			n.Value = x | b.(uint32)
  2502  		case int64:
  2503  			n.Value = x | b.(int64)
  2504  		case uint64:
  2505  			n.Value = x | b.(uint64)
  2506  		default:
  2507  			panic(fmt.Sprintf("internal error: %T", x))
  2508  		}
  2509  	case 42: // Expression "&&" Expression
  2510  		n.Type = m.IntType
  2511  		a, _ := n.Expression.eval(lx)
  2512  		if a != nil && isZero(a) {
  2513  			n.Value = m.cBool(false)
  2514  			break
  2515  		}
  2516  
  2517  		b, _ := n.Expression2.eval(lx)
  2518  		if a != nil && b != nil {
  2519  			if isZero(b) {
  2520  				n.Value = m.cBool(false)
  2521  				break
  2522  			}
  2523  
  2524  			n.Value = m.cBool(true)
  2525  			break
  2526  		}
  2527  	case 43: // Expression "||" Expression
  2528  		n.Type = m.IntType
  2529  		av, _ := n.Expression.eval(lx)
  2530  		if av != nil && isNonZero(av) {
  2531  			n.Value = m.cBool(true)
  2532  			break
  2533  		}
  2534  
  2535  		bv, _ := n.Expression2.eval(lx)
  2536  		if av != nil && bv != nil {
  2537  			n.Value = m.cBool(isNonZero(bv))
  2538  			break
  2539  		}
  2540  	case 44: // Expression '?' ExpressionList ':' Expression
  2541  		n.cond(lx, n.ExpressionList)
  2542  		break
  2543  	case 45: // Expression '=' Expression
  2544  		_, at := n.Expression.eval(lx)
  2545  		_, bt := n.Expression2.eval(lx)
  2546  		if bt.Kind() == Function {
  2547  			bt = bt.Pointer()
  2548  		}
  2549  		if !bt.CanAssignTo(at) {
  2550  			lx.report.Err(n.Expression2.Pos(), "assignment from incompatible type ('%s' = '%s')", at, bt)
  2551  			break
  2552  		}
  2553  
  2554  		n.Type = at
  2555  		if at.Kind() == Array && bt.Kind() == Ptr {
  2556  			n.Type = bt
  2557  		}
  2558  	case 46: // Expression "*=" Expression
  2559  		_, n.Type = n.Expression.eval(lx)
  2560  		if _, _, n.BinOpType = m.binOp(lx, n.Expression, n.Expression2); n.BinOpType.Kind() == Undefined {
  2561  			lx.report.ErrTok(n.Token, "incompatible types") //TODO have ...
  2562  		}
  2563  	case
  2564  		47, // Expression "/=" Expression
  2565  		48: // Expression "%=" Expression
  2566  		m.checkArithmeticType(lx, n.Expression, n.Expression2)
  2567  		n.Type = n.Expression.Type
  2568  		if v := n.Expression2.Value; v != nil && isZero(v) && IsIntType(n.Type) {
  2569  			lx.report.Err(n.Expression2.Pos(), "division by zero")
  2570  			break
  2571  		}
  2572  
  2573  		if _, _, n.BinOpType = m.binOp(lx, n.Expression, n.Expression2); n.BinOpType.Kind() == Undefined {
  2574  			lx.report.ErrTok(n.Token, "incompatible types") //TODO have ...
  2575  		}
  2576  	case
  2577  		49, // Expression "+=" Expression
  2578  		50: // Expression "-=" Expression
  2579  		_, at := n.Expression.eval(lx)
  2580  		_, bt := n.Expression2.eval(lx)
  2581  		n.Type = at
  2582  		switch {
  2583  		case at.Kind() == Ptr:
  2584  			if IsIntType(bt) || bt.Kind() == Bool {
  2585  				break
  2586  			}
  2587  
  2588  			lx.report.ErrTok(n.Token, "incompatible types") //TODO have ...
  2589  		case IsArithmeticType(at):
  2590  			fallthrough
  2591  		default:
  2592  			if _, _, n.BinOpType = m.binOp(lx, n.Expression, n.Expression2); n.BinOpType.Kind() == Undefined {
  2593  				lx.report.ErrTok(n.Token, "incompatible types") //TODO have ...
  2594  			}
  2595  		}
  2596  	case
  2597  		51, // Expression "<<=" Expression
  2598  		52: // Expression ">>=" Expression
  2599  		m.checkIntegerOrBoolType(lx, n.Expression, n.Expression2)
  2600  		n.Type = n.Expression.Type
  2601  	case
  2602  		53, // Expression "&=" Expression
  2603  		54, // Expression "^=" Expression
  2604  		55: // Expression "|=" Expression
  2605  		m.checkIntegerOrBoolType(lx, n.Expression, n.Expression2)
  2606  		if _, _, n.BinOpType = m.binOp(lx, n.Expression, n.Expression2); n.BinOpType.Kind() == Undefined {
  2607  			lx.report.ErrTok(n.Token, "incompatible types") //TODO have ...
  2608  		}
  2609  		n.Type = n.BinOpType
  2610  	case 56: // "_Alignof" '(' TypeName ')'
  2611  		n.Type = lx.model.getSizeType(lx)
  2612  		t := n.TypeName.Type
  2613  		el := true
  2614  	again:
  2615  		switch t.Kind() {
  2616  		case Undefined, Function:
  2617  			t = nil
  2618  		case Struct, Union:
  2619  			if _, isIncomplete := t.Members(); isIncomplete {
  2620  				t = nil
  2621  				break
  2622  			}
  2623  		case Array:
  2624  			if el {
  2625  				el = false
  2626  				t = t.Element()
  2627  				goto again
  2628  			}
  2629  		}
  2630  		if t == nil {
  2631  			lx.report.Err(n.TypeName.Pos(), "invalid argument of _Alignof")
  2632  			n.Value = lx.model.MustConvert(1, n.Type)
  2633  			break
  2634  		}
  2635  
  2636  		al := t.AlignOf()
  2637  		if al < 0 {
  2638  			lx.report.Err(n.TypeName.Pos(), "invalid argument of _Alignof")
  2639  			al = 1
  2640  		}
  2641  		n.Value = lx.model.MustConvert(int32(al), n.Type)
  2642  	case 57: // '(' CompoundStatement ')'                          // Case 57
  2643  		if !lx.tweaks.enableParenCompoundStmt {
  2644  			lx.report.Err(n.Pos(), "non-standard parenthesized compound statement as expression not enabled")
  2645  			break
  2646  		}
  2647  
  2648  		n.Type = lx.model.VoidType
  2649  		o := n.CompoundStatement.BlockItemListOpt
  2650  		if o == nil {
  2651  			break
  2652  		}
  2653  
  2654  		var last *BlockItem
  2655  		for l := o.BlockItemList; l != nil; l = l.BlockItemList {
  2656  			if l.BlockItemList == nil {
  2657  				last = l.BlockItem
  2658  			}
  2659  		}
  2660  
  2661  		if last == nil {
  2662  			break
  2663  		}
  2664  
  2665  		switch last.Case {
  2666  		case 0: // Declaration
  2667  			// nop
  2668  		case 1: // Statement    // Case 1
  2669  			if es := last.Statement.ExpressionStatement; es != nil {
  2670  				o := es.ExpressionListOpt
  2671  				if o != nil {
  2672  					el := o.ExpressionList
  2673  					n.Type, n.Value = el.Type, el.Value
  2674  				}
  2675  			}
  2676  		default:
  2677  			panic("internal error")
  2678  		}
  2679  	case 58: // "&&" IDENTIFIER                                    // Case 58
  2680  		n.Type = lx.model.VoidType.Pointer()
  2681  		n.Value = ComputedGotoID(n.Token2.Val)
  2682  	case 59: // Expression '?' ':' Expression                      // Case 59
  2683  		n.cond(lx, n.Expression)
  2684  	default:
  2685  		//dbg("", PrettyString(n))
  2686  		panic(fmt.Errorf("%s: internal error: Expression.Case: %v", position(n.Pos()), n.Case))
  2687  	}
  2688  	//ct := n.Type.(*ctype)
  2689  	//s := ""
  2690  	//if n.Value != nil {
  2691  	//	s = fmt.Sprintf("value: %T(%#v)", n.Value, n.Value)
  2692  	//}
  2693  	//dbg("tc %v %v %v %v %v: %v %v", position(n.Pos()), n.Case, ct.resultStars, ct.stars, ct, ct.Kind(), s)
  2694  	return n.Value, n.Type
  2695  }
  2696  
  2697  // IdentResolutionScope returns the scope an identifier is resolved in. If n is
  2698  // not an identifier (n.Case == 0), IdentResolutionScope returns nil.
  2699  func (n *Expression) IdentResolutionScope() *Bindings {
  2700  	if n.Case == 0 { // IDENTIFIER
  2701  		return n.scope
  2702  	}
  2703  
  2704  	return nil
  2705  }
  2706  
  2707  // ------------------------------------------------------------- ExpressionList
  2708  
  2709  func (n *ExpressionList) eval(lx *lexer) (interface{}, Type) {
  2710  	if n.Type != nil {
  2711  		return n.Value, n.Type
  2712  	}
  2713  
  2714  	n0 := n
  2715  	for ; n != nil; n = n.ExpressionList {
  2716  		n.Value, n.Type = n.Expression.eval(lx)
  2717  		n0.Value, n0.Type = n.Value, n.Type
  2718  	}
  2719  	return n0.Value, n0.Type
  2720  }
  2721  
  2722  // Len returns the number of items in n.
  2723  func (n *ExpressionList) Len() (r int) {
  2724  	for ; n != nil; n = n.ExpressionList {
  2725  		r++
  2726  	}
  2727  	return r
  2728  }
  2729  
  2730  // --------------------------------------------------------- FunctionDefinition
  2731  
  2732  func (*FunctionDefinition) post(lx *lexer, d *Declarator, dlo *DeclarationListOpt) {
  2733  	lx.scope.mergeScope = nil
  2734  	done := false
  2735  	for dd := d.DirectDeclarator.bottom(); !done && dd != nil; dd = dd.parent {
  2736  		switch dd.Case {
  2737  		case 6: // DirectDeclarator '(' ParameterTypeList ')'
  2738  			done = true
  2739  			lx.scope.mergeScope = dd.paramsScope
  2740  			if dlo != nil {
  2741  				lx.report.Err(dlo.Pos(), "declaration list not allowed in a function definition with parameter type list")
  2742  			}
  2743  		case 7: // DirectDeclarator '(' IdentifierListOpt ')'
  2744  			done = true
  2745  			ilo := dd.IdentifierListOpt
  2746  			if ilo != nil && dlo == nil {
  2747  				if !lx.tweaks.enableOmitFuncArgTypes {
  2748  					lx.report.Err(ilo.Pos(), "missing parameter declaration list")
  2749  					break
  2750  				}
  2751  
  2752  				lx.pushScope(ScopeParams)
  2753  				for l := ilo.IdentifierList; l != nil; l = l.IdentifierList {
  2754  					tok := l.Token
  2755  					if l.Case == 1 {
  2756  						tok = l.Token2
  2757  					}
  2758  					d := lx.model.makeDeclarator(0, tsInt)
  2759  					d.Type = lx.model.IntType
  2760  					lx.scope.declareIdentifier(tok, d.DirectDeclarator, lx.report)
  2761  					ilo.params = append(ilo.params, Parameter{d, tok.Val, d.Type})
  2762  				}
  2763  				lx.scope.mergeScope, _ = lx.popScope(dd.Token2)
  2764  				break
  2765  			}
  2766  
  2767  			if ilo == nil {
  2768  				if dlo != nil {
  2769  					lx.report.Err(dlo.Pos(), "unexpected parameter declaration list")
  2770  				}
  2771  				break
  2772  			}
  2773  
  2774  			// ilo != nil && dlo != nil
  2775  			lx.scope.mergeScope = dlo.paramsScope
  2776  			ilo.post(lx, dlo)
  2777  		}
  2778  	}
  2779  	d.setFull(lx)
  2780  	if !done {
  2781  		lx.report.Err(d.Pos(), "declarator is not a function (have '%s': %v)", d.Type, d.Type.Kind())
  2782  	}
  2783  	lx.fnDeclarator = d
  2784  }
  2785  
  2786  // ---------------------------------------------------------- IdentifierListOpt
  2787  
  2788  func (n *IdentifierListOpt) post(lx *lexer, dlo *DeclarationListOpt) {
  2789  	type r struct {
  2790  		pos token.Pos
  2791  		i   int
  2792  	}
  2793  	var a []xc.Token
  2794  	ilm := map[int]r{}
  2795  	i := 0
  2796  	for il := n.IdentifierList; il != nil; il, i = il.IdentifierList, i+1 {
  2797  		t := il.Token
  2798  		if il.Case == 1 {
  2799  			t = il.Token2
  2800  		}
  2801  		nm := t.Val
  2802  		if r, ok := ilm[nm]; ok {
  2803  			lx.report.ErrTok(t, "duplicate parameter name declaration, previous at %s", position(r.pos))
  2804  			continue
  2805  		}
  2806  
  2807  		v := r{t.Pos(), i}
  2808  		ilm[nm] = v
  2809  		a = append(a, t)
  2810  	}
  2811  	params := make([]Parameter, len(ilm))
  2812  	if dlo != nil {
  2813  		for dl := dlo.DeclarationList; dl != nil; dl = dl.DeclarationList {
  2814  			decl := dl.Declaration
  2815  			o := decl.InitDeclaratorListOpt
  2816  			if o == nil {
  2817  				lx.report.Err(decl.Pos(), "invalid parameter declaration")
  2818  				continue
  2819  			}
  2820  
  2821  			for l := o.InitDeclaratorList; l != nil; l = l.InitDeclaratorList {
  2822  				id := l.InitDeclarator
  2823  				if id.Case == 1 { // Declarator '=' Initializer
  2824  					lx.report.Err(id.Pos(), "invalid parameter declarator")
  2825  				}
  2826  
  2827  				d := id.Declarator
  2828  				nm, _ := d.Identifier()
  2829  				r, ok := ilm[nm]
  2830  				if !ok {
  2831  					lx.report.Err(d.Pos(), "parameter name not declared")
  2832  					continue
  2833  				}
  2834  
  2835  				params[r.i] = Parameter{d, nm, d.Type}
  2836  			}
  2837  		}
  2838  	}
  2839  	for i, v := range params {
  2840  		if v.Declarator == nil {
  2841  			tok := a[i]
  2842  			d := lx.model.makeDeclarator(0, tsInt)
  2843  			d.Type = lx.model.IntType
  2844  			dlo.paramsScope.declareIdentifier(tok, d.DirectDeclarator, lx.report)
  2845  			params[i] = Parameter{d, tok.Val, d.Type}
  2846  		}
  2847  	}
  2848  	n.params = params
  2849  	fixParams(n.params)
  2850  }
  2851  
  2852  // ---------------------------------------------------------------- Initializer
  2853  
  2854  func (n *Initializer) typeCheck(pt *Type, dt Type, static bool, lx *lexer) {
  2855  	static = static && !lx.tweaks.enableNonConstStaticInitExpressions
  2856  	if dt == nil {
  2857  		return
  2858  	}
  2859  
  2860  	k := dt.Kind()
  2861  	d := dt.Declarator()
  2862  	dd := d.DirectDeclarator
  2863  	if dd.isArray() && dd.isVLA() != nil {
  2864  		lx.report.Err(n.Pos(), "variable length array cannot have initializers")
  2865  		return
  2866  	}
  2867  
  2868  	switch n.Case {
  2869  	case 0: // Expression
  2870  		x := n.Expression
  2871  		xt := n.Expression.Type
  2872  		switch v := x.Value.(type) {
  2873  		case StringLitID:
  2874  			switch k {
  2875  			case Array, Ptr:
  2876  				switch dt.Element().Kind() {
  2877  				case Char, SChar, UChar:
  2878  					if pt != nil && dd.isArray() && dt.Elements() < 0 {
  2879  						*pt = dt.(*ctype).setElements(len(xc.Dict.S(int(v))) + 1)
  2880  					}
  2881  				default:
  2882  					if !xt.CanAssignTo(dt) {
  2883  						lx.report.Err(x.Pos(), "cannot initialize type '%v' using expression of type '%v'", dt, xt)
  2884  					}
  2885  				}
  2886  			default:
  2887  				if !xt.CanAssignTo(dt) {
  2888  					lx.report.Err(x.Pos(), "cannot initialize type '%v' using expression of type '%v'", dt, xt)
  2889  				}
  2890  			}
  2891  			return
  2892  		case LongStringLitID:
  2893  			switch k {
  2894  			case Array, Ptr:
  2895  				switch dt.Element().Kind() {
  2896  				case Short, UShort, Int, UInt, Long, ULong:
  2897  					if pt != nil && dd.isArray() && dt.Elements() < 0 {
  2898  						*pt = dt.(*ctype).setElements(len([]rune(string(xc.Dict.S(int(v))))) + 1)
  2899  					}
  2900  				default:
  2901  					if !xt.CanAssignTo(dt) {
  2902  						lx.report.Err(x.Pos(), "cannot initialize type '%v' using expression of type '%v'", dt, xt)
  2903  					}
  2904  				}
  2905  			default:
  2906  				if !xt.CanAssignTo(dt) {
  2907  					lx.report.Err(x.Pos(), "cannot initialize type '%v' using expression of type '%v'", dt, xt)
  2908  				}
  2909  			}
  2910  			return
  2911  		case nil:
  2912  			if static {
  2913  				switch x.Case {
  2914  				case 0: // IDENTIFIER
  2915  					if xt.Kind() == Array && xt.CanAssignTo(dt) {
  2916  						break
  2917  					}
  2918  
  2919  					if xt.Kind() == Function && xt.Pointer().CanAssignTo(dt) {
  2920  						break
  2921  					}
  2922  
  2923  					lx.report.Err(x.Pos(), "cannot initialize type '%v' using expression of type '%v'", dt, xt)
  2924  				case 17: // '&' Expression                                     // Case 17
  2925  					if !xt.CanAssignTo(dt) {
  2926  						lx.report.Err(x.Pos(), "cannot initialize type '%v' using expression of type '%v'", dt, xt)
  2927  					}
  2928  				default:
  2929  					lx.report.Err(x.Pos(), "expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.")
  2930  				}
  2931  				return
  2932  			}
  2933  
  2934  		}
  2935  
  2936  		if !xt.CanAssignTo(dt) {
  2937  			//dbg("", dt, xt)
  2938  			if dt.Kind() == Struct || dt.Kind() == Union {
  2939  				if ma, _ := dt.Members(); len(ma) == 1 {
  2940  					//dbg("")
  2941  					n.typeCheck(nil, ma[0].Type, static, lx)
  2942  					//dbg("")
  2943  					return
  2944  				}
  2945  			}
  2946  
  2947  			if dt.Kind() == Array {
  2948  				n.typeCheck(nil, dt.Element(), static, lx)
  2949  				return
  2950  			}
  2951  
  2952  			lx.report.Err(x.Pos(), "cannot initialize type '%v' using expression of type '%v'", dt, xt)
  2953  			return
  2954  		}
  2955  	case 1: // '{' InitializerList CommaOpt '}'  // Case 1
  2956  		n.InitializerList.typeCheck(pt, dt, static, lx)
  2957  	case 2: // IDENTIFIER ':' Initializer        // Case 2
  2958  		p := *pt
  2959  		if p.Kind() != Struct && dt.Kind() != Union {
  2960  			lx.report.Err(n.Pos(), "invalid designator for type %v", dt)
  2961  			break
  2962  		}
  2963  
  2964  		m, err := p.Member(n.Token.Val)
  2965  		if err != nil {
  2966  			lx.report.Err(n.Pos(), "type %v has no member %s: %v", p, dict.S(n.Token.Val), err)
  2967  			break
  2968  		}
  2969  
  2970  		n.InitializerList.typeCheck(&p, m.Type, static, lx)
  2971  	default:
  2972  		panic("internal error")
  2973  	}
  2974  }
  2975  
  2976  // ------------------------------------------------------------ InitializerList
  2977  
  2978  // Len returns the number of items in n.
  2979  func (n *InitializerList) Len() (r int) {
  2980  	for ; n != nil; n = n.InitializerList {
  2981  		r++
  2982  	}
  2983  	return r
  2984  }
  2985  
  2986  func (n *InitializerList) typeCheck(pt *Type, dt Type, static bool, lx *lexer) {
  2987  	if n == nil || dt == nil {
  2988  		return
  2989  	}
  2990  
  2991  	d := dt.Declarator()
  2992  	dd := d.DirectDeclarator
  2993  	switch dt.Kind() {
  2994  	case Struct, Union:
  2995  		ma, incomplete := dt.Members()
  2996  		if incomplete {
  2997  			lx.report.Err(n.Pos(), "cannot initialize incomplete type")
  2998  			return
  2999  		}
  3000  
  3001  		if len(ma) == 1 {
  3002  			//dbg("%s: %v -> %v", position(n.Pos()), dt, ma[0].Type)
  3003  			n.InitializerList.typeCheck(nil, ma[0].Type, static, lx)
  3004  			return
  3005  		}
  3006  
  3007  		i := 0
  3008  		var stack []int
  3009  		for l := n; l != nil; l = l.InitializerList {
  3010  			var m Member
  3011  			switch o := l.DesignationOpt; {
  3012  			case o != nil:
  3013  				ma := ma
  3014  				j := 0
  3015  				for l := o.Designation.DesignatorList; l != nil; l = l.DesignatorList {
  3016  					switch d := l.Designator; d.Case {
  3017  					case 0: // '[' ConstantExpression ']'
  3018  						panic("TODO")
  3019  					case 1: // '.' IDENTIFIER              // Case 1
  3020  						if j != 0 {
  3021  							ma, _ = m.Type.Members()
  3022  						}
  3023  						found := false
  3024  						for k, v := range ma {
  3025  							if d.Token2.Val == v.Name {
  3026  								found = true
  3027  								m = v
  3028  								if j == 0 {
  3029  									i = k
  3030  								}
  3031  								break
  3032  							}
  3033  						}
  3034  						if !found {
  3035  							panic("TODO")
  3036  						}
  3037  
  3038  						j++
  3039  					default:
  3040  						panic("internal error")
  3041  					}
  3042  				}
  3043  			default:
  3044  				if i >= len(ma) {
  3045  					//dbg("", i, len(ma))
  3046  					panic("TODO")
  3047  				}
  3048  
  3049  				switch l := len(stack); {
  3050  				case l != 0:
  3051  					stack[l-1]--
  3052  					if stack[l-1] != 0 {
  3053  						i--
  3054  						break
  3055  					}
  3056  
  3057  					stack = stack[:l-1]
  3058  					i++
  3059  					fallthrough
  3060  				default:
  3061  					m = ma[i]
  3062  					if mt := m.Type; mt.Kind() == Array && mt.Elements() >= 0 {
  3063  						stack = append(stack, mt.Elements())
  3064  						i--
  3065  					}
  3066  				}
  3067  			}
  3068  			p := dt
  3069  			l.Initializer.typeCheck(&p, m.Type, static, lx)
  3070  			i++
  3071  		}
  3072  	case Array, Ptr:
  3073  		elems := dt.Elements()
  3074  		elem := dt.Element()
  3075  		elem0 := elem
  3076  		for elem0.Kind() == Array {
  3077  			n := elem0.Elements()
  3078  			if n >= 0 {
  3079  				if elems < 0 {
  3080  					elems = 1
  3081  				}
  3082  				elems *= n
  3083  			}
  3084  			elem0 = elem0.Element()
  3085  		}
  3086  		i := 0
  3087  		for l := n; l != nil; l = l.InitializerList {
  3088  			if o := l.DesignationOpt; o != nil {
  3089  				elem := elem
  3090  				j := 0
  3091  				m := lx.model
  3092  				for l := o.Designation.DesignatorList; l != nil; l = l.DesignatorList {
  3093  					switch d := l.Designator; d.Case {
  3094  					case 0: // '[' ConstantExpression ']'
  3095  						if !IsIntType(d.ConstantExpression.Type) {
  3096  							panic("TODO")
  3097  						}
  3098  
  3099  						switch {
  3100  						case j == 0:
  3101  							i = int(m.MustConvert(d.ConstantExpression.Value, m.IntType).(int32))
  3102  						default:
  3103  							elem = elem.Element()
  3104  						}
  3105  						j++
  3106  					case 1: // '.' IDENTIFIER              // Case 1
  3107  						panic("TODO")
  3108  					default:
  3109  						panic("internal error")
  3110  					}
  3111  				}
  3112  			}
  3113  
  3114  			if elems >= 0 && i >= elems {
  3115  				panic("TODO")
  3116  			}
  3117  
  3118  			switch in := l.Initializer; in.Case {
  3119  			case 0: // Expression
  3120  				in.typeCheck(nil, elem0, static, lx)
  3121  				i++
  3122  			case 1: // '{' InitializerList CommaOpt '}'  // Case 1
  3123  				if !elem.Declarator().DirectDeclarator.isArray() {
  3124  					panic("TODO")
  3125  				}
  3126  
  3127  				in.InitializerList.typeCheck(nil, elem, static, lx)
  3128  				i++
  3129  			default:
  3130  				panic("internal error")
  3131  			}
  3132  		}
  3133  		if pt != nil && dd.isArray() && elems < 0 {
  3134  			//dbg("", position(n.Pos()), elem, elems)
  3135  			*pt = dt.(*ctype).setElements(i)
  3136  		}
  3137  	default:
  3138  		i := 0
  3139  		for l := n; l != nil; l = l.InitializerList {
  3140  			if i != 0 {
  3141  				//dbg("%s: %v", position(n.Pos()), dt)
  3142  				panic("TODO")
  3143  			}
  3144  
  3145  			l.Initializer.typeCheck(nil, dt, static, lx)
  3146  			i++
  3147  		}
  3148  	}
  3149  }
  3150  
  3151  // ---------------------------------------------------------- ParameterTypeList
  3152  
  3153  func (n *ParameterTypeList) post() {
  3154  	for l := n.ParameterList; l != nil; l = l.ParameterList {
  3155  		d := l.ParameterDeclaration.declarator
  3156  		nm, _ := d.Identifier()
  3157  		t := d.Type
  3158  		n.params = append(n.params, Parameter{
  3159  			Declarator: d,
  3160  			Name:       nm,
  3161  			Type:       t,
  3162  		})
  3163  	}
  3164  	if len(n.params) == 1 && n.params[0].Type.Kind() == Void {
  3165  		n.params = make([]Parameter, 0) // Must be non nil.
  3166  	}
  3167  	fixParams(n.params)
  3168  }
  3169  
  3170  // -------------------------------------------------------------------- Pointer
  3171  
  3172  func (n *Pointer) stars() (r int) {
  3173  	for ; n != nil; n = n.Pointer {
  3174  		r++
  3175  	}
  3176  	return r
  3177  }
  3178  
  3179  // ----------------------------------------------------------------- PointerOpt
  3180  
  3181  func (n *PointerOpt) stars() int {
  3182  	if n == nil {
  3183  		return 0
  3184  	}
  3185  
  3186  	return n.Pointer.stars()
  3187  }
  3188  
  3189  // ----------------------------------------------------- SpecifierQualifierList
  3190  
  3191  func (n *SpecifierQualifierList) isCompatible(m *SpecifierQualifierList) bool {
  3192  	if n.typeSpecifier != m.typeSpecifier {
  3193  		return false
  3194  	}
  3195  
  3196  	switch n.TypeSpecifier.Case {
  3197  	case 11: // StructOrUnionSpecifier       // Case 11
  3198  		return true //TODO nil deref panic: return m.TypeQualifier.Case == 11 && n.TypeSpecifier.StructOrUnionSpecifier.isCompatible(m.TypeSpecifier.StructOrUnionSpecifier)
  3199  	default:
  3200  		return true
  3201  	}
  3202  }
  3203  
  3204  // IsInline implements specifier.
  3205  func (n *SpecifierQualifierList) IsInline() bool {
  3206  	return n.attr&saInline != 0
  3207  }
  3208  
  3209  // IsTypedef implements specifier.
  3210  func (n *SpecifierQualifierList) IsTypedef() bool {
  3211  	return n.attr&saTypedef != 0
  3212  }
  3213  
  3214  // IsExtern implements specifier.
  3215  func (n *SpecifierQualifierList) IsExtern() bool {
  3216  	return n.attr&saExtern != 0
  3217  }
  3218  
  3219  // IsStatic implements specifier.
  3220  func (n *SpecifierQualifierList) IsStatic() bool {
  3221  	return n.attr&saStatic != 0
  3222  }
  3223  
  3224  // IsAuto implements specifier.
  3225  func (n *SpecifierQualifierList) IsAuto() bool {
  3226  	return n.attr&saAuto != 0
  3227  }
  3228  
  3229  // IsRegister implements specifier.
  3230  func (n *SpecifierQualifierList) IsRegister() bool {
  3231  	return n.attr&saRegister != 0
  3232  }
  3233  
  3234  // IsConst returns whether n includes the 'const' type qualifier.
  3235  func (n *SpecifierQualifierList) IsConst() bool {
  3236  	return n.attr&saConst != 0
  3237  }
  3238  
  3239  // IsRestrict implements specifier.
  3240  func (n *SpecifierQualifierList) IsRestrict() bool {
  3241  	return n.attr&saRestrict != 0
  3242  }
  3243  
  3244  // IsVolatile implements specifier.
  3245  func (n *SpecifierQualifierList) IsVolatile() bool {
  3246  	return n.attr&saVolatile != 0
  3247  }
  3248  
  3249  // kind implements specifier.
  3250  func (n *SpecifierQualifierList) kind() Kind { return tsValid[n.typeSpecifiers()] }
  3251  
  3252  // typeSpecifiers implements specifier.
  3253  func (n *SpecifierQualifierList) typeSpecifiers() int {
  3254  	return n.typeSpecifier
  3255  }
  3256  
  3257  // firstTypeSpecifier implements specifier.
  3258  func (n *SpecifierQualifierList) firstTypeSpecifier() *TypeSpecifier {
  3259  	for n.Case != 0 { // TypeSpecifier SpecifierQualifierListOpt
  3260  		o := n.SpecifierQualifierListOpt
  3261  		if o == nil {
  3262  			return nil
  3263  		}
  3264  
  3265  		n = o.SpecifierQualifierList
  3266  	}
  3267  	return n.TypeSpecifier
  3268  }
  3269  
  3270  // attrs implements specifier.
  3271  func (n *SpecifierQualifierList) attrs() int { return n.attr }
  3272  
  3273  // member implements specifier.
  3274  func (n *SpecifierQualifierList) member(nm int) (*Member, error) {
  3275  	return n.firstTypeSpecifier().member(nm)
  3276  }
  3277  
  3278  // str implements specifier.
  3279  func (n *SpecifierQualifierList) str() string {
  3280  	return specifierString(n)
  3281  }
  3282  
  3283  // TypedefName implements Specifier.
  3284  func (n *SpecifierQualifierList) TypedefName() int {
  3285  	if n.kind() == TypedefName {
  3286  		return n.firstTypeSpecifier().Token.Val
  3287  	}
  3288  	return 0
  3289  }
  3290  
  3291  // ----------------------------------------------------------- StructDeclarator
  3292  
  3293  func (n *StructDeclarator) post(lx *lexer) {
  3294  	sc := lx.scope
  3295  	switch n.Case {
  3296  	case 0: // Declarator
  3297  		if sc.bitOffset != 0 {
  3298  			finishBitField(n, lx)
  3299  		}
  3300  
  3301  		t := n.Declarator.Type
  3302  		sz := t.sizeOf(lx)
  3303  		al := t.structAlignOf(lx)
  3304  		switch {
  3305  		case sc.isUnion:
  3306  			// Track union size.
  3307  			sc.maxSize = mathutil.Max(sc.maxSize, sz)
  3308  		default:
  3309  			off := sc.offset
  3310  			sc.offset = align(sc.offset, al) // Bump offset if necessary.
  3311  			if pd := sc.prevStructDeclarator; pd != nil {
  3312  				pd.padding = sc.offset - off
  3313  			}
  3314  			n.Declarator.offsetOf = sc.offset
  3315  			sc.offset += sz // Allocate sz.
  3316  		}
  3317  		sc.maxAlign = mathutil.Max(sc.maxAlign, al)
  3318  		sc.prevStructDeclarator = n.Declarator
  3319  	case 1: // DeclaratorOpt ':' ConstantExpression
  3320  		t := lx.model.IntType
  3321  		if o := n.DeclaratorOpt; o != nil {
  3322  			t = o.Declarator.Type
  3323  		}
  3324  
  3325  		var w int
  3326  		switch x := n.ConstantExpression.Value.(type) {
  3327  		case int32:
  3328  			w = int(x)
  3329  		case int64:
  3330  			w = int(x)
  3331  			if m := t.sizeOf(lx) * 8; x > int64(m) {
  3332  				lx.report.Err(n.ConstantExpression.Pos(), "width of bit field exceeds its type")
  3333  				w = m
  3334  			}
  3335  		case uint64:
  3336  			w = int(x)
  3337  			m := t.sizeOf(lx) * 8
  3338  			if x > uint64(m) {
  3339  				lx.report.Err(n.ConstantExpression.Pos(), "width of bit field exceeds its type")
  3340  				w = m
  3341  				break
  3342  			}
  3343  
  3344  			if x > uint64(lx.model.Items[Int].Size*8) {
  3345  				lx.report.Err(n.ConstantExpression.Pos(), "width of bit field exceeds int bits")
  3346  				w = m
  3347  				break
  3348  			}
  3349  		default:
  3350  			panic("internal error")
  3351  		}
  3352  		if m := t.sizeOf(lx) * 8; w > m {
  3353  			lx.report.Err(n.ConstantExpression.Pos(), "width of bit field exceeds its type")
  3354  			w = m
  3355  		}
  3356  		maxLLBits := lx.model.LongLongType.sizeOf(lx) * 8
  3357  		maxBits := lx.model.LongType.sizeOf(lx) * 8
  3358  		if sum := sc.bitOffset + w; sum > maxBits {
  3359  			if sum > maxLLBits || w <= maxBits {
  3360  				finishBitField(n, lx)
  3361  			}
  3362  		}
  3363  		if o := n.DeclaratorOpt; o != nil {
  3364  			d := o.Declarator
  3365  			d.offsetOf = sc.offset
  3366  			d.bitOffset = sc.bitOffset
  3367  			d.bitFieldGroup = sc.bitFieldGroup
  3368  			sc.prevStructDeclarator = o.Declarator
  3369  			t = d.Type
  3370  			switch t.Kind() {
  3371  			case Char, SChar, UChar, Int, UInt, Long, ULong, Short, UShort, Enum, Bool:
  3372  				// ok
  3373  			case LongLong, ULongLong:
  3374  				if lx.tweaks.enableWideBitFieldTypes {
  3375  					// Non-standard, but enabled.
  3376  					break
  3377  				}
  3378  				lx.report.Err(n.ConstantExpression.Pos(), "bit field has invalid type (have %s)", t)
  3379  				t = lx.model.IntType
  3380  			default:
  3381  				lx.report.Err(n.ConstantExpression.Pos(), "bit field has invalid type (have %s)", t)
  3382  				t = lx.model.IntType
  3383  			}
  3384  		}
  3385  		sc.bitOffset += w
  3386  	default:
  3387  		panic(n.Case)
  3388  	}
  3389  }
  3390  
  3391  func (n *StructDeclarator) isCompatible(m *StructDeclarator) bool {
  3392  	if n.Case != m.Case {
  3393  		return false
  3394  	}
  3395  
  3396  	switch n.Case {
  3397  	case 0: // Declarator
  3398  		return n.Declarator.isCompatible(m.Declarator)
  3399  	case 1: //  DeclaratorOpt ':' ConstantExpression  // Case 1
  3400  		ty1 := n.ConstantExpression.Expression.Type.(*ctype)
  3401  		ty2 := m.ConstantExpression.Expression.Type.(*ctype)
  3402  		return n.DeclaratorOpt.isCompatible(m.DeclaratorOpt) && ty1.isCompatible(ty2)
  3403  	default:
  3404  		panic(fmt.Errorf("%s: internal error", position(n.Pos())))
  3405  	}
  3406  }
  3407  
  3408  // -------------------------------------------------------------- StructDeclaratorList
  3409  
  3410  func (n *StructDeclaratorList) isCompatible(m *StructDeclaratorList) bool {
  3411  	for ; n != nil; n = n.StructDeclaratorList {
  3412  		if m == nil {
  3413  			return false
  3414  		}
  3415  
  3416  		sda := n.StructDeclarator
  3417  		sdb := m.StructDeclarator
  3418  		if !sda.isCompatible(sdb) {
  3419  			return false
  3420  		}
  3421  		m = m.StructDeclaratorList
  3422  	}
  3423  	if m != nil {
  3424  		return false
  3425  	}
  3426  	return true
  3427  }
  3428  
  3429  // -------------------------------------------------------------- StructOrUnion
  3430  
  3431  func (n *StructOrUnion) typeSpecifiers() int {
  3432  	switch n.Token.Rune {
  3433  	case STRUCT:
  3434  		return tsStructSpecifier
  3435  	case UNION:
  3436  		return tsUnionSpecifier
  3437  	default:
  3438  		panic("internal error")
  3439  	}
  3440  }
  3441  
  3442  func (n *StructOrUnion) isCompatible(m *StructOrUnion) (r bool) {
  3443  	return n == m || n.Case == m.Case
  3444  }
  3445  
  3446  func (n *StructOrUnion) str() string {
  3447  	switch n.Token.Rune {
  3448  	case STRUCT:
  3449  		return "struct"
  3450  	case UNION:
  3451  		return "union"
  3452  	default:
  3453  		panic("internal error")
  3454  	}
  3455  }
  3456  
  3457  // ----------------------------------------------------- StructOrUnionSpecifier
  3458  
  3459  // Declarator returns a synthetic Declarator when a tagged struc/union type is
  3460  // defined inline a declaration.
  3461  func (n *StructOrUnionSpecifier) Declarator() *Declarator { return n.declarator }
  3462  
  3463  func (n *StructOrUnionSpecifier) typeSpecifiers() int { return n.StructOrUnion.typeSpecifiers() }
  3464  
  3465  func (n *StructOrUnionSpecifier) isCompatible(m *StructOrUnionSpecifier) (r bool) {
  3466  	if n == m {
  3467  		return true
  3468  	}
  3469  
  3470  	if !n.StructOrUnion.isCompatible(m.StructOrUnion) {
  3471  		return false
  3472  	}
  3473  
  3474  	if n.Case > m.Case {
  3475  		n, m = m, n
  3476  	}
  3477  	switch n.Case {
  3478  	case 0: // StructOrUnion IdentifierOpt '{' StructDeclarationList '}'
  3479  		switch m.Case {
  3480  		case 0: // StructOrUnion IdentifierOpt '{' StructDeclarationList '}'
  3481  			b := m.StructDeclarationList
  3482  			for a := n.StructDeclarationList; a != nil; a = a.StructDeclarationList {
  3483  				if b == nil {
  3484  					return false
  3485  				}
  3486  
  3487  				sda := a.StructDeclaration
  3488  				sdb := b.StructDeclaration
  3489  				if sda.Case != sdb.Case {
  3490  					return false
  3491  				}
  3492  
  3493  				switch sda.Case {
  3494  				case 0: // SpecifierQualifierList StructDeclaratorList ';'
  3495  					if !sda.StructDeclaratorList.isCompatible(sdb.StructDeclaratorList) {
  3496  						return false
  3497  					}
  3498  				case 1: // SpecifierQualifierList ';'                       // Case 1
  3499  					switch sdb.Case {
  3500  					case 1: // SpecifierQualifierList ';'                       // Case 1
  3501  						if !sda.SpecifierQualifierList.isCompatible(sdb.SpecifierQualifierList) {
  3502  							return false
  3503  						}
  3504  					default:
  3505  						return false
  3506  					}
  3507  				case 2: // StaticAssertDeclaration                          // Case 2
  3508  					panic(fmt.Errorf("%s: TODO", position(n.Pos())))
  3509  				default:
  3510  					panic(fmt.Errorf("%s: internal error", position(n.Pos())))
  3511  				}
  3512  
  3513  				b = b.StructDeclarationList
  3514  			}
  3515  
  3516  			return b == nil
  3517  		case 1: // StructOrUnion IDENTIFIER
  3518  			if o := n.IdentifierOpt; o != nil {
  3519  				return o.Token.Val == m.Token.Val
  3520  			}
  3521  
  3522  			panic("TODO")
  3523  		default:
  3524  			panic(m.Case)
  3525  		}
  3526  	case 1: // StructOrUnion IDENTIFIER
  3527  		switch m.Case {
  3528  		case 1: // StructOrUnion IDENTIFIER
  3529  			return n.Token.Val == m.Token.Val
  3530  		default:
  3531  			panic(m.Case)
  3532  		}
  3533  	default:
  3534  		panic(n.Case)
  3535  	}
  3536  }
  3537  
  3538  func (n *StructOrUnionSpecifier) member(nm int) (*Member, error) {
  3539  	switch n.Case {
  3540  	case 0: // StructOrUnion IdentifierOpt '{' StructDeclarationList '}'
  3541  		b, s := n.scope.Lookup2(NSIdentifiers, nm)
  3542  		if s != n.scope {
  3543  			var t []byte
  3544  			if o := n.IdentifierOpt; o != nil {
  3545  				t = o.Token.S()
  3546  			}
  3547  			return nil, fmt.Errorf("%s %s has no member named %s", n.StructOrUnion.str(), t, xc.Dict.S(nm))
  3548  		}
  3549  
  3550  		d := b.Node.(*DirectDeclarator).top().declarator
  3551  		return &Member{
  3552  			Bits:       d.bits,
  3553  			Declarator: d,
  3554  			Name:       nm,
  3555  			OffsetOf:   d.offsetOf,
  3556  			Type:       d.Type,
  3557  		}, nil
  3558  	case 1: // StructOrUnion IDENTIFIER
  3559  		b := n.scope.Lookup(NSTags, n.Token.Val)
  3560  		n2, def := b.Node.(*StructOrUnionSpecifier)
  3561  		if !def {
  3562  			return nil, fmt.Errorf("invalid use of undefined type '%s %s'", n.StructOrUnion.str(), n.Token.S())
  3563  		}
  3564  
  3565  		return n2.member(nm)
  3566  	default:
  3567  		panic(n.Case)
  3568  	}
  3569  }
  3570  
  3571  // -------------------------------------------------------------- TypeSpecifier
  3572  
  3573  func (n *TypeSpecifier) member(nm int) (*Member, error) {
  3574  	switch n.Case {
  3575  	case 11: // StructOrUnionSpecifier
  3576  		return n.StructOrUnionSpecifier.member(nm)
  3577  	default:
  3578  		panic("internal error")
  3579  	}
  3580  }