source: src/ResolvExpr/CommonType.cc

Last change on this file was 13de4478, checked in by Andrew Beach <ajbeach@…>, 5 days ago

Updated files in ResolvExpr? to the new indentation style. It seems the remaining places have reason to break from the style.

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