transformer: minor optimization for `'string literal'.len` (#14207)
							parent
							
								
									59ddaa9ede
								
							
						
					
					
						commit
						da71bced46
					
				|  | @ -3267,8 +3267,7 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) { | ||||||
| 		info := sym.info as ast.ArrayFixed | 		info := sym.info as ast.ArrayFixed | ||||||
| 		g.write('$info.size') | 		g.write('$info.size') | ||||||
| 		return | 		return | ||||||
| 	} | 	} else if sym.kind == .chan && (node.field_name == 'len' || node.field_name == 'closed') { | ||||||
| 	if sym.kind == .chan && (node.field_name == 'len' || node.field_name == 'closed') { |  | ||||||
| 		g.write('sync__Channel_${node.field_name}(') | 		g.write('sync__Channel_${node.field_name}(') | ||||||
| 		g.expr(node.expr) | 		g.expr(node.expr) | ||||||
| 		g.write(')') | 		g.write(')') | ||||||
|  |  | ||||||
|  | @ -1,8 +1,8 @@ | ||||||
| ## Purpose: tests data for the output of V's C code generator | ## Purpose: tests data for the output of V's C code generator | ||||||
| 
 | 
 | ||||||
| ## TLDR: `v vlib/v/gen/c/coutput_test.v` | ## TLDR: `v run vlib/v/gen/c/coutput_test.v` | ||||||
| 
 | 
 | ||||||
| coutput_test.v is a *test runner*, that checks whether the generated C  | coutput_test.v is a *test runner*, that checks whether the generated C | ||||||
| source code matches *all* expectations, specified in *.c.must_have files, | source code matches *all* expectations, specified in *.c.must_have files, | ||||||
| in the folder vlib/v/gen/c/testdata/ . | in the folder vlib/v/gen/c/testdata/ . | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -0,0 +1,2 @@ | ||||||
|  | int a = 5; | ||||||
|  | int b = 7; | ||||||
|  | @ -0,0 +1,2 @@ | ||||||
|  | 5 | ||||||
|  | 7 | ||||||
|  | @ -0,0 +1,6 @@ | ||||||
|  | fn main() { | ||||||
|  | 	a := 'Vlang'.len | ||||||
|  | 	b := r'Vlang\n'.len | ||||||
|  | 	println(a) | ||||||
|  | 	println(b) | ||||||
|  | } | ||||||
|  | @ -636,6 +636,12 @@ pub fn (mut t Transformer) expr(mut node ast.Expr) ast.Expr { | ||||||
| 		} | 		} | ||||||
| 		ast.SelectorExpr { | 		ast.SelectorExpr { | ||||||
| 			node.expr = t.expr(mut node.expr) | 			node.expr = t.expr(mut node.expr) | ||||||
|  | 			if mut node.expr is ast.StringLiteral && node.field_name == 'len' { | ||||||
|  | 				return ast.IntegerLiteral{ | ||||||
|  | 					val: node.expr.val.len.str() | ||||||
|  | 					pos: node.pos | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
| 		} | 		} | ||||||
| 		ast.SizeOf { | 		ast.SizeOf { | ||||||
| 			node.expr = t.expr(mut node.expr) | 			node.expr = t.expr(mut node.expr) | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue