errors.go raw

   1  package ircheck
   2  
   3  import (
   4  	"go/scanner"
   5  	"go/token"
   6  	"path/filepath"
   7  
   8  	"tinygo.org/x/go-llvm"
   9  )
  10  
  11  // errorAt returns an error value at the location of the instruction.
  12  // The location information may not be complete as it depends on debug
  13  // information in the IR.
  14  func errorAt(inst llvm.Value, msg string) scanner.Error {
  15  	return scanner.Error{
  16  		Pos: getPosition(inst),
  17  		Msg: msg,
  18  	}
  19  }
  20  
  21  // getPosition returns the position information for the given value, as far as
  22  // it is available.
  23  func getPosition(val llvm.Value) token.Position {
  24  	if !val.IsAInstruction().IsNil() {
  25  		loc := val.InstructionDebugLoc()
  26  		if loc.IsNil() {
  27  			return token.Position{}
  28  		}
  29  		file := loc.LocationScope().ScopeFile()
  30  		return token.Position{
  31  			Filename: filepath.Join(file.FileDirectory(), file.FileFilename()),
  32  			Line:     int(loc.LocationLine()),
  33  			Column:   int(loc.LocationColumn()),
  34  		}
  35  	} else if !val.IsAFunction().IsNil() {
  36  		loc := val.Subprogram()
  37  		if loc.IsNil() {
  38  			return token.Position{}
  39  		}
  40  		file := loc.ScopeFile()
  41  		return token.Position{
  42  			Filename: filepath.Join(file.FileDirectory(), file.FileFilename()),
  43  			Line:     int(loc.SubprogramLine()),
  44  		}
  45  	} else {
  46  		return token.Position{}
  47  	}
  48  }
  49