preprocess.odin 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510
  1. package c_frontend_preprocess
  2. import "../tokenizer"
  3. import "core:strings"
  4. import "core:strconv"
  5. import "core:path/filepath"
  6. import "core:unicode/utf8"
  7. import "core:unicode/utf16"
  8. import "core:os"
  9. import "core:io"
  10. @(private)
  11. Tokenizer :: tokenizer.Tokenizer
  12. @(private)
  13. Token :: tokenizer.Token
  14. Error_Handler :: tokenizer.Error_Handler
  15. Macro_Param :: struct {
  16. next: ^Macro_Param,
  17. name: string,
  18. }
  19. Macro_Arg :: struct {
  20. next: ^Macro_Arg,
  21. name: string,
  22. tok: ^Token,
  23. is_va_args: bool,
  24. }
  25. Macro_Kind :: enum u8 {
  26. Function_Like,
  27. Value_Like,
  28. }
  29. Macro_Handler :: #type proc(^Preprocessor, ^Token) -> ^Token
  30. Macro :: struct {
  31. name: string,
  32. kind: Macro_Kind,
  33. params: ^Macro_Param,
  34. va_args_name: string,
  35. body: ^Token,
  36. handler: Macro_Handler,
  37. }
  38. Cond_Incl_State :: enum u8 {
  39. In_Then,
  40. In_Elif,
  41. In_Else,
  42. }
  43. Cond_Incl :: struct {
  44. next: ^Cond_Incl,
  45. tok: ^Token,
  46. state: Cond_Incl_State,
  47. included: bool,
  48. }
  49. Pragma_Handler :: #type proc(^Preprocessor, ^Token)
  50. Preprocessor :: struct {
  51. // Lookup tables
  52. macros: map[string]^Macro,
  53. pragma_once: map[string]bool,
  54. include_guards: map[string]string,
  55. filepath_cache: map[string]string,
  56. // Include path data
  57. include_paths: []string,
  58. // Counter for __COUNTER__ macro
  59. counter: i64,
  60. // Include information
  61. cond_incl: ^Cond_Incl,
  62. include_level: int,
  63. include_next_index: int,
  64. wide_char_size: int,
  65. // Mutable data
  66. err: Error_Handler,
  67. warn: Error_Handler,
  68. pragma_handler: Pragma_Handler,
  69. error_count: int,
  70. warning_count: int,
  71. }
  72. MAX_INCLUDE_LEVEL :: 1024
  73. error :: proc(cpp: ^Preprocessor, tok: ^Token, msg: string, args: ..any) {
  74. if cpp.err != nil {
  75. cpp.err(tok.pos, msg, ..args)
  76. }
  77. cpp.error_count += 1
  78. }
  79. warn :: proc(cpp: ^Preprocessor, tok: ^Token, msg: string, args: ..any) {
  80. if cpp.warn != nil {
  81. cpp.warn(tok.pos, msg, ..args)
  82. }
  83. cpp.warning_count += 1
  84. }
  85. is_hash :: proc(tok: ^Token) -> bool {
  86. return tok.at_bol && tok.lit == "#"
  87. }
  88. skip_line :: proc(cpp: ^Preprocessor, tok: ^Token) -> ^Token {
  89. tok := tok
  90. if tok.at_bol {
  91. return tok
  92. }
  93. warn(cpp, tok, "extra token")
  94. for tok.at_bol {
  95. tok = tok.next
  96. }
  97. return tok
  98. }
  99. append_token :: proc(a, b: ^Token) -> ^Token {
  100. if a.kind == .EOF {
  101. return b
  102. }
  103. head: Token
  104. curr := &head
  105. for tok := a; tok.kind != .EOF; tok = tok.next {
  106. curr.next = tokenizer.copy_token(tok)
  107. curr = curr.next
  108. }
  109. curr.next = b
  110. return head.next
  111. }
  112. is_hex_digit :: proc(x: byte) -> bool {
  113. switch x {
  114. case '0'..='9', 'a'..='f', 'A'..='F':
  115. return true
  116. }
  117. return false
  118. }
  119. from_hex :: proc(x: byte) -> i32 {
  120. switch x {
  121. case '0'..='9':
  122. return i32(x) - '0'
  123. case 'a'..='f':
  124. return i32(x) - 'a' + 10
  125. case 'A'..='F':
  126. return i32(x) - 'A' + 10
  127. }
  128. return 16
  129. }
  130. convert_pp_number :: proc(tok: ^Token) {
  131. convert_pp_int :: proc(tok: ^Token) -> bool {
  132. p := tok.lit
  133. base := 10
  134. if len(p) > 2 {
  135. if strings.equal_fold(p[:2], "0x") && is_hex_digit(p[2]) {
  136. p = p[2:]
  137. base = 16
  138. } else if strings.equal_fold(p[:2], "0b") && p[2] == '0' || p[2] == '1' {
  139. p = p[2:]
  140. base = 2
  141. }
  142. }
  143. if base == 10 && p[0] == '0' {
  144. base = 8
  145. }
  146. tok.val, _ = strconv.parse_i64_of_base(p, base)
  147. l, u: int
  148. suf: [3]byte
  149. suf_n := 0
  150. i := len(p)-1
  151. for /**/; i >= 0 && suf_n < len(suf); i -= 1 {
  152. switch p[i] {
  153. case 'l', 'L':
  154. suf[suf_n] = 'l'
  155. l += 1
  156. suf_n += 1
  157. case 'u', 'U':
  158. suf[suf_n] = 'u'
  159. u += 1
  160. suf_n += 1
  161. }
  162. }
  163. if i < len(p) {
  164. if !is_hex_digit(p[i]) && p[i] != '.' {
  165. return false
  166. }
  167. }
  168. if u > 1 {
  169. return false
  170. }
  171. if l > 2 {
  172. return false
  173. }
  174. if u == 1 {
  175. switch l {
  176. case 0: tok.type_hint = .Unsigned_Int
  177. case 1: tok.type_hint = .Unsigned_Long
  178. case 2: tok.type_hint = .Unsigned_Long_Long
  179. }
  180. } else {
  181. switch l {
  182. case 0: tok.type_hint = .Int
  183. case 1: tok.type_hint = .Long
  184. case 2: tok.type_hint = .Long_Long
  185. }
  186. }
  187. return true
  188. }
  189. if convert_pp_int(tok) {
  190. return
  191. }
  192. fval, _ := strconv.parse_f64(tok.lit)
  193. tok.val = fval
  194. end := tok.lit[len(tok.lit)-1]
  195. switch end {
  196. case 'f', 'F':
  197. tok.type_hint = .Float
  198. case 'l', 'L':
  199. tok.type_hint = .Long_Double
  200. case:
  201. tok.type_hint = .Double
  202. }
  203. }
  204. convert_pp_char :: proc(tok: ^Token) {
  205. assert(len(tok.lit) >= 2)
  206. r, _, _, _ := unquote_char(tok.lit, tok.lit[0])
  207. tok.val = i64(r)
  208. tok.type_hint = .Int
  209. switch tok.prefix {
  210. case "u": tok.type_hint = .UTF_16
  211. case "U": tok.type_hint = .UTF_32
  212. case "L": tok.type_hint = .UTF_Wide
  213. }
  214. }
  215. wide_char_size :: proc(cpp: ^Preprocessor) -> int {
  216. char_size := 4
  217. if cpp.wide_char_size > 0 {
  218. char_size = clamp(cpp.wide_char_size, 1, 4)
  219. assert(char_size & (char_size-1) == 0)
  220. }
  221. return char_size
  222. }
  223. convert_pp_string :: proc(cpp: ^Preprocessor, tok: ^Token) {
  224. assert(len(tok.lit) >= 2)
  225. str, _, _ := unquote_string(tok.lit)
  226. tok.val = str
  227. char_size := 1
  228. switch tok.prefix {
  229. case "u8":
  230. tok.type_hint = .UTF_8
  231. char_size = 1
  232. case "u":
  233. tok.type_hint = .UTF_16
  234. char_size = 2
  235. case "U":
  236. tok.type_hint = .UTF_32
  237. char_size = 4
  238. case "L":
  239. tok.type_hint = .UTF_Wide
  240. char_size = wide_char_size(cpp)
  241. }
  242. switch char_size {
  243. case 2:
  244. n: int
  245. buf := make([]u16, len(str))
  246. for c in str {
  247. ch := c
  248. if ch < 0x10000 {
  249. buf[n] = u16(ch)
  250. n += 1
  251. } else {
  252. ch -= 0x10000
  253. buf[n+0] = 0xd800 + u16((ch >> 10) & 0x3ff)
  254. buf[n+1] = 0xdc00 + u16(ch & 0x3ff)
  255. n += 2
  256. }
  257. }
  258. tok.val = buf[:n]
  259. case 4:
  260. n: int
  261. buf := make([]u32, len(str))
  262. for ch in str {
  263. buf[n] = u32(ch)
  264. n += 1
  265. }
  266. tok.val = buf[:n]
  267. }
  268. }
  269. convert_pp_token :: proc(cpp: ^Preprocessor, t: ^Token, is_keyword: tokenizer.Is_Keyword_Proc) {
  270. switch {
  271. case t.kind == .Char:
  272. convert_pp_char(t)
  273. case t.kind == .String:
  274. convert_pp_string(cpp, t)
  275. case is_keyword != nil && is_keyword(t):
  276. t.kind = .Keyword
  277. case t.kind == .PP_Number:
  278. convert_pp_number(t)
  279. }
  280. }
  281. convert_pp_tokens :: proc(cpp: ^Preprocessor, tok: ^Token, is_keyword: tokenizer.Is_Keyword_Proc) {
  282. for t := tok; t != nil && t.kind != .EOF; t = t.next {
  283. convert_pp_token(cpp, tok, is_keyword)
  284. }
  285. }
  286. join_adjacent_string_literals :: proc(cpp: ^Preprocessor, initial_tok: ^Token) {
  287. for tok1 := initial_tok; tok1.kind != .EOF; /**/ {
  288. if tok1.kind != .String || tok1.next.kind != .String {
  289. tok1 = tok1.next
  290. continue
  291. }
  292. type_hint := tokenizer.Token_Type_Hint.None
  293. char_size := 1
  294. start := tok1
  295. for t := tok1; t != nil && t.kind == .String; t = t.next {
  296. if t.val == nil {
  297. convert_pp_string(cpp, t)
  298. }
  299. tok1 = t.next
  300. if type_hint != t.type_hint {
  301. if t.type_hint != .None && type_hint != .None {
  302. error(cpp, t, "unsupported non-standard concatenation of string literals of different types")
  303. }
  304. prev_char_size := char_size
  305. #partial switch type_hint {
  306. case .UTF_8: char_size = max(char_size, 1)
  307. case .UTF_16: char_size = max(char_size, 2)
  308. case .UTF_32: char_size = max(char_size, 4)
  309. case .UTF_Wide: char_size = max(char_size, wide_char_size(cpp))
  310. }
  311. if type_hint == .None || prev_char_size < char_size {
  312. type_hint = t.type_hint
  313. }
  314. }
  315. }
  316. // NOTE(bill): Verbose logic in order to correctly concantenate strings, even if they different in type
  317. max_len := 0
  318. switch char_size {
  319. case 1:
  320. for t := start; t != nil && t.kind == .String; t = t.next {
  321. #partial switch v in t.val {
  322. case string: max_len += len(v)
  323. case []u16: max_len += 2*len(v)
  324. case []u32: max_len += 4*len(v)
  325. }
  326. }
  327. n := 0
  328. buf := make([]byte, max_len)
  329. for t := start; t != nil && t.kind == .String; t = t.next {
  330. #partial switch v in t.val {
  331. case string:
  332. n += copy(buf[n:], v)
  333. case []u16:
  334. for i := 0; i < len(v); /**/ {
  335. c1 := v[i]
  336. r: rune
  337. if !utf16.is_surrogate(rune(c1)) {
  338. r = rune(c1)
  339. i += 1
  340. } else if i+1 == len(v) {
  341. r = utf16.REPLACEMENT_CHAR
  342. i += 1
  343. } else {
  344. c2 := v[i+1]
  345. i += 2
  346. r = utf16.decode_surrogate_pair(rune(c1), rune(c2))
  347. }
  348. b, w := utf8.encode_rune(r)
  349. n += copy(buf[n:], b[:w])
  350. }
  351. case []u32:
  352. for r in v {
  353. b, w := utf8.encode_rune(rune(r))
  354. n += copy(buf[n:], b[:w])
  355. }
  356. }
  357. }
  358. new_tok := tokenizer.copy_token(start)
  359. new_tok.lit = ""
  360. new_tok.val = string(buf[:n])
  361. new_tok.next = tok1
  362. new_tok.type_hint = type_hint
  363. start^ = new_tok^
  364. case 2:
  365. for t := start; t != nil && t.kind == .String; t = t.next {
  366. #partial switch v in t.val {
  367. case string: max_len += len(v)
  368. case []u16: max_len += len(v)
  369. case []u32: max_len += 2*len(v)
  370. }
  371. }
  372. n := 0
  373. buf := make([]u16, max_len)
  374. for t := start; t != nil && t.kind == .String; t = t.next {
  375. #partial switch v in t.val {
  376. case string:
  377. for r in v {
  378. if r >= 0x10000 {
  379. c1, c2 := utf16.encode_surrogate_pair(r)
  380. buf[n+0] = u16(c1)
  381. buf[n+1] = u16(c2)
  382. n += 2
  383. } else {
  384. buf[n] = u16(r)
  385. n += 1
  386. }
  387. }
  388. case []u16:
  389. n += copy(buf[n:], v)
  390. case []u32:
  391. for r in v {
  392. if r >= 0x10000 {
  393. c1, c2 := utf16.encode_surrogate_pair(rune(r))
  394. buf[n+0] = u16(c1)
  395. buf[n+1] = u16(c2)
  396. n += 2
  397. } else {
  398. buf[n] = u16(r)
  399. n += 1
  400. }
  401. }
  402. }
  403. }
  404. new_tok := tokenizer.copy_token(start)
  405. new_tok.lit = ""
  406. new_tok.val = buf[:n]
  407. new_tok.next = tok1
  408. new_tok.type_hint = type_hint
  409. start^ = new_tok^
  410. case 4:
  411. for t := start; t != nil && t.kind == .String; t = t.next {
  412. #partial switch v in t.val {
  413. case string: max_len += len(v)
  414. case []u16: max_len += len(v)
  415. case []u32: max_len += len(v)
  416. }
  417. }
  418. n := 0
  419. buf := make([]u32, max_len)
  420. for t := start; t != nil && t.kind == .String; t = t.next {
  421. #partial switch v in t.val {
  422. case string:
  423. for r in v {
  424. buf[n] = u32(r)
  425. n += 1
  426. }
  427. case []u16:
  428. for i := 0; i < len(v); /**/ {
  429. c1 := v[i]
  430. if !utf16.is_surrogate(rune(c1)) {
  431. buf[n] = u32(c1)
  432. n += 1
  433. i += 1
  434. } else if i+1 == len(v) {
  435. buf[n] = utf16.REPLACEMENT_CHAR
  436. n += 1
  437. i += 1
  438. } else {
  439. c2 := v[i+1]
  440. i += 2
  441. r := utf16.decode_surrogate_pair(rune(c1), rune(c2))
  442. buf[n] = u32(r)
  443. n += 1
  444. }
  445. }
  446. case []u32:
  447. n += copy(buf[n:], v)
  448. }
  449. }
  450. new_tok := tokenizer.copy_token(start)
  451. new_tok.lit = ""
  452. new_tok.val = buf[:n]
  453. new_tok.next = tok1
  454. new_tok.type_hint = type_hint
  455. start^ = new_tok^
  456. }
  457. }
  458. }
  459. quote_string :: proc(s: string) -> []byte {
  460. b := strings.builder_make(0, len(s)+2)
  461. io.write_quoted_string(strings.to_writer(&b), s, '"')
  462. return b.buf[:]
  463. }
  464. _init_tokenizer_from_preprocessor :: proc(t: ^Tokenizer, cpp: ^Preprocessor) -> ^Tokenizer {
  465. t.warn = cpp.warn
  466. t.err = cpp.err
  467. return t
  468. }
  469. new_string_token :: proc(cpp: ^Preprocessor, str: string, tok: ^Token) -> ^Token {
  470. assert(tok != nil)
  471. assert(str != "")
  472. t := _init_tokenizer_from_preprocessor(&Tokenizer{}, cpp)
  473. src := quote_string(str)
  474. return tokenizer.inline_tokenize(t, tok, src)
  475. }
  476. stringize :: proc(cpp: ^Preprocessor, hash, arg: ^Token) -> ^Token {
  477. s := join_tokens(arg, nil)
  478. return new_string_token(cpp, s, hash)
  479. }
  480. new_number_token :: proc(cpp: ^Preprocessor, i: i64, tok: ^Token) -> ^Token {
  481. t := _init_tokenizer_from_preprocessor(&Tokenizer{}, cpp)
  482. buf: [32]byte
  483. n := len(strconv.append_int(buf[:], i, 10))
  484. src := make([]byte, n)
  485. copy(src, buf[:n])
  486. return tokenizer.inline_tokenize(t, tok, src)
  487. }
  488. find_macro :: proc(cpp: ^Preprocessor, tok: ^Token) -> ^Macro {
  489. if tok.kind != .Ident {
  490. return nil
  491. }
  492. return cpp.macros[tok.lit]
  493. }
  494. add_macro :: proc(cpp: ^Preprocessor, name: string, kind: Macro_Kind, body: ^Token) -> ^Macro {
  495. m := new(Macro)
  496. m.name = name
  497. m.kind = kind
  498. m.body = body
  499. cpp.macros[name] = m
  500. return m
  501. }
  502. undef_macro :: proc(cpp: ^Preprocessor, name: string) {
  503. delete_key(&cpp.macros, name)
  504. }
  505. add_builtin :: proc(cpp: ^Preprocessor, name: string, handler: Macro_Handler) -> ^Macro {
  506. m := add_macro(cpp, name, .Value_Like, nil)
  507. m.handler = handler
  508. return m
  509. }
  510. skip :: proc(cpp: ^Preprocessor, tok: ^Token, op: string) -> ^Token {
  511. if tok.lit != op {
  512. error(cpp, tok, "expected '%q'", op)
  513. }
  514. return tok.next
  515. }
  516. consume :: proc(rest: ^^Token, tok: ^Token, lit: string) -> bool {
  517. if tok.lit == lit {
  518. rest^ = tok.next
  519. return true
  520. }
  521. rest^ = tok
  522. return false
  523. }
  524. read_macro_params :: proc(cpp: ^Preprocessor, rest: ^^Token, tok: ^Token) -> (param: ^Macro_Param, va_args_name: string) {
  525. head: Macro_Param
  526. curr := &head
  527. tok := tok
  528. for tok.lit != ")" && tok.kind != .EOF {
  529. if curr != &head {
  530. tok = skip(cpp, tok, ",")
  531. }
  532. if tok.lit == "..." {
  533. va_args_name = "__VA_ARGS__"
  534. rest^ = skip(cpp, tok.next, ")")
  535. param = head.next
  536. return
  537. }
  538. if tok.kind != .Ident {
  539. error(cpp, tok, "expected an identifier")
  540. }
  541. if tok.next.lit == "..." {
  542. va_args_name = tok.lit
  543. rest^ = skip(cpp, tok.next.next, ")")
  544. param = head.next
  545. return
  546. }
  547. m := new(Macro_Param)
  548. m.name = tok.lit
  549. curr.next = m
  550. curr = curr.next
  551. tok = tok.next
  552. }
  553. rest^ = tok.next
  554. param = head.next
  555. return
  556. }
  557. copy_line :: proc(rest: ^^Token, tok: ^Token) -> ^Token {
  558. head: Token
  559. curr := &head
  560. tok := tok
  561. for ; !tok.at_bol; tok = tok.next {
  562. curr.next = tokenizer.copy_token(tok)
  563. curr = curr.next
  564. }
  565. curr.next = tokenizer.new_eof(tok)
  566. rest^ = tok
  567. return head.next
  568. }
  569. read_macro_definition :: proc(cpp: ^Preprocessor, rest: ^^Token, tok: ^Token) {
  570. tok := tok
  571. if tok.kind != .Ident {
  572. error(cpp, tok, "macro name must be an identifier")
  573. }
  574. name := tok.lit
  575. tok = tok.next
  576. if !tok.has_space && tok.lit == "(" {
  577. params, va_args_name := read_macro_params(cpp, &tok, tok.next)
  578. m := add_macro(cpp, name, .Function_Like, copy_line(rest, tok))
  579. m.params = params
  580. m.va_args_name = va_args_name
  581. } else {
  582. add_macro(cpp, name, .Value_Like, copy_line(rest, tok))
  583. }
  584. }
  585. join_tokens :: proc(tok, end: ^Token) -> string {
  586. n := 1
  587. for t := tok; t != end && t.kind != .EOF; t = t.next {
  588. if t != tok && t.has_space {
  589. n += 1
  590. }
  591. n += len(t.lit)
  592. }
  593. buf := make([]byte, n)
  594. pos := 0
  595. for t := tok; t != end && t.kind != .EOF; t = t.next {
  596. if t != tok && t.has_space {
  597. buf[pos] = ' '
  598. pos += 1
  599. }
  600. copy(buf[pos:], t.lit)
  601. pos += len(t.lit)
  602. }
  603. return string(buf[:pos])
  604. }
  605. read_include_filename :: proc(cpp: ^Preprocessor, rest: ^^Token, tok: ^Token) -> (filename: string, is_quote: bool) {
  606. tok := tok
  607. if tok.kind == .String {
  608. rest^ = skip_line(cpp, tok.next)
  609. filename = tok.lit[1:len(tok.lit)-1]
  610. is_quote = true
  611. return
  612. }
  613. if tok.lit == "<" {
  614. start := tok
  615. for ; tok.kind != .EOF; tok = tok.next {
  616. if tok.at_bol || tok.kind == .EOF {
  617. error(cpp, tok, "expected '>'")
  618. }
  619. is_quote = false
  620. if tok.lit == ">" {
  621. break
  622. }
  623. }
  624. rest^ = skip_line(cpp, tok.next)
  625. filename = join_tokens(start.next, tok)
  626. return
  627. }
  628. if tok.kind == .Ident {
  629. tok2 := preprocess_internal(cpp, copy_line(rest, tok))
  630. return read_include_filename(cpp, &tok2, tok2)
  631. }
  632. error(cpp, tok, "expected a filename")
  633. return
  634. }
  635. skip_cond_incl :: proc(tok: ^Token) -> ^Token {
  636. next_skip :: proc(tok: ^Token) -> ^Token {
  637. tok := tok
  638. for tok.kind != .EOF {
  639. if is_hash(tok) {
  640. switch tok.next.lit {
  641. case "if", "ifdef", "ifndef":
  642. tok = next_skip(tok.next.next)
  643. continue
  644. case "endif":
  645. return tok.next.next
  646. }
  647. }
  648. tok = tok.next
  649. }
  650. return tok
  651. }
  652. tok := tok
  653. loop: for tok.kind != .EOF {
  654. if is_hash(tok) {
  655. switch tok.next.lit {
  656. case "if", "ifdef", "ifndef":
  657. tok = next_skip(tok.next.next)
  658. continue loop
  659. case "elif", "else", "endif":
  660. break loop
  661. }
  662. }
  663. tok = tok.next
  664. }
  665. return tok
  666. }
  667. check_for_include_guard :: proc(tok: ^Token) -> (guard: string, ok: bool) {
  668. if !is_hash(tok) || tok.next.lit != "ifndef" {
  669. return
  670. }
  671. tok := tok
  672. tok = tok.next.next
  673. if tok.kind != .Ident {
  674. return
  675. }
  676. m := tok.lit
  677. tok = tok.next
  678. if !is_hash(tok) || tok.next.lit != "define" || tok.next.lit != "macro" {
  679. return
  680. }
  681. for tok.kind != .EOF {
  682. if !is_hash(tok) {
  683. tok = tok.next
  684. continue
  685. }
  686. if tok.next.lit == "endif" && tok.next.next.kind == .EOF {
  687. return m, true
  688. }
  689. switch tok.lit {
  690. case "if", "ifdef", "ifndef":
  691. tok = skip_cond_incl(tok.next)
  692. case:
  693. tok = tok.next
  694. }
  695. }
  696. return
  697. }
  698. include_file :: proc(cpp: ^Preprocessor, tok: ^Token, path: string, filename_tok: ^Token) -> ^Token {
  699. if cpp.pragma_once[path] {
  700. return tok
  701. }
  702. guard_name, guard_name_found := cpp.include_guards[path]
  703. if guard_name_found && cpp.macros[guard_name] != nil {
  704. return tok
  705. }
  706. if !os.exists(path) {
  707. error(cpp, filename_tok, "%s: cannot open file", path)
  708. return tok
  709. }
  710. cpp.include_level += 1
  711. if cpp.include_level > MAX_INCLUDE_LEVEL {
  712. error(cpp, tok, "exceeded maximum nest amount: %d", MAX_INCLUDE_LEVEL)
  713. return tok
  714. }
  715. t := _init_tokenizer_from_preprocessor(&Tokenizer{}, cpp)
  716. tok2 := tokenizer.tokenize_file(t, path, /*file.id*/1)
  717. if tok2 == nil {
  718. error(cpp, filename_tok, "%s: cannot open file", path)
  719. }
  720. cpp.include_level -= 1
  721. guard_name, guard_name_found = check_for_include_guard(tok2)
  722. if guard_name_found {
  723. cpp.include_guards[path] = guard_name
  724. }
  725. return append_token(tok2, tok)
  726. }
  727. find_arg :: proc(args: ^Macro_Arg, tok: ^Token) -> ^Macro_Arg {
  728. for ap := args; ap != nil; ap = ap.next {
  729. if tok.lit == ap.name {
  730. return ap
  731. }
  732. }
  733. return nil
  734. }
  735. paste :: proc(cpp: ^Preprocessor, lhs, rhs: ^Token) -> ^Token {
  736. buf := strings.concatenate({lhs.lit, rhs.lit})
  737. t := _init_tokenizer_from_preprocessor(&Tokenizer{}, cpp)
  738. tok := tokenizer.inline_tokenize(t, lhs, transmute([]byte)buf)
  739. if tok.next.kind != .EOF {
  740. error(cpp, lhs, "pasting forms '%s', an invalid token", buf)
  741. }
  742. return tok
  743. }
  744. has_varargs :: proc(args: ^Macro_Arg) -> bool {
  745. for ap := args; ap != nil; ap = ap.next {
  746. if ap.name == "__VA_ARGS__" {
  747. return ap.tok.kind != .EOF
  748. }
  749. }
  750. return false
  751. }
  752. substitute_token :: proc(cpp: ^Preprocessor, tok: ^Token, args: ^Macro_Arg) -> ^Token {
  753. head: Token
  754. curr := &head
  755. tok := tok
  756. for tok.kind != .EOF {
  757. if tok.lit == "#" {
  758. arg := find_arg(args, tok.next)
  759. if arg == nil {
  760. error(cpp, tok.next, "'#' is not followed by a macro parameter")
  761. }
  762. arg_tok := arg.tok if arg != nil else tok.next
  763. curr.next = stringize(cpp, tok, arg_tok)
  764. curr = curr.next
  765. tok = tok.next.next
  766. continue
  767. }
  768. if tok.lit == "," && tok.next.lit == "##" {
  769. if arg := find_arg(args, tok.next.next); arg != nil && arg.is_va_args {
  770. if arg.tok.kind == .EOF {
  771. tok = tok.next.next.next
  772. } else {
  773. curr.next = tokenizer.copy_token(tok)
  774. curr = curr.next
  775. tok = tok.next.next
  776. }
  777. continue
  778. }
  779. }
  780. if tok.lit == "##" {
  781. if curr == &head {
  782. error(cpp, tok, "'##' cannot appear at start of macro expansion")
  783. }
  784. if tok.next.kind == .EOF {
  785. error(cpp, tok, "'##' cannot appear at end of macro expansion")
  786. }
  787. if arg := find_arg(args, tok.next); arg != nil {
  788. if arg.tok.kind != .EOF {
  789. curr^ = paste(cpp, curr, arg.tok)^
  790. for t := arg.tok.next; t.kind != .EOF; t = t.next {
  791. curr.next = tokenizer.copy_token(t)
  792. curr = curr.next
  793. }
  794. }
  795. tok = tok.next.next
  796. continue
  797. }
  798. curr^ = paste(cpp, curr, tok.next)^
  799. tok = tok.next.next
  800. continue
  801. }
  802. arg := find_arg(args, tok)
  803. if arg != nil && tok.next.lit == "##" {
  804. rhs := tok.next.next
  805. if arg.tok.kind == .EOF {
  806. args2 := find_arg(args, rhs)
  807. if args2 != nil {
  808. for t := args.tok; t.kind != .EOF; t = t.next {
  809. curr.next = tokenizer.copy_token(t)
  810. curr = curr.next
  811. }
  812. } else {
  813. curr.next = tokenizer.copy_token(rhs)
  814. curr = curr.next
  815. }
  816. tok = rhs.next
  817. continue
  818. }
  819. for t := arg.tok; t.kind != .EOF; t = t.next {
  820. curr.next = tokenizer.copy_token(t)
  821. curr = curr.next
  822. }
  823. tok = tok.next
  824. continue
  825. }
  826. if tok.lit == "__VA_OPT__" && tok.next.lit == "(" {
  827. opt_arg := read_macro_arg_one(cpp, &tok, tok.next.next, true)
  828. if has_varargs(args) {
  829. for t := opt_arg.tok; t.kind != .EOF; t = t.next {
  830. curr.next = t
  831. curr = curr.next
  832. }
  833. }
  834. tok = skip(cpp, tok, ")")
  835. continue
  836. }
  837. if arg != nil {
  838. t := preprocess_internal(cpp, arg.tok)
  839. t.at_bol = tok.at_bol
  840. t.has_space = tok.has_space
  841. for ; t.kind != .EOF; t = t.next {
  842. curr.next = tokenizer.copy_token(t)
  843. curr = curr.next
  844. }
  845. tok = tok.next
  846. continue
  847. }
  848. curr.next = tokenizer.copy_token(tok)
  849. curr = curr.next
  850. tok = tok.next
  851. continue
  852. }
  853. curr.next = tok
  854. return head.next
  855. }
  856. read_macro_arg_one :: proc(cpp: ^Preprocessor, rest: ^^Token, tok: ^Token, read_rest: bool) -> ^Macro_Arg {
  857. tok := tok
  858. head: Token
  859. curr := &head
  860. level := 0
  861. for {
  862. if level == 0 && tok.lit == ")" {
  863. break
  864. }
  865. if level == 0 && !read_rest && tok.lit == "," {
  866. break
  867. }
  868. if tok.kind == .EOF {
  869. error(cpp, tok, "premature end of input")
  870. }
  871. switch tok.lit {
  872. case "(": level += 1
  873. case ")": level -= 1
  874. }
  875. curr.next = tokenizer.copy_token(tok)
  876. curr = curr.next
  877. tok = tok.next
  878. }
  879. curr.next = tokenizer.new_eof(tok)
  880. arg := new(Macro_Arg)
  881. arg.tok = head.next
  882. rest^ = tok
  883. return arg
  884. }
  885. read_macro_args :: proc(cpp: ^Preprocessor, rest: ^^Token, tok: ^Token, params: ^Macro_Param, va_args_name: string) -> ^Macro_Arg {
  886. tok := tok
  887. start := tok
  888. tok = tok.next.next
  889. head: Macro_Arg
  890. curr := &head
  891. pp := params
  892. for ; pp != nil; pp = pp.next {
  893. if curr != &head {
  894. tok = skip(cpp, tok, ",")
  895. }
  896. curr.next = read_macro_arg_one(cpp, &tok, tok, false)
  897. curr = curr.next
  898. curr.name = pp.name
  899. }
  900. if va_args_name != "" {
  901. arg: ^Macro_Arg
  902. if tok.lit == ")" {
  903. arg = new(Macro_Arg)
  904. arg.tok = tokenizer.new_eof(tok)
  905. } else {
  906. if pp != params {
  907. tok = skip(cpp, tok, ",")
  908. }
  909. arg = read_macro_arg_one(cpp, &tok, tok, true)
  910. }
  911. arg.name = va_args_name
  912. arg.is_va_args = true
  913. curr.next = arg
  914. curr = curr.next
  915. } else if pp != nil {
  916. error(cpp, start, "too many arguments")
  917. }
  918. skip(cpp, tok, ")")
  919. rest^ = tok
  920. return head.next
  921. }
  922. expand_macro :: proc(cpp: ^Preprocessor, rest: ^^Token, tok: ^Token) -> bool {
  923. if tokenizer.hide_set_contains(tok.hide_set, tok.lit) {
  924. return false
  925. }
  926. tok := tok
  927. m := find_macro(cpp, tok)
  928. if m == nil {
  929. return false
  930. }
  931. if m.handler != nil {
  932. rest^ = m.handler(cpp, tok)
  933. rest^.next = tok.next
  934. return true
  935. }
  936. if m.kind == .Value_Like {
  937. hs := tokenizer.hide_set_union(tok.hide_set, tokenizer.new_hide_set(m.name))
  938. body := tokenizer.add_hide_set(m.body, hs)
  939. for t := body; t.kind != .EOF; t = t.next {
  940. t.origin = tok
  941. }
  942. rest^ = append_token(body, tok.next)
  943. rest^.at_bol = tok.at_bol
  944. rest^.has_space = tok.has_space
  945. return true
  946. }
  947. if tok.next.lit != "(" {
  948. return false
  949. }
  950. macro_token := tok
  951. args := read_macro_args(cpp, &tok, tok, m.params, m.va_args_name)
  952. close_paren := tok
  953. hs := tokenizer.hide_set_intersection(macro_token.hide_set, close_paren.hide_set)
  954. hs = tokenizer.hide_set_union(hs, tokenizer.new_hide_set(m.name))
  955. body := substitute_token(cpp, m.body, args)
  956. body = tokenizer.add_hide_set(body, hs)
  957. for t := body; t.kind != .EOF; t = t.next {
  958. t.origin = macro_token
  959. }
  960. rest^ = append_token(body, tok.next)
  961. rest^.at_bol = macro_token.at_bol
  962. rest^.has_space = macro_token.has_space
  963. return true
  964. }
  965. search_include_next :: proc(cpp: ^Preprocessor, filename: string) -> (path: string, ok: bool) {
  966. for ; cpp.include_next_index < len(cpp.include_paths); cpp.include_next_index += 1 {
  967. tpath := filepath.join({cpp.include_paths[cpp.include_next_index], filename}, allocator=context.temp_allocator)
  968. if os.exists(tpath) {
  969. return strings.clone(tpath), true
  970. }
  971. }
  972. return
  973. }
  974. search_include_paths :: proc(cpp: ^Preprocessor, filename: string) -> (path: string, ok: bool) {
  975. if filepath.is_abs(filename) {
  976. return filename, true
  977. }
  978. if path, ok = cpp.filepath_cache[filename]; ok {
  979. return
  980. }
  981. for include_path in cpp.include_paths {
  982. tpath := filepath.join({include_path, filename}, allocator=context.temp_allocator)
  983. if os.exists(tpath) {
  984. path, ok = strings.clone(tpath), true
  985. cpp.filepath_cache[filename] = path
  986. return
  987. }
  988. }
  989. return
  990. }
  991. read_const_expr :: proc(cpp: ^Preprocessor, rest: ^^Token, tok: ^Token) -> ^Token {
  992. tok := tok
  993. tok = copy_line(rest, tok)
  994. head: Token
  995. curr := &head
  996. for tok.kind != .EOF {
  997. if tok.lit == "defined" {
  998. start := tok
  999. has_paren := consume(&tok, tok.next, "(")
  1000. if tok.kind != .Ident {
  1001. error(cpp, start, "macro name must be an identifier")
  1002. }
  1003. m := find_macro(cpp, tok)
  1004. tok = tok.next
  1005. if has_paren {
  1006. tok = skip(cpp, tok, ")")
  1007. }
  1008. curr.next = new_number_token(cpp, 1 if m != nil else 0, start)
  1009. curr = curr.next
  1010. continue
  1011. }
  1012. curr.next = tok
  1013. curr = curr.next
  1014. tok = tok.next
  1015. }
  1016. curr.next = tok
  1017. return head.next
  1018. }
  1019. eval_const_expr :: proc(cpp: ^Preprocessor, rest: ^^Token, tok: ^Token) -> (val: i64) {
  1020. tok := tok
  1021. start := tok
  1022. expr := read_const_expr(cpp, rest, tok.next)
  1023. expr = preprocess_internal(cpp, expr)
  1024. if expr.kind == .EOF {
  1025. error(cpp, start, "no expression")
  1026. }
  1027. for t := expr; t.kind != .EOF; t = t.next {
  1028. if t.kind == .Ident {
  1029. next := t.next
  1030. t^ = new_number_token(cpp, 0, t)^
  1031. t.next = next
  1032. }
  1033. }
  1034. val = 1
  1035. convert_pp_tokens(cpp, expr, tokenizer.default_is_keyword)
  1036. rest2: ^Token
  1037. val = const_expr(&rest2, expr)
  1038. if rest2 != nil && rest2.kind != .EOF {
  1039. error(cpp, rest2, "extra token")
  1040. }
  1041. return
  1042. }
  1043. push_cond_incl :: proc(cpp: ^Preprocessor, tok: ^Token, included: bool) -> ^Cond_Incl {
  1044. ci := new(Cond_Incl)
  1045. ci.next = cpp.cond_incl
  1046. ci.state = .In_Then
  1047. ci.tok = tok
  1048. ci.included = included
  1049. cpp.cond_incl = ci
  1050. return ci
  1051. }
  1052. read_line_marker:: proc(cpp: ^Preprocessor, rest: ^^Token, tok: ^Token) {
  1053. tok := tok
  1054. start := tok
  1055. tok = preprocess(cpp, copy_line(rest, tok))
  1056. if tok.kind != .Number {
  1057. error(cpp, tok, "invalid line marker")
  1058. }
  1059. ival, _ := tok.val.(i64)
  1060. start.file.line_delta = int(ival - i64(start.pos.line))
  1061. tok = tok.next
  1062. if tok.kind == .EOF {
  1063. return
  1064. }
  1065. if tok.kind != .String {
  1066. error(cpp, tok, "filename expected")
  1067. }
  1068. start.file.display_name = tok.lit
  1069. }
  1070. preprocess_internal :: proc(cpp: ^Preprocessor, tok: ^Token) -> ^Token {
  1071. head: Token
  1072. curr := &head
  1073. tok := tok
  1074. for tok != nil && tok.kind != .EOF {
  1075. if expand_macro(cpp, &tok, tok) {
  1076. continue
  1077. }
  1078. if !is_hash(tok) {
  1079. if tok.file != nil {
  1080. tok.line_delta = tok.file.line_delta
  1081. }
  1082. curr.next = tok
  1083. curr = curr.next
  1084. tok = tok.next
  1085. continue
  1086. }
  1087. start := tok
  1088. tok = tok.next
  1089. switch tok.lit {
  1090. case "include":
  1091. filename, is_quote := read_include_filename(cpp, &tok, tok.next)
  1092. is_absolute := filepath.is_abs(filename)
  1093. if is_absolute {
  1094. tok = include_file(cpp, tok, filename, start.next.next)
  1095. continue
  1096. }
  1097. if is_quote {
  1098. dir := ""
  1099. if start.file != nil {
  1100. dir = filepath.dir(start.file.name)
  1101. }
  1102. path := filepath.join({dir, filename})
  1103. if os.exists(path) {
  1104. tok = include_file(cpp, tok, path, start.next.next)
  1105. continue
  1106. }
  1107. }
  1108. path, ok := search_include_paths(cpp, filename)
  1109. if !ok {
  1110. path = filename
  1111. }
  1112. tok = include_file(cpp, tok, path, start.next.next)
  1113. continue
  1114. case "include_next":
  1115. filename, _ := read_include_filename(cpp, &tok, tok.next)
  1116. path, ok := search_include_next(cpp, filename)
  1117. if !ok {
  1118. path = filename
  1119. }
  1120. tok = include_file(cpp, tok, path, start.next.next)
  1121. continue
  1122. case "define":
  1123. read_macro_definition(cpp, &tok, tok.next)
  1124. continue
  1125. case "undef":
  1126. tok = tok.next
  1127. if tok.kind != .Ident {
  1128. error(cpp, tok, "macro name must be an identifier")
  1129. }
  1130. undef_macro(cpp, tok.lit)
  1131. tok = skip_line(cpp, tok.next)
  1132. continue
  1133. case "if":
  1134. val := eval_const_expr(cpp, &tok, tok)
  1135. push_cond_incl(cpp, start, val != 0)
  1136. if val == 0 {
  1137. tok = skip_cond_incl(tok)
  1138. }
  1139. continue
  1140. case "ifdef":
  1141. defined := find_macro(cpp, tok.next)
  1142. push_cond_incl(cpp, tok, defined != nil)
  1143. tok = skip_line(cpp, tok.next.next)
  1144. if defined == nil {
  1145. tok = skip_cond_incl(tok)
  1146. }
  1147. continue
  1148. case "ifndef":
  1149. defined := find_macro(cpp, tok.next)
  1150. push_cond_incl(cpp, tok, defined != nil)
  1151. tok = skip_line(cpp, tok.next.next)
  1152. if !(defined == nil) {
  1153. tok = skip_cond_incl(tok)
  1154. }
  1155. continue
  1156. case "elif":
  1157. if cpp.cond_incl == nil || cpp.cond_incl.state == .In_Else {
  1158. error(cpp, start, "stray #elif")
  1159. }
  1160. if cpp.cond_incl != nil {
  1161. cpp.cond_incl.state = .In_Elif
  1162. }
  1163. if (cpp.cond_incl != nil && !cpp.cond_incl.included) && eval_const_expr(cpp, &tok, tok) != 0 {
  1164. cpp.cond_incl.included = true
  1165. } else {
  1166. tok = skip_cond_incl(tok)
  1167. }
  1168. continue
  1169. case "else":
  1170. if cpp.cond_incl == nil || cpp.cond_incl.state == .In_Else {
  1171. error(cpp, start, "stray #else")
  1172. }
  1173. if cpp.cond_incl != nil {
  1174. cpp.cond_incl.state = .In_Else
  1175. }
  1176. tok = skip_line(cpp, tok.next)
  1177. if cpp.cond_incl != nil {
  1178. tok = skip_cond_incl(tok)
  1179. }
  1180. continue
  1181. case "endif":
  1182. if cpp.cond_incl == nil {
  1183. error(cpp, start, "stray #endif")
  1184. } else {
  1185. cpp.cond_incl = cpp.cond_incl.next
  1186. }
  1187. tok = skip_line(cpp, tok.next)
  1188. continue
  1189. case "line":
  1190. read_line_marker(cpp, &tok, tok.next)
  1191. continue
  1192. case "pragma":
  1193. if tok.next.lit == "once" {
  1194. cpp.pragma_once[tok.pos.file] = true
  1195. tok = skip_line(cpp, tok.next.next)
  1196. continue
  1197. }
  1198. pragma_tok, pragma_end := tok, tok
  1199. for tok != nil && tok.kind != .EOF {
  1200. pragma_end = tok
  1201. tok = tok.next
  1202. if tok.at_bol {
  1203. break
  1204. }
  1205. }
  1206. pragma_end.next = tokenizer.new_eof(tok)
  1207. if cpp.pragma_handler != nil {
  1208. cpp.pragma_handler(cpp, pragma_tok.next)
  1209. continue
  1210. }
  1211. continue
  1212. case "error":
  1213. error(cpp, tok, "error")
  1214. }
  1215. if tok.kind == .PP_Number {
  1216. read_line_marker(cpp, &tok, tok)
  1217. continue
  1218. }
  1219. if !tok.at_bol {
  1220. error(cpp, tok, "invalid preprocessor directive")
  1221. }
  1222. }
  1223. curr.next = tok
  1224. return head.next
  1225. }
  1226. preprocess :: proc(cpp: ^Preprocessor, tok: ^Token) -> ^Token {
  1227. tok := tok
  1228. tok = preprocess_internal(cpp, tok)
  1229. if cpp.cond_incl != nil {
  1230. error(cpp, tok, "unterminated conditional directive")
  1231. }
  1232. convert_pp_tokens(cpp, tok, tokenizer.default_is_keyword)
  1233. join_adjacent_string_literals(cpp, tok)
  1234. for t := tok; t != nil; t = t.next {
  1235. t.pos.line += t.line_delta
  1236. }
  1237. return tok
  1238. }
  1239. define_macro :: proc(cpp: ^Preprocessor, name, def: string) {
  1240. src := transmute([]byte)def
  1241. file := new(tokenizer.File)
  1242. file.id = -1
  1243. file.src = src
  1244. file.name = "<built-in>"
  1245. file.display_name = file.name
  1246. t := _init_tokenizer_from_preprocessor(&Tokenizer{}, cpp)
  1247. tok := tokenizer.tokenize(t, file)
  1248. add_macro(cpp, name, .Value_Like, tok)
  1249. }
  1250. file_macro :: proc(cpp: ^Preprocessor, tok: ^Token) -> ^Token {
  1251. tok := tok
  1252. for tok.origin != nil {
  1253. tok = tok.origin
  1254. }
  1255. i := i64(tok.pos.line + tok.file.line_delta)
  1256. return new_number_token(cpp, i, tok)
  1257. }
  1258. line_macro :: proc(cpp: ^Preprocessor, tok: ^Token) -> ^Token {
  1259. tok := tok
  1260. for tok.origin != nil {
  1261. tok = tok.origin
  1262. }
  1263. return new_string_token(cpp, tok.file.display_name, tok)
  1264. }
  1265. counter_macro :: proc(cpp: ^Preprocessor, tok: ^Token) -> ^Token {
  1266. i := cpp.counter
  1267. cpp.counter += 1
  1268. return new_number_token(cpp, i, tok)
  1269. }
  1270. init_default_macros :: proc(cpp: ^Preprocessor) {
  1271. define_macro(cpp, "__C99_MACRO_WITH_VA_ARGS", "1")
  1272. define_macro(cpp, "__alignof__", "_Alignof")
  1273. define_macro(cpp, "__const__", "const")
  1274. define_macro(cpp, "__inline__", "inline")
  1275. define_macro(cpp, "__signed__", "signed")
  1276. define_macro(cpp, "__typeof__", "typeof")
  1277. define_macro(cpp, "__volatile__", "volatile")
  1278. add_builtin(cpp, "__FILE__", file_macro)
  1279. add_builtin(cpp, "__LINE__", line_macro)
  1280. add_builtin(cpp, "__COUNTER__", counter_macro)
  1281. }
  1282. init_lookup_tables :: proc(cpp: ^Preprocessor, allocator := context.allocator) {
  1283. context.allocator = allocator
  1284. reserve(&cpp.macros, max(16, cap(cpp.macros)))
  1285. reserve(&cpp.pragma_once, max(16, cap(cpp.pragma_once)))
  1286. reserve(&cpp.include_guards, max(16, cap(cpp.include_guards)))
  1287. reserve(&cpp.filepath_cache, max(16, cap(cpp.filepath_cache)))
  1288. }
  1289. init_defaults :: proc(cpp: ^Preprocessor, lookup_tables_allocator := context.allocator) {
  1290. if cpp.warn == nil {
  1291. cpp.warn = tokenizer.default_warn_handler
  1292. }
  1293. if cpp.err == nil {
  1294. cpp.err = tokenizer.default_error_handler
  1295. }
  1296. init_lookup_tables(cpp, lookup_tables_allocator)
  1297. init_default_macros(cpp)
  1298. }