@@ -73,15 +73,15 @@ class TranslatedFunction extends TranslatedElement, TTranslatedFunction {
7373 final override Function getFunction ( ) { result = func }
7474
7575 final override TranslatedElement getChild ( int id ) {
76- id = - 4 and result = getReadEffects ( )
76+ id = - 5 and result = getReadEffects ( )
7777 or
78- id = - 3 and result = getConstructorInitList ( )
78+ id = - 4 and result = getConstructorInitList ( )
7979 or
80- id = - 2 and result = getBody ( )
80+ id = - 3 and result = getBody ( )
8181 or
82- id = - 1 and result = getDestructorDestructionList ( )
82+ id = - 2 and result = getDestructorDestructionList ( )
8383 or
84- id >= 0 and result = getParameter ( id )
84+ id >= - 1 and result = getParameter ( id )
8585 }
8686
8787 final private TranslatedConstructorInitList getConstructorInitList ( ) {
@@ -97,6 +97,9 @@ class TranslatedFunction extends TranslatedElement, TTranslatedFunction {
9797 final private TranslatedReadEffects getReadEffects ( ) { result = getTranslatedReadEffects ( func ) }
9898
9999 final private TranslatedParameter getParameter ( int index ) {
100+ result = getTranslatedThisParameter ( func ) and
101+ index = - 1
102+ or
100103 result = getTranslatedParameter ( func .getParameter ( index ) )
101104 or
102105 index = getEllipsisParameterIndexForFunction ( func ) and
@@ -117,20 +120,13 @@ class TranslatedFunction extends TranslatedElement, TTranslatedFunction {
117120 (
118121 tag = InitializeNonLocalTag ( ) and
119122 if exists ( getThisType ( ) )
120- then result = getInstruction ( InitializeThisTag ( ) )
123+ then result = getParameter ( - 1 ) . getFirstInstruction ( )
121124 else
122125 if exists ( getParameter ( 0 ) )
123126 then result = getParameter ( 0 ) .getFirstInstruction ( )
124127 else result = getBody ( ) .getFirstInstruction ( )
125128 )
126129 or
127- (
128- tag = InitializeThisTag ( ) and
129- if exists ( getParameter ( 0 ) )
130- then result = getParameter ( 0 ) .getFirstInstruction ( )
131- else result = getConstructorInitList ( ) .getFirstInstruction ( )
132- )
133- or
134130 tag = ReturnValueAddressTag ( ) and
135131 result = getInstruction ( ReturnTag ( ) )
136132 or
@@ -184,10 +180,6 @@ class TranslatedFunction extends TranslatedElement, TTranslatedFunction {
184180 opcode instanceof Opcode:: InitializeNonLocal and
185181 resultType = getUnknownType ( )
186182 or
187- tag = InitializeThisTag ( ) and
188- opcode instanceof Opcode:: InitializeThis and
189- resultType = getTypeForGLValue ( getThisType ( ) )
190- or
191183 tag = ReturnValueAddressTag ( ) and
192184 opcode instanceof Opcode:: VariableAddress and
193185 resultType = getTypeForGLValue ( getReturnType ( ) ) and
@@ -228,10 +220,8 @@ class TranslatedFunction extends TranslatedElement, TTranslatedFunction {
228220 final override Instruction getInstructionRegisterOperand ( InstructionTag tag , OperandTag operandTag ) {
229221 tag = ReturnTag ( ) and
230222 hasReturnValue ( ) and
231- (
232- operandTag instanceof AddressOperandTag and
233- result = getInstruction ( ReturnValueAddressTag ( ) )
234- )
223+ operandTag instanceof AddressOperandTag and
224+ result = getInstruction ( ReturnValueAddressTag ( ) )
235225 }
236226
237227 final override CppType getInstructionMemoryOperandType (
@@ -264,6 +254,9 @@ class TranslatedFunction extends TranslatedElement, TTranslatedFunction {
264254 tag = EllipsisTempVar ( ) and
265255 func .isVarargs ( ) and
266256 type = getEllipsisVariablePRValueType ( )
257+ or
258+ tag = ThisTempVar ( ) and
259+ type = getTypeForGLValue ( getThisType ( ) )
267260 }
268261
269262 /**
@@ -286,6 +279,11 @@ class TranslatedFunction extends TranslatedElement, TTranslatedFunction {
286279 */
287280 final IREllipsisVariable getEllipsisVariable ( ) { result .getEnclosingFunction ( ) = func }
288281
282+ /**
283+ * Gets the variable that represents the `this` pointer for this function, if any.
284+ */
285+ final IRThisVariable getThisVariable ( ) { result = getIRTempVariable ( func , ThisTempVar ( ) ) }
286+
289287 /**
290288 * Holds if the function has a non-`void` return type.
291289 */
@@ -295,7 +293,9 @@ class TranslatedFunction extends TranslatedElement, TTranslatedFunction {
295293 * Gets the single `InitializeThis` instruction for this function. Holds only
296294 * if the function is an instance member function, constructor, or destructor.
297295 */
298- final Instruction getInitializeThisInstruction ( ) { result = getInstruction ( InitializeThisTag ( ) ) }
296+ final Instruction getInitializeThisInstruction ( ) {
297+ result = getTranslatedThisParameter ( func ) .getInstruction ( InitializerStoreTag ( ) )
298+ }
299299
300300 /**
301301 * Gets the type pointed to by the `this` pointer for this function (i.e. `*this`).
@@ -336,6 +336,11 @@ class TranslatedFunction extends TranslatedElement, TTranslatedFunction {
336336 final Type getReturnType ( ) { result = func .getType ( ) }
337337}
338338
339+ /**
340+ * Gets the `TranslatedThisParameter` for function `func`, if one exists.
341+ */
342+ TranslatedThisParameter getTranslatedThisParameter ( Function func ) { result .getFunction ( ) = func }
343+
339344/**
340345 * Gets the `TranslatedPositionalParameter` that represents parameter `param`.
341346 */
@@ -350,8 +355,9 @@ TranslatedEllipsisParameter getTranslatedEllipsisParameter(Function func) {
350355
351356/**
352357 * The IR translation of a parameter to a function. This can be either a user-declared parameter
353- * (`TranslatedPositionParameter`) or the synthesized parameter used to represent a `...` in a
354- * varargs function (`TranslatedEllipsisParameter`).
358+ * (`TranslatedPositionParameter`), the synthesized parameter used to represent `this`, or the
359+ * synthesized parameter used to represent a `...` in a varargs function
360+ * (`TranslatedEllipsisParameter`).
355361 */
356362abstract class TranslatedParameter extends TranslatedElement {
357363 final override TranslatedElement getChild ( int id ) { none ( ) }
@@ -398,7 +404,7 @@ abstract class TranslatedParameter extends TranslatedElement {
398404 hasIndirection ( ) and
399405 tag = InitializerIndirectStoreTag ( ) and
400406 opcode instanceof Opcode:: InitializeIndirection and
401- resultType = getUnknownType ( )
407+ resultType = getInitializationResultType ( )
402408 }
403409
404410 final override IRVariable getInstructionVariable ( InstructionTag tag ) {
@@ -435,9 +441,43 @@ abstract class TranslatedParameter extends TranslatedElement {
435441
436442 abstract CppType getPRValueType ( ) ;
437443
444+ abstract CppType getInitializationResultType ( ) ;
445+
438446 abstract IRAutomaticVariable getIRVariable ( ) ;
439447}
440448
449+ /**
450+ * The IR translation of the synthesized parameter used to represent the `...` in a varargs
451+ * function.
452+ */
453+ class TranslatedThisParameter extends TranslatedParameter , TTranslatedThisParameter {
454+ Function func ;
455+
456+ TranslatedThisParameter ( ) { this = TTranslatedThisParameter ( func ) }
457+
458+ final override string toString ( ) { result = "this" }
459+
460+ final override Locatable getAST ( ) { result = func }
461+
462+ final override Function getFunction ( ) { result = func }
463+
464+ final override predicate hasIndirection ( ) { any ( ) }
465+
466+ final override CppType getGLValueType ( ) { result = getTypeForGLValue ( any ( UnknownType t ) ) }
467+
468+ final override CppType getPRValueType ( ) {
469+ result = getTypeForGLValue ( getTranslatedFunction ( func ) .getThisType ( ) )
470+ }
471+
472+ final override CppType getInitializationResultType ( ) {
473+ result = getTypeForPRValue ( getTranslatedFunction ( func ) .getThisType ( ) )
474+ }
475+
476+ final override IRThisVariable getIRVariable ( ) {
477+ result = getTranslatedFunction ( func ) .getThisVariable ( )
478+ }
479+ }
480+
441481/**
442482 * Represents the IR translation of a function parameter, including the
443483 * initialization of that parameter with the incoming argument.
@@ -468,6 +508,8 @@ class TranslatedPositionalParameter extends TranslatedParameter, TTranslatedPara
468508
469509 final override CppType getPRValueType ( ) { result = getTypeForPRValue ( getVariableType ( param ) ) }
470510
511+ final override CppType getInitializationResultType ( ) { result = getUnknownType ( ) }
512+
471513 final override IRAutomaticUserVariable getIRVariable ( ) {
472514 result = getIRUserVariable ( getFunction ( ) , param )
473515 }
@@ -494,6 +536,8 @@ class TranslatedEllipsisParameter extends TranslatedParameter, TTranslatedEllips
494536
495537 final override CppType getPRValueType ( ) { result = getEllipsisVariablePRValueType ( ) }
496538
539+ final override CppType getInitializationResultType ( ) { result = getUnknownType ( ) }
540+
497541 final override IREllipsisVariable getIRVariable ( ) {
498542 result = getTranslatedFunction ( func ) .getEllipsisVariable ( )
499543 }
0 commit comments