OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
| 5 import os |
| 6 |
5 from code import Code | 7 from code import Code |
6 from model import PropertyType | 8 from model import PropertyType |
7 import cpp_util | 9 import cpp_util |
8 import schema_util | 10 import schema_util |
9 | 11 |
10 class HGenerator(object): | 12 class HGenerator(object): |
11 def __init__(self, type_generator, cpp_namespace): | 13 def __init__(self, type_generator, cpp_namespace_pattern): |
12 self._type_generator = type_generator | 14 self._type_generator = type_generator |
13 self._cpp_namespace = cpp_namespace | 15 self._cpp_namespace_pattern = cpp_namespace_pattern |
14 | 16 |
15 def Generate(self, namespace): | 17 def Generate(self, namespace): |
16 return _Generator(namespace, | 18 return _Generator(namespace, |
17 self._type_generator, | 19 self._type_generator, |
18 self._cpp_namespace).Generate() | 20 self._cpp_namespace_pattern).Generate() |
19 | 21 |
20 | 22 |
21 class _Generator(object): | 23 class _Generator(object): |
22 """A .h generator for a namespace. | 24 """A .h generator for a namespace. |
23 """ | 25 """ |
24 def __init__(self, namespace, cpp_type_generator, cpp_namespace): | 26 def __init__(self, namespace, cpp_type_generator, cpp_namespace_pattern): |
25 self._namespace = namespace | 27 self._namespace = namespace |
26 self._type_helper = cpp_type_generator | 28 self._type_helper = cpp_type_generator |
27 self._cpp_namespace = cpp_namespace | 29 self._cpp_namespace_pattern = cpp_namespace_pattern |
28 self._target_namespace = ( | |
29 self._type_helper.GetCppNamespaceName(self._namespace)) | |
30 self._generate_error_messages = namespace.compiler_options.get( | 30 self._generate_error_messages = namespace.compiler_options.get( |
31 'generate_error_messages', False) | 31 'generate_error_messages', False) |
32 | 32 |
33 def Generate(self): | 33 def Generate(self): |
34 """Generates a Code object with the .h for a single namespace. | 34 """Generates a Code object with the .h for a single namespace. |
35 """ | 35 """ |
36 c = Code() | 36 c = Code() |
37 (c.Append(cpp_util.CHROMIUM_LICENSE) | 37 (c.Append(cpp_util.CHROMIUM_LICENSE) |
38 .Append() | 38 .Append() |
39 .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) | 39 .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) |
40 .Append() | 40 .Append() |
41 ) | 41 ) |
42 | 42 |
43 ifndef_name = cpp_util.GenerateIfndefName(self._namespace.source_file_dir, | 43 # Hack: for the purpose of gyp the header file will always be the source |
44 self._target_namespace) | 44 # file with its file extension replaced by '.h'. Assume so. |
| 45 output_file = os.path.splitext(self._namespace.source_file)[0] + '.h' |
| 46 ifndef_name = cpp_util.GenerateIfndefName(output_file) |
| 47 |
45 (c.Append('#ifndef %s' % ifndef_name) | 48 (c.Append('#ifndef %s' % ifndef_name) |
46 .Append('#define %s' % ifndef_name) | 49 .Append('#define %s' % ifndef_name) |
47 .Append() | 50 .Append() |
48 .Append('#include <map>') | 51 .Append('#include <map>') |
49 .Append('#include <string>') | 52 .Append('#include <string>') |
50 .Append('#include <vector>') | 53 .Append('#include <vector>') |
51 .Append() | 54 .Append() |
52 .Append('#include "base/basictypes.h"') | 55 .Append('#include "base/basictypes.h"') |
53 .Append('#include "base/logging.h"') | 56 .Append('#include "base/logging.h"') |
54 .Append('#include "base/memory/linked_ptr.h"') | 57 .Append('#include "base/memory/linked_ptr.h"') |
55 .Append('#include "base/memory/scoped_ptr.h"') | 58 .Append('#include "base/memory/scoped_ptr.h"') |
56 .Append('#include "base/values.h"') | 59 .Append('#include "base/values.h"') |
57 .Cblock(self._type_helper.GenerateIncludes()) | 60 .Cblock(self._type_helper.GenerateIncludes()) |
58 .Append() | 61 .Append() |
59 ) | 62 ) |
60 | 63 |
61 c.Concat(cpp_util.OpenNamespace(self._cpp_namespace)) | |
62 # TODO(calamity): These forward declarations should be #includes to allow | 64 # TODO(calamity): These forward declarations should be #includes to allow |
63 # $ref types from other files to be used as required params. This requires | 65 # $ref types from other files to be used as required params. This requires |
64 # some detangling of windows and tabs which will currently lead to circular | 66 # some detangling of windows and tabs which will currently lead to circular |
65 # #includes. | 67 # #includes. |
66 forward_declarations = ( | 68 c.Cblock(self._type_helper.GenerateForwardDeclarations( |
67 self._type_helper.GenerateForwardDeclarations()) | 69 self._cpp_namespace_pattern)) |
68 if not forward_declarations.IsEmpty(): | |
69 (c.Append() | |
70 .Cblock(forward_declarations) | |
71 ) | |
72 | 70 |
73 c.Concat(self._type_helper.GetNamespaceStart()) | 71 cpp_namespace = cpp_util.GetCppNamespace(self._cpp_namespace_pattern, |
| 72 self._namespace.unix_name) |
| 73 c.Concat(cpp_util.OpenNamespace(cpp_namespace)) |
74 c.Append() | 74 c.Append() |
75 if self._namespace.properties: | 75 if self._namespace.properties: |
76 (c.Append('//') | 76 (c.Append('//') |
77 .Append('// Properties') | 77 .Append('// Properties') |
78 .Append('//') | 78 .Append('//') |
79 .Append() | 79 .Append() |
80 ) | 80 ) |
81 for property in self._namespace.properties.values(): | 81 for property in self._namespace.properties.values(): |
82 property_code = self._type_helper.GeneratePropertyValues( | 82 property_code = self._type_helper.GeneratePropertyValues( |
83 property, | 83 property, |
(...skipping 18 matching lines...) Expand all Loading... |
102 for function in self._namespace.functions.values(): | 102 for function in self._namespace.functions.values(): |
103 c.Cblock(self._GenerateFunction(function)) | 103 c.Cblock(self._GenerateFunction(function)) |
104 if self._namespace.events: | 104 if self._namespace.events: |
105 (c.Append('//') | 105 (c.Append('//') |
106 .Append('// Events') | 106 .Append('// Events') |
107 .Append('//') | 107 .Append('//') |
108 .Append() | 108 .Append() |
109 ) | 109 ) |
110 for event in self._namespace.events.values(): | 110 for event in self._namespace.events.values(): |
111 c.Cblock(self._GenerateEvent(event)) | 111 c.Cblock(self._GenerateEvent(event)) |
112 (c.Concat(self._type_helper.GetNamespaceEnd()) | 112 (c.Concat(cpp_util.CloseNamespace(cpp_namespace)) |
113 .Concat(cpp_util.CloseNamespace(self._cpp_namespace)) | |
114 .Append('#endif // %s' % ifndef_name) | 113 .Append('#endif // %s' % ifndef_name) |
115 .Append() | 114 .Append() |
116 ) | 115 ) |
117 return c | 116 return c |
118 | 117 |
119 def _FieldDependencyOrder(self): | 118 def _FieldDependencyOrder(self): |
120 """Generates the list of types in the current namespace in an order in which | 119 """Generates the list of types in the current namespace in an order in which |
121 depended-upon types appear before types which depend on them. | 120 depended-upon types appear before types which depend on them. |
122 """ | 121 """ |
123 dependency_order = [] | 122 dependency_order = [] |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 """Builds the parameter list for a function, given an array of parameters. | 392 """Builds the parameter list for a function, given an array of parameters. |
394 """ | 393 """ |
395 # |error| is populated with warnings and/or errors found during parsing. | 394 # |error| is populated with warnings and/or errors found during parsing. |
396 # |error| being set does not necessarily imply failure and may be | 395 # |error| being set does not necessarily imply failure and may be |
397 # recoverable. | 396 # recoverable. |
398 # For example, optional properties may have failed to parse, but the | 397 # For example, optional properties may have failed to parse, but the |
399 # parser was able to continue. | 398 # parser was able to continue. |
400 if self._generate_error_messages: | 399 if self._generate_error_messages: |
401 params += ('base::string16* error',) | 400 params += ('base::string16* error',) |
402 return ', '.join(str(p) for p in params) | 401 return ', '.join(str(p) for p in params) |
OLD | NEW |