source: src/ResolvExpr/CommonType.cc @ fc1a3e2

Last change on this file since fc1a3e2 was fc1a3e2, checked in by Andrew Beach <ajbeach@…>, 4 weeks ago

Style update. Focused on indentation and trailing whitespace.

  • Property mode set to 100644
File size: 50.5 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// CommonType.cc --
8//
9// Author           : Richard C. Bilson
10// Created On       : Sun May 17 06:59:27 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Thu Feb 14 17:10:10 2019
13// Update Count     : 24
14//
15
16#include "CommonType.hpp"
17
18#include <cassert>                       // for strict_dynamic_cast
19#include <map>                           // for _Rb_tree_const_iterator
20#include <utility>                       // for pair
21
22#include "AST/Decl.hpp"
23#include "AST/Pass.hpp"
24#include "AST/Type.hpp"
25#include "Unify.h"                       // for unifyExact, WidenMode
26#include "typeops.h"                     // for isFtype
27#include "Tuples/Tuples.h"
28
29// #define DEBUG
30#ifdef DEBUG
31#define PRINT(x) x
32#else
33#define PRINT(x)
34#endif
35
36namespace ResolvExpr {
37
38        // GENERATED START, DO NOT EDIT
39        // GENERATED BY BasicTypes-gen.cc
40        #define BT ast::BasicKind::
41        static const ast::BasicKind commonTypes[BT NUMBER_OF_BASIC_TYPES][BT NUMBER_OF_BASIC_TYPES] = { // nearest common ancestor
42                /*                                      B                       C                      SC                      UC                      SI                     SUI
43                                                        I                      UI                      LI                     LUI                     LLI                    LLUI
44                                                       IB                     UIB                     _FH                     _FH                      _F                     _FC
45                                                        F                      FC                     _FX                    _FXC                      FD                    _FDC
46                                                        D                      DC                    F80X                   _FDXC                     F80                     _FB
47                                                    _FLDC                      FB                      LD                     LDC                    _FBX                  _FLDXC
48                                 */
49                                  {
50                /*      B */                BT Bool,                BT Char,          BT SignedChar,        BT UnsignedChar,      BT ShortSignedInt,    BT ShortUnsignedInt,
51                                             BT SignedInt,         BT UnsignedInt,       BT LongSignedInt,     BT LongUnsignedInt,   BT LongLongSignedInt, BT LongLongUnsignedInt,
52                                          BT SignedInt128,      BT UnsignedInt128,            BT uFloat16,     BT uFloat16Complex,            BT uFloat32,     BT uFloat32Complex,
53                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
54                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
55                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
56                                  },
57                                  {
58                /*      C */                BT Char,                BT Char,          BT SignedChar,        BT UnsignedChar,      BT ShortSignedInt,    BT ShortUnsignedInt,
59                                             BT SignedInt,         BT UnsignedInt,       BT LongSignedInt,     BT LongUnsignedInt,   BT LongLongSignedInt, BT LongLongUnsignedInt,
60                                          BT SignedInt128,      BT UnsignedInt128,            BT uFloat16,     BT uFloat16Complex,            BT uFloat32,     BT uFloat32Complex,
61                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
62                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
63                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
64                                  },
65                                  {
66                /*     SC */          BT SignedChar,          BT SignedChar,          BT SignedChar,        BT UnsignedChar,      BT ShortSignedInt,    BT ShortUnsignedInt,
67                                             BT SignedInt,         BT UnsignedInt,       BT LongSignedInt,     BT LongUnsignedInt,   BT LongLongSignedInt, BT LongLongUnsignedInt,
68                                          BT SignedInt128,      BT UnsignedInt128,            BT uFloat16,     BT uFloat16Complex,            BT uFloat32,     BT uFloat32Complex,
69                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
70                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
71                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
72                                  },
73                                  {
74                /*     UC */        BT UnsignedChar,        BT UnsignedChar,        BT UnsignedChar,        BT UnsignedChar,      BT ShortSignedInt,    BT ShortUnsignedInt,
75                                             BT SignedInt,         BT UnsignedInt,       BT LongSignedInt,     BT LongUnsignedInt,   BT LongLongSignedInt, BT LongLongUnsignedInt,
76                                          BT SignedInt128,      BT UnsignedInt128,            BT uFloat16,     BT uFloat16Complex,            BT uFloat32,     BT uFloat32Complex,
77                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
78                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
79                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
80                                  },
81                                  {
82                /*     SI */      BT ShortSignedInt,      BT ShortSignedInt,      BT ShortSignedInt,      BT ShortSignedInt,      BT ShortSignedInt,    BT ShortUnsignedInt,
83                                             BT SignedInt,         BT UnsignedInt,       BT LongSignedInt,     BT LongUnsignedInt,   BT LongLongSignedInt, BT LongLongUnsignedInt,
84                                          BT SignedInt128,      BT UnsignedInt128,            BT uFloat16,     BT uFloat16Complex,            BT uFloat32,     BT uFloat32Complex,
85                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
86                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
87                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
88                                  },
89                                  {
90                /*    SUI */    BT ShortUnsignedInt,    BT ShortUnsignedInt,    BT ShortUnsignedInt,    BT ShortUnsignedInt,    BT ShortUnsignedInt,    BT ShortUnsignedInt,
91                                             BT SignedInt,         BT UnsignedInt,       BT LongSignedInt,     BT LongUnsignedInt,   BT LongLongSignedInt, BT LongLongUnsignedInt,
92                                          BT SignedInt128,      BT UnsignedInt128,            BT uFloat16,     BT uFloat16Complex,            BT uFloat32,     BT uFloat32Complex,
93                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
94                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
95                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
96                                  },
97                                  {
98                /*      I */           BT SignedInt,           BT SignedInt,           BT SignedInt,           BT SignedInt,           BT SignedInt,           BT SignedInt,
99                                             BT SignedInt,         BT UnsignedInt,       BT LongSignedInt,     BT LongUnsignedInt,   BT LongLongSignedInt, BT LongLongUnsignedInt,
100                                          BT SignedInt128,      BT UnsignedInt128,            BT uFloat16,     BT uFloat16Complex,            BT uFloat32,     BT uFloat32Complex,
101                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
102                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
103                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
104                                  },
105                                  {
106                /*     UI */         BT UnsignedInt,         BT UnsignedInt,         BT UnsignedInt,         BT UnsignedInt,         BT UnsignedInt,         BT UnsignedInt,
107                                           BT UnsignedInt,         BT UnsignedInt,       BT LongSignedInt,     BT LongUnsignedInt,   BT LongLongSignedInt, BT LongLongUnsignedInt,
108                                          BT SignedInt128,      BT UnsignedInt128,            BT uFloat16,     BT uFloat16Complex,            BT uFloat32,     BT uFloat32Complex,
109                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
110                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
111                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
112                                  },
113                                  {
114                /*     LI */       BT LongSignedInt,       BT LongSignedInt,       BT LongSignedInt,       BT LongSignedInt,       BT LongSignedInt,       BT LongSignedInt,
115                                         BT LongSignedInt,       BT LongSignedInt,       BT LongSignedInt,     BT LongUnsignedInt,   BT LongLongSignedInt, BT LongLongUnsignedInt,
116                                          BT SignedInt128,      BT UnsignedInt128,            BT uFloat16,     BT uFloat16Complex,            BT uFloat32,     BT uFloat32Complex,
117                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
118                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
119                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
120                                  },
121                                  {
122                /*    LUI */     BT LongUnsignedInt,     BT LongUnsignedInt,     BT LongUnsignedInt,     BT LongUnsignedInt,     BT LongUnsignedInt,     BT LongUnsignedInt,
123                                       BT LongUnsignedInt,     BT LongUnsignedInt,     BT LongUnsignedInt,     BT LongUnsignedInt,   BT LongLongSignedInt, BT LongLongUnsignedInt,
124                                          BT SignedInt128,      BT UnsignedInt128,            BT uFloat16,     BT uFloat16Complex,            BT uFloat32,     BT uFloat32Complex,
125                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
126                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
127                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
128                                  },
129                                  {
130                /*    LLI */   BT LongLongSignedInt,   BT LongLongSignedInt,   BT LongLongSignedInt,   BT LongLongSignedInt,   BT LongLongSignedInt,   BT LongLongSignedInt,
131                                     BT LongLongSignedInt,   BT LongLongSignedInt,   BT LongLongSignedInt,   BT LongLongSignedInt,   BT LongLongSignedInt, BT LongLongUnsignedInt,
132                                          BT SignedInt128,      BT UnsignedInt128,            BT uFloat16,     BT uFloat16Complex,            BT uFloat32,     BT uFloat32Complex,
133                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
134                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
135                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
136                                  },
137                                  {
138                /*   LLUI */ BT LongLongUnsignedInt, BT LongLongUnsignedInt, BT LongLongUnsignedInt, BT LongLongUnsignedInt, BT LongLongUnsignedInt, BT LongLongUnsignedInt,
139                                   BT LongLongUnsignedInt, BT LongLongUnsignedInt, BT LongLongUnsignedInt, BT LongLongUnsignedInt, BT LongLongUnsignedInt, BT LongLongUnsignedInt,
140                                          BT SignedInt128,      BT UnsignedInt128,            BT uFloat16,     BT uFloat16Complex,            BT uFloat32,     BT uFloat32Complex,
141                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
142                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
143                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
144                                  },
145                                  {
146                /*     IB */        BT SignedInt128,        BT SignedInt128,        BT SignedInt128,        BT SignedInt128,        BT SignedInt128,        BT SignedInt128,
147                                          BT SignedInt128,        BT SignedInt128,        BT SignedInt128,        BT SignedInt128,        BT SignedInt128,        BT SignedInt128,
148                                          BT SignedInt128,      BT UnsignedInt128,            BT uFloat16,     BT uFloat16Complex,            BT uFloat32,     BT uFloat32Complex,
149                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
150                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
151                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
152                                  },
153                                  {
154                /*    UIB */      BT UnsignedInt128,      BT UnsignedInt128,      BT UnsignedInt128,      BT UnsignedInt128,      BT UnsignedInt128,      BT UnsignedInt128,
155                                        BT UnsignedInt128,      BT UnsignedInt128,      BT UnsignedInt128,      BT UnsignedInt128,      BT UnsignedInt128,      BT UnsignedInt128,
156                                        BT UnsignedInt128,      BT UnsignedInt128,            BT uFloat16,     BT uFloat16Complex,            BT uFloat32,     BT uFloat32Complex,
157                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
158                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
159                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
160                                  },
161                                  {
162                /*    _FH */            BT uFloat16,            BT uFloat16,            BT uFloat16,            BT uFloat16,            BT uFloat16,            BT uFloat16,
163                                              BT uFloat16,            BT uFloat16,            BT uFloat16,            BT uFloat16,            BT uFloat16,            BT uFloat16,
164                                              BT uFloat16,            BT uFloat16,            BT uFloat16,     BT uFloat16Complex,            BT uFloat32,     BT uFloat32Complex,
165                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
166                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
167                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
168                                  },
169                                  {
170                /*    _FH */     BT uFloat16Complex,     BT uFloat16Complex,     BT uFloat16Complex,     BT uFloat16Complex,     BT uFloat16Complex,     BT uFloat16Complex,
171                                       BT uFloat16Complex,     BT uFloat16Complex,     BT uFloat16Complex,     BT uFloat16Complex,     BT uFloat16Complex,     BT uFloat16Complex,
172                                       BT uFloat16Complex,     BT uFloat16Complex,     BT uFloat16Complex,     BT uFloat16Complex,     BT uFloat32Complex,     BT uFloat32Complex,
173                                          BT FloatComplex,        BT FloatComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,     BT uFloat64Complex,     BT uFloat64Complex,
174                                         BT DoubleComplex,       BT DoubleComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat128Complex,
175                                      BT uFloat128Complex,    BT uFloat128Complex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,
176                                  },
177                                  {
178                /*     _F */            BT uFloat32,            BT uFloat32,            BT uFloat32,            BT uFloat32,            BT uFloat32,            BT uFloat32,
179                                              BT uFloat32,            BT uFloat32,            BT uFloat32,            BT uFloat32,            BT uFloat32,            BT uFloat32,
180                                              BT uFloat32,            BT uFloat32,            BT uFloat32,     BT uFloat32Complex,            BT uFloat32,     BT uFloat32Complex,
181                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
182                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
183                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
184                                  },
185                                  {
186                /*    _FC */     BT uFloat32Complex,     BT uFloat32Complex,     BT uFloat32Complex,     BT uFloat32Complex,     BT uFloat32Complex,     BT uFloat32Complex,
187                                       BT uFloat32Complex,     BT uFloat32Complex,     BT uFloat32Complex,     BT uFloat32Complex,     BT uFloat32Complex,     BT uFloat32Complex,
188                                       BT uFloat32Complex,     BT uFloat32Complex,     BT uFloat32Complex,     BT uFloat32Complex,     BT uFloat32Complex,     BT uFloat32Complex,
189                                          BT FloatComplex,        BT FloatComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,     BT uFloat64Complex,     BT uFloat64Complex,
190                                         BT DoubleComplex,       BT DoubleComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat128Complex,
191                                      BT uFloat128Complex,    BT uFloat128Complex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,
192                                  },
193                                  {
194                /*      F */               BT Float,               BT Float,               BT Float,               BT Float,               BT Float,               BT Float,
195                                                 BT Float,               BT Float,               BT Float,               BT Float,               BT Float,               BT Float,
196                                                 BT Float,               BT Float,               BT Float,        BT FloatComplex,               BT Float,        BT FloatComplex,
197                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
198                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
199                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
200                                  },
201                                  {
202                /*     FC */        BT FloatComplex,        BT FloatComplex,        BT FloatComplex,        BT FloatComplex,        BT FloatComplex,        BT FloatComplex,
203                                          BT FloatComplex,        BT FloatComplex,        BT FloatComplex,        BT FloatComplex,        BT FloatComplex,        BT FloatComplex,
204                                          BT FloatComplex,        BT FloatComplex,        BT FloatComplex,        BT FloatComplex,        BT FloatComplex,        BT FloatComplex,
205                                          BT FloatComplex,        BT FloatComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,     BT uFloat64Complex,     BT uFloat64Complex,
206                                         BT DoubleComplex,       BT DoubleComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat128Complex,
207                                      BT uFloat128Complex,    BT uFloat128Complex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,
208                                  },
209                                  {
210                /*    _FX */           BT uFloat32x,           BT uFloat32x,           BT uFloat32x,           BT uFloat32x,           BT uFloat32x,           BT uFloat32x,
211                                             BT uFloat32x,           BT uFloat32x,           BT uFloat32x,           BT uFloat32x,           BT uFloat32x,           BT uFloat32x,
212                                             BT uFloat32x,           BT uFloat32x,           BT uFloat32x,    BT uFloat32xComplex,           BT uFloat32x,    BT uFloat32xComplex,
213                                             BT uFloat32x,    BT uFloat32xComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
214                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
215                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
216                                  },
217                                  {
218                /*   _FXC */    BT uFloat32xComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,
219                                      BT uFloat32xComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,
220                                      BT uFloat32xComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,
221                                      BT uFloat32xComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,     BT uFloat64Complex,     BT uFloat64Complex,
222                                         BT DoubleComplex,       BT DoubleComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat128Complex,
223                                      BT uFloat128Complex,    BT uFloat128Complex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,
224                                  },
225                                  {
226                /*     FD */            BT uFloat64,            BT uFloat64,            BT uFloat64,            BT uFloat64,            BT uFloat64,            BT uFloat64,
227                                              BT uFloat64,            BT uFloat64,            BT uFloat64,            BT uFloat64,            BT uFloat64,            BT uFloat64,
228                                              BT uFloat64,            BT uFloat64,            BT uFloat64,     BT uFloat64Complex,            BT uFloat64,     BT uFloat64Complex,
229                                              BT uFloat64,     BT uFloat64Complex,            BT uFloat64,     BT uFloat64Complex,            BT uFloat64,     BT uFloat64Complex,
230                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
231                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
232                                  },
233                                  {
234                /*   _FDC */     BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,
235                                       BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,
236                                       BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,
237                                       BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,
238                                         BT DoubleComplex,       BT DoubleComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat128Complex,
239                                      BT uFloat128Complex,    BT uFloat128Complex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,
240                                  },
241                                  {
242                /*      D */              BT Double,              BT Double,              BT Double,              BT Double,              BT Double,              BT Double,
243                                                BT Double,              BT Double,              BT Double,              BT Double,              BT Double,              BT Double,
244                                                BT Double,              BT Double,              BT Double,       BT DoubleComplex,              BT Double,       BT DoubleComplex,
245                                                BT Double,       BT DoubleComplex,              BT Double,       BT DoubleComplex,              BT Double,       BT DoubleComplex,
246                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
247                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
248                                  },
249                                  {
250                /*     DC */       BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,
251                                         BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,
252                                         BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,
253                                         BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,
254                                         BT DoubleComplex,       BT DoubleComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat128Complex,
255                                      BT uFloat128Complex,    BT uFloat128Complex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,
256                                  },
257                                  {
258                /*   F80X */           BT uFloat64x,           BT uFloat64x,           BT uFloat64x,           BT uFloat64x,           BT uFloat64x,           BT uFloat64x,
259                                             BT uFloat64x,           BT uFloat64x,           BT uFloat64x,           BT uFloat64x,           BT uFloat64x,           BT uFloat64x,
260                                             BT uFloat64x,           BT uFloat64x,           BT uFloat64x,    BT uFloat64xComplex,           BT uFloat64x,    BT uFloat64xComplex,
261                                             BT uFloat64x,    BT uFloat64xComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uFloat64x,    BT uFloat64xComplex,
262                                             BT uFloat64x,    BT uFloat64xComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
263                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
264                                  },
265                                  {
266                /*  _FDXC */    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,
267                                      BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,
268                                      BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,
269                                      BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,
270                                      BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat128Complex,
271                                      BT uFloat128Complex,    BT uFloat128Complex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,
272                                  },
273                                  {
274                /*    F80 */           BT uuFloat80,           BT uuFloat80,           BT uuFloat80,           BT uuFloat80,           BT uuFloat80,           BT uuFloat80,
275                                             BT uuFloat80,           BT uuFloat80,           BT uuFloat80,           BT uuFloat80,           BT uuFloat80,           BT uuFloat80,
276                                             BT uuFloat80,           BT uuFloat80,           BT uuFloat80,    BT uFloat64xComplex,           BT uuFloat80,    BT uFloat64xComplex,
277                                             BT uuFloat80,    BT uFloat64xComplex,           BT uuFloat80,    BT uFloat64xComplex,           BT uuFloat80,    BT uFloat64xComplex,
278                                             BT uuFloat80,    BT uFloat64xComplex,           BT uuFloat80,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
279                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
280                                  },
281                                  {
282                /*    _FB */           BT uFloat128,           BT uFloat128,           BT uFloat128,           BT uFloat128,           BT uFloat128,           BT uFloat128,
283                                             BT uFloat128,           BT uFloat128,           BT uFloat128,           BT uFloat128,           BT uFloat128,           BT uFloat128,
284                                             BT uFloat128,           BT uFloat128,           BT uFloat128,    BT uFloat128Complex,           BT uFloat128,    BT uFloat128Complex,
285                                             BT uFloat128,    BT uFloat128Complex,           BT uFloat128,    BT uFloat128Complex,           BT uFloat128,    BT uFloat128Complex,
286                                             BT uFloat128,    BT uFloat128Complex,           BT uFloat128,    BT uFloat128Complex,           BT uFloat128,           BT uFloat128,
287                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
288                                  },
289                                  {
290                /*  _FLDC */    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,
291                                      BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,
292                                      BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,
293                                      BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,
294                                      BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,
295                                      BT uFloat128Complex,    BT uFloat128Complex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,
296                                  },
297                                  {
298                /*     FB */          BT uuFloat128,          BT uuFloat128,          BT uuFloat128,          BT uuFloat128,          BT uuFloat128,          BT uuFloat128,
299                                            BT uuFloat128,          BT uuFloat128,          BT uuFloat128,          BT uuFloat128,          BT uuFloat128,          BT uuFloat128,
300                                            BT uuFloat128,          BT uuFloat128,          BT uuFloat128,    BT uFloat128Complex,          BT uuFloat128,    BT uFloat128Complex,
301                                            BT uuFloat128,    BT uFloat128Complex,          BT uuFloat128,    BT uFloat128Complex,          BT uuFloat128,    BT uFloat128Complex,
302                                            BT uuFloat128,    BT uFloat128Complex,          BT uuFloat128,    BT uFloat128Complex,          BT uuFloat128,          BT uuFloat128,
303                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
304                                  },
305                                  {
306                /*     LD */          BT LongDouble,          BT LongDouble,          BT LongDouble,          BT LongDouble,          BT LongDouble,          BT LongDouble,
307                                            BT LongDouble,          BT LongDouble,          BT LongDouble,          BT LongDouble,          BT LongDouble,          BT LongDouble,
308                                            BT LongDouble,          BT LongDouble,          BT LongDouble,   BT LongDoubleComplex,          BT LongDouble,   BT LongDoubleComplex,
309                                            BT LongDouble,   BT LongDoubleComplex,          BT LongDouble,   BT LongDoubleComplex,          BT LongDouble,   BT LongDoubleComplex,
310                                            BT LongDouble,   BT LongDoubleComplex,          BT LongDouble,   BT LongDoubleComplex,          BT LongDouble,          BT LongDouble,
311                                     BT LongDoubleComplex,          BT LongDouble,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
312                                  },
313                                  {
314                /*    LDC */   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,
315                                     BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,
316                                     BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,
317                                     BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,
318                                     BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,
319                                     BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,
320                                  },
321                                  {
322                /*   _FBX */          BT uFloat128x,          BT uFloat128x,          BT uFloat128x,          BT uFloat128x,          BT uFloat128x,          BT uFloat128x,
323                                            BT uFloat128x,          BT uFloat128x,          BT uFloat128x,          BT uFloat128x,          BT uFloat128x,          BT uFloat128x,
324                                            BT uFloat128x,          BT uFloat128x,          BT uFloat128x,   BT uFloat128xComplex,          BT uFloat128x,   BT uFloat128xComplex,
325                                            BT uFloat128x,   BT uFloat128xComplex,          BT uFloat128x,   BT uFloat128xComplex,          BT uFloat128x,   BT uFloat128xComplex,
326                                            BT uFloat128x,   BT uFloat128xComplex,          BT uFloat128x,   BT uFloat128xComplex,          BT uFloat128x,          BT uFloat128x,
327                                     BT uFloat128xComplex,          BT uFloat128x,          BT uFloat128x,   BT uFloat128xComplex,          BT uFloat128x,   BT uFloat128xComplex,
328                                  },
329                                  {
330                /* _FLDXC */   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,
331                                     BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,
332                                     BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,
333                                     BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,
334                                     BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,
335                                     BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,
336                                  },
337        }; // commonTypes
338        #undef BT
339        // GENERATED END
340        static_assert(
341                sizeof(commonTypes)/sizeof(commonTypes[0][0]) == ast::BasicKind::NUMBER_OF_BASIC_TYPES * ast::BasicKind::NUMBER_OF_BASIC_TYPES,
342                "Each basic type kind should have a corresponding row in the combined type matrix"
343        );
344
345class CommonType final : public ast::WithShortCircuiting {
346        const ast::Type * type2;
347        WidenMode widen;
348        ast::TypeEnvironment & tenv;
349        const ast::OpenVarSet & open;
350        ast::AssertionSet & need;
351        ast::AssertionSet & have;
352public:
353        static size_t traceId;
354        ast::ptr< ast::Type > result;
355
356        CommonType(
357                const ast::Type * t2, WidenMode w,
358                ast::TypeEnvironment & env, const ast::OpenVarSet & o,
359                ast::AssertionSet & need, ast::AssertionSet & have )
360        : type2( t2 ), widen( w ), tenv( env ), open( o ), need (need), have (have) ,result() {}
361
362        void previsit( const ast::Node * ) { visit_children = false; }
363
364        void postvisit( const ast::VoidType * ) {}
365
366        void postvisit( const ast::BasicType * basic ) {
367                if ( auto basic2 = dynamic_cast< const ast::BasicType * >( type2 ) ) {
368                        ast::BasicKind kind;
369                        if (basic->kind != basic2->kind && !widen.first && !widen.second) return;
370                        else if (!widen.first) kind = basic->kind; // widen.second
371                        else if (!widen.second) kind = basic2->kind;
372                        else kind = commonTypes[ basic->kind ][ basic2->kind ];
373                        // xxx - what does qualifiers even do here??
374                        if ( (basic->qualifiers >= basic2->qualifiers || widen.first)
375                                && (basic->qualifiers <= basic2->qualifiers || widen.second) ) {
376                                result = new ast::BasicType{ kind, basic->qualifiers | basic2->qualifiers };
377                        }
378                } else if (
379                        dynamic_cast< const ast::ZeroType * >( type2 )
380                        || dynamic_cast< const ast::OneType * >( type2 )
381                ) {
382                        if (widen.second) {
383                                result = new ast::BasicType{ basic->kind, basic->qualifiers | type2->qualifiers };
384                        }
385                } else if ( const ast::EnumInstType * enumInst = dynamic_cast< const ast::EnumInstType * >( type2 ) ) {
386                        const ast::EnumDecl* enumDecl = enumInst->base;
387                        if ( !enumDecl->base ) {
388                                ast::BasicKind kind = commonTypes[ basic->kind ][ ast::BasicKind::SignedInt ];
389                                if (
390                                        ( ( kind == basic->kind && basic->qualifiers >= type2->qualifiers )
391                                                || widen.first )
392                                        && ( ( kind != basic->kind && basic->qualifiers <= type2->qualifiers )
393                                                || widen.second )
394                                ) {
395                                        result = new ast::BasicType{ kind, basic->qualifiers | type2->qualifiers };
396                                }
397                        }
398                } else if ( auto type2AsAttr = dynamic_cast< const ast::EnumAttrType * >( type2 ) ) {
399                        if ( type2AsAttr->attr == ast::EnumAttribute::Posn ) {
400                                ast::BasicKind kind = commonTypes[ basic->kind ][ ast::BasicKind::SignedInt ];
401                                if (
402                                        ( ( kind == basic->kind && basic->qualifiers >= type2->qualifiers )
403                                                || widen.first )
404                                        && ( ( kind != basic->kind && basic->qualifiers <= type2->qualifiers )
405                                                || widen.second )
406                                ) {
407                                        result = new ast::BasicType{ kind, basic->qualifiers | type2->qualifiers };
408                                }
409                        }
410                }
411        }
412
413private:
414        template< typename Pointer >
415        void getCommonWithVoidPointer( const Pointer * voidPtr, const Pointer * oPtr ) {
416                const ast::Type * base = oPtr->base;
417                if ( auto var = dynamic_cast< const ast::TypeInstType * >( base ) ) {
418                        auto entry = open.find( *var );
419                        if ( entry != open.end() ) {
420                                ast::AssertionSet need, have;
421                                if ( ! tenv.bindVar(
422                                        var, voidPtr->base, entry->second, need, have, open, widen )
423                                ) return;
424                        }
425                }
426                result = voidPtr;
427                add_qualifiers( result, oPtr->qualifiers );
428        }
429
430public:
431        void postvisit( const ast::PointerType * pointer ) {
432                if ( auto pointer2 = dynamic_cast< const ast::PointerType * >( type2 ) ) {
433                        if (
434                                widen.first
435                                && pointer2->base.as< ast::VoidType >()
436                                && ! ast::isFtype( pointer->base )
437                        ) {
438                                getCommonWithVoidPointer( pointer2, pointer );
439                        } else if (
440                                widen.second
441                                && pointer->base.as< ast::VoidType >()
442                                && ! ast::isFtype( pointer2->base )
443                        ) {
444                                getCommonWithVoidPointer( pointer, pointer2 );
445                        } else if (
446                                ( pointer->base->qualifiers >= pointer2->base->qualifiers || widen.first )
447                                && ( pointer->base->qualifiers <= pointer2->base->qualifiers || widen.second )
448                        ) {
449                                ast::CV::Qualifiers q1 = pointer->base->qualifiers;
450                                ast::CV::Qualifiers q2 = pointer2->base->qualifiers;
451
452                                // force t{1,2} to be cloned if their qualifiers must be stripped, so that
453                                // pointer{,2}->base are unchanged
454                                ast::ptr< ast::Type > t1{ pointer->base }, t2{ pointer2->base };
455                                reset_qualifiers( t1 );
456                                reset_qualifiers( t2 );
457
458                                ast::OpenVarSet newOpen{ open };
459                                if ( unifyExact( t1, t2, tenv, have, need, newOpen, noWiden() ) ) {
460                                        result = pointer;
461                                        if ( q1.val != q2.val ) {
462                                                // reset result->base->qualifiers to be union of two base qualifiers
463                                                strict_dynamic_cast< ast::PointerType * >(
464                                                        result.get_and_mutate()
465                                                )->base.get_and_mutate()->qualifiers = q1 | q2;
466                                        }
467                                } else if ( isFtype (t1) && isFtype (t2) ) {
468                                        auto f1 = t1.as<ast::FunctionType>();
469                                        if (!f1) return;
470                                        auto f2 = t2.strict_as<ast::FunctionType>();
471
472                                        assertf(f1->returns.size() <= 1, "Function return should not be a list");
473                                        assertf(f2->returns.size() <= 1, "Function return should not be a list");
474
475                                        if (
476                                                ( f1->params.size() != f2->params.size() || f1->returns.size() != f2->returns.size() )
477                                                && ! f1->isTtype()
478                                                && ! f2->isTtype()
479                                        ) return;
480
481                                        auto params1 = flattenList( f1->params, tenv );
482                                        auto params2 = flattenList( f2->params, tenv );
483
484                                        auto crnt1 = params1.begin();
485                                        auto crnt2 = params2.begin();
486                                        auto end1 = params1.end();
487                                        auto end2 = params2.end();
488
489                                        while (crnt1 != end1 && crnt2 != end2 ) {
490                                                const ast::Type * arg1 = *crnt1;
491                                                const ast::Type * arg2 = *crnt2;
492
493                                                bool isTuple1 = Tuples::isTtype( t1 );
494                                                bool isTuple2 = Tuples::isTtype( t2 );
495
496                                                // assumes here that ttype *must* be last parameter
497                                                if ( isTuple1 && ! isTuple2 ) {
498                                                        // combine remainder of list2, then unify
499                                                        if (unifyExact(
500                                                                arg1, tupleFromTypes( crnt2, end2 ), tenv, need, have, open,
501                                                                noWiden() )) {
502                                                                        break;
503                                                        } else return;
504                                                } else if ( ! isTuple1 && isTuple2 ) {
505                                                        // combine remainder of list1, then unify
506                                                        if (unifyExact(
507                                                                tupleFromTypes( crnt1, end1 ), arg2, tenv, need, have, open,
508                                                                noWiden() )) {
509                                                                        break;
510                                                        } else return;
511                                                }
512
513                                                // allow qualifiers of pointer and reference base to become more specific
514                                                if (auto ref1 = dynamic_cast<const ast::ReferenceType *> (arg1)) {
515                                                        if (auto ref2 = dynamic_cast<const ast::ReferenceType *> (arg2)) {
516                                                                ast::ptr<ast::Type> base1 = ref1->base;
517                                                                ast::ptr<ast::Type> base2 = ref2->base;
518
519                                                                // xxx - assume LHS is always the target type
520
521                                                                if ( ! ((widen.second && ref2->qualifiers.is_mutex)
522                                                                || (ref1->qualifiers.is_mutex == ref2->qualifiers.is_mutex ))) return;
523
524                                                                if ( (widen.second && base1->qualifiers <= base2->qualifiers ) || (base2->qualifiers == base1->qualifiers) ) {
525
526                                                                        reset_qualifiers(base1);
527                                                                        reset_qualifiers(base2);
528
529                                                                        if ( !unifyExact(
530                                                                                base1, base2, tenv, need, have, open, noWiden() )
531                                                                        ) return;
532                                                                }
533                                                        } else return;
534                                                } else if (auto ptr1 = dynamic_cast<const ast::PointerType *> (arg1)) {
535                                                        if (auto ptr2 = dynamic_cast<const ast::PointerType *> (arg2)) {
536                                                                ast::ptr<ast::Type> base1 = ptr1->base;
537                                                                ast::ptr<ast::Type> base2 = ptr2->base;
538
539                                                                // xxx - assume LHS is always the target type
540                                                                // a function accepting const can always be called by non-const arg
541
542                                                                if ( (widen.second && base1->qualifiers <= base2->qualifiers ) || (base2->qualifiers == base1->qualifiers) ) {
543
544                                                                        reset_qualifiers(base1);
545                                                                        reset_qualifiers(base2);
546
547                                                                        if ( ! unifyExact(
548                                                                                base1, base2, tenv, need, have, open, noWiden() )
549                                                                        ) return;
550                                                                }
551                                                        } else return;
552                                                } else if (! unifyExact(
553                                                                arg1, arg2, tenv, need, have, open, noWiden() )) {
554                                                        return;
555                                                }
556                                                ++crnt1; ++crnt2;
557                                        }
558                                        if ( crnt1 != end1 ) {
559                                                // try unifying empty tuple with ttype
560                                                const ast::Type * t1 = *crnt1;
561                                                if (! Tuples::isTtype( t1 ) ) return;
562                                                if (! unifyExact(
563                                                        t1, tupleFromTypes( crnt2, end2 ), tenv, need, have, open,
564                                                        noWiden() )) return;
565                                        } else if ( crnt2 != end2 ) {
566                                                // try unifying empty tuple with ttype
567                                                const ast::Type * t2 = *crnt2;
568                                                if ( !Tuples::isTtype( t2 ) ) return;
569                                                if ( !unifyExact(
570                                                        tupleFromTypes( crnt1, end1 ), t2, tenv, need, have, open,
571                                                        noWiden() )) return;
572                                        }
573                                        if ((f1->returns.size() == 0 && f2->returns.size() == 0)
574                                          || (f1->returns.size() == 1 && f2->returns.size() == 1 && unifyExact(f1->returns[0], f2->returns[0], tenv, need, have, open, noWiden()))) {
575                                                result = pointer;
576
577                                                for (auto & assn : f1->assertions) {
578                                                        auto i = need.find(assn);
579                                                        if (i != need.end()) i->second.isUsed = true;
580                                                        auto j = have.find(assn);
581                                                        if (j != have.end()) j->second.isUsed = true;
582                                                }
583
584                                                for (auto & assn : f2->assertions) {
585                                                        auto i = need.find(assn);
586                                                        if (i != need.end()) i->second.isUsed = true;
587                                                        auto j = have.find(assn);
588                                                        if (j != have.end()) j->second.isUsed = true;
589                                                }
590                                        }
591                                } // if ftype
592                        }
593                } else if ( widen.second && dynamic_cast< const ast::ZeroType * >( type2 ) ) {
594                        result = pointer;
595                        add_qualifiers( result, type2->qualifiers );
596                }
597        }
598
599        void postvisit( const ast::ArrayType * ) {}
600
601        void postvisit( const ast::ReferenceType * ref ) {
602                if ( auto ref2 = dynamic_cast< const ast::ReferenceType * >( type2 ) ) {
603                        if (
604                                widen.first && ref2->base.as< ast::VoidType >() && ! ast::isFtype( ref->base )
605                        ) {
606                                getCommonWithVoidPointer( ref2, ref );
607                        } else if (
608                                widen.second && ref->base.as< ast::VoidType>() && ! ast::isFtype( ref2->base )
609                        ) {
610                                getCommonWithVoidPointer( ref, ref2 );
611                        } else if (
612                                ( ref->base->qualifiers >= ref2->base->qualifiers || widen.first )
613                                && ( ref->base->qualifiers <= ref2->base->qualifiers || widen.second )
614                        ) {
615                                ast::CV::Qualifiers q1 = ref->base->qualifiers, q2 = ref2->base->qualifiers;
616
617                                // force t{1,2} to be cloned if their qualifiers must be stripped, so that
618                                // ref{,2}->base are unchanged
619                                ast::ptr< ast::Type > t1{ ref->base }, t2{ ref2->base };
620                                reset_qualifiers( t1 );
621                                reset_qualifiers( t2 );
622
623                                ast::OpenVarSet newOpen{ open };
624                                if ( unifyExact( t1, t2, tenv, have, need, newOpen, noWiden() ) ) {
625                                        result = ref;
626                                        if ( q1.val != q2.val ) {
627                                                // reset result->base->qualifiers to be union of two base qualifiers
628                                                strict_dynamic_cast< ast::ReferenceType * >(
629                                                        result.get_and_mutate()
630                                                )->base.get_and_mutate()->qualifiers = q1 | q2;
631                                        }
632                                }
633                        }
634                } else if ( widen.second && dynamic_cast< const ast::ZeroType * >( type2 ) ) {
635                        result = ref;
636                        add_qualifiers( result, type2->qualifiers );
637                } else {
638                        if (!dynamic_cast<const ast::EnumInstType *>(type2))
639                                result = commonType( type2, ref, tenv, need, have, open, widen );
640                }
641        }
642
643        void postvisit( const ast::FunctionType * ) {}
644
645        void postvisit( const ast::StructInstType * ) {}
646
647        void postvisit( const ast::UnionInstType * ) {}
648
649        void postvisit( const ast::EnumInstType * enumInst ) {
650                if ( enumInst->base && !enumInst->base->base ) {
651                        auto basicType = new ast::BasicType( ast::BasicKind::UnsignedInt );
652                        result = commonType( basicType, type2, tenv, need, have, open, widen);
653                }
654        }
655
656        void postvisit( const ast::EnumAttrType * ) {}
657
658        void postvisit( const ast::TraitInstType * ) {}
659
660        void postvisit( const ast::TypeInstType * ) {}
661
662        void postvisit( const ast::TupleType * ) {}
663
664        void postvisit( const ast::VarArgsType * ) {}
665
666        void postvisit( const ast::ZeroType * zero ) {
667                if ( !widen.first ) return;
668                if ( dynamic_cast< const ast::BasicType * >( type2 )
669                                || dynamic_cast< const ast::PointerType * >( type2 ) ) {
670                        if ( widen.second || zero->qualifiers <= type2->qualifiers ) {
671                                result = type2;
672                                add_qualifiers( result, zero->qualifiers );
673                        }
674                } else if ( widen.second && dynamic_cast< const ast::OneType * >( type2 ) ) {
675                        result = new ast::BasicType{
676                                ast::BasicKind::SignedInt, zero->qualifiers | type2->qualifiers };
677                } else if ( const ast::EnumInstType * enumInst = dynamic_cast< const ast::EnumInstType * >( type2 ) ) {
678                        const ast::EnumDecl * enumDecl = enumInst->base;
679                        if ( !enumDecl->base ) {
680                                if ( widen.second || zero->qualifiers <= type2->qualifiers ) {
681                                        result = type2;
682                                        add_qualifiers( result, zero->qualifiers );
683                                }
684                        }
685                }
686        }
687
688        void postvisit( const ast::OneType * one ) {
689                if ( !widen.first ) return;
690                if ( dynamic_cast< const ast::BasicType * >( type2 ) ) {
691                        if ( widen.second || one->qualifiers <= type2->qualifiers ) {
692                                result = type2;
693                                add_qualifiers( result, one->qualifiers );
694                        }
695                } else if ( widen.second && dynamic_cast< const ast::ZeroType * >( type2 ) ) {
696                        result = new ast::BasicType{
697                                ast::BasicKind::SignedInt, one->qualifiers | type2->qualifiers };
698                } else if ( const ast::EnumInstType * enumInst = dynamic_cast< const ast::EnumInstType * >( type2 ) ) {
699                        const ast::EnumDecl * enumDecl = enumInst->base;
700                        if ( !enumDecl->base ) {
701                                if ( widen.second || one->qualifiers <= type2->qualifiers ) {
702                                        result = type2;
703                                        add_qualifiers( result, one->qualifiers );
704                                }
705                        }
706                }
707        }
708};
709
710// size_t CommonType::traceId = Stats::Heap::new_stacktrace_id("CommonType");
711
712namespace {
713        ast::ptr< ast::Type > handleReference(
714                const ast::ptr< ast::Type > & t1, const ast::ptr< ast::Type > & t2, WidenMode widen,
715                ast::TypeEnvironment & env,
716                const ast::OpenVarSet & open
717        ) {
718                ast::ptr<ast::Type> common;
719                ast::AssertionSet have, need;
720                ast::OpenVarSet newOpen{ open };
721
722                // need unify to bind type variables
723                if ( unify( t1, t2, env, have, need, newOpen, common ) ) {
724                        ast::CV::Qualifiers q1 = t1->qualifiers, q2 = t2->qualifiers;
725                        PRINT(
726                                std::cerr << "unify success: " << widenFirst << " " << widenSecond << std::endl;
727                        )
728                        if ( ( widen.first || q2 <= q1 ) && ( widen.second || q1 <= q2 ) ) {
729                                PRINT(
730                                        std::cerr << "widen okay" << std::endl;
731                                )
732                                add_qualifiers( common, q1 | q2 );
733                                return common;
734                        }
735                }
736
737                PRINT(
738                        std::cerr << "exact unify failed: " << t1 << " " << t2 << std::endl;
739                )
740                return { nullptr };
741        }
742}
743
744ast::ptr< ast::Type > commonType(
745        const ast::ptr< ast::Type > & type1, const ast::ptr< ast::Type > & type2,
746        ast::TypeEnvironment & env, ast::AssertionSet & need, ast::AssertionSet & have,
747        const ast::OpenVarSet & open, WidenMode widen
748) {
749        unsigned depth1 = type1->referenceDepth();
750        unsigned depth2 = type2->referenceDepth();
751
752        if ( depth1 != depth2 ) {  // implies depth1 > 0 || depth2 > 0
753                PRINT(
754                        std::cerr << "reference depth diff: " << (depth1-depth2) << std::endl;
755                )
756                ast::ptr< ast::Type > result;
757                const ast::ReferenceType * ref1 = type1.as< ast::ReferenceType >();
758                const ast::ReferenceType * ref2 = type2.as< ast::ReferenceType >();
759
760                if ( depth1 > depth2 ) {
761                        assert( ref1 );
762                        result = handleReference( ref1->base, type2, widen, env, open );
763                } else {  // Implies depth1 < depth2
764                        assert( ref2 );
765                        result = handleReference( type1, ref2->base, widen, env, open );
766                }
767
768                if ( result && ref1 ) {
769                        // formal is reference, so result should be reference
770                        PRINT(
771                                std::cerr << "formal is reference; result should be reference" << std::endl;
772                        )
773                        result = new ast::ReferenceType{ result, ref1->qualifiers };
774                }
775
776                PRINT(
777                        std::cerr << "common type of reference [" << type1 << "] and [" << type2 << "] is "
778                        "[" << result << "]" << std::endl;
779                )
780                return result;
781        }
782        // otherwise both are reference types of the same depth and this is handled by the visitor
783        ast::Pass<CommonType> visitor{ type2, widen, env, open, need, have };
784        type1->accept( visitor );
785        // ast::ptr< ast::Type > result = visitor.core.result;
786
787        return visitor.core.result;
788}
789
790} // namespace ResolvExpr
791
792// Local Variables: //
793// tab-width: 4 //
794// mode: c++ //
795// compile-command: "make install" //
796// End: //
Note: See TracBrowser for help on using the repository browser.