Parameters = "(" [ ParameterList [ "," ] ] ")" . ParameterList = ParameterDecl { "," ParameterDecl } . "(" or "[" has already been consumed. If name != nil, it is the first name after "(" or "[". If typ != nil, name must be != nil, and (name, typ) is the first field in the list. In the result lis
(name *Name, typ Expr, close token, requireNames bool)
| 1974 | // If typ != nil, name must be != nil, and (name, typ) is the first field in the list. |
| 1975 | // In the result list, either all fields have a name, or no field has a name. |
| 1976 | func (p *parser) paramList(name *Name, typ Expr, close token, requireNames bool) (list []*Field) { |
| 1977 | if trace { |
| 1978 | defer p.trace("paramList")() |
| 1979 | } |
| 1980 | |
| 1981 | // p.list won't invoke its function argument if we're at the end of the |
| 1982 | // parameter list. If we have a complete field, handle this case here. |
| 1983 | if name != nil && typ != nil && p.tok == close { |
| 1984 | p.next() |
| 1985 | par := new(Field) |
| 1986 | par.pos = name.pos |
| 1987 | par.Name = name |
| 1988 | par.Type = typ |
| 1989 | return []*Field{par} |
| 1990 | } |
| 1991 | |
| 1992 | var named int // number of parameters that have an explicit name and type |
| 1993 | var typed int // number of parameters that have an explicit type |
| 1994 | end := p.list(_Comma, close, func() bool { |
| 1995 | var par *Field |
| 1996 | if typ != nil { |
| 1997 | if debug && name == nil { |
| 1998 | panic("initial type provided without name") |
| 1999 | } |
| 2000 | par = new(Field) |
| 2001 | par.pos = name.pos |
| 2002 | par.Name = name |
| 2003 | par.Type = typ |
| 2004 | } else { |
| 2005 | par = p.paramDeclOrNil(name, close) |
| 2006 | } |
| 2007 | name = nil // 1st name was consumed if present |
| 2008 | typ = nil // 1st type was consumed if present |
| 2009 | if par != nil { |
| 2010 | if debug && par.Name == nil && par.Type == nil { |
| 2011 | panic("parameter without name or type") |
| 2012 | } |
| 2013 | if par.Name != nil && par.Type != nil { |
| 2014 | named++ |
| 2015 | } |
| 2016 | if par.Type != nil { |
| 2017 | typed++ |
| 2018 | } |
| 2019 | list = append(list, par) |
| 2020 | } |
| 2021 | return false |
| 2022 | }) |
| 2023 | |
| 2024 | if len(list) == 0 { |
| 2025 | return |
| 2026 | } |
| 2027 | |
| 2028 | // distribute parameter types (len(list) > 0) |
| 2029 | if named == 0 && !requireNames { |
| 2030 | // all unnamed => found names are named types |
| 2031 | for _, par := range list { |
| 2032 | if typ := par.Name; typ != nil { |
| 2033 | par.Type = typ |
no test coverage detected