-
Notifications
You must be signed in to change notification settings - Fork 5
/
functions.h
128 lines (100 loc) · 3.64 KB
/
functions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* -*-C++-*- */
/*
* Functions.
*
* Copyright 2003-2005 Carnegie Mellon University and Rutgers University
* Copyright 2007 Håkan Younes
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <config.h>
#include "types.h"
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
/* ====================================================================== */
/* Function */
/*
* A function.
*/
struct Function {
/* Constructs a function. */
explicit Function(int index) : index_(index) {}
private:
/* Function index. */
int index_;
friend bool operator==(const Function& f1, const Function& f2);
friend bool operator<(const Function& f1, const Function& f2);
friend std::ostream& operator<<(std::ostream& os, const Function& f);
friend struct FunctionTable;
};
/* Equality operator for functions. */
inline bool operator==(const Function& f1, const Function& f2) {
return f1.index_ == f2.index_;
}
/* Inequality operator for functions. */
inline bool operator!=(const Function& f1, const Function& f2) {
return !(f1 == f2);
}
/* Less-than operator for functions. */
inline bool operator<(const Function& f1, const Function& f2) {
return f1.index_ < f2.index_;
}
/* Output operator for functions. */
std::ostream& operator<<(std::ostream& os, const Function& f);
/* ====================================================================== */
/* FunctionSet */
/* Set of function declarations. */
struct FunctionSet : public std::set<Function> {
};
/* ====================================================================== */
/* FunctionTable */
/*
* Function table.
*/
struct FunctionTable {
/* Adds a parameter with the given type to the given function. */
static void add_parameter(const Function& function, const Type& type);
/* Returns the name of the given function. */
static const std::string& name(const Function& function);
/* Returns the parameter types of the given function. */
static const TypeList& parameters(const Function& function);
/* Makes the given function dynamic. */
static void make_dynamic(const Function& function);
/* Tests if the given function is static. */
static bool static_function(const Function& function);
/* Adds a function with the given name to this table and returns the
function. */
const Function& add_function(const std::string& name);
/* Returns a pointer to the function with the given name, or 0 if no
function with the given name exists. */
const Function* find_function(const std::string& name) const;
private:
/* Function names. */
static std::vector<std::string> names_;
/* Function parameters. */
static std::vector<TypeList> parameters_;
/* Static functions. */
static FunctionSet static_functions_;
/* Mapping of function names to functions. */
std::map<std::string, Function> functions_;
friend std::ostream& operator<<(std::ostream& os, const FunctionTable& t);
friend std::ostream& operator<<(std::ostream& os, const Function& f);
};
/* Output operator for function tables. */
std::ostream& operator<<(std::ostream& os, const FunctionTable& t);
#endif /* FUNCTIONS_H */