/usr/lib/python2.7/dist-packages/ffc/formatting.py is in python-ffc 1.4.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | """
Compiler stage 5: Code formatting
---------------------------------
This module implements the formatting of UFC code from a given
dictionary of generated C++ code for the body of each UFC function.
It relies on templates for UFC code available as part of the module
ufc_utils.
"""
# Copyright (C) 2009-2014 Anders Logg
#
# This file is part of FFC.
#
# FFC is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# FFC 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with FFC. If not, see <http://www.gnu.org/licenses/>.
#
# First added: 2009-12-16
# Last changed: 2014-03-19
# Python modules
import os
# FFC modules
from ffc.log import info, error, begin, end, dstr
from ffc.constants import FFC_VERSION, UFC_VERSION
from ffc.cpp import format
from ffc.backends.ufc import templates
def format_code(code, wrapper_code, prefix, parameters):
"Format given code in UFC format."
begin("Compiler stage 5: Formatting code")
# Extract code
code_elements, code_dofmaps, code_integrals, code_forms = code
# Header and implementation code
code_h = ""
code_c = ""
# Generate code for comment on top of file
code_h += _generate_comment(parameters) + "\n"
code_c += _generate_comment(parameters) + "\n"
# Generate code for header
code_h += format["header_h"] % {"prefix_upper": prefix.upper()}
code_h += _generate_additional_includes(code_integrals) + "\n"
code_c += format["header_c"] % {"prefix": prefix}
# Generate code for elements
if code_elements:
for code_element in code_elements:
code_h += _format_h("finite_element", code_element, parameters)
code_c += _format_c("finite_element", code_element, parameters)
# Generate code for dofmaps
if code_dofmaps:
for code_dofmap in code_dofmaps:
code_h += _format_h("dofmap", code_dofmap, parameters)
code_c += _format_c("dofmap", code_dofmap, parameters)
# Generate code for integrals
if code_integrals:
for code_integral in code_integrals:
classname = code_integral["classname"]
if "cell_integral" in classname:
code_h += _format_h("cell_integral", code_integral, parameters)
code_c += _format_c("cell_integral", code_integral, parameters)
elif "exterior_facet_integral" in classname:
code_h += _format_h("exterior_facet_integral", code_integral, parameters)
code_c += _format_c("exterior_facet_integral", code_integral, parameters)
elif "interior_facet_integral" in classname:
code_h += _format_h("interior_facet_integral", code_integral, parameters)
code_c += _format_c("interior_facet_integral", code_integral, parameters)
elif "point_integral" in classname:
code_h += _format_h("point_integral", code_integral, parameters)
code_c += _format_c("point_integral", code_integral, parameters)
elif "custom_integral" in classname:
code_h += _format_h("custom_integral", code_integral, parameters)
code_c += _format_c("custom_integral", code_integral, parameters)
else:
error("Unable to figure out base class for %s" % classname)
# Generate code for form
if code_forms:
for code_form in code_forms:
code_h += _format_h("form", code_form, parameters)
code_c += _format_c("form", code_form, parameters)
# Add wrappers
if wrapper_code:
code_h += wrapper_code
# Generate code for footer
code_h += format["footer"]
# Write file(s)
if parameters["split"]:
_write_file(code_h, prefix, ".h", parameters)
_write_file(code_c, prefix, ".cpp", parameters)
else:
_write_file(code_h, prefix, ".h", parameters)
end()
def _format_h(class_type, code, parameters):
"Format header code for given class type."
if parameters["split"]:
return templates[class_type + "_header"] % code + "\n"
else:
return templates[class_type + "_combined"] % code + "\n"
def _format_c(class_type, code, parameters):
"Format implementation code for given class type."
if parameters["split"]:
return templates[class_type + "_implementation"] % code + "\n"
else:
return ""
def _write_file(output, prefix, postfix, parameters):
"Write generated code to file."
prefix = prefix.split(os.path.join(' ',' ').split()[0])[-1]
full_prefix = os.path.join(parameters["output_dir"], prefix)
filename = "%s%s" % (full_prefix, postfix)
hfile = open(filename, "w")
hfile.write(output)
hfile.close()
info("Output written to " + filename + ".")
def _generate_comment(parameters):
"Generate code for comment on top of file."
# Generate top level comment
args = {"ffc_version": FFC_VERSION, "ufc_version": UFC_VERSION}
if parameters["format"] == "ufc":
comment = format["ufc comment"] % args
elif parameters["format"] == "dolfin":
comment = format["dolfin comment"] % args
else:
error("Unable to format code, unknown format \"%s\".", parameters["format"])
# Add parameter information
comment += format["comment"]("") + "\n"
comment += format["comment"]("This code was generated with the following parameters:") + "\n"
comment += format["comment"]("")
comment += "\n".join([""] + [format["comment"](" " + l) for l in dstr(parameters).split("\n")][:-1])
comment += "\n"
return comment
def _generate_additional_includes(codes):
s = set()
for code in codes:
if "additional_includes_set" in code:
s.update(code["additional_includes_set"])
if s:
return "\n".join(list(s)) + "\n"
return ""
|