// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package types

import (
	
	
	
	
	
	
)

// A goVersion is a Go language version string of the form "go1.%d"
// where d is the minor version number. goVersion strings don't
// contain release numbers ("go1.20.1" is not a valid goVersion).
type goVersion string

// asGoVersion returns v as a goVersion (e.g., "go1.20.1" becomes "go1.20").
// If v is not a valid Go version, the result is the empty string.
func asGoVersion( string) goVersion {
	return goVersion(version.Lang())
}

// isValid reports whether v is a valid Go version.
func ( goVersion) () bool {
	return  != ""
}

// cmp returns -1, 0, or +1 depending on whether x < y, x == y, or x > y,
// interpreted as Go versions.
func ( goVersion) ( goVersion) int {
	return version.Compare(string(), string())
}

var (
	// Go versions that introduced language changes
	go1_9  = asGoVersion("go1.9")
	go1_13 = asGoVersion("go1.13")
	go1_14 = asGoVersion("go1.14")
	go1_17 = asGoVersion("go1.17")
	go1_18 = asGoVersion("go1.18")
	go1_20 = asGoVersion("go1.20")
	go1_21 = asGoVersion("go1.21")
	go1_22 = asGoVersion("go1.22")

	// current (deployed) Go version
	go_current = asGoVersion(fmt.Sprintf("go1.%d", goversion.Version))
)

// langCompat reports an error if the representation of a numeric
// literal is not compatible with the current language version.
func ( *Checker) ( *ast.BasicLit) {
	 := .Value
	if len() <= 2 || .allowVersion(.pkg, , go1_13) {
		return
	}
	// len(s) > 2
	if strings.Contains(, "_") {
		.versionErrorf(, go1_13, "underscores in numeric literals")
		return
	}
	if [0] != '0' {
		return
	}
	 := [1]
	if  == 'b' ||  == 'B' {
		.versionErrorf(, go1_13, "binary literals")
		return
	}
	if  == 'o' ||  == 'O' {
		.versionErrorf(, go1_13, "0o/0O-style octal literals")
		return
	}
	if .Kind != token.INT && ( == 'x' ||  == 'X') {
		.versionErrorf(, go1_13, "hexadecimal floating-point literals")
	}
}

// allowVersion reports whether the given package is allowed to use version v.
func ( *Checker) ( *Package,  positioner,  goVersion) bool {
	// We assume that imported packages have all been checked,
	// so we only have to check for the local package.
	if  != .pkg {
		return true
	}

	// If no explicit file version is specified,
	// fileVersion corresponds to the module version.
	var  goVersion
	if  := .Pos(); .IsValid() {
		// We need version.Lang below because file versions
		// can be (unaltered) Config.GoVersion strings that
		// may contain dot-release information.
		 = asGoVersion(.versions[.fileFor()])
	}
	return !.isValid() || .cmp() >= 0
}

// verifyVersionf is like allowVersion but also accepts a format string and arguments
// which are used to report a version error if allowVersion returns false. It uses the
// current package.
func ( *Checker) ( positioner,  goVersion,  string,  ...interface{}) bool {
	if !.allowVersion(.pkg, , ) {
		.versionErrorf(, , , ...)
		return false
	}
	return true
}

// TODO(gri) Consider a more direct (position-independent) mechanism
//           to identify which file we're in so that version checks
//           work correctly in the absence of correct position info.

// fileFor returns the *ast.File which contains the position pos.
// If there are no files, the result is nil.
// The position must be valid.
func ( *Checker) ( token.Pos) *ast.File {
	assert(.IsValid())
	// Eval and CheckExpr tests may not have any source files.
	if len(.files) == 0 {
		return nil
	}
	for ,  := range .files {
		if .FileStart <=  &&  < .FileEnd {
			return 
		}
	}
	panic(.sprintf("file not found for pos = %d (%s)", int(), .fset.Position()))
}