// 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 fxreflect

import (
	
	
	
	
)

// Frame holds information about a single frame in the call stack.
type Frame struct {
	// Unique, package path-qualified name for the function of this call
	// frame.
	Function string

	// File and line number of our location in the frame.
	//
	// Note that the line number does not refer to where the function was
	// defined but where in the function the next call was made.
	File string
	Line int
}

func ( Frame) () string {
	// This takes the following forms.
	//  (path/to/file.go)
	//  (path/to/file.go:42)
	//  path/to/package.MyFunction
	//  path/to/package.MyFunction (path/to/file.go)
	//  path/to/package.MyFunction (path/to/file.go:42)

	var  strings.Builder
	.WriteString(.Function)
	if len(.File) > 0 {
		if .Len() > 0 {
			.WriteRune(' ')
		}
		fmt.Fprintf(&, "(%v", .File)
		if .Line > 0 {
			fmt.Fprintf(&, ":%d", .Line)
		}
		.WriteRune(')')
	}

	if .Len() == 0 {
		return "unknown"
	}

	return .String()
}

const _defaultCallersDepth = 8

// Stack is a stack of call frames.
//
// Formatted with %v, the output is in a single-line, in the form,
//
//	foo/bar.Baz() (path/to/foo.go:42); bar/baz.Qux() (bar/baz/qux.go:12); ...
//
// Formatted with %+v, the output is in the form,
//
//	foo/bar.Baz()
//		path/to/foo.go:42
//	bar/baz.Qux()
//		bar/baz/qux.go:12
type Stack []Frame

// String returns a single-line, semi-colon representation of a Stack.
// For a list of strings where each represents one frame, use Strings.
// For a cleaner multi-line representation, use %+v.
func ( Stack) () string {
	return strings.Join(.Strings(), "; ")
}

// Strings returns a list of strings, each representing a frame in the stack.
// Each line will be in the form,
//
//	foo/bar.Baz() (path/to/foo.go:42)
func ( Stack) () []string {
	 := make([]string, len())
	for ,  := range  {
		[] = .String()
	}
	return 
}

// Format implements fmt.Formatter to handle "%+v".
func ( Stack) ( fmt.State,  rune) {
	if !.Flag('+') {
		// Without %+v, fall back to String().
		io.WriteString(, .String())
		return
	}

	for ,  := range  {
		fmt.Fprintln(, .Function)
		fmt.Fprintf(, "\t%v:%v\n", .File, .Line)
	}
}

// CallerName returns the name of the first caller in this stack that isn't
// owned by the Fx library.
func ( Stack) () string {
	for ,  := range  {
		if shouldIgnoreFrame() {
			continue
		}
		return .Function
	}
	return "n/a"
}

// CallerStack returns the call stack for the calling function, up to depth frames
// deep, skipping the provided number of frames, not including Callers itself.
//
// If zero, depth defaults to 8.
func (,  int) Stack {
	if  <= 0 {
		 = _defaultCallersDepth
	}

	 := make([]uintptr, )

	// +2 to skip this frame and runtime.Callers.
	 := runtime.Callers(+2, )
	 = [:] // truncate to number of frames actually read

	 := make([]Frame, 0, )
	 := runtime.CallersFrames()
	for ,  := .Next(); ; ,  = .Next() {
		 = append(, Frame{
			Function: sanitize(.Function),
			File:     .File,
			Line:     .Line,
		})
	}
	return 
}