Compiler Development Series -- Ocelot language 5. Expression validity check, -- ocelot Validity

Source: Internet
Author: User

Compiler Development Series -- Ocelot language 5. Expression validity check, -- ocelot Validity

This article will check the incorrect expressions that cannot be evaluated as "1 = 3" "& 5.

The following problems will be checked.
● Assign values to expressions that cannot be assigned values (for example, 1 = 2 + 2)
● Use an invalid function name to call a function (for example, "string" ("% d \ n", I ))
● Array reference with invalid operands (for example, 1 [0])
● Member reference with invalid operands (for example, 1. memb)
● Indirect reference of an Invalid Pointer (for example, 1-> memb)
● Values of non-pointer objects (for example, * 1)
● Obtain the address of a non-left expression

See Table 10.1 For examples and methods of problem detection.

Check for non-pointer type value operations

/* Check for non-pointer type value operations * indicates the processing of the DereferenceNode of the value operator. * This method checks whether the type of the operand of the value operator is a pointer. * // # @ Range/DereferenceNode {public Void visit (DereferenceNode node) {/** first, use super. visit (node) calls the base-class Visitor method to traverse the operands (node. expr () (that is, check the operand ). */Super. visit (node);/** next, call the isPointer method of the operand node. expr () to check whether the type of the operand is a pointer, that is, whether the value can be taken. If the value cannot be set, the undereferableError method is called to output a compilation error. */If (! Node. expr (). isPointer () {undereferableError (node. location ();}/** Finally, call the handleImplicitAddress method to handle the array type and function type. This processing is also related to the processing of the next AddressNode, */handleImplicitAddress (node); return null ;}

Check for obtaining non-left expression addresses

/* Check for obtaining non-left expression addresses * check if the operand is left. AddressNode processing of the address operator * // # @ range/AddressNode {public Void visit (AddressNode node) {super. visit (node);/** first. expr () calls the isLvalue method to check whether expr in & expr is an expression that can perform the address fetch operation. ExprNode # isLvalue is a method to check whether the expression of the node can obtain the address. */If (! Node. expr (). isLvalue () {semanticError (node. location (), "invalid expression for &") ;}/ ** the remaining statements are used to determine the AddressNode type. Generally, node. expr (). isLoadable () returns true, that is, the else processing is executed. The & expr type is a pointer to the expr type. Therefore, the pointer type pointing to node. expr (). type () can be used as the overall type of the node. */Type base = node. expr (). type ();/** when you set the puts type to the pointer to the function, you must also set the & puts type to the pointer to the function. The node. expr () type is specially processed when arrays or functions are used, so that the type of & puts is consistent with that of puts. */If (! Node. expr (). isLoadable () {// node. expr. type is already pointer. node. setType (base);} else {node. setType (typeTable. pointerTo (base);} return null ;}

Implicit pointer generation

A single array or function type variable represents the address of an array or function. For example, if the type of the variable puts is a function type (generally called a function pointer), the values of puts and & puts are the same.

/** The handleImplicitAddress method converts the array type or function type to a pointer to an array or function type, that is, the pointer type is implicitly generated. */Private void handleImplicitAddress (LHSNode node) {if (! Node. isLoadable () {Type t = node. type (); if (t. isArray () {// int [4] ary; shoshould generate int * node. setType (typeTable. pointerTo (t. baseType ();} else {node. setType (typeTable. pointerTo (t ));}}}

Puts is a pointer to a function, so the result of its value calculation * puts is a function type, but it is implicitly converted to a pointer to a function. * Puts still points to the pointer of the function. Therefore, you can still perform value calculation and convert it to the pointer to the function. Such as this can be repeated infinitely. In C, the values of "& puts", "puts", "* puts", and "** puts" are the same.

 

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.