C Compiler Anatomy _4.2 semantic Check _ Expression semantic Check (5) _ Struct member selection

Source: Internet
Author: User

4.2.5 member selection operator

In the C language, struct struct and union union are called record type recordtype, in suffix expressions such as dt.a and ptr->a, operators. And--are called member selection operators. The function checkmemberaccess () is used for semantic checking of these expressions, as shown in code 4.2.28. In the expression dt.a, DT and A are equivalent to operators. The two operands, the type of the corresponding syntax tree node of DT should be the record type, which is judged by the 8th line of the figure 4.2.28, whereas in an expression such as ptr->a, the PTR corresponding node type should be a pointer to "record". This is checked by the IF statement on line 15th. Line 4th is used for semantic checking of the left operand of the member selection operator, and the struct object dt can be treated as an lvalue, so dt.a can also act as an lvalue, but since the return value of a function is only stored in a temporary variable, it cannot act as an lvalue, so GetData (). for Ptr->a, the left operand does not have to be an lvalue, even if it is a temporary variable, for example, in the expression (Data *) ptr2)->a, the Forced Transformation result ((data*) ptr2) is stored in a temporary variable, but PTR or (data * The value of PTR2) represents the address of an addressable memory unit, so we can use Ptr->a as an lvalue, and line 19th completes this setting. Of course, even if the expression dt.a or ptr->a corresponds to the Lvalue field of the syntax tree node is 1, when assigning a value to Dt.a or ptr->a, we also examine its type information to see if there is a const qualifier, as shown in the comment on line 27th. If the DT variable is defined with a const qualifier, then DT.A is also equivalent to the Const qualifier, and the 28th line of the qualify () function is used to add the type qualifier. We also need to check to see if there is a domain member A in the struct data, and the Lookupfield (Type Ty,char *id) function of line 21st is used for this purpose. In the 2nd Chapter we give the "figure 2.4.4 structure", it is not too difficult to understand this function. If member A is not found in the definition of the struct, it indicates that dt.a is an illegal expression, and the error is in line 23rd. The type information we get from the query about domain member A is stored in the code on line 29th, on the syntax tree node corresponding to the DT.A expression. The struct struct filed, which corresponds to the field on line 3rd, describes the information about the domain member, which we introduced in the "type description of Figure 2.4.3 struct" in chapter 2nd, which is not repeated here.


Figure 4.2.28 Checkmemberaccess ()

Figure 4.2.29 gives the syntax tree of expression dt.a after parsing and semantic checking, and the OP field is Op_ The semantic check of the syntax tree node of ID is done by the Checkprimaryexpression function in Exprchk.c, and after the semantic check, the offset of the domain member a in struct-struct data is determined by the struct in the graph. The filed object is saved, and the symbol for the structure object DT is stored by the struct symbol object.

Figure 4.2.29 Syntax tree for member selection operations

Let's take a look at the semantic checks for "post-gaga" and "post-decrement" expressions, such as a++ and a--。 The code associated with it is shown in 4.2.30. From the comments in lines No. 396 and 397 of Figure 4.2.30, we know that the UCC compiler will convert a++ and ++a to A+=1, and a--and--a to A-=1, line No. 394 creates a syntax tree node, and No. 398 line has its OP field op_add_. ASSIGN or op_sub_assign, or + = or-=, in line No. 402 we call Checkexpression () to perform a semantic check on the assignment operator + = or-=. It should be noted that the OP field of the node expr still holds the original "post-gaga op_postinc" or "post-decrement op_postdec" operators, so that when generating the code, we can differentiate between a++, a--、 ++a, or-a, resulting in different code.


Figure 4.2.30 Transformincrement ()

At this point, after some twists and turns, we have completed the semantic checking of all suffix expressions. In subsequent chapters, we will examine the unary and two-tuple expressions for semantics. Sinking boat side thousand sails, let us continue to sail forward.

C Compiler Anatomy _4.2 semantic Check _ Expression semantic Check (5) _ Struct member selection

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.