github.com/hikaru7719/go@v0.0.0-20181025140707-c8b2ac68906a/src/cmd/compile/internal/gc/lex.go (about)

     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 gc
     6  
     7  import (
     8  	"cmd/compile/internal/syntax"
     9  	"cmd/internal/objabi"
    10  	"cmd/internal/src"
    11  	"fmt"
    12  	"strings"
    13  )
    14  
    15  // lineno is the source position at the start of the most recently lexed token.
    16  // TODO(gri) rename and eventually remove
    17  var lineno src.XPos
    18  
    19  func makePos(base *src.PosBase, line, col uint) src.XPos {
    20  	return Ctxt.PosTable.XPos(src.MakePos(base, line, col))
    21  }
    22  
    23  func isSpace(c rune) bool {
    24  	return c == ' ' || c == '\t' || c == '\n' || c == '\r'
    25  }
    26  
    27  func isQuoted(s string) bool {
    28  	return len(s) >= 2 && s[0] == '"' && s[len(s)-1] == '"'
    29  }
    30  
    31  const (
    32  	// Func pragmas.
    33  	Nointerface    syntax.Pragma = 1 << iota
    34  	Noescape                     // func parameters don't escape
    35  	Norace                       // func must not have race detector annotations
    36  	Nosplit                      // func should not execute on separate stack
    37  	Noinline                     // func should not be inlined
    38  	CgoUnsafeArgs                // treat a pointer to one arg as a pointer to them all
    39  	UintptrEscapes               // pointers converted to uintptr escape
    40  
    41  	// Runtime-only func pragmas.
    42  	// See ../../../../runtime/README.md for detailed descriptions.
    43  	Systemstack        // func must run on system stack
    44  	Nowritebarrier     // emit compiler error instead of write barrier
    45  	Nowritebarrierrec  // error on write barrier in this or recursive callees
    46  	Yeswritebarrierrec // cancels Nowritebarrierrec in this function and callees
    47  
    48  	// Runtime-only type pragmas
    49  	NotInHeap // values of this type must not be heap allocated
    50  )
    51  
    52  func pragmaValue(verb string) syntax.Pragma {
    53  	switch verb {
    54  	case "go:nointerface":
    55  		if objabi.Fieldtrack_enabled != 0 {
    56  			return Nointerface
    57  		}
    58  	case "go:noescape":
    59  		return Noescape
    60  	case "go:norace":
    61  		return Norace
    62  	case "go:nosplit":
    63  		return Nosplit
    64  	case "go:noinline":
    65  		return Noinline
    66  	case "go:systemstack":
    67  		return Systemstack
    68  	case "go:nowritebarrier":
    69  		return Nowritebarrier
    70  	case "go:nowritebarrierrec":
    71  		return Nowritebarrierrec | Nowritebarrier // implies Nowritebarrier
    72  	case "go:yeswritebarrierrec":
    73  		return Yeswritebarrierrec
    74  	case "go:cgo_unsafe_args":
    75  		return CgoUnsafeArgs
    76  	case "go:uintptrescapes":
    77  		// For the next function declared in the file
    78  		// any uintptr arguments may be pointer values
    79  		// converted to uintptr. This directive
    80  		// ensures that the referenced allocated
    81  		// object, if any, is retained and not moved
    82  		// until the call completes, even though from
    83  		// the types alone it would appear that the
    84  		// object is no longer needed during the
    85  		// call. The conversion to uintptr must appear
    86  		// in the argument list.
    87  		// Used in syscall/dll_windows.go.
    88  		return UintptrEscapes
    89  	case "go:notinheap":
    90  		return NotInHeap
    91  	}
    92  	return 0
    93  }
    94  
    95  // pragcgo is called concurrently if files are parsed concurrently.
    96  func (p *noder) pragcgo(pos syntax.Pos, text string) {
    97  	f := pragmaFields(text)
    98  
    99  	verb := strings.TrimPrefix(f[0], "go:")
   100  	f[0] = verb
   101  
   102  	switch verb {
   103  	case "cgo_export_static", "cgo_export_dynamic":
   104  		switch {
   105  		case len(f) == 2 && !isQuoted(f[1]):
   106  		case len(f) == 3 && !isQuoted(f[1]) && !isQuoted(f[2]):
   107  		default:
   108  			p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf(`usage: //go:%s local [remote]`, verb)})
   109  			return
   110  		}
   111  	case "cgo_import_dynamic":
   112  		switch {
   113  		case len(f) == 2 && !isQuoted(f[1]):
   114  		case len(f) == 3 && !isQuoted(f[1]) && !isQuoted(f[2]):
   115  		case len(f) == 4 && !isQuoted(f[1]) && !isQuoted(f[2]) && isQuoted(f[3]):
   116  			f[3] = strings.Trim(f[3], `"`)
   117  		default:
   118  			p.error(syntax.Error{Pos: pos, Msg: `usage: //go:cgo_import_dynamic local [remote ["library"]]`})
   119  			return
   120  		}
   121  	case "cgo_import_static":
   122  		switch {
   123  		case len(f) == 2 && !isQuoted(f[1]):
   124  		default:
   125  			p.error(syntax.Error{Pos: pos, Msg: `usage: //go:cgo_import_static local`})
   126  			return
   127  		}
   128  	case "cgo_dynamic_linker":
   129  		switch {
   130  		case len(f) == 2 && isQuoted(f[1]):
   131  			f[1] = strings.Trim(f[1], `"`)
   132  		default:
   133  			p.error(syntax.Error{Pos: pos, Msg: `usage: //go:cgo_dynamic_linker "path"`})
   134  			return
   135  		}
   136  	case "cgo_ldflag":
   137  		switch {
   138  		case len(f) == 2 && isQuoted(f[1]):
   139  			f[1] = strings.Trim(f[1], `"`)
   140  		default:
   141  			p.error(syntax.Error{Pos: pos, Msg: `usage: //go:cgo_ldflag "arg"`})
   142  			return
   143  		}
   144  	default:
   145  		return
   146  	}
   147  	p.pragcgobuf = append(p.pragcgobuf, f)
   148  }
   149  
   150  // pragmaFields is similar to strings.FieldsFunc(s, isSpace)
   151  // but does not split when inside double quoted regions and always
   152  // splits before the start and after the end of a double quoted region.
   153  // pragmaFields does not recognize escaped quotes. If a quote in s is not
   154  // closed the part after the opening quote will not be returned as a field.
   155  func pragmaFields(s string) []string {
   156  	var a []string
   157  	inQuote := false
   158  	fieldStart := -1 // Set to -1 when looking for start of field.
   159  	for i, c := range s {
   160  		switch {
   161  		case c == '"':
   162  			if inQuote {
   163  				inQuote = false
   164  				a = append(a, s[fieldStart:i+1])
   165  				fieldStart = -1
   166  			} else {
   167  				inQuote = true
   168  				if fieldStart >= 0 {
   169  					a = append(a, s[fieldStart:i])
   170  				}
   171  				fieldStart = i
   172  			}
   173  		case !inQuote && isSpace(c):
   174  			if fieldStart >= 0 {
   175  				a = append(a, s[fieldStart:i])
   176  				fieldStart = -1
   177  			}
   178  		default:
   179  			if fieldStart == -1 {
   180  				fieldStart = i
   181  			}
   182  		}
   183  	}
   184  	if !inQuote && fieldStart >= 0 { // Last field might end at the end of the string.
   185  		a = append(a, s[fieldStart:])
   186  	}
   187  	return a
   188  }