Source file src/cmd/compile/internal/noder/lex.go

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package noder
     6  
     7  import (
     8  	"fmt"
     9  	"internal/buildcfg"
    10  	"strconv"
    11  	"strings"
    12  
    13  	"cmd/compile/internal/ir"
    14  	"cmd/compile/internal/syntax"
    15  )
    16  
    17  func isSpace(c rune) bool {
    18  	return c == ' ' || c == '\t' || c == '\n' || c == '\r'
    19  }
    20  
    21  func isQuoted(s string) bool {
    22  	return len(s) >= 2 && s[0] == '"' && s[len(s)-1] == '"'
    23  }
    24  
    25  const (
    26  	funcPragmas = ir.Nointerface |
    27  		ir.Noescape |
    28  		ir.Norace |
    29  		ir.Nosplit |
    30  		ir.Noinline |
    31  		ir.NoCheckPtr |
    32  		ir.RegisterParams | // TODO(register args) remove after register abi is working
    33  		ir.CgoUnsafeArgs |
    34  		ir.UintptrKeepAlive |
    35  		ir.UintptrEscapes |
    36  		ir.Systemstack |
    37  		ir.Nowritebarrier |
    38  		ir.Nowritebarrierrec |
    39  		ir.Yeswritebarrierrec
    40  )
    41  
    42  func pragmaFlag(verb string) ir.PragmaFlag {
    43  	switch verb {
    44  	case "go:build":
    45  		return ir.GoBuildPragma
    46  	case "go:nointerface":
    47  		if buildcfg.Experiment.FieldTrack {
    48  			return ir.Nointerface
    49  		}
    50  	case "go:noescape":
    51  		return ir.Noescape
    52  	case "go:norace":
    53  		return ir.Norace
    54  	case "go:nosplit":
    55  		return ir.Nosplit | ir.NoCheckPtr // implies NoCheckPtr (see #34972)
    56  	case "go:noinline":
    57  		return ir.Noinline
    58  	case "go:nocheckptr":
    59  		return ir.NoCheckPtr
    60  	case "go:systemstack":
    61  		return ir.Systemstack
    62  	case "go:nowritebarrier":
    63  		return ir.Nowritebarrier
    64  	case "go:nowritebarrierrec":
    65  		return ir.Nowritebarrierrec | ir.Nowritebarrier // implies Nowritebarrier
    66  	case "go:yeswritebarrierrec":
    67  		return ir.Yeswritebarrierrec
    68  	case "go:cgo_unsafe_args":
    69  		return ir.CgoUnsafeArgs | ir.NoCheckPtr // implies NoCheckPtr (see #34968)
    70  	case "go:uintptrkeepalive":
    71  		return ir.UintptrKeepAlive
    72  	case "go:uintptrescapes":
    73  		// This directive extends //go:uintptrkeepalive by forcing
    74  		// uintptr arguments to escape to the heap, which makes stack
    75  		// growth safe.
    76  		return ir.UintptrEscapes | ir.UintptrKeepAlive // implies UintptrKeepAlive
    77  	case "go:registerparams": // TODO(register args) remove after register abi is working
    78  		return ir.RegisterParams
    79  	}
    80  	return 0
    81  }
    82  
    83  // pragcgo is called concurrently if files are parsed concurrently.
    84  func (p *noder) pragcgo(pos syntax.Pos, text string) {
    85  	f := pragmaFields(text)
    86  
    87  	verb := strings.TrimPrefix(f[0], "go:")
    88  	f[0] = verb
    89  
    90  	switch verb {
    91  	case "cgo_export_static", "cgo_export_dynamic":
    92  		switch {
    93  		case len(f) == 2 && !isQuoted(f[1]):
    94  		case len(f) == 3 && !isQuoted(f[1]) && !isQuoted(f[2]):
    95  		default:
    96  			p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf(`usage: //go:%s local [remote]`, verb)})
    97  			return
    98  		}
    99  		for _, symName := range f[1:] {
   100  			if strings.Contains(symName, ",") {
   101  				p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf("invalid symbol name %q in //go:%s", symName, verb)})
   102  				return
   103  			}
   104  		}
   105  	case "cgo_import_dynamic":
   106  		switch {
   107  		case len(f) == 2 && !isQuoted(f[1]):
   108  		case len(f) == 3 && !isQuoted(f[1]) && !isQuoted(f[2]):
   109  		case len(f) == 4 && !isQuoted(f[1]) && !isQuoted(f[2]) && isQuoted(f[3]):
   110  			var err error
   111  			f[3], err = strconv.Unquote(f[3])
   112  			if err != nil {
   113  				p.error(syntax.Error{Pos: pos, Msg: `usage: //go:cgo_import_dynamic local [remote ["library"]]`})
   114  				return
   115  			}
   116  			if buildcfg.GOOS == "aix" && f[3] != "" {
   117  				// On Aix, library pattern must be "lib.a/object.o"
   118  				// or "lib.a/libname.so.X"
   119  				n := strings.Split(f[3], "/")
   120  				if len(n) != 2 || !strings.HasSuffix(n[0], ".a") || (!strings.HasSuffix(n[1], ".o") && !strings.Contains(n[1], ".so.")) {
   121  					p.error(syntax.Error{Pos: pos, Msg: `usage: //go:cgo_import_dynamic local [remote ["lib.a/object.o"]]`})
   122  					return
   123  				}
   124  			}
   125  		default:
   126  			p.error(syntax.Error{Pos: pos, Msg: `usage: //go:cgo_import_dynamic local [remote ["library"]]`})
   127  			return
   128  		}
   129  		end := len(f)
   130  		if end > 3 {
   131  			end = 3
   132  		}
   133  		for _, symName := range f[1:end] {
   134  			if strings.Contains(symName, ",") {
   135  				p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf("invalid symbol name %q in //go:%s", symName, verb)})
   136  				return
   137  			}
   138  		}
   139  	case "cgo_import_static":
   140  		switch {
   141  		case len(f) == 2 && !isQuoted(f[1]):
   142  		default:
   143  			p.error(syntax.Error{Pos: pos, Msg: `usage: //go:cgo_import_static local`})
   144  			return
   145  		}
   146  		if strings.Contains(f[1], ",") {
   147  			p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf("invalid symbol name %q in //go:%s", f[1], verb)})
   148  			return
   149  		}
   150  	case "cgo_dynamic_linker":
   151  		switch {
   152  		case len(f) == 2 && isQuoted(f[1]):
   153  			var err error
   154  			f[1], err = strconv.Unquote(f[1])
   155  			if err != nil {
   156  				p.error(syntax.Error{Pos: pos, Msg: `usage: //go:cgo_dynamic_linker "path"`})
   157  				return
   158  			}
   159  		default:
   160  			p.error(syntax.Error{Pos: pos, Msg: `usage: //go:cgo_dynamic_linker "path"`})
   161  			return
   162  		}
   163  		if strings.Contains(f[1], ",") {
   164  			p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf("invalid path %q in //go:%s", f[1], verb)})
   165  			return
   166  		}
   167  	case "cgo_ldflag":
   168  		switch {
   169  		case len(f) == 2 && isQuoted(f[1]):
   170  			var err error
   171  			f[1], err = strconv.Unquote(f[1])
   172  			if err != nil {
   173  				p.error(syntax.Error{Pos: pos, Msg: `usage: //go:cgo_ldflag "arg"`})
   174  				return
   175  			}
   176  		default:
   177  			p.error(syntax.Error{Pos: pos, Msg: `usage: //go:cgo_ldflag "arg"`})
   178  			return
   179  		}
   180  	default:
   181  		return
   182  	}
   183  	p.pragcgobuf = append(p.pragcgobuf, f)
   184  }
   185  
   186  // pragmaFields is similar to strings.FieldsFunc(s, isSpace)
   187  // but does not split when inside double quoted regions and always
   188  // splits before the start and after the end of a double quoted region.
   189  // If a quote in s is not closed the part after the opening quote will
   190  // not be returned as a field.
   191  func pragmaFields(s string) []string {
   192  	var a []string
   193  	inQuote := false
   194  	escaped := false
   195  	fieldStart := -1 // Set to -1 when looking for start of field.
   196  	for i, c := range s {
   197  		if escaped {
   198  			escaped = false
   199  			continue
   200  		}
   201  		if inQuote && c == '\\' {
   202  			escaped = true
   203  			continue
   204  		}
   205  		switch {
   206  		case c == '"':
   207  			if inQuote {
   208  				inQuote = false
   209  				a = append(a, s[fieldStart:i+1])
   210  				fieldStart = -1
   211  			} else {
   212  				inQuote = true
   213  				if fieldStart >= 0 {
   214  					a = append(a, s[fieldStart:i])
   215  				}
   216  				fieldStart = i
   217  			}
   218  		case !inQuote && isSpace(c):
   219  			if fieldStart >= 0 {
   220  				a = append(a, s[fieldStart:i])
   221  				fieldStart = -1
   222  			}
   223  		default:
   224  			if fieldStart == -1 {
   225  				fieldStart = i
   226  			}
   227  		}
   228  	}
   229  	if !inQuote && fieldStart >= 0 { // Last field might end at the end of the string.
   230  		a = append(a, s[fieldStart:])
   231  	}
   232  	return a
   233  }
   234  

View as plain text