File: codegen_accumulator.py

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (93 lines) | stat: -rw-r--r-- 3,505 bytes parent folder | download | duplicates (6)
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
# Copyright 2019 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from typing import Optional, Sequence, Set, Union
import dataclasses


@dataclasses.dataclass(frozen=True)
class IncludeDefinition:
    """Definition for an #include statement."""
    filename: str  # Header filename to include.
    annotation: Optional[str] = None  # End-of-line comment (e.g. IWYU pragma).


class CodeGenAccumulator(object):
    """
    Accumulates a variety of information and helps generate code based on the
    information.
    """

    def __init__(self):
        # Headers of non-standard library to be included
        self._include_headers = set()  # type: Set[IncludeDefinition]
        # Headers of C++ standard library to be included
        self._stdcpp_include_headers = set()
        # Forward declarations of C++ class
        self._class_decls = set()
        # Forward declarations of C++ struct
        self._struct_decls = set()

    def total_size(self):
        return (len(self.include_headers) + len(self.class_decls) +
                len(self.struct_decls) + len(self.stdcpp_include_headers))

    @property
    def include_headers(self) -> Set[IncludeDefinition]:
        return self._include_headers

    def add_include_headers(self, headers: Sequence[Union[str,
                                                          IncludeDefinition]]):
        """Add a list of headers to include. Individual headers can be specified
        either as a filename string, or as an IncludeDefinition instance (useful
        to add IWYU pragma annotations)."""
        self._include_headers.update(
            IncludeDefinition(header) if isinstance(header, str) else header
            for header in headers if header)

    @staticmethod
    def require_include_headers(headers: Sequence[Union[str,
                                                        IncludeDefinition]]):
        return lambda accumulator: accumulator.add_include_headers(headers)

    @property
    def stdcpp_include_headers(self) -> Set[IncludeDefinition]:
        return self._stdcpp_include_headers

    def add_stdcpp_include_headers(
            self, headers: Sequence[Union[str, IncludeDefinition]]):
        """Add a list of standard headers to include. Individual headers can
        be specified either as a filename string, or as an IncludeDefinition
        instance (useful to add IWYU pragma annotations)."""
        self._stdcpp_include_headers.update(
            IncludeDefinition(header) if isinstance(header, str) else header
            for header in headers if header)

    @staticmethod
    def require_stdcpp_include_headers(
            headers: Sequence[Union[str, IncludeDefinition]]):
        return lambda accumulator: accumulator.add_stdcpp_include_headers(
            headers)

    @property
    def class_decls(self):
        return self._class_decls

    def add_class_decls(self, class_names):
        self._class_decls.update(filter(None, class_names))

    @staticmethod
    def require_class_decls(class_names):
        return lambda accumulator: accumulator.add_class_decls(class_names)

    @property
    def struct_decls(self):
        return self._struct_decls

    def add_struct_decls(self, struct_names):
        self._struct_decls.update(filter(None, struct_names))

    @staticmethod
    def require_struct_decls(struct_names):
        return lambda accumulator: accumulator.add_struct_decls(struct_names)