| 128 | } |
| 129 | |
| 130 | void FCompileContext::CheckReturn(PPrototype *proto, FScriptPosition &pos) |
| 131 | { |
| 132 | assert(proto != nullptr); |
| 133 | bool fail = false; |
| 134 | |
| 135 | if (ReturnProto == nullptr) |
| 136 | { |
| 137 | ReturnProto = proto; |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | // A prototype that defines fewer return types can be compatible with |
| 142 | // one that defines more if the shorter one matches the initial types |
| 143 | // for the longer one. |
| 144 | bool swapped = false; |
| 145 | |
| 146 | if (ReturnProto->ReturnTypes.Size() < proto->ReturnTypes.Size()) |
| 147 | { // Make proto the shorter one to avoid code duplication below. |
| 148 | std::swap(proto, ReturnProto); |
| 149 | swapped = true; |
| 150 | } |
| 151 | // If one prototype returns nothing, they both must. |
| 152 | if (proto->ReturnTypes.Size() == 0) |
| 153 | { |
| 154 | if (ReturnProto->ReturnTypes.Size() != 0) |
| 155 | { |
| 156 | fail = true; |
| 157 | } |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | for (unsigned i = 0; i < proto->ReturnTypes.Size(); i++) |
| 162 | { |
| 163 | PType* expected = ReturnProto->ReturnTypes[i]; |
| 164 | PType* actual = proto->ReturnTypes[i]; |
| 165 | if (swapped) std::swap(expected, actual); |
| 166 | // this must pass for older ZScripts. |
| 167 | if (Version < MakeVersion(4, 12, 0)) |
| 168 | { |
| 169 | if (expected == TypeTranslationID) expected = TypeSInt32; |
| 170 | if (actual == TypeTranslationID) actual = TypeSInt32; |
| 171 | } |
| 172 | |
| 173 | if (expected != actual && !AreCompatiblePointerTypes(expected, actual)) |
| 174 | { // Incompatible |
| 175 | Printf("Return type %s mismatch with %s\n", expected->DescriptiveName(), actual->DescriptiveName()); |
| 176 | fail = true; |
| 177 | break; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | if (fail) |
| 183 | { |
| 184 | pos.Message(MSG_ERROR, "Return type mismatch"); |
| 185 | } |
| 186 | } |
| 187 |
no test coverage detected