1 // Copyright (c) 2018 Google LLC.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "source/spirv_target_env.h"
16 #include "source/val/instruction.h"
17 #include "source/val/validate.h"
18 #include "source/val/validation_state.h"
19
20 namespace spvtools {
21 namespace val {
22 namespace {
23
ValidateMemberName(ValidationState_t & _,const Instruction * inst)24 spv_result_t ValidateMemberName(ValidationState_t& _, const Instruction* inst) {
25 const auto type_id = inst->GetOperandAs<uint32_t>(0);
26 const auto type = _.FindDef(type_id);
27 if (!type || spv::Op::OpTypeStruct != type->opcode()) {
28 return _.diag(SPV_ERROR_INVALID_ID, inst)
29 << "OpMemberName Type <id> " << _.getIdName(type_id)
30 << " is not a struct type.";
31 }
32 const auto member_id = inst->GetOperandAs<uint32_t>(1);
33 const auto member_count = (uint32_t)(type->words().size() - 2);
34 if (member_count <= member_id) {
35 return _.diag(SPV_ERROR_INVALID_ID, inst)
36 << "OpMemberName Member <id> " << _.getIdName(member_id)
37 << " index is larger than Type <id> " << _.getIdName(type->id())
38 << "s member count.";
39 }
40 return SPV_SUCCESS;
41 }
42
ValidateLine(ValidationState_t & _,const Instruction * inst)43 spv_result_t ValidateLine(ValidationState_t& _, const Instruction* inst) {
44 const auto file_id = inst->GetOperandAs<uint32_t>(0);
45 const auto file = _.FindDef(file_id);
46 if (!file || spv::Op::OpString != file->opcode()) {
47 return _.diag(SPV_ERROR_INVALID_ID, inst)
48 << "OpLine Target <id> " << _.getIdName(file_id)
49 << " is not an OpString.";
50 }
51 return SPV_SUCCESS;
52 }
53
54 } // namespace
55
DebugPass(ValidationState_t & _,const Instruction * inst)56 spv_result_t DebugPass(ValidationState_t& _, const Instruction* inst) {
57 switch (inst->opcode()) {
58 case spv::Op::OpMemberName:
59 if (auto error = ValidateMemberName(_, inst)) return error;
60 break;
61 case spv::Op::OpLine:
62 if (auto error = ValidateLine(_, inst)) return error;
63 break;
64 default:
65 break;
66 }
67
68 return SPV_SUCCESS;
69 }
70
71 } // namespace val
72 } // namespace spvtools
73