|
| 1 | +import semmle.code.cpp.ir.implementation.raw.internal.TranslatedElement |
| 2 | +private import TranslatedExpr |
| 3 | +private import cpp |
| 4 | +private import semmle.code.cpp.ir.implementation.internal.OperandTag |
| 5 | +private import semmle.code.cpp.ir.internal.TempVariableTag |
| 6 | +private import semmle.code.cpp.ir.internal.CppType |
| 7 | +private import TranslatedInitialization |
| 8 | +private import InstructionTag |
| 9 | +private import semmle.code.cpp.ir.internal.IRUtilities |
| 10 | + |
| 11 | +class TranslatedNonStaticDataMemberVarInit extends TranslatedRootElement, |
| 12 | + TTranslatedNonStaticDataMemberVarInit, InitializationContext |
| 13 | +{ |
| 14 | + Field field; |
| 15 | + Class cls; |
| 16 | + |
| 17 | + TranslatedNonStaticDataMemberVarInit() { |
| 18 | + this = TTranslatedNonStaticDataMemberVarInit(field) and |
| 19 | + cls.getAMember() = field |
| 20 | + } |
| 21 | + |
| 22 | + override string toString() { result = cls.toString() + "::" + field.toString() } |
| 23 | + |
| 24 | + final override Field getAst() { result = field } |
| 25 | + |
| 26 | + final override Declaration getFunction() { result = field } |
| 27 | + |
| 28 | + override Instruction getFirstInstruction(EdgeKind kind) { |
| 29 | + result = this.getInstruction(EnterFunctionTag()) and |
| 30 | + kind instanceof GotoEdge |
| 31 | + } |
| 32 | + |
| 33 | + override Instruction getALastInstructionInternal() { |
| 34 | + result = this.getInstruction(ExitFunctionTag()) |
| 35 | + } |
| 36 | + |
| 37 | + override TranslatedElement getChild(int n) { |
| 38 | + n = 1 and |
| 39 | + result = getTranslatedInitialization(field.getInitializer().getExpr().getFullyConverted()) |
| 40 | + } |
| 41 | + |
| 42 | + override predicate hasInstruction(Opcode op, InstructionTag tag, CppType type) { |
| 43 | + op instanceof Opcode::EnterFunction and |
| 44 | + tag = EnterFunctionTag() and |
| 45 | + type = getVoidType() |
| 46 | + or |
| 47 | + op instanceof Opcode::AliasedDefinition and |
| 48 | + tag = AliasedDefinitionTag() and |
| 49 | + type = getUnknownType() |
| 50 | + or |
| 51 | + op instanceof Opcode::InitializeNonLocal and |
| 52 | + tag = InitializeNonLocalTag() and |
| 53 | + type = getUnknownType() |
| 54 | + or |
| 55 | + tag = ThisAddressTag() and |
| 56 | + op instanceof Opcode::VariableAddress and |
| 57 | + type = getTypeForGLValue(any(UnknownType t)) |
| 58 | + or |
| 59 | + tag = InitializerStoreTag() and |
| 60 | + op instanceof Opcode::InitializeParameter and |
| 61 | + type = this.getThisType() |
| 62 | + or |
| 63 | + tag = ThisLoadTag() and |
| 64 | + op instanceof Opcode::Load and |
| 65 | + type = this.getThisType() |
| 66 | + or |
| 67 | + tag = InitializerIndirectStoreTag() and |
| 68 | + op instanceof Opcode::InitializeIndirection and |
| 69 | + type = getTypeForPRValue(cls) |
| 70 | + or |
| 71 | + op instanceof Opcode::FieldAddress and |
| 72 | + tag = InitializerFieldAddressTag() and |
| 73 | + type = getTypeForGLValue(field.getType()) |
| 74 | + or |
| 75 | + op instanceof Opcode::ReturnVoid and |
| 76 | + tag = ReturnTag() and |
| 77 | + type = getVoidType() |
| 78 | + or |
| 79 | + op instanceof Opcode::AliasedUse and |
| 80 | + tag = AliasedUseTag() and |
| 81 | + type = getVoidType() |
| 82 | + or |
| 83 | + op instanceof Opcode::ExitFunction and |
| 84 | + tag = ExitFunctionTag() and |
| 85 | + type = getVoidType() |
| 86 | + } |
| 87 | + |
| 88 | + override Instruction getInstructionSuccessorInternal(InstructionTag tag, EdgeKind kind) { |
| 89 | + kind instanceof GotoEdge and |
| 90 | + ( |
| 91 | + tag = EnterFunctionTag() and |
| 92 | + result = this.getInstruction(AliasedDefinitionTag()) |
| 93 | + or |
| 94 | + tag = AliasedDefinitionTag() and |
| 95 | + result = this.getInstruction(InitializeNonLocalTag()) |
| 96 | + or |
| 97 | + tag = InitializeNonLocalTag() and |
| 98 | + result = this.getInstruction(ThisAddressTag()) |
| 99 | + or |
| 100 | + tag = ThisAddressTag() and |
| 101 | + result = this.getInstruction(InitializerStoreTag()) |
| 102 | + or |
| 103 | + tag = InitializerStoreTag() and |
| 104 | + result = this.getInstruction(ThisLoadTag()) |
| 105 | + or |
| 106 | + tag = ThisLoadTag() and |
| 107 | + result = this.getInstruction(InitializerIndirectStoreTag()) |
| 108 | + or |
| 109 | + tag = InitializerIndirectStoreTag() and |
| 110 | + result = this.getInstruction(InitializerFieldAddressTag()) |
| 111 | + ) |
| 112 | + or |
| 113 | + tag = InitializerFieldAddressTag() and |
| 114 | + result = this.getChild(1).getFirstInstruction(kind) |
| 115 | + or |
| 116 | + kind instanceof GotoEdge and |
| 117 | + ( |
| 118 | + tag = ReturnTag() and |
| 119 | + result = this.getInstruction(AliasedUseTag()) |
| 120 | + or |
| 121 | + tag = AliasedUseTag() and |
| 122 | + result = this.getInstruction(ExitFunctionTag()) |
| 123 | + ) |
| 124 | + } |
| 125 | + |
| 126 | + override Instruction getChildSuccessorInternal(TranslatedElement child, EdgeKind kind) { |
| 127 | + child = this.getChild(1) and |
| 128 | + result = this.getInstruction(ReturnTag()) and |
| 129 | + kind instanceof GotoEdge |
| 130 | + } |
| 131 | + |
| 132 | + final override CppType getInstructionMemoryOperandType( |
| 133 | + InstructionTag tag, TypedOperandTag operandTag |
| 134 | + ) { |
| 135 | + tag = AliasedUseTag() and |
| 136 | + operandTag instanceof SideEffectOperandTag and |
| 137 | + result = getUnknownType() |
| 138 | + } |
| 139 | + |
| 140 | + override IRVariable getInstructionVariable(InstructionTag tag) { |
| 141 | + ( |
| 142 | + tag = ThisAddressTag() or |
| 143 | + tag = InitializerStoreTag() or |
| 144 | + tag = InitializerIndirectStoreTag() |
| 145 | + ) and |
| 146 | + result = getIRTempVariable(field, ThisTempVar()) |
| 147 | + } |
| 148 | + |
| 149 | + override Field getInstructionField(InstructionTag tag) { |
| 150 | + tag = InitializerFieldAddressTag() and |
| 151 | + result = field |
| 152 | + } |
| 153 | + |
| 154 | + override predicate hasTempVariable(TempVariableTag tag, CppType type) { |
| 155 | + tag = ThisTempVar() and |
| 156 | + type = this.getThisType() |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Holds if this variable defines or accesses variable `var` with type `type`. This includes all |
| 161 | + * parameters and local variables, plus any global variables or static data members that are |
| 162 | + * directly accessed by the function. |
| 163 | + */ |
| 164 | + final predicate hasUserVariable(Variable varUsed, CppType type) { |
| 165 | + ( |
| 166 | + ( |
| 167 | + varUsed instanceof GlobalOrNamespaceVariable |
| 168 | + or |
| 169 | + varUsed instanceof StaticLocalVariable |
| 170 | + or |
| 171 | + varUsed instanceof MemberVariable and not varUsed instanceof Field |
| 172 | + ) and |
| 173 | + exists(VariableAccess access | |
| 174 | + access.getTarget() = varUsed and |
| 175 | + getEnclosingVariable(access) = field |
| 176 | + ) |
| 177 | + or |
| 178 | + field = varUsed |
| 179 | + or |
| 180 | + varUsed.(LocalScopeVariable).getEnclosingElement*() = field |
| 181 | + or |
| 182 | + varUsed.(Parameter).getCatchBlock().getEnclosingElement*() = field |
| 183 | + ) and |
| 184 | + type = getTypeForPRValue(getVariableType(varUsed)) |
| 185 | + } |
| 186 | + |
| 187 | + override Instruction getInstructionRegisterOperand(InstructionTag tag, OperandTag operandTag) { |
| 188 | + ( |
| 189 | + tag = InitializerStoreTag() |
| 190 | + or |
| 191 | + tag = ThisLoadTag() |
| 192 | + ) and |
| 193 | + operandTag instanceof AddressOperandTag and |
| 194 | + result = this.getInstruction(ThisAddressTag()) |
| 195 | + or |
| 196 | + ( |
| 197 | + tag = InitializerIndirectStoreTag() and |
| 198 | + operandTag instanceof AddressOperandTag |
| 199 | + or |
| 200 | + tag = InitializerFieldAddressTag() and |
| 201 | + operandTag instanceof UnaryOperandTag |
| 202 | + ) and |
| 203 | + result = this.getInstruction(ThisLoadTag()) |
| 204 | + } |
| 205 | + |
| 206 | + override Instruction getTargetAddress() { |
| 207 | + result = this.getInstruction(InitializerFieldAddressTag()) |
| 208 | + } |
| 209 | + |
| 210 | + override Type getTargetType() { result = field.getUnspecifiedType() } |
| 211 | + |
| 212 | + final Instruction getLoadThisInstruction() { result = this.getInstruction(ThisLoadTag()) } |
| 213 | + |
| 214 | + private CppType getThisType() { result = getTypeForGLValue(cls) } |
| 215 | +} |
| 216 | + |
| 217 | +TranslatedNonStaticDataMemberVarInit getTranslatedFieldInit(Field field) { result.getAst() = field } |
0 commit comments