aboutsummaryrefslogtreecommitdiff
path: root/gcc/cp/gen-cxxapi-file.py
blob: 6d08f46069bb5fdfab48c4389ef66dc2796ead66 (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env python3

# Copyright (C) 2022-2024 Free Software Foundation, Inc.
# This file is part of GCC.

# GCC is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.

# GCC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with GCC; see the file COPYING3.  If not see
# <http://www.gnu.org/licenses/>.


# Generate gcc source files:
# - the export description of the standard C++ library module
# - the gperf tables used to hint at actions to fix problems
#   related to missing symbols in the std:: namespace

import csv
import os
import time

# The CSV file contains the following columns:
#  column  value
#       1  header file, including angle brackets
#       2  symbol name without std:: prefix
#       3  nonzero if to be exported
#       4  "no" if not to be added to the hint table else the appropriate enum cxx_dialect value
#       5  optional, a string used after #if in a line inserted first to enable conditional definitions


def print_condition(prev, e):
    if len(e) > 4 and e[4] != '':
        if not prev or prev != e[4]:
            if prev:
                print('#endif')
            print(f'#if {e[4]}')
        return e[4]
    if prev:
        print('#endif')
    return None


def export(script, content):
    print("""// This file is auto-generated by {:s}.
#if __cplusplus <= 202002L
# if __cplusplus == 202002L
#  ifdef __STRICT_ANSI__
#   error "module `std' is only available before C++23 if using -std=gnu++20"
#  endif
# else
#  error "module `std' is not available before C++23"
# endif
#endif

export module std;

import <bits/stdc++.h>;

// new/delete operators in global namespace from <new>
export using ::operator new;
export using ::operator delete;
export using ::operator new[];
export using ::operator delete[];""".format(script))
    header = ''
    printed_header = False
    cond = None
    for e in content:
        if e[0] != header:
            header = e[0]
            printed_header = False
        if e[2] != 0:
            if not printed_header:
                if cond:
                    print('#endif')
                    cond = None
                print(f'\n// {header}')
                printed_header = True
            cond = print_condition(cond, e)
            print(f'export using std::{e[1]};')
    if cond:
        print('#endif')


def hints(script, content):
    print("""%language=C++
%define class-name std_name_hint_lookup
%struct-type
%{{
/* This file is auto-generated by {:s}.  */
/* Copyright (C) 2022-{:s} Free Software Foundation, Inc.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */
%}}
struct std_name_hint
{{
  /* A name within "std::".  */
  const char *name;

  /* The header name defining it within the C++ Standard Library
     (with '<' and '>').  */
  const char* header;

  /* The dialect of C++ in which this was added.  */
  enum cxx_dialect min_dialect;
}};
%%
# The standard-defined types, functions, and options in the std:: namespace
# as defined in the C++ language specification.  The result is used in the
# get_std_name_hint functions.
#   throws an exception.
#""".format(script, time.strftime('%Y')))
    header = ''
    printed_header = False
    for e in content:
        if e[0] != header:
            header = e[0]
            printed_header = False
        if e[3] != 'no':
            if not printed_header:
                print(f'# {header}')
                printed_header = True
            print(f'{e[1]}, "{e[0]}", {e[3]}')


def remove_comment(f):
    for row in f:
        row = row.strip()
        if row[0] != '#':
            yield row


modes = {
    'export': export,
    'hints': hints
}


def usage(script):
    print(f'Usage: {script} [{"|".join(modes.keys())}] CSVFILE')
    exit(1)


def main(argv):
    if len(argv) < 3:
        usage(argv[0] if len(argv) > 0 else '???')

    script = argv[0]
    mode = argv[1]
    filename = argv[2]

    if mode not in modes:
        print(f"{script}: unrecognized mode '{mode}'")
        usage(script)

    try:
        with open(filename, 'r') as csvfile:
            modes[mode](os.path.basename(script), sorted(csv.reader(remove_comment(csvfile), delimiter=',')))
    except FileNotFoundError:
        print(f"{script}: cannot find CSV file '{filename}'")
        exit(1)
    except PermissionError:
        print(f"{script}: insufficient permission to read CSV file '{filename}'")
        exit(1)


if __name__ == '__main__':
    import sys
    main(sys.argv)