1 // Prototypes for functions for storing and retrieving function information. These functions also
2 // take care of autoloading functions in the $fish_function_path. Actual function evaluation is
3 // taken care of by the parser and to some degree the builtin handling library.
4 #ifndef FISH_FUNCTION_H
5 #define FISH_FUNCTION_H
6 
7 #include <map>
8 #include <vector>
9 
10 #include "common.h"
11 #include "env.h"
12 #include "event.h"
13 #include "parse_tree.h"
14 
15 class parser_t;
16 
17 namespace ast {
18 struct block_statement_t;
19 }
20 
21 /// A function's constant properties. These do not change once initialized.
22 struct function_properties_t {
23     /// Parsed source containing the function.
24     parsed_source_ref_t parsed_source;
25 
26     /// Node containing the function statement, pointing into parsed_source.
27     /// We store block_statement, not job_list, so that comments attached to the header are
28     /// preserved.
29     const ast::block_statement_t *func_node;
30 
31     /// List of all named arguments for this function.
32     wcstring_list_t named_arguments;
33 
34     /// Mapping of all variables that were inherited from the function definition scope to their
35     /// values.
36     std::map<wcstring, wcstring_list_t> inherit_vars;
37 
38     /// Set to true if invoking this function shadows the variables of the underlying function.
39     bool shadow_scope{true};
40 };
41 
42 using function_properties_ref_t = std::shared_ptr<const function_properties_t>;
43 
44 /// Add a function.
45 void function_add(wcstring name, wcstring description, function_properties_ref_t props,
46                   const wchar_t *filename);
47 
48 /// Remove the function with the specified name.
49 void function_remove(const wcstring &name);
50 
51 /// Returns the properties for a function, or nullptr if none. This does not trigger autoloading.
52 function_properties_ref_t function_get_properties(const wcstring &name);
53 
54 /// Returns by reference the definition of the function with the name \c name. Returns true if
55 /// successful, false if no function with the given name exists.
56 /// This does not trigger autoloading.
57 bool function_get_definition(const wcstring &name, wcstring &out_definition);
58 
59 /// Returns by reference the description of the function with the name \c name. Returns true if the
60 /// function exists and has a nonempty description, false if it does not.
61 /// This does not trigger autoloading.
62 bool function_get_desc(const wcstring &name, wcstring &out_desc);
63 
64 /// Sets the description of the function with the name \c name.
65 void function_set_desc(const wcstring &name, const wcstring &desc, parser_t &parser);
66 
67 /// Returns true if the function with the name name exists.
68 /// This may autoload.
69 int function_exists(const wcstring &cmd, parser_t &parser);
70 
71 /// Attempts to load a function if not yet loaded. This is used by the completion machinery.
72 void function_load(const wcstring &cmd, parser_t &parser);
73 
74 /// Returns true if the function with the name name exists, without triggering autoload.
75 bool function_exists_no_autoload(const wcstring &cmd);
76 
77 /// Returns all function names.
78 ///
79 /// \param get_hidden whether to include hidden functions, i.e. ones starting with an underscore.
80 wcstring_list_t function_get_names(int get_hidden);
81 
82 /// Returns true if the function was autoloaded.
83 bool function_is_autoloaded(const wcstring &name);
84 
85 /// Returns tha absolute path of the file where the specified function was defined. Returns 0 if the
86 /// file was defined on the commandline.
87 ///
88 /// This function does not autoload functions, it will only work on functions that have already been
89 /// defined.
90 ///
91 /// This returns an intern'd string.
92 const wchar_t *function_get_definition_file(const wcstring &name);
93 
94 /// Returns the linenumber where the definition of the specified function started.
95 /// This does not trigger autoloading.
96 int function_get_definition_lineno(const wcstring &name);
97 
98 /// Creates a new function using the same definition as the specified function. Returns true if copy
99 /// is successful.
100 bool function_copy(const wcstring &name, const wcstring &new_name);
101 
102 /// Observes that fish_function_path has changed.
103 void function_invalidate_path();
104 
105 wcstring functions_def(const wcstring &name);
106 #endif
107