|
| 1 | +// Author: Diffblue Ltd. |
| 2 | + |
| 3 | +/// \file |
| 4 | +/// Java-specific type qualifiers |
| 5 | + |
| 6 | +#include "java_qualifiers.h" |
| 7 | +#include <sstream> |
| 8 | +#include <util/make_unique.h> |
| 9 | +#include "expr2java.h" |
| 10 | + |
| 11 | +java_qualifierst::java_qualifierst(const namespacet &ns) : ns(ns) |
| 12 | +{ |
| 13 | +} |
| 14 | + |
| 15 | +java_qualifierst &java_qualifierst::operator=(const java_qualifierst &other) |
| 16 | +{ |
| 17 | + INVARIANT( |
| 18 | + &other.ns == &ns, |
| 19 | + "Can only assign from a java_qualifierst using the same namespace"); |
| 20 | + annotations = other.annotations; |
| 21 | + c_qualifierst::operator=(other); |
| 22 | + return *this; |
| 23 | +} |
| 24 | + |
| 25 | +java_qualifierst &java_qualifierst::operator=(java_qualifierst &&other) |
| 26 | +{ |
| 27 | + INVARIANT( |
| 28 | + &other.ns == &ns, |
| 29 | + "Can only assign from a java_qualifierst using the same namespace"); |
| 30 | + annotations = std::move(other.annotations); |
| 31 | + c_qualifierst::operator=(other); |
| 32 | + return *this; |
| 33 | +} |
| 34 | + |
| 35 | +c_qualifierst &java_qualifierst::operator=(const c_qualifierst &c_other) |
| 36 | +{ |
| 37 | + auto other = dynamic_cast<const java_qualifierst *>(&c_other); |
| 38 | + if(other != nullptr) |
| 39 | + *this = *other; |
| 40 | + else |
| 41 | + { |
| 42 | + annotations.clear(); |
| 43 | + c_qualifierst::operator=(c_other); |
| 44 | + } |
| 45 | + return *this; |
| 46 | +} |
| 47 | + |
| 48 | +std::unique_ptr<c_qualifierst> java_qualifierst::clone() const |
| 49 | +{ |
| 50 | + auto other = util_make_unique<java_qualifierst>(ns); |
| 51 | + *other = *this; |
| 52 | + return std::move(other); |
| 53 | +} |
| 54 | + |
| 55 | +const std::vector<java_annotationt> &java_qualifierst::get_annotations() const |
| 56 | +{ |
| 57 | + return annotations; |
| 58 | +} |
| 59 | + |
| 60 | +std::size_t java_qualifierst::count() const |
| 61 | +{ |
| 62 | + return c_qualifierst::count() + annotations.size(); |
| 63 | +} |
| 64 | + |
| 65 | +void java_qualifierst::clear() |
| 66 | +{ |
| 67 | + c_qualifierst::clear(); |
| 68 | + annotations.clear(); |
| 69 | +} |
| 70 | + |
| 71 | +void java_qualifierst::read(const typet &src) |
| 72 | +{ |
| 73 | + c_qualifierst::read(src); |
| 74 | + auto &annotated_type = static_cast<const annotated_typet &>(src); |
| 75 | + annotations = annotated_type.get_annotations(); |
| 76 | +} |
| 77 | + |
| 78 | +void java_qualifierst::write(typet &src) const |
| 79 | +{ |
| 80 | + c_qualifierst::write(src); |
| 81 | + auto &annotated_type = static_cast<annotated_typet &>(src); |
| 82 | + annotated_type.get_annotations() = annotations; |
| 83 | +} |
| 84 | + |
| 85 | +c_qualifierst &java_qualifierst::operator+=(const c_qualifierst &c_other) |
| 86 | +{ |
| 87 | + c_qualifierst::operator+=(c_other); |
| 88 | + auto other = dynamic_cast<const java_qualifierst *>(&c_other); |
| 89 | + if(other != nullptr) |
| 90 | + { |
| 91 | + std::copy( |
| 92 | + other->annotations.begin(), |
| 93 | + other->annotations.end(), |
| 94 | + std::back_inserter(annotations)); |
| 95 | + } |
| 96 | + return *this; |
| 97 | +} |
| 98 | + |
| 99 | +bool java_qualifierst::operator==(const c_qualifierst &c_other) const |
| 100 | +{ |
| 101 | + auto other = dynamic_cast<const java_qualifierst *>(&c_other); |
| 102 | + if(other == nullptr) |
| 103 | + return false; |
| 104 | + return |
| 105 | + c_qualifierst::operator==(c_other) && annotations == other->annotations; |
| 106 | +} |
| 107 | + |
| 108 | +bool java_qualifierst::is_subset_of(const c_qualifierst &c_other) const |
| 109 | +{ |
| 110 | + if(!c_qualifierst::is_subset_of(c_other)) |
| 111 | + return false; |
| 112 | + auto other = dynamic_cast<const java_qualifierst *>(&c_other); |
| 113 | + if(other == nullptr) |
| 114 | + return annotations.empty(); |
| 115 | + auto &other_a = other->annotations; |
| 116 | + for(const auto &annotation : annotations) |
| 117 | + { |
| 118 | + if(std::find(other_a.begin(), other_a.end(), annotation) == other_a.end()) |
| 119 | + return false; |
| 120 | + } |
| 121 | + return true; |
| 122 | +} |
| 123 | + |
| 124 | +std::string java_qualifierst::as_string() const |
| 125 | +{ |
| 126 | + std::stringstream out; |
| 127 | + for(const java_annotationt &annotation : annotations) |
| 128 | + { |
| 129 | + out << '@'; |
| 130 | + out << annotation.get_type().subtype().get(ID_identifier); |
| 131 | + |
| 132 | + if(!annotation.get_values().empty()) |
| 133 | + { |
| 134 | + out << '('; |
| 135 | + |
| 136 | + bool first = true; |
| 137 | + for(const java_annotationt::valuet &value : annotation.get_values()) |
| 138 | + { |
| 139 | + if(first) |
| 140 | + first = false; |
| 141 | + else |
| 142 | + out << ", "; |
| 143 | + |
| 144 | + out << '"' << value.get_name() << '"' << '='; |
| 145 | + out << expr2java(value.get_value(), ns); |
| 146 | + } |
| 147 | + |
| 148 | + out << ')'; |
| 149 | + } |
| 150 | + out << ' '; |
| 151 | + } |
| 152 | + out << c_qualifierst::as_string(); |
| 153 | + return out.str(); |
| 154 | +} |
0 commit comments