// Copyright (c) 2019 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package digreflect

import (
	
	
	
	
	
)

// Func contains runtime information about a function.
type Func struct {
	// Name of the function.
	Name string

	// Name of the package in which this function is defined.
	Package string

	// Path to the file in which this function is defined.
	File string

	// Line number in the file at which this function is defined.
	Line int
}

// String returns a string representation of the function.
func ( *Func) () string {
	return fmt.Sprint()
}

// Format implements fmt.Formatter for Func, printing a single-line
// representation for %v and a multi-line one for %+v.
func ( *Func) ( fmt.State,  rune) {
	if .Flag('+') &&  == 'v' {
		// "path/to/package".MyFunction
		// 	path/to/file.go:42
		fmt.Fprintf(, "%q.%v", .Package, .Name)
		fmt.Fprintf(, "\n\t%v:%v", .File, .Line)
	} else {
		// "path/to/package".MyFunction (path/to/file.go:42)
		fmt.Fprintf(, "%q.%v (%v:%v)", .Package, .Name, .File, .Line)
	}
}

// InspectFunc inspects and returns runtime information about the given
// function.
func ( interface{}) *Func {
	 := reflect.ValueOf().Pointer()
	return InspectFuncPC()
}

// InspectFuncPC inspects and returns runtime information about the function
// at the given program counter address.
func ( uintptr) *Func {
	 := runtime.FuncForPC()
	if  == nil {
		return nil
	}
	,  := splitFuncName(.Name())
	,  := .FileLine()
	return &Func{
		Name:    ,
		Package: ,
		File:    ,
		Line:    ,
	}
}

const _vendor = "/vendor/"

func splitFuncName( string) ( string,  string) {
	if len() == 0 {
		return
	}

	// We have something like "path.to/my/pkg.MyFunction". If the function is
	// a closure, it is something like, "path.to/my/pkg.MyFunction.func1".

	 := 0

	// Everything up to the first "." after the last "/" is the package name.
	// Everything after the "." is the full function name.
	if  := strings.LastIndex(, "/");  >= 0 {
		 = 
	}
	if  := strings.Index([:], ".");  >= 0 {
		 += 
	}
	,  = [:], [+1:]

	// The package may be vendored.
	if  := strings.Index(, _vendor);  > 0 {
		 = [+len(_vendor):]
	}

	// Package names are URL-encoded to avoid ambiguity in the case where the
	// package name contains ".git". Otherwise, "foo/bar.git.MyFunction" would
	// mean that "git" is the top-level function and "MyFunction" is embedded
	// inside it.
	if ,  := url.QueryUnescape();  == nil {
		 = 
	}

	return
}