1# Copyright 2019 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import functools
6
7from .code_generator_info import CodeGeneratorInfo
8from .composition_parts import Identifier
9from .composition_parts import WithCodeGeneratorInfo
10from .composition_parts import WithComponent
11from .composition_parts import WithDebugInfo
12from .composition_parts import WithExposure
13from .composition_parts import WithExtendedAttributes
14from .composition_parts import WithOwner
15from .composition_parts import WithOwnerMixin
16from .exposure import Exposure
17from .function_like import FunctionLike
18from .function_like import OverloadGroup
19from .make_copy import make_copy
20
21
22class Constructor(FunctionLike, WithExtendedAttributes, WithCodeGeneratorInfo,
23                  WithExposure, WithOwner, WithOwnerMixin, WithComponent,
24                  WithDebugInfo):
25    """https://heycam.github.io/webidl/#idl-constructors"""
26
27    class IR(FunctionLike.IR, WithExtendedAttributes, WithCodeGeneratorInfo,
28             WithExposure, WithOwnerMixin, WithComponent, WithDebugInfo):
29        def __init__(self,
30                     identifier,
31                     arguments,
32                     return_type,
33                     extended_attributes=None,
34                     component=None,
35                     debug_info=None):
36            assert identifier is None or isinstance(identifier, Identifier)
37            FunctionLike.IR.__init__(
38                self,
39                identifier=(identifier or Identifier('constructor')),
40                arguments=arguments,
41                return_type=return_type)
42            WithExtendedAttributes.__init__(self, extended_attributes)
43            WithCodeGeneratorInfo.__init__(self)
44            WithExposure.__init__(self)
45            WithOwnerMixin.__init__(self)
46            WithComponent.__init__(self, component)
47            WithDebugInfo.__init__(self, debug_info)
48
49    def __init__(self, ir, owner):
50        assert isinstance(ir, Constructor.IR)
51
52        FunctionLike.__init__(self, ir)
53        WithExtendedAttributes.__init__(self, ir, readonly=True)
54        WithCodeGeneratorInfo.__init__(self, ir, readonly=True)
55        WithExposure.__init__(self, ir, readonly=True)
56        WithOwner.__init__(self, owner)
57        WithOwnerMixin.__init__(self, ir)
58        WithComponent.__init__(self, ir, readonly=True)
59        WithDebugInfo.__init__(self, ir)
60
61
62class ConstructorGroup(OverloadGroup, WithExtendedAttributes,
63                       WithCodeGeneratorInfo, WithExposure, WithOwner,
64                       WithComponent, WithDebugInfo):
65    """
66    Represents a group of constructors of an interface.
67
68    The number of constructors in this group may be 1 or 2+.  In the latter
69    case, the constructors are overloaded.
70    """
71
72    class IR(OverloadGroup.IR, WithExtendedAttributes, WithCodeGeneratorInfo,
73             WithExposure, WithDebugInfo):
74        def __init__(self,
75                     constructors,
76                     extended_attributes=None,
77                     code_generator_info=None,
78                     debug_info=None):
79            OverloadGroup.IR.__init__(self, constructors)
80            WithExtendedAttributes.__init__(self, extended_attributes)
81            WithCodeGeneratorInfo.__init__(self, code_generator_info)
82            WithExposure.__init__(self)
83            WithDebugInfo.__init__(self, debug_info)
84
85    def __init__(self, ir, constructors, owner):
86        assert isinstance(ir, ConstructorGroup.IR)
87        assert isinstance(constructors, (list, tuple))
88        assert all(
89            isinstance(constructor, Constructor)
90            for constructor in constructors)
91        assert all(constructor.identifier == ir.identifier
92                   for constructor in constructors)
93
94        components = functools.reduce(
95            lambda s, constructor: s.union(constructor.components),
96            constructors, set())
97
98        ir = make_copy(ir)
99        OverloadGroup.__init__(self, functions=constructors)
100        WithExtendedAttributes.__init__(self, ir, readonly=True)
101        WithCodeGeneratorInfo.__init__(self, ir, readonly=True)
102        WithExposure.__init__(self, ir, readonly=True)
103        WithOwner.__init__(self, owner)
104        WithComponent.__init__(self, sorted(components))
105        WithDebugInfo.__init__(self, ir)
106