30
30
#include < libsolidity/codegen/CompilerUtils.h>
31
31
#include < libsolidity/codegen/ReturnInfo.h>
32
32
#include < libsolidity/ast/TypeProvider.h>
33
+ #include < libsolidity/ast/ASTUtils.h>
33
34
34
35
#include < libevmasm/GasMeter.h>
35
36
@@ -110,7 +111,7 @@ struct CopyTranslate: public yul::ASTCopier
110
111
solUnimplementedAssert (varDecl, " " );
111
112
string const & suffix = reference.suffix ;
112
113
113
- if (suffix.empty ())
114
+ if (suffix.empty () && !varDecl-> isStateVariable () )
114
115
{
115
116
auto const & var = m_context.localVariable (*varDecl);
116
117
solAssert (var.type ().sizeOnStack () == 1 , " " );
@@ -122,7 +123,48 @@ struct CopyTranslate: public yul::ASTCopier
122
123
}
123
124
124
125
string value;
125
- if (varDecl->isStateVariable ())
126
+ if (varDecl->isConstant ())
127
+ {
128
+ VariableDeclaration const * variable = rootConstVariableDeclaration (*varDecl);
129
+ solAssert (variable, " " );
130
+
131
+ if (variable->value ()->annotation ().type ->category () == Type::Category::RationalNumber)
132
+ {
133
+ u256 intValue = dynamic_cast <RationalNumberType const &>(*variable->value ()->annotation ().type ).literalValue (nullptr );
134
+ if (auto const * bytesType = dynamic_cast <FixedBytesType const *>(variable->type ()))
135
+ intValue <<= 256 - 8 * bytesType->numBytes ();
136
+ else
137
+ solAssert (variable->type ()->category () == Type::Category::Integer, " " );
138
+ value = intValue.str ();
139
+ }
140
+ else if (auto const * literal = dynamic_cast <Literal const *>(variable->value ().get ()))
141
+ {
142
+ TypePointer type = literal->annotation ().type ;
143
+
144
+ switch (type->category ())
145
+ {
146
+ case Type::Category::Bool:
147
+ case Type::Category::Address:
148
+ solAssert (type->category () == variable->annotation ().type ->category (), " " );
149
+ value = toCompactHexWithPrefix (type->literalValue (literal));
150
+ break ;
151
+ case Type::Category::StringLiteral:
152
+ {
153
+ auto const & stringLiteral = dynamic_cast <StringLiteralType const &>(*type);
154
+ solAssert (variable->type ()->category () == Type::Category::FixedBytes, " " );
155
+ unsigned const numBytes = dynamic_cast <FixedBytesType const &>(*variable->type ()).numBytes ();
156
+ solAssert (stringLiteral.value ().size () <= numBytes, " " );
157
+ value = formatNumber (u256 (h256 (stringLiteral.value (), h256::AlignLeft)));
158
+ break ;
159
+ }
160
+ default :
161
+ solAssert (false , " " );
162
+ }
163
+ }
164
+ else
165
+ solAssert (false , " Invalid constant in inline assembly." );
166
+ }
167
+ else if (varDecl->isStateVariable ())
126
168
{
127
169
if (suffix == " slot" )
128
170
value = m_context.storageLocationOfStateVariable (*varDecl).first .str ();
0 commit comments