@@ -11,7 +11,7 @@ namespace LFortran {
1111
1212struct AttributeHandler {
1313
14- typedef ASR ::asr_t * (*attribute_eval_callback)(ASR ::symbol_t *, Allocator &,
14+ typedef ASR ::asr_t * (*attribute_eval_callback)(ASR ::expr_t *, Allocator &,
1515 const Location &, Vec<ASR ::expr_t *> &, diag::Diagnostics &);
1616
1717 std::map<std::string, attribute_eval_callback> attribute_map;
@@ -41,10 +41,9 @@ struct AttributeHandler {
4141 return " " ;
4242 }
4343
44- ASR ::asr_t * get_attribute (ASR ::symbol_t *s , std::string attr_name,
44+ ASR ::asr_t * get_attribute (ASR ::expr_t *e , std::string attr_name,
4545 Allocator &al, const Location &loc, Vec<ASR ::expr_t *> &args, diag::Diagnostics &diag) {
46- ASR ::Variable_t *v = ASR ::down_cast<ASR ::Variable_t>(s);
47- ASR ::ttype_t *type = v->m_type ;
46+ ASR ::ttype_t *type = ASRUtils::expr_type (e);
4847 std::string class_name = get_type_name (type);
4948 if (class_name == " " ) {
5049 throw SemanticError (" Type name is not implemented yet." , loc);
@@ -53,21 +52,20 @@ struct AttributeHandler {
5352 auto search = attribute_map.find (key);
5453 if (search != attribute_map.end ()) {
5554 attribute_eval_callback cb = search->second ;
56- return cb (s , al, loc, args, diag);
55+ return cb (e , al, loc, args, diag);
5756 } else {
5857 throw SemanticError (class_name + " ." + attr_name + " is not implemented yet" ,
5958 loc);
6059 }
6160 }
6261
63- static ASR ::asr_t * eval_list_append (ASR ::symbol_t *s, Allocator &al, const Location &loc,
62+ static ASR ::asr_t * eval_list_append (ASR ::expr_t *s, Allocator &al, const Location &loc,
6463 Vec<ASR ::expr_t *> &args, diag::Diagnostics &diag) {
6564 if (args.size () != 1 ) {
6665 throw SemanticError (" append() takes exactly one argument" ,
6766 loc);
6867 }
69- ASR ::Variable_t *v = ASR ::down_cast<ASR ::Variable_t>(s);
70- ASR ::ttype_t *type = v->m_type ;
68+ ASR ::ttype_t *type = ASRUtils::expr_type (s);
7169 ASR ::ttype_t *list_type = ASR ::down_cast<ASR ::List_t>(type)->m_type ;
7270 ASR ::ttype_t *ele_type = ASRUtils::expr_type (args[0 ]);
7371 if (!ASRUtils::check_equal_type (ele_type, list_type)) {
@@ -85,14 +83,13 @@ struct AttributeHandler {
8583 return make_ListAppend_t (al, loc, s, args[0 ]);
8684 }
8785
88- static ASR ::asr_t * eval_list_remove (ASR ::symbol_t *s, Allocator &al, const Location &loc,
86+ static ASR ::asr_t * eval_list_remove (ASR ::expr_t *s, Allocator &al, const Location &loc,
8987 Vec<ASR ::expr_t *> &args, diag::Diagnostics &diag) {
9088 if (args.size () != 1 ) {
9189 throw SemanticError (" remove() takes exactly one argument" ,
9290 loc);
9391 }
94- ASR ::Variable_t *v = ASR ::down_cast<ASR ::Variable_t>(s);
95- ASR ::ttype_t *type = v->m_type ;
92+ ASR ::ttype_t *type = ASRUtils::expr_type (s);
9693 ASR ::ttype_t *list_type = ASR ::down_cast<ASR ::List_t>(type)->m_type ;
9794 ASR ::ttype_t *ele_type = ASRUtils::expr_type (args[0 ]);
9895 if (!ASRUtils::check_equal_type (ele_type, list_type)) {
@@ -110,7 +107,7 @@ struct AttributeHandler {
110107 return make_ListRemove_t (al, loc, s, args[0 ]);
111108 }
112109
113- static ASR ::asr_t * eval_list_insert (ASR ::symbol_t *s, Allocator &al, const Location &loc,
110+ static ASR ::asr_t * eval_list_insert (ASR ::expr_t *s, Allocator &al, const Location &loc,
114111 Vec<ASR ::expr_t *> &args, diag::Diagnostics &diag) {
115112 if (args.size () != 2 ) {
116113 throw SemanticError (" insert() takes exactly two arguments" ,
@@ -125,8 +122,7 @@ struct AttributeHandler {
125122 }
126123
127124 ASR ::ttype_t *ele_type = ASRUtils::expr_type (args[1 ]);
128- ASR ::Variable_t *v = ASR ::down_cast<ASR ::Variable_t>(s);
129- ASR ::ttype_t *type = v->m_type ;
125+ ASR ::ttype_t *type = ASRUtils::expr_type (s);
130126 ASR ::ttype_t *list_type = ASR ::down_cast<ASR ::List_t>(type)->m_type ;
131127 if (!ASRUtils::check_equal_type (ele_type, list_type)) {
132128 std::string fnd = ASRUtils::type_to_str_python (ele_type);
@@ -143,7 +139,7 @@ struct AttributeHandler {
143139 return make_ListInsert_t (al, loc, s, args[0 ], args[1 ]);
144140 }
145141
146- static ASR ::asr_t * eval_list_pop (ASR ::symbol_t *s, Allocator &al, const Location &loc,
142+ static ASR ::asr_t * eval_list_pop (ASR ::expr_t *s, Allocator &al, const Location &loc,
147143 Vec<ASR ::expr_t *> &args, diag::Diagnostics &diag) {
148144 if (args.size () > 1 ) {
149145 throw SemanticError (" pop() takes atmost one argument" ,
@@ -152,8 +148,7 @@ struct AttributeHandler {
152148 ASR ::expr_t *idx = nullptr ;
153149 ASR ::ttype_t *int_type = ASRUtils::TYPE (ASR::make_Integer_t (al, loc,
154150 4 , nullptr , 0 ));
155- ASR ::Variable_t *v = ASR ::down_cast<ASR ::Variable_t>(s);
156- ASR ::ttype_t *type = v->m_type ;
151+ ASR ::ttype_t *type = ASRUtils::expr_type (s);
157152 ASR ::ttype_t *list_type = ASR ::down_cast<ASR ::List_t>(type)->m_type ;
158153 if (args.size () == 1 ) {
159154 ASR ::ttype_t *pos_type = ASRUtils::expr_type (args[0 ]);
@@ -178,28 +173,24 @@ struct AttributeHandler {
178173 return make_ListPop_t (al, loc, s, idx, list_type, nullptr );
179174 }
180175
181- static ASR ::asr_t * eval_set_pop (ASR ::symbol_t *s, Allocator &al, const Location &loc,
176+ static ASR ::asr_t * eval_set_pop (ASR ::expr_t *s, Allocator &al, const Location &loc,
182177 Vec<ASR ::expr_t *> &args, diag::Diagnostics &/* diag*/ ) {
183178 if (args.size () != 0 ) {
184179 throw SemanticError (" pop() takes no arguments (" + std::to_string (args.size ()) + " given)" , loc);
185180 }
186-
187- ASR ::Variable_t *v = ASR ::down_cast<ASR ::Variable_t>(s);
188-
189- ASR ::ttype_t *type = v->m_type ;
181+ ASR ::ttype_t *type = ASRUtils::expr_type (s);
190182 ASR ::ttype_t *set_type = ASR ::down_cast<ASR ::Set_t>(type)->m_type ;
191183
192184 return make_SetPop_t (al, loc, s, set_type, nullptr );
193185 }
194186
195- static ASR ::asr_t * eval_set_add (ASR ::symbol_t *s, Allocator &al, const Location &loc,
187+ static ASR ::asr_t * eval_set_add (ASR ::expr_t *s, Allocator &al, const Location &loc,
196188 Vec<ASR ::expr_t *> &args, diag::Diagnostics &diag) {
197189 if (args.size () != 1 ) {
198190 throw SemanticError (" add() takes exactly one argument" , loc);
199191 }
200192
201- ASR ::Variable_t *v = ASR ::down_cast<ASR ::Variable_t>(s);
202- ASR ::ttype_t *type = v->m_type ;
193+ ASR ::ttype_t *type = ASRUtils::expr_type (s);
203194 ASR ::ttype_t *set_type = ASR ::down_cast<ASR ::Set_t>(type)->m_type ;
204195 ASR ::ttype_t *ele_type = ASRUtils::expr_type (args[0 ]);
205196 if (!ASRUtils::check_equal_type (ele_type, set_type)) {
@@ -218,14 +209,13 @@ struct AttributeHandler {
218209 return make_SetInsert_t (al, loc, s, args[0 ]);
219210 }
220211
221- static ASR ::asr_t * eval_set_remove (ASR ::symbol_t *s, Allocator &al, const Location &loc,
212+ static ASR ::asr_t * eval_set_remove (ASR ::expr_t *s, Allocator &al, const Location &loc,
222213 Vec<ASR ::expr_t *> &args, diag::Diagnostics &diag) {
223214 if (args.size () != 1 ) {
224215 throw SemanticError (" remove() takes exactly one argument" , loc);
225216 }
226217
227- ASR ::Variable_t *v = ASR ::down_cast<ASR ::Variable_t>(s);
228- ASR ::ttype_t *type = v->m_type ;
218+ ASR ::ttype_t *type = ASRUtils::expr_type (s);
229219 ASR ::ttype_t *set_type = ASR ::down_cast<ASR ::Set_t>(type)->m_type ;
230220 ASR ::ttype_t *ele_type = ASRUtils::expr_type (args[0 ]);
231221 if (!ASRUtils::check_equal_type (ele_type, set_type)) {
@@ -244,15 +234,14 @@ struct AttributeHandler {
244234 return make_SetRemove_t (al, loc, s, args[0 ]);
245235 }
246236
247- static ASR ::asr_t * eval_dict_get (ASR ::symbol_t *s, Allocator &al, const Location &loc,
237+ static ASR ::asr_t * eval_dict_get (ASR ::expr_t *s, Allocator &al, const Location &loc,
248238 Vec<ASR ::expr_t *> &args, diag::Diagnostics &diag) {
249239 ASR ::expr_t *def = nullptr ;
250240 if (args.size () > 2 || args.size () < 1 ) {
251241 throw SemanticError (" 'get' takes atleast 1 and atmost 2 arguments" ,
252242 loc);
253243 }
254- ASR ::Variable_t *v = ASR ::down_cast<ASR ::Variable_t>(s);
255- ASR ::ttype_t *type = v->m_type ;
244+ ASR ::ttype_t *type = ASRUtils::expr_type (s);
256245 ASR ::ttype_t *key_type = ASR ::down_cast<ASR ::Dict_t>(type)->m_key_type ;
257246 ASR ::ttype_t *value_type = ASR ::down_cast<ASR ::Dict_t>(type)->m_value_type ;
258247 if (args.size () == 2 ) {
@@ -285,13 +274,12 @@ struct AttributeHandler {
285274 return make_DictItem_t (al, loc, s, args[0 ], def, value_type, nullptr );
286275 }
287276
288- static ASR ::asr_t * eval_dict_pop (ASR ::symbol_t *s, Allocator &al, const Location &loc,
277+ static ASR ::asr_t * eval_dict_pop (ASR ::expr_t *s, Allocator &al, const Location &loc,
289278 Vec<ASR ::expr_t *> &args, diag::Diagnostics &diag) {
290279 if (args.size () != 1 ) {
291280 throw SemanticError (" 'pop' takes only one argument for now" , loc);
292281 }
293- ASR ::Variable_t *v = ASR ::down_cast<ASR ::Variable_t>(s);
294- ASR ::ttype_t *type = v->m_type ;
282+ ASR ::ttype_t *type = ASRUtils::expr_type (s);
295283 ASR ::ttype_t *key_type = ASR ::down_cast<ASR ::Dict_t>(type)->m_key_type ;
296284 ASR ::ttype_t *value_type = ASR ::down_cast<ASR ::Dict_t>(type)->m_value_type ;
297285 if (!ASRUtils::check_equal_type (ASRUtils::expr_type (args[0 ]), key_type)) {
0 commit comments