[388] | 1 | # -*- coding: utf-8 -*-
|
---|
| 2 | #
|
---|
| 3 | # TECS Generator
|
---|
| 4 | # Generator for TOPPERS Embedded Component System
|
---|
| 5 | #
|
---|
| 6 | # Copyright (C) 2008-2017 by TOPPERS Project
|
---|
| 7 | #--
|
---|
| 8 | # ä¸è¨èä½æ¨©è
|
---|
| 9 | ã¯ï¼ä»¥ä¸ã®(1)ï½(4)ã®æ¡ä»¶ãæºããå ´åã«éãï¼æ¬ã½ããã¦ã§
|
---|
| 10 | # ã¢ï¼æ¬ã½ããã¦ã§ã¢ãæ¹å¤ãããã®ãå«ãï¼ä»¥ä¸åãï¼ã使ç¨ã»è¤è£½ã»æ¹
|
---|
| 11 | # å¤ã»åé
|
---|
| 12 | å¸ï¼ä»¥ä¸ï¼å©ç¨ã¨å¼ã¶ï¼ãããã¨ãç¡åã§è¨±è«¾ããï¼
|
---|
| 13 | # (1) æ¬ã½ããã¦ã§ã¢ãã½ã¼ã¹ã³ã¼ãã®å½¢ã§å©ç¨ããå ´åã«ã¯ï¼ä¸è¨ã®èä½
|
---|
| 14 | # 権表示ï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãï¼ãã®ã¾ã¾ã®å½¢ã§ã½ã¼
|
---|
| 15 | # ã¹ã³ã¼ãä¸ã«å«ã¾ãã¦ãããã¨ï¼
|
---|
| 16 | # (2) æ¬ã½ããã¦ã§ã¢ãï¼ã©ã¤ãã©ãªå½¢å¼ãªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
| 17 | # ç¨ã§ããå½¢ã§åé
|
---|
| 18 | å¸ããå ´åã«ã¯ï¼åé
|
---|
| 19 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨
|
---|
| 20 | # è
|
---|
| 21 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®èä½æ¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨
|
---|
| 22 | # ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
| 23 | # (3) æ¬ã½ããã¦ã§ã¢ãï¼æ©å¨ã«çµã¿è¾¼ããªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
| 24 | # ç¨ã§ããªãå½¢ã§åé
|
---|
| 25 | å¸ããå ´åã«ã¯ï¼æ¬¡ã®ããããã®æ¡ä»¶ãæºããã
|
---|
| 26 | # ã¨ï¼
|
---|
| 27 | # (a) åé
|
---|
| 28 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨è
|
---|
| 29 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®è
|
---|
| 30 | # ä½æ¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
| 31 | # (b) åé
|
---|
| 32 | å¸ã®å½¢æ
|
---|
| 33 | ãï¼å¥ã«å®ããæ¹æ³ã«ãã£ã¦ï¼TOPPERSããã¸ã§ã¯ãã«
|
---|
| 34 | # å ±åãããã¨ï¼
|
---|
| 35 | # (4) æ¬ã½ããã¦ã§ã¢ã®å©ç¨ã«ããç´æ¥çã¾ãã¯éæ¥çã«çãããããªãæ
|
---|
| 36 | # 害ãããï¼ä¸è¨èä½æ¨©è
|
---|
| 37 | ããã³TOPPERSããã¸ã§ã¯ããå
|
---|
| 38 | 責ãããã¨ï¼
|
---|
| 39 | # ã¾ãï¼æ¬ã½ããã¦ã§ã¢ã®ã¦ã¼ã¶ã¾ãã¯ã¨ã³ãã¦ã¼ã¶ããã®ãããªãç
|
---|
| 40 | # ç±ã«åºã¥ãè«æ±ãããï¼ä¸è¨èä½æ¨©è
|
---|
| 41 | ããã³TOPPERSããã¸ã§ã¯ãã
|
---|
| 42 | # å
|
---|
| 43 | 責ãããã¨ï¼
|
---|
| 44 | #
|
---|
| 45 | # æ¬ã½ããã¦ã§ã¢ã¯ï¼ç¡ä¿è¨¼ã§æä¾ããã¦ãããã®ã§ããï¼ä¸è¨èä½æ¨©è
|
---|
| 46 | ã
|
---|
| 47 | # ãã³TOPPERSããã¸ã§ã¯ãã¯ï¼æ¬ã½ããã¦ã§ã¢ã«é¢ãã¦ï¼ç¹å®ã®ä½¿ç¨ç®ç
|
---|
| 48 | # ã«å¯¾ããé©åæ§ãå«ãã¦ï¼ãããªãä¿è¨¼ãè¡ããªãï¼ã¾ãï¼æ¬ã½ããã¦ã§
|
---|
| 49 | # ã¢ã®å©ç¨ã«ããç´æ¥çã¾ãã¯éæ¥çã«çãããããªãæ害ã«é¢ãã¦ãï¼ã
|
---|
| 50 | # ã®è²¬ä»»ãè² ããªãï¼
|
---|
| 51 | #
|
---|
| 52 | # $Id$
|
---|
| 53 | #++
|
---|
| 54 |
|
---|
| 55 | #= HasType: @type ãå
|
---|
| 56 | é¨ã«æã¤åã®ããã®ã¢ã¸ã¥ã¼ã«
|
---|
| 57 | # @b_cloned::Bool : true if @type is cloned
|
---|
| 58 | #
|
---|
| 59 | # ãã®ã¢ã¸ã¥ã¼ã«ã¯ DefinedType, PtrType, ArrayType ã« include ããã
|
---|
| 60 | # æ¬å½ã¯ typedef ãããæã® Decl ã®è¦ç´ ã®ã¿ clone ããã°ããã®ã ããget_type, get_original_type ã§
|
---|
| 61 | # åãåºãããã¨ããset_scs, set_qualifier ãããã¨ãã«ç¡æ¡ä»¶ã§ clone ãã (ç¡é§ã«ã¡ã¢ãªã使ç¨ãã)
|
---|
| 62 | # ãã ããclone ããã®ã¯ä¸åã®ã¿ã§ãã (äºå clone ããã¨å¥ã®åãåç
|
---|
| 63 | §ãã¦ãã¾ã)
|
---|
| 64 | #
|
---|
| 65 | # initialize 㧠clone ãã¦ããå
|
---|
| 66 | ±æããã¦ããã¨ãã« clone ãããªã
|
---|
| 67 | #
|
---|
| 68 | module HasType
|
---|
| 69 | def initHasType
|
---|
| 70 | @b_cloned = false
|
---|
| 71 | end
|
---|
| 72 |
|
---|
| 73 | #=== HasType# @type ãã¯ãã¼ã³ãã
|
---|
| 74 | def clone_type
|
---|
| 75 | # if @b_cloned == false then
|
---|
| 76 | @type = @type.clone
|
---|
| 77 | @b_cloned = true
|
---|
| 78 | # end
|
---|
| 79 | end
|
---|
| 80 | end
|
---|
| 81 |
|
---|
| 82 | class Type < Node
|
---|
| 83 | # @b_const : bool
|
---|
| 84 | # @b_volatile : bool
|
---|
| 85 |
|
---|
| 86 | def initialize
|
---|
| 87 | super
|
---|
| 88 | end
|
---|
| 89 |
|
---|
| 90 | def set_qualifier( qualifier )
|
---|
| 91 | case qualifier
|
---|
| 92 | when :CONST
|
---|
| 93 | # if @b_const then
|
---|
| 94 | # cdl_error( "T1001 const duplicate" )
|
---|
| 95 | # end
|
---|
| 96 | @b_const = true
|
---|
| 97 | when :VOLATILE
|
---|
| 98 | # if @b_volatile then
|
---|
| 99 | # cdl_error( "T1002 volatile duplicate" )
|
---|
| 100 | # end
|
---|
| 101 | @b_volatile = true
|
---|
| 102 | else
|
---|
| 103 | raise "Unknown qualifier #{qualifier}"
|
---|
| 104 | end
|
---|
| 105 | end
|
---|
| 106 |
|
---|
| 107 | def is_const?
|
---|
| 108 | if @b_const then
|
---|
| 109 | return true
|
---|
| 110 | else
|
---|
| 111 | return false
|
---|
| 112 | end
|
---|
| 113 | end
|
---|
| 114 |
|
---|
| 115 | def is_volatile?
|
---|
| 116 | if @b_volatile then
|
---|
| 117 | return true
|
---|
| 118 | else
|
---|
| 119 | return false
|
---|
| 120 | end
|
---|
| 121 | end
|
---|
| 122 |
|
---|
| 123 | def is_void?
|
---|
| 124 | if self.kind_of? DefinedType then
|
---|
| 125 | return @type.is_void?
|
---|
| 126 | elsif self.kind_of? VoidType then
|
---|
| 127 | return true
|
---|
| 128 | else
|
---|
| 129 | return false
|
---|
| 130 | end
|
---|
| 131 | end
|
---|
| 132 |
|
---|
| 133 | #=== size_is, count_is, string ãè¨å®
|
---|
| 134 | # æ´¾çã¯ã©ã¹ã§ãªã¼ãã¼ã©ã¤ãããï¼ããã©ã«ãã§ã¯ã¨ã©ã¼ï¼
|
---|
| 135 | def set_scs( size, count, string, max = nil, b_nullable = false )
|
---|
| 136 | str = ""
|
---|
| 137 | delim = ""
|
---|
| 138 | if size then
|
---|
| 139 | str = "size_is"
|
---|
| 140 | delim = ", "
|
---|
| 141 | end
|
---|
| 142 | if count then
|
---|
| 143 | str = "#{str}#{delim}count_is"
|
---|
| 144 | delim = ", "
|
---|
| 145 | end
|
---|
| 146 | if string then
|
---|
| 147 | str = "#{str}#{delim}string"
|
---|
| 148 | delim = ", "
|
---|
| 149 | end
|
---|
| 150 | if b_nullable then
|
---|
| 151 | str = "#{str}#{delim}nullable"
|
---|
| 152 | delim = ", "
|
---|
| 153 | end
|
---|
| 154 | cdl_error( "T1003 $1: unsuitable specifier for $2" , str, self.class )
|
---|
| 155 | end
|
---|
| 156 |
|
---|
| 157 | def clear_max
|
---|
| 158 | raise "clear_max called: #{self.class}"
|
---|
| 159 | end
|
---|
| 160 |
|
---|
| 161 | def get_type_str
|
---|
| 162 | str = ""
|
---|
| 163 | if @b_const then
|
---|
| 164 | str = "#{str}const "
|
---|
| 165 | end
|
---|
| 166 | if @b_volatile then
|
---|
| 167 | str = "#{str}volatile "
|
---|
| 168 | end
|
---|
| 169 | return str
|
---|
| 170 | end
|
---|
| 171 |
|
---|
| 172 | #=== åããã§ãã¯
|
---|
| 173 | # æ£å½ãªåå®ç¾©ãã©ããããã§ãã¯ãã
|
---|
| 174 | def check
|
---|
| 175 | # åã«èª¤ããããã°ãã¨ã©ã¼æååãè¿ã
|
---|
| 176 | end
|
---|
| 177 |
|
---|
| 178 | #=== struct ã® tag ããã§ãã¯
|
---|
| 179 | # æ£å½ãªåå®ç¾©ãã©ããããã§ãã¯ãã
|
---|
| 180 | #kind:: Decl ã® @kind ãåç
|
---|
| 181 | §
|
---|
| 182 | def check_struct_tag kind
|
---|
| 183 | # tag ãåå¨ããªããã°ã¨ã©ã¼ãåºåãã
|
---|
| 184 | # é
|
---|
| 185 | ååã§ã¯ãè¦ç´ ã®åãå帰çã«ãã§ãã¯
|
---|
| 186 | # ãã¤ã³ã¿åã§ã¯ãæãå
|
---|
| 187 | ã® tag ãã§ãã¯ã¯ããªã
|
---|
| 188 | # é¢æ°åã§ã¯ãã©ã¡ã¼ã¿ãªã¹ãã®ãã¹ã¦ã«ã¤ãã¦è¡ã
|
---|
| 189 | end
|
---|
| 190 |
|
---|
| 191 | #=== åæåå¯è½ããã§ãã¯
|
---|
| 192 | # attribute ãªã©åæåå¯è½ããã§ãã¯ããï¼åã«å¯¾ãæ£å½ãªåæååãä¸ãããã¦ãããï¼
|
---|
| 193 | #ident:: string 被代å
|
---|
| 194 | ¥å¤æ°å½
|
---|
| 195 | #initialize:: Expression, Array of initializer or C_EXP
|
---|
| 196 | # 代å
|
---|
| 197 | ¥å¤ãC_EXP ãä¸ããããã®ã¯ IntType ã®å ´åã®ã¿
|
---|
| 198 | #kind:: symbol (:ATTRIBUTE, :VAR, :CONSTNAT )
|
---|
| 199 | #attribute:: NameList kind == :VAR ã®ã¨ãåç
|
---|
| 200 | §ã§ãã attribute
|
---|
| 201 | #
|
---|
| 202 | # locale ã第ä¸å¼æ°ã¨ãã¦åãã®ã¯ã以ä¸ã®çç±ã«ããã
|
---|
| 203 | # ãã®ã¡ã½ããã¯ãå¤æ°ã¸ã®ä»£å
|
---|
| 204 | ¥ãè¡ããããè¡ãã«å¯¾ãã¦å¼ã³åºããããã
|
---|
| 205 | # Type ã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ã¯ãå¤æ°ãå®ç¾©ããããè¡ããè¨æ¶ãã¦ããã
|
---|
| 206 | #
|
---|
| 207 | # STAGE: S
|
---|
| 208 | def check_init( locale, ident, initializer, kind, attribute = nil )
|
---|
| 209 | #
|
---|
| 210 | end
|
---|
| 211 |
|
---|
| 212 | #=== const_val ãæå®ã®åã«ãã£ã¹ããã
|
---|
| 213 | # æ´¾çã¯ã©ã¹ã§ãªã¼ãã¼ã©ã¤ããã¦ããªãã¨ã¨ã©ã¼
|
---|
| 214 | def cast( const_val )
|
---|
| 215 | cdl_error( "T1004 cannot cast to $1" , self.class )
|
---|
| 216 | end
|
---|
| 217 |
|
---|
| 218 | #=== åãä¸è´ãããã®ãã§ãã¯
|
---|
| 219 | # ååã®åé¢ã§ãã§ãã¯ï¼
|
---|
| 220 | # typedef ãããåãåé¢ã§ä¸è´ãè¦ããããå
|
---|
| 221 | ã®åãåãã§ãååãç°ãªãã°ä¸ä¸è´ã¨ãªã
|
---|
| 222 | def equal? type2
|
---|
| 223 | return ( get_type_str == type2.get_type_str ) && ( get_type_str_post == type2.get_type_str_post )
|
---|
| 224 | end
|
---|
| 225 |
|
---|
| 226 | #=== bit size ãå¾ã
|
---|
| 227 | # IntType, FloatType 以å¤ã¯0
|
---|
| 228 | def get_bit_size
|
---|
| 229 | return 0
|
---|
| 230 | end
|
---|
| 231 |
|
---|
| 232 | #=== å
|
---|
| 233 | ã®åãå¾ã
|
---|
| 234 | # typedef ãããåã®å ´åããã®å
|
---|
| 235 | ã®åãè¿ã.
|
---|
| 236 | # ãã以å¤ã¯ãèªåèªèº«ãè¿ãï¼
|
---|
| 237 | # (DefinedType ã§ã¯æ¬ã¡ã½ããããªã¼ãã¼ã©ã¤ãããã)
|
---|
| 238 | def get_original_type
|
---|
| 239 | return self
|
---|
| 240 | end
|
---|
| 241 |
|
---|
| 242 | #=== å
|
---|
| 243 | é¨ã«ãã¤ã³ã¿åãæã¤
|
---|
| 244 | # ãã¤ã³ã¿åãã¾ãã¯ãã¤ã³ã¿åã¡ã³ããæã¤æ§é ä½ãã¾ãã¯è¦ç´ ããã¤ã³ã¿åãæã¤é
|
---|
| 245 | å
|
---|
| 246 | def has_pointer?
|
---|
| 247 | false
|
---|
| 248 | end
|
---|
| 249 |
|
---|
| 250 | #=== size_is, count_is, string æå®ããããã¤ã³ã¿ãæã¤ã
|
---|
| 251 | # size_is, count_is, string æå®ããããã¤ã³ã¿åãã¾ãã¯ãããã¡ã³ãã«æã¤æ§é ä½ãã¾ãã¯ããããè¦ç´ ã«æã¤é
|
---|
| 252 | å
|
---|
| 253 | def has_sized_pointer?
|
---|
| 254 | false
|
---|
| 255 | end
|
---|
| 256 |
|
---|
| 257 | #=== é·ãæå®ã®ãªã string ãæã¤
|
---|
| 258 | # ãªãæå®ã®ãªã string æå®ããããã¤ã³ã¿åãã¾ãã¯ãããã¡ã³ãã«æã¤æ§é ä½ãã¾ãã¯ãããè¦ç´ ã«æã¤é
|
---|
| 259 | å
|
---|
| 260 | def has_unsized_string?
|
---|
| 261 | false
|
---|
| 262 | end
|
---|
| 263 |
|
---|
| 264 | def show_tree indent
|
---|
| 265 | indent.times { print " " }
|
---|
| 266 | puts "const=#{@b_const} volatile=#{@b_volatile} #{locale_str}"
|
---|
| 267 | indent.times { print " " }
|
---|
| 268 | puts "has_pointer=#{has_pointer?} has_sized_pointer=#{has_sized_pointer?} has_unsized_string=#{has_unsized_string?}"
|
---|
| 269 | end
|
---|
| 270 | end
|
---|
| 271 |
|
---|
| 272 | class DefinedType < Type
|
---|
| 273 | # @type_name::string
|
---|
| 274 | # @typedef::Typedef
|
---|
| 275 | # @type:: kind_of Type
|
---|
| 276 |
|
---|
| 277 | include HasType
|
---|
| 278 |
|
---|
| 279 | def initialize( type_name )
|
---|
| 280 | super()
|
---|
| 281 | @type_name = type_name
|
---|
| 282 |
|
---|
| 283 | # mikan type_name ã path ã«ãªã£ã¦ããªãããæ«å®
|
---|
| 284 | @typedef = Namespace.find( [ type_name ] ) #1
|
---|
| 285 |
|
---|
| 286 | # if @type.class != Typedef then
|
---|
| 287 | # raise NotTypedef
|
---|
| 288 | # end
|
---|
| 289 | if @typedef == nil then
|
---|
| 290 | cdl_error( "T1005 \'$1\' not defined" , type_name )
|
---|
| 291 | elsif @typedef.class != Typedef then
|
---|
| 292 | cdl_error( "T1006 \'$1\' not type name. expecting type name here" , type_name )
|
---|
| 293 | end
|
---|
| 294 | @type = @typedef.get_declarator.get_type
|
---|
| 295 | initHasType
|
---|
| 296 | end
|
---|
| 297 |
|
---|
| 298 | def get_type
|
---|
| 299 | clone_type
|
---|
| 300 | return @type
|
---|
| 301 | end
|
---|
| 302 |
|
---|
| 303 | def get_type_str
|
---|
| 304 | "#{super}#{@type_name}"
|
---|
| 305 | end
|
---|
| 306 |
|
---|
| 307 | def get_type_str_post
|
---|
| 308 | ""
|
---|
| 309 | end
|
---|
| 310 |
|
---|
| 311 | def get_size
|
---|
| 312 | return @type.get_size
|
---|
| 313 | end
|
---|
| 314 |
|
---|
| 315 | def is_nullable?
|
---|
| 316 | return @type.is_nullable?
|
---|
| 317 | end
|
---|
| 318 |
|
---|
| 319 | #=== qualifier(const, volatile) ã®è¨å®
|
---|
| 320 | def set_qualifier( qualifier )
|
---|
| 321 | clone_type
|
---|
| 322 | @type.set_qualifier( qualifier )
|
---|
| 323 | super
|
---|
| 324 | end
|
---|
| 325 |
|
---|
| 326 | def set_scs( size, count, string, max = nil, b_nullable = false )
|
---|
| 327 | clone_type
|
---|
| 328 | @type.set_scs( size, count, string, max, b_nullable )
|
---|
| 329 | end
|
---|
| 330 |
|
---|
| 331 | def clear_max
|
---|
| 332 | @type.clear_max
|
---|
| 333 | end
|
---|
| 334 |
|
---|
| 335 | def get_original_type
|
---|
| 336 | clone_type
|
---|
| 337 | return @type.get_original_type
|
---|
| 338 | end
|
---|
| 339 |
|
---|
| 340 | def check # æå³ç誤ããããã°ãæååãè¿ã
|
---|
| 341 | nil # typedef ã®æ®µéã§æå³ãã§ãã¯ããã¦ãã
|
---|
| 342 | end
|
---|
| 343 |
|
---|
| 344 | def check_init( locale, ident, initializer, kind, attribute = nil )
|
---|
| 345 | get_type.check_init( locale, ident, initializer, kind, attribute )
|
---|
| 346 | end
|
---|
| 347 |
|
---|
| 348 | #=== å
|
---|
| 349 | é¨ã«ãã¤ã³ã¿åãæã¤
|
---|
| 350 | # ãã¤ã³ã¿åãã¾ãã¯ãã¤ã³ã¿åã¡ã³ããæã¤æ§é ä½ãã¾ãã¯è¦ç´ ããã¤ã³ã¿åãæã¤é
|
---|
| 351 | å
|
---|
| 352 | def has_pointer?
|
---|
| 353 | @type.has_pointer?
|
---|
| 354 | end
|
---|
| 355 |
|
---|
| 356 | #=== size_is, count_is, string æå®ããããã¤ã³ã¿ãæã¤ã
|
---|
| 357 | # size_is, count_is, string æå®ããããã¤ã³ã¿åãã¾ãã¯ãããã¡ã³ãã«æã¤æ§é ä½ãã¾ãã¯ããããè¦ç´ ã«æã¤é
|
---|
| 358 | å
|
---|
| 359 | def has_sized_pointer?
|
---|
| 360 | @type.has_sized_pointer?
|
---|
| 361 | end
|
---|
| 362 |
|
---|
| 363 | #=== é·ãæå®ã®ãªã string ãæã¤
|
---|
| 364 | # ãªãæå®ã®ãªã string æå®ããããã¤ã³ã¿åãã¾ãã¯ãããã¡ã³ãã«æã¤æ§é ä½ãã¾ãã¯ãããè¦ç´ ã«æã¤é
|
---|
| 365 | å
|
---|
| 366 | def has_unsized_string?
|
---|
| 367 | @type.has_unsized_string?
|
---|
| 368 | end
|
---|
| 369 |
|
---|
| 370 | def show_tree( indent )
|
---|
| 371 | indent.times { print " " }
|
---|
| 372 | if @typedef == nil then
|
---|
| 373 | puts "DefinedType: #{@type_name} is missing, const=#{@b_const} volatile=#{@b_volatile} #{locale_str}"
|
---|
| 374 | else
|
---|
| 375 | puts "DefinedType: #{@type_name}, const=#{@b_const} volatile=#{@b_volatile}"
|
---|
| 376 | end
|
---|
| 377 | super( indent + 1 )
|
---|
| 378 | @typedef.show_tree( indent + 1 )
|
---|
| 379 | @type.show_tree( indent + 1 )
|
---|
| 380 | end
|
---|
| 381 | end
|
---|
| 382 |
|
---|
| 383 | class VoidType < Type
|
---|
| 384 |
|
---|
| 385 | def check # æå³ç誤ããããã°ãæååãè¿ã
|
---|
| 386 | nil
|
---|
| 387 | end
|
---|
| 388 |
|
---|
| 389 | def check_init( locale, ident, initializer, kind, attribute = nil )
|
---|
| 390 | cdl_error2( locale, "T1007 $1: void type variable cannot have initializer" , ident )
|
---|
| 391 | end
|
---|
| 392 |
|
---|
| 393 | def get_type_str
|
---|
| 394 | "#{super}void"
|
---|
| 395 | end
|
---|
| 396 |
|
---|
| 397 | def get_type_str_post
|
---|
| 398 | ""
|
---|
| 399 | end
|
---|
| 400 |
|
---|
| 401 | def show_tree( indent )
|
---|
| 402 | indent.times { print " " }
|
---|
| 403 | puts "VoidType #{locale_str}"
|
---|
| 404 | super( indent + 1 )
|
---|
| 405 | end
|
---|
| 406 | end
|
---|
| 407 |
|
---|
| 408 | class BoolType < Type
|
---|
| 409 |
|
---|
| 410 | def check # æå³ç誤ããããã°ãæååãè¿ã
|
---|
| 411 | nil
|
---|
| 412 | end
|
---|
| 413 |
|
---|
| 414 | def get_type_str
|
---|
| 415 | "#{super}bool_t"
|
---|
| 416 | end
|
---|
| 417 |
|
---|
| 418 | def get_type_str_post
|
---|
| 419 | ""
|
---|
| 420 | end
|
---|
| 421 |
|
---|
| 422 | def show_tree( indent )
|
---|
| 423 | indent.times { print " " }
|
---|
| 424 | puts "BoolType #{locale_str}"
|
---|
| 425 | super( indent + 1 )
|
---|
| 426 | end
|
---|
| 427 | end
|
---|
| 428 |
|
---|
| 429 | class IntType < Type
|
---|
| 430 | # @bit_size:: -11: char, -1: char_t, -2: short, -3: int, -4: long, -5: long long
|
---|
| 431 | # 8, 16, 32, 64, 128
|
---|
| 432 | # @sign:: :SIGNED, :UNSIGNED, nil
|
---|
| 433 |
|
---|
| 434 | def initialize( bit_size )
|
---|
| 435 | super()
|
---|
| 436 | @bit_size = bit_size
|
---|
| 437 | @sign = nil
|
---|
| 438 | end
|
---|
| 439 |
|
---|
| 440 | def set_sign( sign, b_uint = false )
|
---|
| 441 | if @sign then
|
---|
| 442 | if @sign != sign then
|
---|
| 443 | cdl_error( "T1008 ambigous signed or unsigned" )
|
---|
| 444 | end
|
---|
| 445 | elsif b_uint == false && @bit_size > 0 then
|
---|
| 446 | if sign == :SIGNED then
|
---|
| 447 | cdl_warning( "W2001 signed int$1_t: obsolete. use int$2_t" , @bit_size, @bit_size )
|
---|
| 448 | else
|
---|
| 449 | cdl_warning( "W2002 unsinged int$1_t: obsolete. use uint$2_t" , @bit_size, @bit_size )
|
---|
| 450 | end
|
---|
| 451 | end
|
---|
| 452 |
|
---|
| 453 | @sign = sign
|
---|
| 454 |
|
---|
| 455 | if @sign != :SIGNED && @sign != :UNSIGNED then
|
---|
| 456 | raise "set_sign: unknown sign: #{@sign}"
|
---|
| 457 | end
|
---|
| 458 | end
|
---|
| 459 |
|
---|
| 460 | def check # æå³ç誤ããããã°ãæååãè¿ã
|
---|
| 461 | nil
|
---|
| 462 | end
|
---|
| 463 |
|
---|
| 464 | def check_init( locale, ident, initializer, kind, attribute = nil )
|
---|
| 465 | val = initializer # C_EXP, Array
|
---|
| 466 | if val.instance_of?( Expression ) then
|
---|
| 467 | val = val.eval_const2( nil, attribute )
|
---|
| 468 | # è©ä¾¡ã®çµæ C_EXP ã Array ã¨ãªãå¯è½æ§ããã
|
---|
| 469 | end
|
---|
| 470 |
|
---|
| 471 | if val.instance_of? Token then # StringVal å°å
|
---|
| 472 | ¥ã«ããããã¯ã Token ã¯æ¥ãªãã¯ã
|
---|
| 473 | # val ã Token ã®å ´å == ã®å³è¾ºã String ã ã¨ã¨ã©ã¼ãèµ·ãã (#198)
|
---|
| 474 | cdl_error2( locale, "T1009 $1: $2: not integer" , ident, val )
|
---|
| 475 | return
|
---|
| 476 | elsif val.is_a? C_EXP then
|
---|
| 477 | # #192 var ã attribute ãåç
|
---|
| 478 | §ããattribute ã®å³è¾ºã C_EXP ã®å ´å
|
---|
| 479 | # const ã®å³è¾ºã C_EXP ã®å ´åã
|
---|
| 480 | return
|
---|
| 481 | elsif val.kind_of? FloatVal then
|
---|
| 482 | cdl_error2( locale, "T1011 $1: need cast to assign float to integer" , ident )
|
---|
| 483 | return
|
---|
| 484 | elsif val.instance_of? Array then
|
---|
| 485 | cdl_error2( locale, "T1017 $1: unsuitable initializer for scalar type" , ident )
|
---|
| 486 | return
|
---|
| 487 | elsif val == nil then
|
---|
| 488 | cdl_error2( locale, "T1010 $1: initializer is not constant" , ident )
|
---|
| 489 | return
|
---|
| 490 | end
|
---|
| 491 |
|
---|
| 492 | if ! val.kind_of? IntegerVal then
|
---|
| 493 | cdl_error2( locale, "T1012 $1: $2: not integer" , ident, val )
|
---|
| 494 | return
|
---|
| 495 | end
|
---|
| 496 |
|
---|
| 497 | val = val.to_i
|
---|
| 498 | max = get_max
|
---|
| 499 | min = get_min
|
---|
| 500 | dbgPrint "sign=#{@sign} ident=#{ident} val=#{val} max=#{max} min=#{min}\n"
|
---|
| 501 |
|
---|
| 502 | if max != nil then
|
---|
| 503 | if val > max then
|
---|
| 504 | if @sign == :SIGNED || @sign == nil then
|
---|
| 505 | cdl_error2( locale, "T1013 $1: too large (max=$2)" , ident, max )
|
---|
| 506 | else
|
---|
| 507 | cdl_error2( locale, "T1016 $1: too large (max=$2)" , ident, max )
|
---|
| 508 | end
|
---|
| 509 | end
|
---|
| 510 | end
|
---|
| 511 |
|
---|
| 512 | if min != nil then
|
---|
| 513 | if val < min
|
---|
| 514 | if @sign == :SIGNED || @sign == nil then
|
---|
| 515 | cdl_error2( locale, "T1014 $1: too large negative value (min=$2)" , ident, min )
|
---|
| 516 | else
|
---|
| 517 | cdl_error2( locale, "T1015 $1: negative value for unsigned" , ident )
|
---|
| 518 | end
|
---|
| 519 | end
|
---|
| 520 | end
|
---|
| 521 | end
|
---|
| 522 |
|
---|
| 523 | #=== IntType# æ大å¤ãæå°å¤ããã§ãã¯ãã¦ã¯ãªãããã
|
---|
| 524 | # ãã£ã¹ãæ¼ç®ãè¡ã
|
---|
| 525 | #in_val:: IntegerVal, FloatVal: ãã®åã«ãã£ã¹ãããå¤
|
---|
| 526 | #from_type:: Symbol: :IntType, :FloatType IntType ã®å ´åã¯ãããæ°ã§ã¯ãªãããFloatType ã®å ´åã¯æ大å¤ã§ã¯ãªãã
|
---|
| 527 | def check_and_clip( in_val, from_type = :IntType )
|
---|
| 528 | bit_size = get_bit_size
|
---|
| 529 | if bit_size == -1 then
|
---|
| 530 | bit_size = 8
|
---|
| 531 | end
|
---|
| 532 | val = in_val.to_i
|
---|
| 533 | if get_max && val > get_max then
|
---|
| 534 | if from_type == :IntType then
|
---|
| 535 | rval = ((1 << bit_size)-1) & val # bit æ°ã§ã¯ãªãã
|
---|
| 536 | else
|
---|
| 537 | rval = get_max # æ大å¤ã§ã¯ãªãã (FloatType)
|
---|
| 538 | end
|
---|
| 539 | cdl_warning( "W2003 $1: too large to cast to $2, clipped($3)" , in_val, get_type_str, rval )
|
---|
| 540 | elsif get_min && val < get_min then
|
---|
| 541 | if from_type == :IntType then
|
---|
| 542 | rval = ((1 << bit_size)-1) & val
|
---|
| 543 | else
|
---|
| 544 | rval = get_min
|
---|
| 545 | end
|
---|
| 546 | if @sign == :SIGNED || @sign == nil then
|
---|
| 547 | cdl_warning( "W2004 $1: too small to cast to $2, clipped($3)" , in_val, get_type_str, rval )
|
---|
| 548 | else # @sign == :UNSIGNED || @sign == nil (char ã®å ´å)
|
---|
| 549 | cdl_warning( "W2005 $1: negative value for unsigned: convert to $2" , in_val, rval )
|
---|
| 550 | end
|
---|
| 551 | else
|
---|
| 552 | rval = val
|
---|
| 553 | end
|
---|
| 554 | return rval
|
---|
| 555 | end
|
---|
| 556 |
|
---|
| 557 | def get_min
|
---|
| 558 | if @sign == :SIGNED || @sign == nil then
|
---|
| 559 | if @bit_size == -1 then
|
---|
| 560 | bit_sz = 8 # char_t ã¯ãæ符å·ã«æ±ã
|
---|
| 561 | else
|
---|
| 562 | bit_sz = @bit_size
|
---|
| 563 | end
|
---|
| 564 | case bit_sz
|
---|
| 565 | when 8, 16, 32, 64, 128
|
---|
| 566 | return - ( 1 << ( bit_sz - 1 ))
|
---|
| 567 | else # -1, -2, -3, -4, -5, -11
|
---|
| 568 | return nil
|
---|
| 569 | end
|
---|
| 570 | else # @sign == :UNSIGNED
|
---|
| 571 | return 0
|
---|
| 572 | end
|
---|
| 573 | end
|
---|
| 574 |
|
---|
| 575 | def get_max
|
---|
| 576 | if @bit_size == -1 then
|
---|
| 577 | if @sign == nil then
|
---|
| 578 | return 255 # char_t ã¯ãç¡ç¬¦å·ã«æ±ã
|
---|
| 579 | else
|
---|
| 580 | bit_sz = 8
|
---|
| 581 | end
|
---|
| 582 | else
|
---|
| 583 | bit_sz = @bit_size
|
---|
| 584 | end
|
---|
| 585 | if @sign == :SIGNED || @sign == nil then
|
---|
| 586 | case bit_sz
|
---|
| 587 | when 8, 16, 32, 64, 128
|
---|
| 588 | return ( 1 << ( bit_sz - 1 )) -1
|
---|
| 589 | else # -1, -2, -3, -4, -5, -11
|
---|
| 590 | return nil
|
---|
| 591 | end
|
---|
| 592 | else # @sign == :UNSIGNED
|
---|
| 593 | case bit_sz
|
---|
| 594 | when 8, 16, 32, 64, 128
|
---|
| 595 | return ( 1 << bit_sz ) - 1
|
---|
| 596 | else # -2, -3, -4, -5, -11
|
---|
| 597 | return nil
|
---|
| 598 | end
|
---|
| 599 | end
|
---|
| 600 | end
|
---|
| 601 |
|
---|
| 602 | #=== IntType# C è¨èªã«ãããååï¼ä¿®é£¾åä»ãï¼
|
---|
| 603 | def get_type_str
|
---|
| 604 | str = super
|
---|
| 605 |
|
---|
| 606 | # NEW_MODE
|
---|
| 607 | case @sign
|
---|
| 608 | when :SIGNED
|
---|
| 609 | sign = ""
|
---|
| 610 | signL = "signed "
|
---|
| 611 | when :UNSIGNED
|
---|
| 612 | sign = "u"
|
---|
| 613 | signL = "unsigned "
|
---|
| 614 | else
|
---|
| 615 | sign = ""
|
---|
| 616 | signL = ""
|
---|
| 617 | end
|
---|
| 618 |
|
---|
| 619 | # p "get_type_str: sign:#{@sign} signL=#{signL}"
|
---|
| 620 |
|
---|
| 621 | case @bit_size
|
---|
| 622 | when -1 # char_t å
|
---|
| 623 | if @sign == :SIGNED then
|
---|
| 624 | sign = "s"
|
---|
| 625 | end
|
---|
| 626 | str = "#{str}#{sign}char_t"
|
---|
| 627 | when -11 # char å(obsolete)
|
---|
| 628 | str = "#{str}#{signL}char"
|
---|
| 629 | when -2 # short å
|
---|
| 630 | str = "#{str}#{signL}short"
|
---|
| 631 | when -3 # int å
|
---|
| 632 | str = "#{str}#{signL}int"
|
---|
| 633 | when -4 # long å
|
---|
| 634 | str = "#{str}#{signL}long"
|
---|
| 635 | when -5 # long long å
|
---|
| 636 | str = "#{str}#{signL}long long"
|
---|
| 637 | when 8, 16, 32, 64, 128 # int16, int32, int64, int128 å
|
---|
| 638 | str = "#{str}#{sign}int#{@bit_size}_t"
|
---|
| 639 | end
|
---|
| 640 |
|
---|
| 641 | return str
|
---|
| 642 | end
|
---|
| 643 |
|
---|
| 644 | #=== IntType# C è¨èªã«ãããååï¼å¾ç½®æååï¼
|
---|
| 645 | def get_type_str_post
|
---|
| 646 | ""
|
---|
| 647 | end
|
---|
| 648 |
|
---|
| 649 | #=== IntType#bit_size ãå¾ã
|
---|
| 650 | # è¿ãããå¤ã¯ @bit_size ã®ä»æ§ãåç
|
---|
| 651 | §
|
---|
| 652 | def get_bit_size
|
---|
| 653 | return @bit_size
|
---|
| 654 | end
|
---|
| 655 |
|
---|
| 656 | #=== IntType# sign ãå¾ã
|
---|
| 657 | # @sign ã®èª¬æãåç
|
---|
| 658 | §
|
---|
| 659 | def get_sign
|
---|
| 660 | @sign
|
---|
| 661 | end
|
---|
| 662 |
|
---|
| 663 | def show_tree( indent )
|
---|
| 664 | indent.times { print " " }
|
---|
| 665 | puts "IntType bit_size=#{@bit_size} sign=#{@sign} const=#{@b_const} volatile=#{@b_volatile} #{locale_str}"
|
---|
| 666 | super( indent + 1 )
|
---|
| 667 | end
|
---|
| 668 | end
|
---|
| 669 |
|
---|
| 670 | class FloatType < Type
|
---|
| 671 | # @bit_size:: 32, 64, (80), -32, -64, -128
|
---|
| 672 |
|
---|
| 673 | def initialize( bit_size )
|
---|
| 674 | super()
|
---|
| 675 | @bit_size = bit_size
|
---|
| 676 | end
|
---|
| 677 |
|
---|
| 678 | def check # æå³ç誤ããããã°ãæååãè¿ã
|
---|
| 679 | nil
|
---|
| 680 | end
|
---|
| 681 |
|
---|
| 682 | # mikan Float åã® C_EXP å¯¾å¿ (generate.rb ã«ãå¤æ´å¿
|
---|
| 683 | è¦)
|
---|
| 684 | def check_init( locale, ident, initializer, kind, attribute = nil )
|
---|
| 685 | # åã«å¯¾ããåæå¤ã«èª¤ããããã°ãã¨ã©ã¼æååãè¿ã
|
---|
| 686 | val = initializer
|
---|
| 687 | if val.instance_of?( Expression ) then
|
---|
| 688 | val = val.eval_const2( nil, attribute )
|
---|
| 689 | # è©ä¾¡ã®çµæ C_EXP ã Array ã¨ãªãå¯è½æ§ããã
|
---|
| 690 | end
|
---|
| 691 |
|
---|
| 692 | if val.instance_of? Token then
|
---|
| 693 | # val ã Token ã®å ´å == ã®å³è¾ºã String ã ã¨ã¨ã©ã¼ãèµ·ãã
|
---|
| 694 | cdl_error2( locale, "T1018 $1: $2: not number" , ident, val )
|
---|
| 695 | return
|
---|
| 696 | elsif val.instance_of? Array then
|
---|
| 697 | cdl_error2( locale, "T1020 $1: unsuitable initializer for scalar type" , ident )
|
---|
| 698 | return
|
---|
| 699 | elsif val.instance_of? C_EXP then
|
---|
| 700 | return
|
---|
| 701 | elsif val == nil then
|
---|
| 702 | cdl_error2( locale, "T1019 $1: initializer is not constant" , ident )
|
---|
| 703 | return
|
---|
| 704 | elsif ! val.kind_of?( IntegerVal ) && ! val.kind_of?( FloatVal ) then
|
---|
| 705 | cdl_error2( locale, "T1037 $1: not number" , ident )
|
---|
| 706 | return
|
---|
| 707 | end
|
---|
| 708 | # else
|
---|
| 709 | # cdl_error2( locale, "T1020 $1: unsuitable initializer for scalar type" , ident )
|
---|
| 710 | # return
|
---|
| 711 | # end
|
---|
| 712 | return
|
---|
| 713 | end
|
---|
| 714 |
|
---|
| 715 | def get_type_str
|
---|
| 716 | str = super
|
---|
| 717 |
|
---|
| 718 | case @bit_size
|
---|
| 719 | when 32
|
---|
| 720 | str = "#{str}float32_t"
|
---|
| 721 | when 64
|
---|
| 722 | str = "#{str}double64_t"
|
---|
| 723 | when -32
|
---|
| 724 | str = "#{str}float"
|
---|
| 725 | when -64
|
---|
| 726 | str = "#{str}double"
|
---|
| 727 | when -128
|
---|
| 728 | str = "#{str}long double"
|
---|
| 729 | end
|
---|
| 730 | return str
|
---|
| 731 | end
|
---|
| 732 |
|
---|
| 733 | def get_type_str_post
|
---|
| 734 | ""
|
---|
| 735 | end
|
---|
| 736 |
|
---|
| 737 | def get_bit_size
|
---|
| 738 | @bit_size
|
---|
| 739 | end
|
---|
| 740 |
|
---|
| 741 | def show_tree( indent )
|
---|
| 742 | indent.times { print " " }
|
---|
| 743 | puts "FloatType bit_size=#{@bit_size} qualifier=#{@qualifier} #{locale_str}"
|
---|
| 744 | super( indent + 1 )
|
---|
| 745 | end
|
---|
| 746 | end
|
---|
| 747 |
|
---|
| 748 | class EnumType < Type # mikan
|
---|
| 749 | # @bit_size:: -1: enum
|
---|
| 750 | # 8, 16, 32, 64, 128
|
---|
| 751 | # @element:: []
|
---|
| 752 | # @element_val:: []
|
---|
| 753 |
|
---|
| 754 | def initialize( bit_size )
|
---|
| 755 | super()
|
---|
| 756 | @bit_size = bit_size
|
---|
| 757 | end
|
---|
| 758 |
|
---|
| 759 | def check
|
---|
| 760 | # mikan enum check
|
---|
| 761 | end
|
---|
| 762 |
|
---|
| 763 | def show_tree( indent )
|
---|
| 764 | indent.times { print " " }
|
---|
| 765 | puts "EnumType bit_size=#{@bit_size} qualifier=#{@qualifier} #{locale_str}"
|
---|
| 766 | super( indent + 1 )
|
---|
| 767 | # mikan element
|
---|
| 768 | end
|
---|
| 769 | end
|
---|
| 770 |
|
---|
| 771 | class StructType < Type
|
---|
| 772 | # @tag::
|
---|
| 773 | # @b_define:: true if define, false if refer
|
---|
| 774 | # @members_decl:: NamedList
|
---|
| 775 | # @definition:: StructType
|
---|
| 776 | # @b_has_pointer_member:: bool : ã¡ã³ãã«ãã¤ã³ã¿åããã
|
---|
| 777 | # @b_has_sized_pointer_member:: bool : ã¡ã³ãã«ãã¤ã³ã¿åããã
|
---|
| 778 | # @b_has_unsized_string_member:: bool : ã¡ã³ãã«ãã¤ã³ã¿åããã
|
---|
| 779 | # @b_hasTag:: bool : ã¿ã°ããã
|
---|
| 780 | # @member_types_symbol:: Symbol : tag ãç¡ãæã®ã¿è¨å® (ãã以å¤ã§ã¯ nil)
|
---|
| 781 |
|
---|
| 782 | @@structtype_current_stack = []
|
---|
| 783 | @@structtype_current_sp = -1
|
---|
| 784 |
|
---|
| 785 | # tag ãªã struct
|
---|
| 786 | @@no_struct_tag_num = 0
|
---|
| 787 | @@no_tag_struct_list = {}
|
---|
| 788 |
|
---|
| 789 | def initialize( tag = nil )
|
---|
| 790 | super()
|
---|
| 791 | @tag = tag
|
---|
| 792 | if tag then
|
---|
| 793 | @b_hasTag = true
|
---|
| 794 | else
|
---|
| 795 | @b_hasTag = false
|
---|
| 796 | end
|
---|
| 797 | @@structtype_current_sp += 1
|
---|
| 798 | @@structtype_current_stack[@@structtype_current_sp] = self
|
---|
| 799 | @b_has_pointer_member = false
|
---|
| 800 | @b_has_sized_pointer_member = false
|
---|
| 801 | @b_has_unsized_string_member = false
|
---|
| 802 | @member_types_symbol = nil
|
---|
| 803 | end
|
---|
| 804 |
|
---|
| 805 | def self.set_define( b_define )
|
---|
| 806 | @@structtype_current_stack[@@structtype_current_sp].set_define( b_define )
|
---|
| 807 | end
|
---|
| 808 |
|
---|
| 809 | def set_define( b_define )
|
---|
| 810 | @b_define = b_define
|
---|
| 811 | if @b_define then
|
---|
| 812 | @members_decl = NamedList.new( nil, "in struct #{@tag}" )
|
---|
| 813 | # if @tag then ç»é²ã¿ã¤ãã³ã°ãçµããã«å¤æ´ V1.0.2.19
|
---|
| 814 | # Namespace.new_structtype( self )
|
---|
| 815 | # end
|
---|
| 816 | else
|
---|
| 817 | @definition = Namespace.find_tag( @tag )
|
---|
| 818 | # check_struct_tag ã«ç§»ã V1.0.2.19
|
---|
| 819 | # if @definition == nil then
|
---|
| 820 | # cdl_error( "T1021 \'$1\': struct not defined" , @tag )
|
---|
| 821 | # end
|
---|
| 822 | end
|
---|
| 823 | end
|
---|
| 824 |
|
---|
| 825 | def self.new_member( member_decl )
|
---|
| 826 | @@structtype_current_stack[@@structtype_current_sp].new_member( member_decl )
|
---|
| 827 | end
|
---|
| 828 |
|
---|
| 829 | def new_member( member_decl )
|
---|
| 830 | member_decl.set_owner self # Decl (StructType)
|
---|
| 831 | @members_decl.add_item( member_decl )
|
---|
| 832 | if member_decl.get_type.has_pointer?
|
---|
| 833 | @b_has_pointer_member = true
|
---|
| 834 | end
|
---|
| 835 | if member_decl.get_type.has_sized_pointer?
|
---|
| 836 | @b_has_sized_pointer_member = true
|
---|
| 837 | end
|
---|
| 838 | if member_decl.get_type.has_unsized_string?
|
---|
| 839 | @b_has_unsized_string_member = true
|
---|
| 840 | end
|
---|
| 841 | end
|
---|
| 842 |
|
---|
| 843 | def check # æå³ç誤ããããã°ãæååãè¿ã
|
---|
| 844 | nil
|
---|
| 845 | end
|
---|
| 846 |
|
---|
| 847 | #=== æ§é ä½ã®ã¿ã°ããã§ãã¯
|
---|
| 848 | # declarator ã®æç¹ã§ãã§ãã¯ãã
|
---|
| 849 | #kind:: Decl ã® @kind ãåç
|
---|
| 850 | §
|
---|
| 851 | def check_struct_tag kind
|
---|
| 852 | if @tag == nil
|
---|
| 853 | return
|
---|
| 854 | end
|
---|
| 855 |
|
---|
| 856 | st = Namespace.find_tag( @tag )
|
---|
| 857 | if st == nil then
|
---|
| 858 | cdl_error( "T1022 struct $1: not defined" , @tag )
|
---|
| 859 | end
|
---|
| 860 | end
|
---|
| 861 |
|
---|
| 862 | # mikan Float åã® C_EXP å¯¾å¿ (generate.rb ã«ãå¤æ´å¿
|
---|
| 863 | è¦)
|
---|
| 864 | def check_init( locale, ident, initializer, kind, attribute = nil )
|
---|
| 865 |
|
---|
| 866 | st = Namespace.find_tag( @tag )
|
---|
| 867 | if st == nil then
|
---|
| 868 | cdl_error2( locale, "T1023 struct $1: not defined" , @tag )
|
---|
| 869 | return
|
---|
| 870 | end
|
---|
| 871 |
|
---|
| 872 | # åæååãå¼ã®å ´åãåï¼ã¿ã°ï¼ãä¸è´ããããã§ãã¯
|
---|
| 873 | if initializer.instance_of?( Expression ) then
|
---|
| 874 | t = initializer.get_type( attribute )
|
---|
| 875 | # print "Check init #{t.class} #{t.get_name}\n"
|
---|
| 876 | if ! t.kind_of?( StructType ) then
|
---|
| 877 | if t then
|
---|
| 878 | str = t.get_type_str
|
---|
| 879 | else
|
---|
| 880 | str = "unknown"
|
---|
| 881 | end
|
---|
| 882 | cdl_error2( locale, "T1038 $1: initializer type mismatch. '$2' & '$3'" , ident, get_type_str, str )
|
---|
| 883 | elsif @tag != t.get_name then
|
---|
| 884 | cdl_error2( locale, "T1039 $1: struct tag mismatch $2 and $3" , ident, @tag, t.get_name )
|
---|
| 885 | end
|
---|
| 886 | initializer = initializer.eval_const( attribute )
|
---|
| 887 | end
|
---|
| 888 |
|
---|
| 889 | if initializer.instance_of?( Array ) then
|
---|
| 890 | i = 0
|
---|
| 891 | st.get_members_decl.get_items.each { |d|
|
---|
| 892 | if initializer[i] then
|
---|
| 893 | d.get_type.check_init( locale, "#{ident}.#{d.get_identifier}", initializer[i], kind )
|
---|
| 894 | end
|
---|
| 895 | i += 1
|
---|
| 896 | }
|
---|
| 897 | else
|
---|
| 898 | cdl_error2( locale, "T1024 $1: unsuitable initializer for struct" , ident )
|
---|
| 899 | end
|
---|
| 900 | end
|
---|
| 901 |
|
---|
| 902 | def self.end_of_parse()
|
---|
| 903 | @@structtype_current_stack[@@structtype_current_sp].end_of_parse
|
---|
| 904 | @@structtype_current_sp -= 1
|
---|
| 905 | end
|
---|
| 906 |
|
---|
| 907 | def end_of_parse()
|
---|
| 908 | if @members_decl == nil # @b_define = false ã¾ãã¯ã¡ã³ãã¼ã®ãªãæ§é ä½ï¼ã¨ã©ã¼ï¼
|
---|
| 909 | return
|
---|
| 910 | end
|
---|
| 911 | @members_decl.get_items.each{ |md|
|
---|
| 912 | size = md.get_size_is
|
---|
| 913 | if size then
|
---|
| 914 | val = size.eval_const( @members_decl )
|
---|
| 915 | if val == nil then
|
---|
| 916 | type = size.get_type( @members_decl )
|
---|
| 917 | if ! type.kind_of?( IntType ) then
|
---|
| 918 | cdl_error( "T1025 size_is argument is not integer type" )
|
---|
| 919 | end
|
---|
| 920 | end
|
---|
| 921 | end
|
---|
| 922 | count = md.get_count_is
|
---|
| 923 | if count then
|
---|
| 924 | val = count.eval_const( @members_decl )
|
---|
| 925 | if val == nil then
|
---|
| 926 | type = count.get_type( @members_decl )
|
---|
| 927 | if ! type.kind_of?( IntType ) then
|
---|
| 928 | cdl_error( "T1026 count_is argument is not integer type" )
|
---|
| 929 | end
|
---|
| 930 | end
|
---|
| 931 | end
|
---|
| 932 | string = md.get_string
|
---|
| 933 | if string == -1 then
|
---|
| 934 | # é·ãæå®ãªã
|
---|
| 935 | elsif string then
|
---|
| 936 | val = string.eval_const( @members_decl )
|
---|
| 937 | if val == nil then
|
---|
| 938 | type = string.get_type( @members_decl )
|
---|
| 939 | if ! type.kind_of?( IntType ) then
|
---|
| 940 | cdl_error( "T1027 string argument is not integer type" )
|
---|
| 941 | end
|
---|
| 942 | end
|
---|
| 943 | end
|
---|
| 944 | }
|
---|
| 945 |
|
---|
| 946 | if @tag == nil then
|
---|
| 947 | @member_types_symbol = get_member_types_symbol
|
---|
| 948 | # print "member_types_symbol = #{get_member_types_symbol}\n"
|
---|
| 949 | if @@no_tag_struct_list[ @member_types_symbol ] then
|
---|
| 950 | @tag = @@no_tag_struct_list[ @member_types_symbol ]
|
---|
| 951 | else
|
---|
| 952 | @tag = :"TAG_#{@@no_struct_tag_num}_TECS_internal__"
|
---|
| 953 | @@no_struct_tag_num += 1
|
---|
| 954 | @@no_tag_struct_list[ @member_types_symbol ] = @tag
|
---|
| 955 | Namespace.new_structtype( self )
|
---|
| 956 | end
|
---|
| 957 | else
|
---|
| 958 | if @b_define then
|
---|
| 959 | Namespace.new_structtype( self )
|
---|
| 960 | end
|
---|
| 961 | end
|
---|
| 962 | end
|
---|
| 963 |
|
---|
| 964 | def get_name
|
---|
| 965 | @tag
|
---|
| 966 | end
|
---|
| 967 |
|
---|
| 968 | def get_type_str # mikan struct get_type_str
|
---|
| 969 | str = super
|
---|
| 970 |
|
---|
| 971 | if @b_hasTag then
|
---|
| 972 | # typedef struct tag StructType; ã®å½¢å¼ã®å ´å
|
---|
| 973 | # struct ã®æ¬ä½ã¯ãå¥ã«çæããã
|
---|
| 974 | return "#{str}struct #{@tag}"
|
---|
| 975 |
|
---|
| 976 | else
|
---|
| 977 | # typedef struct { int a; } StructType; ã®å½¢å¼ã®å ´å
|
---|
| 978 | str += "struct {"
|
---|
| 979 | @members_decl.get_items.each{ |i|
|
---|
| 980 | str += sprintf( "%s %s%s;", "#{i.get_type.get_type_str}", "#{i.get_name}", "#{i.get_type.get_type_str_post}" )
|
---|
| 981 | }
|
---|
| 982 | str += "} "
|
---|
| 983 |
|
---|
| 984 | return str
|
---|
| 985 |
|
---|
| 986 | end
|
---|
| 987 | end
|
---|
| 988 |
|
---|
| 989 | def get_type_str_post
|
---|
| 990 | ""
|
---|
| 991 | end
|
---|
| 992 |
|
---|
| 993 | def get_members_decl
|
---|
| 994 | return @members_decl if @members_decl
|
---|
| 995 |
|
---|
| 996 | st = Namespace.find_tag( @tag )
|
---|
| 997 | if st then
|
---|
| 998 | return st.get_members_decl
|
---|
| 999 | end
|
---|
| 1000 |
|
---|
| 1001 | return nil
|
---|
| 1002 | end
|
---|
| 1003 |
|
---|
| 1004 | def has_pointer?
|
---|
| 1005 | if @definition
|
---|
| 1006 | return @definition.has_pointer?
|
---|
| 1007 | else
|
---|
| 1008 | return @b_has_pointer_member
|
---|
| 1009 | end
|
---|
| 1010 | end
|
---|
| 1011 |
|
---|
| 1012 | def has_sized_pointer?
|
---|
| 1013 | if @definition
|
---|
| 1014 | return @definition.has_sized_pointer?
|
---|
| 1015 | else
|
---|
| 1016 | return @b_has_sized_pointer_member
|
---|
| 1017 | end
|
---|
| 1018 | end
|
---|
| 1019 |
|
---|
| 1020 | def has_unsized_string?
|
---|
| 1021 | if @definition
|
---|
| 1022 | return @definition.has_unsized_string?
|
---|
| 1023 | else
|
---|
| 1024 | return @b_has_unsized_string_member
|
---|
| 1025 | end
|
---|
| 1026 | end
|
---|
| 1027 |
|
---|
| 1028 | #=== åãæ§é ä½ãã©ãããã§ãã¯ãã
|
---|
| 1029 | # tag ã®ãã§ãã¯ã¯è¡ããªã
|
---|
| 1030 | # ãã¹ã¦ã®ã¡ã³ãã®ååã¨åãä¸è´ãããã¨ã確èªãã
|
---|
| 1031 | def same? another
|
---|
| 1032 | md = another.get_members_decl
|
---|
| 1033 | if @members_decl == nil || md == nil
|
---|
| 1034 | return false
|
---|
| 1035 | end
|
---|
| 1036 |
|
---|
| 1037 | md1 = @members_decl.get_items
|
---|
| 1038 | md2 = md.get_items
|
---|
| 1039 | if( md1.length != md2.length )
|
---|
| 1040 | return false
|
---|
| 1041 | end
|
---|
| 1042 |
|
---|
| 1043 | i = 0
|
---|
| 1044 | while i < md1.length
|
---|
| 1045 | if md1[i].get_name != md2[i].get_name ||
|
---|
| 1046 | md1[i].get_type.get_type_str != md2[i].get_type.get_type_str ||
|
---|
| 1047 | md1[i].get_type.get_type_str_post != md2[i].get_type.get_type_str_post
|
---|
| 1048 | return false
|
---|
| 1049 | end
|
---|
| 1050 | i += 1
|
---|
| 1051 | end
|
---|
| 1052 |
|
---|
| 1053 | return true
|
---|
| 1054 | end
|
---|
| 1055 |
|
---|
| 1056 | def get_member_types_symbol
|
---|
| 1057 | mts = ''
|
---|
| 1058 | @members_decl.get_items.each { |member|
|
---|
| 1059 | mts += member.get_type.get_type_str + member.get_type.get_type_str_post + ';'
|
---|
| 1060 | }
|
---|
| 1061 | return mts.to_sym
|
---|
| 1062 | end
|
---|
| 1063 |
|
---|
| 1064 | def show_tree( indent )
|
---|
| 1065 | indent.times { print " " }
|
---|
| 1066 | puts "StructType tag: #{@tag} qualifier=#{@qualifier} has_pointer=#{@b_has_pointer_member} #{locale_str}"
|
---|
| 1067 | super( indent + 1 )
|
---|
| 1068 | if @b_define then
|
---|
| 1069 | @members_decl.show_tree( indent + 1 )
|
---|
| 1070 | end
|
---|
| 1071 | end
|
---|
| 1072 | end
|
---|
| 1073 |
|
---|
| 1074 | class FuncType < Type
|
---|
| 1075 | # @paramlist:: ParamList
|
---|
| 1076 | # @type:: return type : PtrType, ArrayType, FuncType, IntType, FloatType, ...
|
---|
| 1077 | # @b_oneway:: bool: true, false
|
---|
| 1078 | # @has_in:: bool : has [in] parameter
|
---|
| 1079 | # @has_inout:: bool : has [inout] parameter
|
---|
| 1080 | # @has_out:: bool : has [out] parameter
|
---|
| 1081 | # @has_send:: bool : has [send] parameter
|
---|
| 1082 | # @has_receive:: bool : has [receive] parameter
|
---|
| 1083 |
|
---|
| 1084 | def initialize( paramlist = nil )
|
---|
| 1085 | super()
|
---|
| 1086 |
|
---|
| 1087 | @has_in = false
|
---|
| 1088 | @has_inout = false
|
---|
| 1089 | @has_out = false
|
---|
| 1090 | @has_send = false
|
---|
| 1091 | @has_receive = false
|
---|
| 1092 |
|
---|
| 1093 | @paramlist = paramlist
|
---|
| 1094 | @b_oneway = false
|
---|
| 1095 | if paramlist then
|
---|
| 1096 | paramlist.check_param
|
---|
| 1097 | else
|
---|
| 1098 | @paramlist = ParamList.new( nil )
|
---|
| 1099 | end
|
---|
| 1100 | @paramlist.set_owner self # ParamList
|
---|
| 1101 | @paramlist.get_items.each{ |p|
|
---|
| 1102 | case p.get_direction
|
---|
| 1103 | when :IN
|
---|
| 1104 | @has_in = true
|
---|
| 1105 | when :INOUT
|
---|
| 1106 | @has_inout = true
|
---|
| 1107 | when :OUT
|
---|
| 1108 | @has_out = true
|
---|
| 1109 | when :SEND
|
---|
| 1110 | @has_send = true
|
---|
| 1111 | when :RECEIVE
|
---|
| 1112 | @has_receive = true
|
---|
| 1113 | else
|
---|
| 1114 | raise "unkown direction"
|
---|
| 1115 | end
|
---|
| 1116 | }
|
---|
| 1117 |
|
---|
| 1118 | end
|
---|
| 1119 |
|
---|
| 1120 | def check # æå³ç誤ããããã°ãæååãè¿ã
|
---|
| 1121 | if @type.class == ArrayType then # é
|
---|
| 1122 | åãè¿ãé¢æ°
|
---|
| 1123 | return "function returning array"
|
---|
| 1124 | elsif @type.class == FuncType then # é¢æ°ãè¿ãé¢æ°
|
---|
| 1125 | return "function returning function"
|
---|
| 1126 | end
|
---|
| 1127 | return @type.check # é¢æ°ã® return ããåã®ãã§ãã¯
|
---|
| 1128 |
|
---|
| 1129 | # ãã©ã¡ã¼ã¿ã®åã®ãã§ãã¯ã¯ ParamList#check_param ã§è¡ã
|
---|
| 1130 | end
|
---|
| 1131 |
|
---|
| 1132 | def check_struct_tag kind
|
---|
| 1133 | @type.check_struct_tag kind
|
---|
| 1134 | # ParamDecl ã§ããã§ãã¯ãããã®ã§ãããã§ã¯ãã§ãã¯ããªã
|
---|
| 1135 | # @paramlist.check_struct_tag kind
|
---|
| 1136 | end
|
---|
| 1137 |
|
---|
| 1138 | def check_init( locale, ident, initializer, kind, attribute = nil )
|
---|
| 1139 | cdl_error2( locale, "T1028 $1: cannot initialize function pointer" , ident )
|
---|
| 1140 | return
|
---|
| 1141 | end
|
---|
| 1142 |
|
---|
| 1143 | def set_type( type )
|
---|
| 1144 | unless @type then
|
---|
| 1145 | @type = type
|
---|
| 1146 | else
|
---|
| 1147 | @type.set_type( type )
|
---|
| 1148 | end
|
---|
| 1149 | end
|
---|
| 1150 |
|
---|
| 1151 | #=== return type ãè¿ã
|
---|
| 1152 | #
|
---|
| 1153 | # return type ãè¿ã
|
---|
| 1154 | # get_return_type ã¨ãã¹ãã ã£ã
|
---|
| 1155 | def get_type
|
---|
| 1156 | @type
|
---|
| 1157 | end
|
---|
| 1158 |
|
---|
| 1159 | def get_type_str
|
---|
| 1160 | return @type.get_type_str
|
---|
| 1161 | end
|
---|
| 1162 |
|
---|
| 1163 | def get_type_str_post
|
---|
| 1164 | # åã ããè¿ã (ä»®å¼æ°ã®ååãå«ããªã)
|
---|
| 1165 | @paramlist.to_str( false )
|
---|
| 1166 | end
|
---|
| 1167 |
|
---|
| 1168 | def get_paramlist
|
---|
| 1169 | @paramlist
|
---|
| 1170 | end
|
---|
| 1171 |
|
---|
| 1172 | def set_oneway( b_oneway )
|
---|
| 1173 | @b_oneway = b_oneway
|
---|
| 1174 |
|
---|
| 1175 | if ( ( @type.get_type_str != "ER" && @type.get_type_str != "void" ) || @type.get_type_str_post != "" ) then
|
---|
| 1176 | cdl_error( "T1029 oneway function cannot return type \'$1$2\', \'void\' or \'ER\' is permitted" , @type.get_type_str, @type.get_type_str_post )
|
---|
| 1177 | end
|
---|
| 1178 |
|
---|
| 1179 | if @paramlist then
|
---|
| 1180 | @paramlist.get_items.each{ |p|
|
---|
| 1181 | if p.get_direction != :IN && p.get_direction != :SEND then
|
---|
| 1182 | cdl_error( "T1030 oneway function cannot have $1 parameter for \'$2\'" , p.get_direction, p.get_name )
|
---|
| 1183 | end
|
---|
| 1184 | }
|
---|
| 1185 | end
|
---|
| 1186 | end
|
---|
| 1187 |
|
---|
| 1188 | def is_oneway?
|
---|
| 1189 | @b_oneway
|
---|
| 1190 | end
|
---|
| 1191 |
|
---|
| 1192 | #=== Push Pop Allocator ãå¿
|
---|
| 1193 | è¦ãï¼
|
---|
| 1194 | # Transparent RPC ã®å ´å oneway ã㤠in ã®é
|
---|
| 1195 | å(size_is, count_is, string ã®ããããã§ä¿®é£¾ï¼ããã
|
---|
| 1196 | def need_PPAllocator?( b_opaque = false )
|
---|
| 1197 | if @b_oneway || b_opaque then
|
---|
| 1198 | return @paramlist.need_PPAllocator?( b_opaque )
|
---|
| 1199 | else
|
---|
| 1200 | return false
|
---|
| 1201 | end
|
---|
| 1202 | end
|
---|
| 1203 |
|
---|
| 1204 | #=== ãã©ã¡ã¼ã¿ã in, inout, out, send, receive ãæã¤ã
|
---|
| 1205 | def has_in?
|
---|
| 1206 | @has_in
|
---|
| 1207 | end
|
---|
| 1208 | def has_inout?
|
---|
| 1209 | @has_inout
|
---|
| 1210 | end
|
---|
| 1211 | def has_out?
|
---|
| 1212 | @has_out
|
---|
| 1213 | end
|
---|
| 1214 | def has_send?
|
---|
| 1215 | @has_send
|
---|
| 1216 | end
|
---|
| 1217 | def has_receive?
|
---|
| 1218 | @has_receive
|
---|
| 1219 | end
|
---|
| 1220 |
|
---|
| 1221 | #=== å
|
---|
| 1222 | ¥åæ¹åã®ãã©ã¡ã¼ã¿ãæã¤ã
|
---|
| 1223 | def has_inward?
|
---|
| 1224 | @has_in || @has_inout || @has_send
|
---|
| 1225 | end
|
---|
| 1226 | #=== åºåæ¹åã®ãã©ã¡ã¼ã¿ãæã¤ã
|
---|
| 1227 | def has_outward?
|
---|
| 1228 | @has_inout || @has_out || @has_receive
|
---|
| 1229 | end
|
---|
| 1230 |
|
---|
| 1231 | def show_tree( indent )
|
---|
| 1232 | indent.times { print " " }
|
---|
| 1233 | if @b_oneway then
|
---|
| 1234 | puts "FunctType: oneway=true #{locale_str}"
|
---|
| 1235 | else
|
---|
| 1236 | puts "FunctType: oneway=false #{locale_str}"
|
---|
| 1237 | end
|
---|
| 1238 | super( indent + 1 )
|
---|
| 1239 | if @paramlist then
|
---|
| 1240 | @paramlist.show_tree( indent + 1 )
|
---|
| 1241 | end
|
---|
| 1242 | (indent+1).times { print " " }
|
---|
| 1243 | puts "return type:"
|
---|
| 1244 | @type.show_tree( indent + 2 )
|
---|
| 1245 | end
|
---|
| 1246 | end
|
---|
| 1247 |
|
---|
| 1248 | class ArrayType < Type
|
---|
| 1249 | # @type:: element type : ArrayType, FuncType, IntType, FloatType, ...
|
---|
| 1250 | # @subscript:: Expression, nil if '[]'
|
---|
| 1251 |
|
---|
| 1252 | include HasType
|
---|
| 1253 |
|
---|
| 1254 | def initialize( subscript = nil )
|
---|
| 1255 | super()
|
---|
| 1256 | @subscript = subscript
|
---|
| 1257 | initHasType
|
---|
| 1258 | end
|
---|
| 1259 |
|
---|
| 1260 | def set_type( type )
|
---|
| 1261 | unless @type then
|
---|
| 1262 | @type = type
|
---|
| 1263 | else
|
---|
| 1264 | @type.set_type( type )
|
---|
| 1265 | end
|
---|
| 1266 | end
|
---|
| 1267 |
|
---|
| 1268 | #=== Array#qualifier(const, volatile) ã®è¨å®
|
---|
| 1269 | def set_qualifier( qualifier )
|
---|
| 1270 | clone_type
|
---|
| 1271 | @type.set_qualifier( qualifier )
|
---|
| 1272 | super
|
---|
| 1273 | end
|
---|
| 1274 |
|
---|
| 1275 | # é
|
---|
| 1276 | åè¦ç´ ã const ãªã const
|
---|
| 1277 | def is_const?
|
---|
| 1278 | @type.is_const?
|
---|
| 1279 | end
|
---|
| 1280 |
|
---|
| 1281 | # é
|
---|
| 1282 | åè¦ç´ ã volatile ãªã volatile
|
---|
| 1283 | def is_volatile?
|
---|
| 1284 | @type.is_volatile?
|
---|
| 1285 | end
|
---|
| 1286 |
|
---|
| 1287 | def get_type
|
---|
| 1288 | @type
|
---|
| 1289 | end
|
---|
| 1290 |
|
---|
| 1291 | def get_subscript
|
---|
| 1292 | @subscript
|
---|
| 1293 | end
|
---|
| 1294 |
|
---|
| 1295 | def get_type_str
|
---|
| 1296 | return "#{@type.get_type_str}"
|
---|
| 1297 | end
|
---|
| 1298 |
|
---|
| 1299 | def get_type_str_post
|
---|
| 1300 | if @subscript
|
---|
| 1301 | "[#{@subscript.eval_const(nil)}]#{@type.get_type_str_post}"
|
---|
| 1302 | else
|
---|
| 1303 | "[]#{@type.get_type_str_post}"
|
---|
| 1304 | end
|
---|
| 1305 | # "[#{@subscript.to_s}]#{@type.get_type_str_post}"
|
---|
| 1306 | end
|
---|
| 1307 |
|
---|
| 1308 | def check # æå³ç誤ããããã°ãæååãè¿ã
|
---|
| 1309 | if @type.class == FuncType then # é¢æ°ã®é
|
---|
| 1310 | å
|
---|
| 1311 | return "array of function"
|
---|
| 1312 | elsif @type.class == ArrayType then # æ·»æ°ãªãé
|
---|
| 1313 | åã®é
|
---|
| 1314 | å
|
---|
| 1315 | unless @type.get_subscript then
|
---|
| 1316 | return "subscript not specified"
|
---|
| 1317 | end
|
---|
| 1318 | end
|
---|
| 1319 |
|
---|
| 1320 | return @type.check # é
|
---|
| 1321 | åè¦ç´ ã®åããã§ãã¯
|
---|
| 1322 | end
|
---|
| 1323 |
|
---|
| 1324 | def check_struct_tag kind
|
---|
| 1325 | @type.check_struct_tag kind
|
---|
| 1326 | end
|
---|
| 1327 |
|
---|
| 1328 | def check_init( locale, ident, initializer, kind, attribute = nil )
|
---|
| 1329 | if ( initializer.instance_of?( Array ) ) then
|
---|
| 1330 | # è¦ç´ æ°ãæå®ããã¦ããå ´åãåæåè¦ç´ æ°ããã§ãã¯
|
---|
| 1331 | if @subscript then
|
---|
| 1332 | n_sub = @subscript.eval_const( nil )
|
---|
| 1333 | if n_sub then
|
---|
| 1334 | if initializer.length > n_sub then
|
---|
| 1335 | cdl_error2( locale, "T9999 $1: too many initializer, $2 for $3" , ident, initializer.length, n_sub )
|
---|
| 1336 | end
|
---|
| 1337 | end
|
---|
| 1338 | end
|
---|
| 1339 | index = 0
|
---|
| 1340 | initializer.each{ |i|
|
---|
| 1341 | @type.check_init( locale, "#{ident}[#{index}]", i, kind, attribute = nil )
|
---|
| 1342 | index += 1
|
---|
| 1343 | }
|
---|
| 1344 | else
|
---|
| 1345 | cdl_error2( locale, "T1031 $1: unsuitable initializer for array" , ident )
|
---|
| 1346 | end
|
---|
| 1347 | end
|
---|
| 1348 |
|
---|
| 1349 | def has_pointer?
|
---|
| 1350 | @type.has_pointer?
|
---|
| 1351 | end
|
---|
| 1352 |
|
---|
| 1353 | def has_sized_pointer?
|
---|
| 1354 | @type.has_sized_pointer?
|
---|
| 1355 | end
|
---|
| 1356 |
|
---|
| 1357 | def has_unsized_string?
|
---|
| 1358 | @type.has_unsized_string?
|
---|
| 1359 | end
|
---|
| 1360 |
|
---|
| 1361 | def show_tree( indent )
|
---|
| 1362 | indent.times { print " " }
|
---|
| 1363 | puts "ArrayType: #{locale_str}"
|
---|
| 1364 | super( indent + 1 )
|
---|
| 1365 | (indent+1).times { print " " }
|
---|
| 1366 | puts "type:"
|
---|
| 1367 | @type.show_tree( indent + 2 )
|
---|
| 1368 | (indent+1).times { print " " }
|
---|
| 1369 | puts "subscript:"
|
---|
| 1370 | if @subscript then
|
---|
| 1371 | @subscript.show_tree( indent + 2 )
|
---|
| 1372 | else
|
---|
| 1373 | (indent+2).times { print " " }
|
---|
| 1374 | puts "no subscript"
|
---|
| 1375 | end
|
---|
| 1376 | end
|
---|
| 1377 | end
|
---|
| 1378 |
|
---|
| 1379 | class PtrType < Type
|
---|
| 1380 | # @type:: refer to : PtrType, FuncType, ArrayType, IntType, FloatType, ...
|
---|
| 1381 | # @size:: Expr, or nil if not specified
|
---|
| 1382 | # @count:: Expr, or nil if not specified
|
---|
| 1383 | # @string:: Expr or -1(if size not specified) ï¼string å¼æ°ï¼, or nil if not specified
|
---|
| 1384 |
|
---|
| 1385 | include HasType
|
---|
| 1386 |
|
---|
| 1387 | def initialize( referto = nil )
|
---|
| 1388 | super()
|
---|
| 1389 | @type = referto
|
---|
| 1390 | @size = nil
|
---|
| 1391 | @count = nil
|
---|
| 1392 | @string = nil
|
---|
| 1393 | initHasType
|
---|
| 1394 | end
|
---|
| 1395 |
|
---|
| 1396 | def set_type( type )
|
---|
| 1397 | unless @type then
|
---|
| 1398 | @type = type
|
---|
| 1399 | else
|
---|
| 1400 | @type.set_type( type ) # æå
|
---|
| 1401 | ã® type ãè¨å®
|
---|
| 1402 | end
|
---|
| 1403 | end
|
---|
| 1404 |
|
---|
| 1405 | def get_type_str
|
---|
| 1406 | if @type.kind_of?( ArrayType ) || @type.kind_of?( FuncType ) then
|
---|
| 1407 | parenthes = "("
|
---|
| 1408 | else
|
---|
| 1409 | parenthes = ""
|
---|
| 1410 | end
|
---|
| 1411 | return "#{@type.get_type_str}#{parenthes}*"
|
---|
| 1412 | end
|
---|
| 1413 |
|
---|
| 1414 | def get_type_str_post
|
---|
| 1415 | if @type.kind_of?( ArrayType ) || @type.kind_of?( FuncType ) then
|
---|
| 1416 | parenthes = ")"
|
---|
| 1417 | else
|
---|
| 1418 | parenthes = ""
|
---|
| 1419 | end
|
---|
| 1420 | "#{parenthes}#{@type.get_type_str_post}"
|
---|
| 1421 | end
|
---|
| 1422 |
|
---|
| 1423 | def check # æå³ç誤ããããã°ãæååãè¿ã
|
---|
| 1424 | return nil if @type == nil
|
---|
| 1425 | @type.check
|
---|
| 1426 | end
|
---|
| 1427 |
|
---|
| 1428 | def check_struct_tag kind
|
---|
| 1429 | if kind != :MEMBER # æ§é ä½ã¡ã³ãã¼ã®å ´åããã¤ã³ã¿ã®å
|
---|
| 1430 | ã®æ§é ä½ã¿ã°ããã§ãã¯ããªã
|
---|
| 1431 | @type.check_struct_tag kind
|
---|
| 1432 | end
|
---|
| 1433 | end
|
---|
| 1434 |
|
---|
| 1435 | def check_init( locale, ident, initializer, kind, attribute = nil )
|
---|
| 1436 | if ( initializer.instance_of?( Expression ) ) then
|
---|
| 1437 | val = initializer.eval_const2( nil, attribute )
|
---|
| 1438 | if val.kind_of? PointerVal then
|
---|
| 1439 | type = val.get_type # PtrType
|
---|
| 1440 | t1 = self
|
---|
| 1441 | t2 = type
|
---|
| 1442 | while( t1.kind_of?( PtrType ) && t2.kind_of?( PtrType ) )
|
---|
| 1443 | t1 = t1.get_type
|
---|
| 1444 | t2 = t2.get_type
|
---|
| 1445 | if ( t1.class == t2.class ) && ( t1.get_bit_size == t2.get_bit_size ) then
|
---|
| 1446 | elsif ( t1.kind_of?( CDefinedType) || t2.kind_of?( CDefinedType ) )&& t1.get_type_str == t2.get_type_str && t1.get_type_str_post && t2.get_type_str_post then
|
---|
| 1447 | # int8_t ãªã©ããä¸æ¹ã¯ .h ã«å®ç¾©ããã¦ããã±ã¼ã¹
|
---|
| 1448 | else
|
---|
| 1449 | cdl_error2( locale, "T1032 $1: incompatible pointer type" , ident )
|
---|
| 1450 | break
|
---|
| 1451 | end
|
---|
| 1452 | end
|
---|
| 1453 | elsif val.kind_of? IntegerVal then
|
---|
| 1454 | if val.to_i != 0 then
|
---|
| 1455 | cdl_error2( locale, "T1033 $1: need cast to assign integer to pointer" , ident )
|
---|
| 1456 | end
|
---|
| 1457 | elsif val.kind_of? StringVal then
|
---|
| 1458 | # æååå®æ°
|
---|
| 1459 | # mikan L"wide string"
|
---|
| 1460 | if @type.get_bit_size != -1 && @type.get_bit_size != -11 then # -1: char_t
|
---|
| 1461 | cdl_error2( locale, "T1034 $1: unsuitable string constant" , ident )
|
---|
| 1462 | end
|
---|
| 1463 | elsif ( val.instance_of?( Array ) ) then
|
---|
| 1464 | i = 0
|
---|
| 1465 | val.each { |ini|
|
---|
| 1466 | @type.check_init( locale, "#{ident}[#{i}]", ini, kind, attribute = nil )
|
---|
| 1467 | i += 1
|
---|
| 1468 | }
|
---|
| 1469 | elsif val.instance_of?( C_EXP ) then
|
---|
| 1470 |
|
---|
| 1471 | else
|
---|
| 1472 | cdl_error2( locale, "T1035 $1: unsuitable initializer for pointer" , ident )
|
---|
| 1473 | end
|
---|
| 1474 | elsif ( initializer.instance_of?( Array ) ) then
|
---|
| 1475 | if @size == nil && @count == nil then
|
---|
| 1476 | cdl_error2( locale, "T9999 $1: non-size_is pointer cannot have array initializer", ident )
|
---|
| 1477 | end
|
---|
| 1478 |
|
---|
| 1479 | i = 0
|
---|
| 1480 | initializer.each { |ini|
|
---|
| 1481 | @type.check_init( locale, "#{ident}[#{i}]", ini, kind, attribute = nil )
|
---|
| 1482 | i += 1
|
---|
| 1483 | }
|
---|
| 1484 | elsif( initializer.instance_of?( C_EXP ) ) then
|
---|
| 1485 |
|
---|
| 1486 | else
|
---|
| 1487 | cdl_error2( locale, "T1036 $1: unsuitable initializer for pointer" , ident )
|
---|
| 1488 | end
|
---|
| 1489 | end
|
---|
| 1490 |
|
---|
| 1491 | def get_referto
|
---|
| 1492 | clone_type
|
---|
| 1493 | @type
|
---|
| 1494 | end
|
---|
| 1495 |
|
---|
| 1496 | def set_scs( size, count, string, max, b_nullable )
|
---|
| 1497 | @size = size
|
---|
| 1498 | @count = count
|
---|
| 1499 | @max = max
|
---|
| 1500 | @b_nullable = b_nullable
|
---|
| 1501 |
|
---|
| 1502 | # string ã¯æãå·¦å´ã® ptr ã«ä½ç¨ãã
|
---|
| 1503 | if @type.kind_of?( PtrType ) then
|
---|
| 1504 | # ptr_level ã 2 以ä¸ã§ãããã¨ã¯ ParamDecl#initializer ã§ãã§ãã¯ããã
|
---|
| 1505 | clone_type
|
---|
| 1506 | @type.set_scs( nil, nil, string, nil, false )
|
---|
| 1507 | elsif @type.kind_of?( VoidType ) && ( size || count || string ) then
|
---|
| 1508 | str = ""
|
---|
| 1509 | if size then
|
---|
| 1510 | str = "size_is"
|
---|
| 1511 | end
|
---|
| 1512 | if count then
|
---|
| 1513 | if str then
|
---|
| 1514 | str += ", "
|
---|
| 1515 | end
|
---|
| 1516 | str += "count_is"
|
---|
| 1517 | end
|
---|
| 1518 | if string then
|
---|
| 1519 | if str then
|
---|
| 1520 | str += ", "
|
---|
| 1521 | end
|
---|
| 1522 | str += "string"
|
---|
| 1523 | end
|
---|
| 1524 |
|
---|
| 1525 | cdl_error( "T1040 $1 specified for void pointer type", str )
|
---|
| 1526 | else
|
---|
| 1527 | @string = string
|
---|
| 1528 | end
|
---|
| 1529 |
|
---|
| 1530 | if (@size != nil) && (@b_nullable != false) then
|
---|
| 1531 | cdl_error( "T9999 size_is & nullable cannot be specified simultaneously. If size is zero, pointer must be null")
|
---|
| 1532 | end
|
---|
| 1533 | end
|
---|
| 1534 |
|
---|
| 1535 | def clear_max
|
---|
| 1536 | @max = nil
|
---|
| 1537 | end
|
---|
| 1538 |
|
---|
| 1539 | def get_size
|
---|
| 1540 | @size
|
---|
| 1541 | end
|
---|
| 1542 |
|
---|
| 1543 | def get_count
|
---|
| 1544 | @count
|
---|
| 1545 | end
|
---|
| 1546 |
|
---|
| 1547 | def get_string
|
---|
| 1548 | @string
|
---|
| 1549 | end
|
---|
| 1550 |
|
---|
| 1551 | #=== PtrType# size_is ã®æ大å¤
|
---|
| 1552 | def get_max
|
---|
| 1553 | @max
|
---|
| 1554 | end
|
---|
| 1555 |
|
---|
| 1556 | def is_nullable?
|
---|
| 1557 | return @b_nullable
|
---|
| 1558 | end
|
---|
| 1559 |
|
---|
| 1560 | def get_type
|
---|
| 1561 | clone_type
|
---|
| 1562 | @type
|
---|
| 1563 | end
|
---|
| 1564 |
|
---|
| 1565 | def has_pointer?
|
---|
| 1566 | true
|
---|
| 1567 | end
|
---|
| 1568 |
|
---|
| 1569 | def has_sized_pointer?
|
---|
| 1570 | @size != nil || @count != nil || @string.instance_of?( Expression ) || @type.has_sized_pointer?
|
---|
| 1571 | end
|
---|
| 1572 |
|
---|
| 1573 | def has_unsized_string?
|
---|
| 1574 | @string == -1 || @type.has_unsized_string?
|
---|
| 1575 | end
|
---|
| 1576 |
|
---|
| 1577 | def show_tree( indent )
|
---|
| 1578 | indent.times { print " " }
|
---|
| 1579 | puts "PtrType: qualifier=#{@qualifier}, nullable=#{@b_nullable} #{locale_str}"
|
---|
| 1580 | super( indent + 1 )
|
---|
| 1581 | (indent+1).times { print " " }
|
---|
| 1582 | if @size then
|
---|
| 1583 | print "size=#{@size.to_s}, "
|
---|
| 1584 | else
|
---|
| 1585 | print "size=nil, "
|
---|
| 1586 | end
|
---|
| 1587 | if @max then
|
---|
| 1588 | print "max=#{@size.to_s}, "
|
---|
| 1589 | else
|
---|
| 1590 | print "max=nil, "
|
---|
| 1591 | end
|
---|
| 1592 | if @count then
|
---|
| 1593 | print "count=#{@count.to_s}, "
|
---|
| 1594 | else
|
---|
| 1595 | print "count=nil, "
|
---|
| 1596 | end
|
---|
| 1597 | if @string then
|
---|
| 1598 | if @string.instance_of?( Expression ) then
|
---|
| 1599 | print "string=#{@string.to_s}\n"
|
---|
| 1600 | else
|
---|
| 1601 | print "string=yes\n"
|
---|
| 1602 | end
|
---|
| 1603 | else
|
---|
| 1604 | print "string=nil\n"
|
---|
| 1605 | end
|
---|
| 1606 |
|
---|
| 1607 | (indent+1).times { print " " }
|
---|
| 1608 | puts "type:"
|
---|
| 1609 | @type.show_tree( indent + 2 )
|
---|
| 1610 | end
|
---|
| 1611 |
|
---|
| 1612 | end
|
---|
| 1613 |
|
---|
| 1614 | #== DescriptorType ã¯ã©ã¹
|
---|
| 1615 | # åççµåã§æ¸¡ããã¹ã¯ãªãã¿å
|
---|
| 1616 | class DescriptorType < Type
|
---|
| 1617 | # @sinagure_nsp::NamespacePath
|
---|
| 1618 |
|
---|
| 1619 | @@descriptors = {}
|
---|
| 1620 |
|
---|
| 1621 | def initialize( signature_nsp )
|
---|
| 1622 | @signature_nsp = signature_nsp
|
---|
| 1623 | # check_signature ##
|
---|
| 1624 | @@descriptors[ self ] = false
|
---|
| 1625 | end
|
---|
| 1626 |
|
---|
| 1627 | def get_type_str
|
---|
| 1628 | "Descriptor( #{@signature_nsp.get_global_name} )"
|
---|
| 1629 | end
|
---|
| 1630 |
|
---|
| 1631 | def get_type_str_post
|
---|
| 1632 | ""
|
---|
| 1633 | end
|
---|
| 1634 |
|
---|
| 1635 | def set_qualifier( qualifier )
|
---|
| 1636 | cdl_error( "T9999 '$1' cannot be specified for Descriptor", qualfier.to_s )
|
---|
| 1637 | end
|
---|
| 1638 |
|
---|
| 1639 | def check
|
---|
| 1640 | end
|
---|
| 1641 |
|
---|
| 1642 | def check_init( locale, ident, initializer, kind, attribute = nil )
|
---|
| 1643 | case kind
|
---|
| 1644 | when :PARAMETER
|
---|
| 1645 | # å¼æ°ã¯åæåã§ããªã
|
---|
| 1646 | else
|
---|
| 1647 | cdl_error2( locale, "T9999 Descriptor cannot be used for $1", kind)
|
---|
| 1648 | end
|
---|
| 1649 | end
|
---|
| 1650 |
|
---|
| 1651 | def self.check_signature
|
---|
| 1652 | @@descriptors.each{ |desc, val|
|
---|
| 1653 | if val != true then
|
---|
| 1654 | desc.check_signature
|
---|
| 1655 | @@descriptors[ desc ] = true
|
---|
| 1656 | end
|
---|
| 1657 | }
|
---|
| 1658 | end
|
---|
| 1659 |
|
---|
| 1660 | def check_signature
|
---|
| 1661 | # p "Desc #{@signature_nsp.to_s}"
|
---|
| 1662 | obj = Namespace.find @signature_nsp
|
---|
| 1663 | if ! obj.kind_of? Signature then
|
---|
| 1664 | cdl_error( "T9999 '$1': not signature or not found", @signature_nsp.to_s )
|
---|
| 1665 | else
|
---|
| 1666 | if obj.has_descriptor? then
|
---|
| 1667 | # cdl_error( "T9999 '$1': has Descriptor in function parameter", @signature_nsp.to_s )
|
---|
| 1668 | end
|
---|
| 1669 | # @signature_nsp = obj.get_namespace_path
|
---|
| 1670 | end
|
---|
| 1671 | end
|
---|
| 1672 |
|
---|
| 1673 | #== DescriptorType#
|
---|
| 1674 | def get_signature
|
---|
| 1675 | Namespace.find @signature_nsp
|
---|
| 1676 | end
|
---|
| 1677 | end
|
---|
| 1678 |
|
---|
| 1679 | # 以ä¸åä½ãã¹ãã³ã¼ã
|
---|
| 1680 | if $unit_test then
|
---|
| 1681 | puts( "===== Unit Test: IntType ===== (types.rb)")
|
---|
| 1682 | sizes = [ 8, 16, 32, 64 ]
|
---|
| 1683 | sizes.each{ |n|
|
---|
| 1684 | int = IntType.new n
|
---|
| 1685 | printf( "%8s max: %d min:%d\n", "int#{n}_t", int.get_max, int.get_min )
|
---|
| 1686 | }
|
---|
| 1687 | puts ""
|
---|
| 1688 | end
|
---|