1 #!/usr/bin/env python 2 3 """ 4 Encoder functions, producing representations of program objects. 5 6 Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 from common import first 23 24 # Output encoding and decoding for the summary files. 25 26 def encode_attrnames(attrnames): 27 28 "Encode the 'attrnames' representing usage." 29 30 return ", ".join(attrnames) or "{}" 31 32 def encode_constrained(constrained): 33 34 "Encode the 'constrained' status for program summaries." 35 36 return constrained and "constrained" or "deduced" 37 38 def encode_usage(usage): 39 40 "Encode attribute details from 'usage'." 41 42 all_attrnames = [] 43 for t in usage: 44 attrname, invocation = t 45 all_attrnames.append("%s%s" % (attrname, invocation and "!" or "")) 46 return ", ".join(all_attrnames) or "{}" 47 48 def decode_usage(s): 49 50 "Decode attribute details from 's'." 51 52 all_attrnames = set() 53 for attrname_str in s.split(", "): 54 all_attrnames.add((attrname_str.rstrip("!"), attrname_str.endswith("!"))) 55 56 all_attrnames = list(all_attrnames) 57 all_attrnames.sort() 58 return tuple(all_attrnames) 59 60 def encode_access_location(t): 61 62 "Encode the access location 't'." 63 64 path, name, attrname, version = t 65 return "%s %s %s:%d" % (path, name or "{}", attrname, version) 66 67 def encode_location(t): 68 69 "Encode the general location 't' in a concise form." 70 71 path, name, attrname, version = t 72 if name is not None and version is not None: 73 return "%s %s:%d" % (path, name, version) 74 elif name is not None: 75 return "%s %s" % (path, name) 76 else: 77 return "%s :%s" % (path, attrname) 78 79 def encode_modifiers(modifiers): 80 81 "Encode assignment details from 'modifiers'." 82 83 all_modifiers = [] 84 for t in modifiers: 85 all_modifiers.append(encode_modifier_term(t)) 86 return "".join(all_modifiers) 87 88 def encode_modifier_term(t): 89 90 "Encode modifier 't' representing assignment status." 91 92 assignment = t 93 return assignment and "A" or "_" 94 95 def decode_modifier_term(s): 96 97 "Decode modifier term 's' representing assignment status." 98 99 return s == "A" 100 101 102 103 # Test generation functions. 104 105 def get_kinds(all_types): 106 107 """ 108 Return object kind details for 'all_types', being a collection of 109 references for program types. 110 """ 111 112 return map(lambda ref: ref.get_kind(), all_types) 113 114 def test_for_kind(prefix, kind): 115 116 "Return a test condition identifier featuring 'prefix' and 'kind'." 117 118 return "%s-%s" % (prefix, kind == "<instance>" and "instance" or "type") 119 120 def test_for_kinds(prefix, all_kinds): 121 122 """ 123 Return an identifier describing test conditions incorporating the given 124 'prefix' and involving 'all_kinds', being a collection of object kinds. 125 """ 126 127 return test_for_kind(prefix, first(all_kinds)) 128 129 def test_for_type(prefix, ref): 130 131 """ 132 Return an identifier describing a test condition incorporating the given 133 'prefix' and involving 'ref', being a program type reference. The kind of 134 the reference is employed in the identifier. 135 """ 136 137 return test_for_kind(prefix, ref.get_kind()) 138 139 140 141 # Output program encoding. 142 143 def encode_function_pointer(path): 144 145 "Encode 'path' as a reference to an output program function." 146 147 return "__fn_%s" % encode_path(path) 148 149 def encode_instantiator_pointer(path): 150 151 "Encode 'path' as a reference to an output program instantiator." 152 153 return "__new_%s" % encode_path(path) 154 155 def encode_path(path): 156 157 "Encode 'path' as an output program object, translating special symbols." 158 159 if path in reserved_words: 160 return "__%s" % path 161 else: 162 return path.replace("#", "__").replace("$", "__").replace(".", "_") 163 164 def encode_symbol(symbol_type, path=None): 165 166 "Encode a symbol with the given 'symbol_type' and optional 'path'." 167 168 return "__%s%s" % (symbol_type, path and "_%s" % encode_path(path) or "") 169 170 171 172 # Output language reserved words. 173 174 reserved_words = [ 175 "break", "char", "const", "continue", 176 "default", "double", "else", 177 "float", "for", 178 "if", "int", "long", 179 "NULL", 180 "return", "struct", 181 "typedef", 182 "void", "while", 183 ] 184 185 # vim: tabstop=4 expandtab shiftwidth=4