|
18 | 18 | from sympy import Integer as sp_Integer |
19 | 19 | from sympy import ceiling |
20 | 20 |
|
| 21 | +from textx.exceptions import TextXSyntaxError |
| 22 | + |
21 | 23 | #============================================================================== |
22 | 24 | from pyccel.utilities.strings import random_string |
23 | 25 | from pyccel.ast.basic import PyccelAstNode, TypedAstNode, ScopedAstNode |
|
139 | 141 |
|
140 | 142 | from pyccel.parser.base import BasicParser |
141 | 143 | from pyccel.parser.syntactic import SyntaxParser |
| 144 | +from pyccel.parser.syntax.headers import types_meta |
142 | 145 |
|
143 | 146 | from pyccel.utilities.stage import PyccelStage |
144 | 147 |
|
@@ -922,6 +925,12 @@ def _create_PyccelOperator(self, expr, visited_args): |
922 | 925 | The new operator. |
923 | 926 | """ |
924 | 927 | arg1 = visited_args[0] |
| 928 | + if isinstance(arg1, FunctionDef): |
| 929 | + msg = ("Function found in a mathematical operation. " |
| 930 | + "Are you trying to declare a type? " |
| 931 | + "If so then the type object must be used as a type hint.") |
| 932 | + errors.report(msg, |
| 933 | + severity='fatal', symbol=expr) |
925 | 934 | class_type = arg1.class_type |
926 | 935 | class_base = self.scope.find(str(class_type), 'classes') or get_cls_base(class_type) |
927 | 936 | magic_method_name = magic_method_map.get(type(expr), None) |
@@ -3289,10 +3298,19 @@ def _visit_Assign(self, expr): |
3289 | 3298 | lhs = lhs.name |
3290 | 3299 |
|
3291 | 3300 | if semantic_lhs_var.class_type is TypeAlias(): |
3292 | | - if not isinstance(rhs, SyntacticTypeAnnotation): |
3293 | | - pyccel_stage.set_stage('syntactic') |
| 3301 | + pyccel_stage.set_stage('syntactic') |
| 3302 | + if isinstance(rhs, LiteralString): |
| 3303 | + try: |
| 3304 | + annotation = types_meta.model_from_str(rhs.python_value) |
| 3305 | + except TextXSyntaxError as e: |
| 3306 | + errors.report(f"Invalid header. {e.message}", |
| 3307 | + symbol = expr, severity = 'fatal') |
| 3308 | + rhs = annotation.expr |
| 3309 | + rhs.set_current_ast(expr.python_ast) |
| 3310 | + elif not isinstance(rhs, (SyntacticTypeAnnotation, FunctionTypeAnnotation, |
| 3311 | + VariableTypeAnnotation, UnionTypeAnnotation)): |
3294 | 3312 | rhs = SyntacticTypeAnnotation(rhs) |
3295 | | - pyccel_stage.set_stage('semantic') |
| 3313 | + pyccel_stage.set_stage('semantic') |
3296 | 3314 | type_annot = self._visit(rhs) |
3297 | 3315 | self.scope.insert_symbolic_alias(lhs, type_annot) |
3298 | 3316 | return EmptyNode() |
@@ -4154,26 +4172,37 @@ def unpack(ann): |
4154 | 4172 | templates = {t: v for t,v in templates.items() if t in used_type_names} |
4155 | 4173 |
|
4156 | 4174 | # Create new temparary templates for the arguments with a Union data type. |
4157 | | - pyccel_stage.set_stage('syntactic') |
4158 | 4175 | tmp_templates = {} |
4159 | 4176 | new_expr_args = [] |
4160 | 4177 | for a in expr.arguments: |
4161 | | - if isinstance(a.annotation, UnionTypeAnnotation): |
4162 | | - annotation = [aa for a in a.annotation for aa in unpack(a)] |
| 4178 | + annot = a.annotation |
| 4179 | + if isinstance(annot, UnionTypeAnnotation): |
| 4180 | + annotation = [aa for a in annot for aa in unpack(a)] |
| 4181 | + elif isinstance(annot, SyntacticTypeAnnotation): |
| 4182 | + elem = annot.dtype |
| 4183 | + if isinstance(elem, IndexedElement): |
| 4184 | + elem = [elem.base] + [a.dtype for a in elem.indices if isinstance(a, SyntacticTypeAnnotation)] |
| 4185 | + else: |
| 4186 | + elem = [elem] |
| 4187 | + if all(e not in templates for e in elem): |
| 4188 | + annotation = unpack(self._visit(annot)) |
| 4189 | + else: |
| 4190 | + annotation = [annot] |
4163 | 4191 | else: |
4164 | | - annotation = [a.annotation] |
| 4192 | + annotation = [annot] |
4165 | 4193 | if len(annotation)>1: |
4166 | 4194 | tmp_template_name = a.name + '_' + random_string(12) |
4167 | 4195 | tmp_template_name = self.scope.get_new_name(tmp_template_name) |
| 4196 | + pyccel_stage.set_stage('syntactic') |
4168 | 4197 | tmp_templates[tmp_template_name] = UnionTypeAnnotation(*[self._visit(vi) for vi in annotation]) |
4169 | 4198 | dtype_symb = PyccelSymbol(tmp_template_name, is_temp=True) |
4170 | 4199 | dtype_symb = SyntacticTypeAnnotation(dtype_symb) |
4171 | 4200 | var_clone = AnnotatedPyccelSymbol(a.var.name, annotation=dtype_symb, is_temp=a.var.name.is_temp) |
4172 | 4201 | new_expr_args.append(FunctionDefArgument(var_clone, bound_argument=a.bound_argument, |
4173 | 4202 | value=a.value, kwonly=a.is_kwonly, annotation=dtype_symb)) |
| 4203 | + pyccel_stage.set_stage('semantic') |
4174 | 4204 | else: |
4175 | 4205 | new_expr_args.append(a) |
4176 | | - pyccel_stage.set_stage('semantic') |
4177 | 4206 |
|
4178 | 4207 | templates.update(tmp_templates) |
4179 | 4208 | template_combinations = list(product(*[v.type_list for v in templates.values()])) |
|
0 commit comments