1f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)# Copyright 2013 The Chromium Authors. All rights reserved.
2f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)# Use of this source code is governed by a BSD-style license that can be
3f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)# found in the LICENSE file.
4f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
5f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)import os.path
6f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
7f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)from code import Code
8f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)import cpp_util
9f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
10f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
11f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)class HGenerator(object):
12f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  def Generate(self, features, source_file, namespace):
13f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return _Generator(features, source_file, namespace).Generate()
14f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
15f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
16f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)class _Generator(object):
17f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  """A .cc generator for features.
18f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  """
19f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  def __init__(self, features, source_file, namespace):
20f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    self._feature_defs = features
21f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    self._source_file = source_file
22f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    self._source_file_filename, _ = os.path.splitext(source_file)
23f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    self._class_name = cpp_util.ClassName(self._source_file_filename)
24f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    self._namespace = namespace
25f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
26f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  def Generate(self):
27f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    """Generates a Code object for features.
28f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    """
29f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    c = Code()
30f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    (c.Append(cpp_util.CHROMIUM_LICENSE)
31f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .Append()
32f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .Append(cpp_util.GENERATED_FEATURE_MESSAGE % self._source_file)
33f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .Append()
34f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    )
355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    # Hack: for the purpose of gyp the header file will always be the source
375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    # file with its file extension replaced by '.h'. Assume so.
385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    output_file = os.path.splitext(self._namespace.source_file)[0] + '.h'
395f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    ifndef_name = cpp_util.GenerateIfndefName(output_file)
405f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
41f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    (c.Append('#ifndef %s' % ifndef_name)
42f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .Append('#define %s' % ifndef_name)
43f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .Append()
44f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    )
45f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
46f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    (c.Append('#include <map>')
47f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .Append('#include <string>')
48f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .Append()
49f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .Concat(cpp_util.OpenNamespace(self._namespace))
50f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .Append()
51f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    )
52f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
53f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    (c.Append('class %s {' % self._class_name)
54f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .Append(' public:')
55f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .Sblock()
56f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .Concat(self._GeneratePublicBody())
57f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .Eblock()
58f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .Append(' private:')
59f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .Sblock()
60f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .Concat(self._GeneratePrivateBody())
61f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .Eblock('};')
62f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .Append()
63f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .Cblock(cpp_util.CloseNamespace(self._namespace))
64f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    )
65f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    (c.Append('#endif  // %s' % ifndef_name)
66f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .Append()
67f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    )
68f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return c
69f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
70f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  def _GeneratePublicBody(self):
71f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    c = Code()
72f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
73f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    (c.Append('%s();' % self._class_name)
74f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .Append()
75f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .Append('enum ID {')
76f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .Concat(self._GenerateEnumConstants())
77f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .Eblock('};')
78f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .Append()
79f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .Append('const char* ToString(ID id) const;')
80f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .Append('ID FromString(const std::string& id) const;')
81f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .Append()
82f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    )
83f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return c
84f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
85f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  def _GeneratePrivateBody(self):
86f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return Code().Append('std::map<std::string, '
87f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                         '%s::ID> features_;' % self._class_name)
88f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
89f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  def _GenerateEnumConstants(self):
90f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    c = Code()
91f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
92f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    (c.Sblock()
93f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .Append('kUnknown,')
94f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    )
95f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    for feature in self._feature_defs:
96f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      c.Append('%s,' % cpp_util.ConstantName(feature.name))
97f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    c.Append('kEnumBoundary')
98f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
99f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return c
100