Line data Source code
1 : /* valastringliteral.vala
2 : *
3 : * Copyright (C) 2006-2011 Jürg Billeter
4 : *
5 : * This library is free software; you can redistribute it and/or
6 : * modify it under the terms of the GNU Lesser General Public
7 : * License as published by the Free Software Foundation; either
8 : * version 2.1 of the License, or (at your option) any later version.
9 :
10 : * This library is distributed in the hope that it will be useful,
11 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : * Lesser General Public License for more details.
14 :
15 : * You should have received a copy of the GNU Lesser General Public
16 : * License along with this library; if not, write to the Free Software
17 : * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 : *
19 : * Author:
20 : * Jürg Billeter <j@bitron.ch>
21 : */
22 :
23 : using GLib;
24 :
25 : /**
26 : * Represents a string literal in the source code.
27 : */
28 1537 : public class Vala.StringLiteral : Literal {
29 : /**
30 : * The literal value.
31 : */
32 290174 : public string value { get; set; }
33 :
34 13100 : public bool translate { get; set; }
35 :
36 : /**
37 : * Creates a new string literal.
38 : *
39 : * @param value the literal value
40 : * @param source_reference reference to source code
41 : * @return newly created string literal
42 : */
43 277113 : public StringLiteral (string value, SourceReference? source_reference = null) {
44 92371 : this.value = value;
45 92371 : this.source_reference = source_reference;
46 : }
47 :
48 : /**
49 : * Evaluates the literal string value.
50 : *
51 : * @return the unescaped string
52 : */
53 7638 : public string? eval () {
54 7638 : if (value == null) {
55 0 : return null;
56 : }
57 :
58 : /* remove quotes */
59 7638 : var noquotes = value.substring (1, (uint) (value.length - 2));
60 : /* unescape string */
61 7638 : return noquotes.compress ();
62 : }
63 :
64 258827 : public override void accept (CodeVisitor visitor) {
65 258827 : visitor.visit_string_literal (this);
66 :
67 258827 : visitor.visit_expression (this);
68 : }
69 :
70 0 : public override bool is_pure () {
71 0 : return true;
72 : }
73 :
74 13216 : public override bool is_non_null () {
75 13216 : return true;
76 : }
77 :
78 0 : public override string to_string () {
79 0 : return value;
80 : }
81 :
82 110230 : public override bool check (CodeContext context) {
83 110230 : if (checked) {
84 23846 : return !error;
85 : }
86 :
87 86384 : checked = true;
88 :
89 86384 : value_type = context.analyzer.string_type.copy ();
90 :
91 86384 : return !error;
92 : }
93 :
94 13016 : public override void emit (CodeGenerator codegen) {
95 13016 : codegen.visit_string_literal (this);
96 :
97 13016 : codegen.visit_expression (this);
98 : }
99 :
100 7568 : public static StringLiteral? get_format_literal (Expression expr) {
101 7568 : unowned StringLiteral? format_literal = expr as StringLiteral;
102 7466 : if (format_literal != null) {
103 7466 : return format_literal;
104 : }
105 :
106 102 : unowned MethodCall? call = expr as MethodCall;
107 11 : if (call != null) {
108 11 : return call.get_format_literal ();
109 : }
110 :
111 7568 : return null;
112 : }
113 : }
|