Class: Ryac::GvarRenameMapping
- Inherits:
-
Object
- Object
- Ryac::GvarRenameMapping
- Defined in:
- lib/ryac/analysis/gvar/rename_mapping.rb,
sig/ryac/analysis/gvar/rename_mapping.rbs
Constant Summary collapse
- PERL_SPELLINGS =
One pairing of English name to punctuation spelling. SpellingShorten rewrites reads and plain writes to the short form; both spellings also join BUILT_IN_GLOBALS below, so the node kinds it does not rewrite (targets, operator/or/and writes) can never be renamed either.
{ '$PROGRAM_NAME': '$0', '$LOAD_PATH': '$:', '$LOADED_FEATURES': '$"', }.freeze
- BUILT_IN_GLOBALS =
Built-in globals that must never be renamed: Ruby defines their storage, so a renamed spelling reads nil and writes into a dead cell.
(Set.new(%i[ $stdout $stderr $stdin $DEBUG $VERBOSE $FILENAME $ERROR_INFO $ERROR_POSITION $FIELD_SEPARATOR $OFS $OUTPUT_FIELD_SEPARATOR $RS $INPUT_RECORD_SEPARATOR $ORS $OUTPUT_RECORD_SEPARATOR $INPUT_LINE_NUMBER $NR $LAST_READ_LINE $DEFAULT_OUTPUT $DEFAULT_INPUT $PID $PROCESS_ID $CHILD_STATUS $LAST_MATCH_INFO $IGNORECASE $ARGV $MATCH $PREMATCH $POSTMATCH $LAST_PAREN_MATCH $FS $0 $: $; $, $. $/ $\\ $_ $< $> $! $@ $~ $& $` $' $+ $* $" $? $$ $-0 $-F $-I $-K $-W $-a $-d $-i $-l $-p $-v $-w $1 $2 $3 $4 $5 $6 $7 $8 $9 ]) + PERL_SPELLINGS.keys + PERL_SPELLINGS.values.map(&:to_sym)).freeze
- PREFIX =
'$'- KEPT_NAME_MAX =
Sigil + one character is already minimal — the same rule the SiteBucketMapping families derive from their prefix.
PREFIX.size + 1
Instance Method Summary collapse
- #add_site(name, node, write: false) ⇒ void
- #assign_short_names ⇒ void
- #exclude_name(name) ⇒ void
-
#initialize ⇒ GvarRenameMapping
constructor
A new instance of GvarRenameMapping.
- #node_mapping ⇒ Hash[[Integer, Integer], String]
Constructor Details
#initialize ⇒ GvarRenameMapping
Returns a new instance of GvarRenameMapping.
29 30 31 32 33 34 |
# File 'lib/ryac/analysis/gvar/rename_mapping.rb', line 29 def initialize @gvars = {} @excluded_names = Set.new @written_names = Set.new @node_short_names = {} end |
Instance Method Details
#add_site(name, node, write: false) ⇒ void
This method returns an undefined value.
36 37 38 39 40 41 |
# File 'lib/ryac/analysis/gvar/rename_mapping.rb', line 36 def add_site(name, node, write: false) return if BUILT_IN_GLOBALS.include?(name) @written_names << name if write @gvars[name] ||= [] @gvars[name] << node end |
#assign_short_names ⇒ void
This method returns an undefined value.
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 |
# File 'lib/ryac/analysis/gvar/rename_mapping.rb', line 52 def assign_short_names existing_names = Set.new @gvars.each_key do |name| existing_names << name.to_s if name.to_s.size <= KEPT_NAME_MAX end generator = NameGenerator.new(existing_names, prefix: PREFIX) sorted_gvars = @gvars.sort_by do |name, nodes| -(name.to_s.size * nodes.size) end sorted_gvars.each do |name, nodes| next if @excluded_names.include?(name) next if name.to_s.size <= KEPT_NAME_MAX # A global the program only reads is either a built-in this list # does not know or nil everywhere — renaming it can only break. next unless @written_names.include?(name) short_name = generator.next_name savings = (name.to_s.size - short_name.size) * nodes.size next unless savings > 0 nodes.each { |n| @node_short_names[AstUtils.location_key(n)] = short_name } end end |
#exclude_name(name) ⇒ void
This method returns an undefined value.
43 44 45 |
# File 'lib/ryac/analysis/gvar/rename_mapping.rb', line 43 def exclude_name(name) @excluded_names << name end |
#node_mapping ⇒ Hash[[Integer, Integer], String]
78 79 80 |
# File 'lib/ryac/analysis/gvar/rename_mapping.rb', line 78 def node_mapping @node_short_names.dup end |