Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
/*
 * File:	ElftosbAST.h
 *
 * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
 * See included license file for license details.
 */
#if !defined(_ElftosbAST_h_)
#define _ElftosbAST_h_

#include "stdafx.h"
#include <string>
#include <list>
#include "smart_ptr.h"
#include "EvalContext.h"

namespace elftosb
{

// forward reference
class SymbolASTNode;

/*!
 * \brief Token location in the source file.
 */
struct token_loc_t
{
	int m_firstLine;	//!< Starting line of the token.
	int m_lastLine;		//!< Ending line of the token.
};

/*!
 * \brief The base class for all AST node classes.
 */
class ASTNode
{
public:
	//! \brief Default constructor.
	ASTNode() : m_parent(0) {}
	
	//! \brief Constructor taking a parent node.
	ASTNode(ASTNode * parent) : m_parent(parent) {}
	
	//! \brief Copy constructor.
	ASTNode(const ASTNode & other) : m_parent(other.m_parent) {}
	
	//! \brief Destructor.
	virtual ~ASTNode() {}
	
	//! \brief Returns an exact duplicate of this object.
	virtual ASTNode * clone() const = 0;
	
	//! \brief Returns the name of the object's class.
	virtual std::string nodeName() const { return "ASTNode"; }

	//! \name Parents
	//@{
	virtual ASTNode * getParent() const { return m_parent; }
	virtual void setParent(ASTNode * newParent) { m_parent = newParent; }
	//@}
	
	//! \name Tree printing
	//@{
	virtual void printTree() const { printTree(0); }
	virtual void printTree(int indent) const;
	//@}
	
	//! \name Location
	//@{
	virtual void setLocation(token_loc_t & loc) { m_location = loc; }
	virtual void setLocation(token_loc_t & first, token_loc_t & last);
	virtual void setLocation(ASTNode * loc) { setLocation(loc->getLocation()); }
	virtual void setLocation(ASTNode * first, ASTNode * last);
	
	virtual token_loc_t & getLocation() { return m_location; }
	virtual const token_loc_t & getLocation() const { return m_location; }
	
	virtual int getFirstLine() { return m_location.m_firstLine; }
	virtual int getLastLine() { return m_location.m_lastLine; }
	//@}

protected:
	ASTNode * m_parent;	//!< Pointer to parent node of this object. May be NULL.
	token_loc_t m_location;	//!< Location of this node in the source file.
	
	//! \brief Prints \a indent number of spaces.
	void printIndent(int indent) const;
};

/*!
 * \brief AST node that contains other AST nodes.
 *
 * Unlike other AST nodes, the location of a ListASTNode is computed dynamically
 * based on the nodes in its list. Or mostly dynamic at least. The updateLocation()
 * method is used to force the list object to recalculate its beginning and ending
 * line numbers.
 *
 * \todo Figure out why it crashes in the destructor when the
 *       ast_list_t type is a list of smart_ptr<ASTNode>.
 */
class ListASTNode : public ASTNode
{
public:
	typedef std::list< /*smart_ptr<ASTNode>*/ ASTNode * > ast_list_t;	
	typedef ast_list_t::iterator iterator;
	typedef ast_list_t::const_iterator const_iterator;

public:
	ListASTNode() {}
	
	ListASTNode(const ListASTNode & other);
	
	virtual ~ListASTNode();
	
	virtual ASTNode * clone() const { return new ListASTNode(*this); }
	
	virtual std::string nodeName() const { return "ListASTNode"; }

	virtual void printTree(int indent) const;

	//! \name List operations
	//@{
	//! \brief Adds \a node to the end of the ordered list of child nodes.
	virtual void appendNode(ASTNode * node);
	
	//! \brief Returns the number of nodes in this list.
	virtual unsigned nodeCount() const { return static_cast<unsigned>(m_list.size()); }
	//@}

	//! \name Node iterators
	//@{
	inline iterator begin() { return m_list.begin(); }
	inline iterator end() { return m_list.end(); }

	inline const_iterator begin() const { return m_list.begin(); }
	inline const_iterator end() const { return m_list.end(); }
	//@}
	
	//! \name Location
	//@{
	virtual void updateLocation();
	//@}

protected:
	ast_list_t m_list;	//!< Ordered list of child nodes.
};

/*!
 *
 */
class OptionsBlockASTNode : public ASTNode
{
public:
	OptionsBlockASTNode(ListASTNode * options) : ASTNode(), m_options(options) {}
	
	inline ListASTNode * getOptions() { return m_options; }
	
	virtual ASTNode * clone() const { return NULL; }

protected:
	smart_ptr<ListASTNode> m_options;
};

/*!
 *
 */
class ConstantsBlockASTNode : public ASTNode
{
public:
	ConstantsBlockASTNode(ListASTNode * constants) : ASTNode(), m_constants(constants) {}
	
	inline ListASTNode * getConstants() { return m_constants; }
	
	virtual ASTNode * clone() const { return NULL; }
	
protected:
	smart_ptr<ListASTNode> m_constants;
};

/*!
 *
 */
class SourcesBlockASTNode : public ASTNode
{
public:
	SourcesBlockASTNode(ListASTNode * sources) : ASTNode(), m_sources(sources) {}
	
	inline ListASTNode * getSources() { return m_sources; }
	
	virtual ASTNode * clone() const { return NULL; }
	
protected:
	smart_ptr<ListASTNode> m_sources;
};

/*!
 * \brief Root node for the entire file.
 */
class CommandFileASTNode : public ASTNode
{
public:
	CommandFileASTNode();
	CommandFileASTNode(const CommandFileASTNode & other);
	
	virtual std::string nodeName() const { return "CommandFileASTNode"; }
	
	virtual ASTNode * clone() const { return new CommandFileASTNode(*this); }

	virtual void printTree(int indent) const;

	inline void setBlocks(ListASTNode * blocks) { m_blocks = blocks; }
	inline void setOptions(ListASTNode * options) { m_options = options; }
	inline void setConstants(ListASTNode * constants) { m_constants = constants; }
	inline void setSources(ListASTNode * sources) { m_sources = sources; }
	inline void setSections(ListASTNode * sections) { m_sections = sections; }
	
	inline ListASTNode * getBlocks() { return m_blocks; }
	inline ListASTNode * getOptions() { return m_options; }
	inline ListASTNode * getConstants() { return m_constants; }
	inline ListASTNode * getSources() { return m_sources; }
	inline ListASTNode * getSections() { return m_sections; }

protected:
	smart_ptr<ListASTNode> m_blocks;
	smart_ptr<ListASTNode> m_options;
	smart_ptr<ListASTNode> m_constants;
	smart_ptr<ListASTNode> m_sources;
	smart_ptr<ListASTNode> m_sections;
};

/*!
 * \brief Abstract base class for all expression AST nodes.
 */
class ExprASTNode : public ASTNode
{
public:
	ExprASTNode() : ASTNode() {}
	ExprASTNode(const ExprASTNode & other) : ASTNode(other) {}

	virtual std::string nodeName() const { return "ExprASTNode"; }

	//! \brief Evaluate the expression and produce a result node to replace this one.
	//!
	//! The default implementation simply return this node unmodified. This
	//! method is responsible for deleting any nodes that are no longer needed.
	//! (?) how to delete this?
	virtual ExprASTNode * reduce(EvalContext & context) { return this; }
	
	int_size_t resultIntSize(int_size_t a, int_size_t b);
};

/*!
 *
 */
class IntConstExprASTNode : public ExprASTNode
{
public:
	IntConstExprASTNode(uint32_t value, int_size_t size=kWordSize)
	:	ExprASTNode(), m_value(value), m_size(size)
	{
	}
	
	IntConstExprASTNode(const IntConstExprASTNode & other);

	virtual std::string nodeName() const { return "IntConstExprASTNode"; }
	
	virtual ASTNode * clone() const { return new IntConstExprASTNode(*this); }

	virtual void printTree(int indent) const;
	
	uint32_t getValue() const { return m_value; }
	int_size_t getSize() const { return m_size; }

protected:
	uint32_t m_value;
	int_size_t m_size;
};

/*!
 *
 */
class VariableExprASTNode : public ExprASTNode
{
public:
	VariableExprASTNode(std::string * name) : ExprASTNode(), m_variable(name) {}
	VariableExprASTNode(const VariableExprASTNode & other);
	
	inline std::string * getVariableName() { return m_variable; }
	
	virtual ASTNode * clone() const { return new VariableExprASTNode(*this); }
	
	virtual std::string nodeName() const { return "VariableExprASTNode"; }

	virtual void printTree(int indent) const;
	
	virtual ExprASTNode * reduce(EvalContext & context);
	
protected:
	smart_ptr<std::string> m_variable;
};

/*!
 * \brief Expression node for a symbol reference.
 *
 * The symbol evaluates to its address.
 */
class SymbolRefExprASTNode : public ExprASTNode
{
public:
	SymbolRefExprASTNode(SymbolASTNode * sym) : ExprASTNode(), m_symbol(sym) {}
	SymbolRefExprASTNode(const SymbolRefExprASTNode & other);
	
	virtual ASTNode * clone() const { return new SymbolRefExprASTNode(*this); }
	
	virtual std::string nodeName() const { return "SymbolRefExprASTNode"; }
	
	virtual void printTree(int indent) const;
	
	virtual ExprASTNode * reduce(EvalContext & context);
	
protected:
	SymbolASTNode * m_symbol;
};

/*!
 * \brief Negates an expression.
 */
class NegativeExprASTNode : public ExprASTNode
{
public:
	NegativeExprASTNode(ExprASTNode * expr) : ExprASTNode(), m_expr(expr) {}
	NegativeExprASTNode(const NegativeExprASTNode & other);

	virtual ASTNode * clone() const { return new NegativeExprASTNode(*this); }

	virtual std::string nodeName() const { return "NegativeExprASTNode"; }
	
	virtual void printTree(int indent) const;
	
	virtual ExprASTNode * reduce(EvalContext & context);
	
	ExprASTNode * getExpr() { return m_expr; }

protected:
	smart_ptr<ExprASTNode> m_expr;
};

/*!
 * \brief Performa a boolean inversion.
 */
class BooleanNotExprASTNode : public ExprASTNode
{
public:
	BooleanNotExprASTNode(ExprASTNode * expr) : ExprASTNode(), m_expr(expr) {}
	BooleanNotExprASTNode(const BooleanNotExprASTNode & other);

	virtual ASTNode * clone() const { return new BooleanNotExprASTNode(*this); }

	virtual std::string nodeName() const { return "BooleanNotExprASTNode"; }
	
	virtual void printTree(int indent) const;
	
	virtual ExprASTNode * reduce(EvalContext & context);
	
	ExprASTNode * getExpr() { return m_expr; }

protected:
	smart_ptr<ExprASTNode> m_expr;
};

/*!
 * \brief Calls a built-in function with a source as the parameter.
 */
class SourceFileFunctionASTNode : public ExprASTNode
{
public:
	SourceFileFunctionASTNode(std::string * functionName, std::string * sourceFileName) : ExprASTNode(), m_functionName(functionName), m_sourceFile(sourceFileName) {}
	SourceFileFunctionASTNode(const SourceFileFunctionASTNode & other);

	virtual ASTNode * clone() const { return new SourceFileFunctionASTNode(*this); }

	virtual std::string nodeName() const { return "SourceFileFunctionASTNode"; }
	
	virtual void printTree(int indent) const;
	
	virtual ExprASTNode * reduce(EvalContext & context);
	
	std::string * getFunctionName() { return m_functionName; }
	std::string * getSourceFile() { return m_sourceFile; }

protected:
	smart_ptr<std::string> m_functionName;
	smart_ptr<std::string> m_sourceFile;
};

/*!
 * \brief Returns true or false depending on whether a constant is defined.
 */
class DefinedOperatorASTNode : public ExprASTNode
{
public:
	DefinedOperatorASTNode(std::string * constantName) : ExprASTNode(), m_constantName(constantName) {}
	DefinedOperatorASTNode(const DefinedOperatorASTNode & other);

	virtual ASTNode * clone() const { return new DefinedOperatorASTNode(*this); }

	virtual std::string nodeName() const { return "DefinedOperatorASTNode"; }
	
	virtual void printTree(int indent) const;
	
	virtual ExprASTNode * reduce(EvalContext & context);
	
	std::string * getConstantName() { return m_constantName; }

protected:
	smart_ptr<std::string> m_constantName;	//!< Name of the constant.
};

class SymbolASTNode;

/*!
 * \brief Returns an integer that is the size in bytes of the operand.
 */
class SizeofOperatorASTNode : public ExprASTNode
{
public:
	SizeofOperatorASTNode(std::string * constantName) : ExprASTNode(), m_constantName(constantName), m_symbol() {}
	SizeofOperatorASTNode(SymbolASTNode * symbol) : ExprASTNode(), m_constantName(), m_symbol(symbol) {}
	SizeofOperatorASTNode(const SizeofOperatorASTNode & other);

	virtual ASTNode * clone() const { return new SizeofOperatorASTNode(*this); }

	virtual std::string nodeName() const { return "SizeofOperatorASTNode"; }
	
	virtual void printTree(int indent) const;
	
	virtual ExprASTNode * reduce(EvalContext & context);
	
	std::string * getConstantName() { return m_constantName; }
	SymbolASTNode * getSymbol() { return m_symbol; }

protected:
	smart_ptr<std::string> m_constantName;	//!< Name of the constant.
	smart_ptr<SymbolASTNode> m_symbol;	//!< Symbol reference. If this is non-NULL then the constant name is used instead.
};

/*!
 *
 */
class BinaryOpExprASTNode : public ExprASTNode
{
public:
	enum operator_t
	{
		kAdd,
		kSubtract,
		kMultiply,
		kDivide,
		kModulus,
		kPower,
		kBitwiseAnd,
		kBitwiseOr,
		kBitwiseXor,
		kShiftLeft,
		kShiftRight,
		kLessThan,
		kGreaterThan,
		kLessThanEqual,
		kGreaterThanEqual,
		kEqual,
		kNotEqual,
		kBooleanAnd,
		kBooleanOr
	};
	
	BinaryOpExprASTNode(ExprASTNode * left, operator_t op, ExprASTNode * right)
	:	ExprASTNode(), m_left(left), m_op(op), m_right(right)
	{
	}
	
	BinaryOpExprASTNode(const BinaryOpExprASTNode & other);
	
	virtual ASTNode * clone() const { return new BinaryOpExprASTNode(*this); }

	virtual std::string nodeName() const { return "BinaryOpExprASTNode"; }
	
	virtual void printTree(int indent) const;
	
	virtual ExprASTNode * reduce(EvalContext & context);
	
	ExprASTNode * getLeftExpr() { return m_left; }
	ExprASTNode * getRightExpr() { return m_right; }
	operator_t getOp() const { return m_op; }

protected:
	smart_ptr<ExprASTNode> m_left;
	smart_ptr<ExprASTNode> m_right;
	operator_t m_op;
	
	std::string getOperatorName() const;
};

/*!
 * \brief Negates an expression.
 */
class IntSizeExprASTNode : public ExprASTNode
{
public:
	IntSizeExprASTNode(ExprASTNode * expr, int_size_t intSize) : ExprASTNode(), m_expr(expr), m_size(intSize) {}
	IntSizeExprASTNode(const IntSizeExprASTNode & other);
	
	virtual ASTNode * clone() const { return new IntSizeExprASTNode(*this); }

	virtual std::string nodeName() const { return "IntSizeExprASTNode"; }
	
	virtual void printTree(int indent) const;
	
	virtual ExprASTNode * reduce(EvalContext & context);
	
	ExprASTNode * getExpr() { return m_expr; }
	int_size_t getIntSize() { return m_size; }

protected:
	smart_ptr<ExprASTNode> m_expr;
	int_size_t m_size;
};

/*!
 * Base class for const AST nodes.
 */
class ConstASTNode : public ASTNode
{
public:
	ConstASTNode() : ASTNode() {}
	ConstASTNode(const ConstASTNode & other) : ASTNode(other) {}

protected:
};

/*!
 *
 */
class ExprConstASTNode : public ConstASTNode
{
public:
	ExprConstASTNode(ExprASTNode * expr) : ConstASTNode(), m_expr(expr) {}
	ExprConstASTNode(const ExprConstASTNode & other);
	
	virtual ASTNode * clone() const { return new ExprConstASTNode(*this); }

	virtual std::string nodeName() const { return "ExprConstASTNode"; }
	
	virtual void printTree(int indent) const;
	
	ExprASTNode * getExpr() { return m_expr; }

protected:
	smart_ptr<ExprASTNode> m_expr;
};

/*!
 *
 */
class StringConstASTNode : public ConstASTNode
{
public:
	StringConstASTNode(std::string * value) : ConstASTNode(), m_value(value) {}
	StringConstASTNode(const StringConstASTNode & other);
	
	virtual ASTNode * clone() const { return new StringConstASTNode(*this); }

	virtual std::string nodeName() const { return "StringConstASTNode"; }
	
	virtual void printTree(int indent) const;

	std::string * getString() { return m_value; }

protected:
	smart_ptr<std::string> m_value;
};

/*!
 *
 */
class BlobConstASTNode : public ConstASTNode
{
public:
	BlobConstASTNode(Blob * value) : ConstASTNode(), m_blob(value) {}
	BlobConstASTNode(const BlobConstASTNode & other);
	
	virtual ASTNode * clone() const { return new BlobConstASTNode(*this); }

	virtual std::string nodeName() const { return "BlobConstASTNode"; }
	
	virtual void printTree(int indent) const;

	Blob * getBlob() { return m_blob; }

protected:
	smart_ptr<Blob> m_blob;
};

// Forward declaration.
struct hab_ivt;

/*!
 * \brief Node for a constant IVT structure as used by HAB4.
 */
class IVTConstASTNode : public ConstASTNode
{
public:
    IVTConstASTNode() : ConstASTNode(), m_fields() {}
    IVTConstASTNode(const IVTConstASTNode & other);
    
	virtual ASTNode * clone() const { return new IVTConstASTNode(*this); }
    
	virtual std::string nodeName() const { return "IVTConstASTNode"; }

	virtual void printTree(int indent) const;
    
    void setFieldAssignments(ListASTNode * fields) { m_fields = fields; }
    ListASTNode * getFieldAssignments() { return m_fields; }

protected:
    //! Fields of the IVT are set through assignment statements.
    smart_ptr<ListASTNode> m_fields;
};

/*!
 *
 */
class AssignmentASTNode : public ASTNode
{
public:
	AssignmentASTNode(std::string * ident, ASTNode * value)
	:	ASTNode(), m_ident(ident), m_value(value)
	{
	}
	
	AssignmentASTNode(const AssignmentASTNode & other);
	
	virtual ASTNode * clone() const { return new AssignmentASTNode(*this); }

	virtual std::string nodeName() const { return "AssignmentASTNode"; }
	
	virtual void printTree(int indent) const;
	
	inline std::string * getIdent() { return m_ident; }
	inline ASTNode * getValue() { return m_value; }

protected:
	smart_ptr<std::string> m_ident;
	smart_ptr<ASTNode> m_value;
};

/*!
 * Base class for PathSourceDefASTNode and ExternSourceDefASTNode.
 */
class SourceDefASTNode : public ASTNode
{
public:
	SourceDefASTNode(std::string * name) : m_name(name) {}
	SourceDefASTNode(const SourceDefASTNode & other);
	
	inline std::string * getName() { return m_name; }

	inline void setAttributes(ListASTNode * attributes) { m_attributes = attributes; }
	inline ListASTNode * getAttributes() { return m_attributes; }
	
protected:
	smart_ptr<std::string> m_name;
	smart_ptr<ListASTNode> m_attributes;
};

/*!
 *
 */
class PathSourceDefASTNode : public SourceDefASTNode
{
public:
	PathSourceDefASTNode(std::string * name, std::string * path)
	:	SourceDefASTNode(name), m_path(path)
	{
	}
	
	PathSourceDefASTNode(const PathSourceDefASTNode & other);
	
	virtual PathSourceDefASTNode * clone() const { return new PathSourceDefASTNode(*this); }

	virtual std::string nodeName() const { return "PathSourceDefASTNode"; }
	
	virtual void printTree(int indent) const;
	
	std::string * getPath() { return m_path; }
	
protected:
	smart_ptr<std::string> m_path;
};

/*!
 *
 */
class ExternSourceDefASTNode : public SourceDefASTNode
{
public:
	ExternSourceDefASTNode(std::string * name, ExprASTNode * expr)
	:	SourceDefASTNode(name), m_expr(expr)
	{
	}
	
	ExternSourceDefASTNode(const ExternSourceDefASTNode & other);
	
	virtual ASTNode * clone() const { return new ExternSourceDefASTNode(*this); }

	virtual std::string nodeName() const { return "ExternSourceDefASTNode"; }
	
	virtual void printTree(int indent) const;
	
	ExprASTNode * getSourceNumberExpr() { return m_expr; }

protected:
	smart_ptr<ExprASTNode> m_expr;
};

/*!
 *
 */
class SectionContentsASTNode : public ASTNode
{
public:
	SectionContentsASTNode() : m_sectionExpr() {}
	SectionContentsASTNode(ExprASTNode * section) : m_sectionExpr(section) {}
	SectionContentsASTNode(const SectionContentsASTNode & other);
	
	virtual ASTNode * clone() const { return new SectionContentsASTNode(*this); }

	virtual std::string nodeName() const { return "SectionContentsASTNode"; }
	
	virtual void printTree(int indent) const;
	
	inline void setSectionNumberExpr(ExprASTNode * section)
	{
		m_sectionExpr = section;
	}
	
	inline ExprASTNode * getSectionNumberExpr()
	{
		return m_sectionExpr;
	}
	
	inline void setOptions(ListASTNode * options)
	{
		m_options = options;
	}
	
	inline ListASTNode * getOptions()
	{
		return m_options;
	}

protected:
	smart_ptr<ExprASTNode> m_sectionExpr;
	smart_ptr<ListASTNode> m_options;
};

/*!
 * @brief Node representing a raw binary section definition.
 */
class DataSectionContentsASTNode : public SectionContentsASTNode
{
public:
	DataSectionContentsASTNode(ASTNode * contents)
	:	SectionContentsASTNode(), m_contents(contents)
	{
	}
	
	DataSectionContentsASTNode(const DataSectionContentsASTNode & other);
	
	virtual ASTNode * clone() const { return new DataSectionContentsASTNode(*this); }

	virtual std::string nodeName() const { return "DataSectionContentsASTNode"; }
	
	virtual void printTree(int indent) const;
	
	ASTNode * getContents() { return m_contents; }
	
protected:
	smart_ptr<ASTNode> m_contents;
};

/*!
 *
 */
class BootableSectionContentsASTNode : public SectionContentsASTNode
{
public:
	BootableSectionContentsASTNode(ListASTNode * statements)
	:	SectionContentsASTNode(), m_statements(statements)
	{
	}
	
	BootableSectionContentsASTNode(const BootableSectionContentsASTNode & other);
	
	virtual ASTNode * clone() const { return new BootableSectionContentsASTNode(*this); }

	virtual std::string nodeName() const { return "BootableSectionContentsASTNode"; }
	
	virtual void printTree(int indent) const;
	
	ListASTNode * getStatements() { return m_statements; }

protected:
	smart_ptr<ListASTNode> m_statements;
};

/*!
 *
 */
class StatementASTNode : public ASTNode
{
public:
	StatementASTNode() : ASTNode() {}
	StatementASTNode(const StatementASTNode & other) : ASTNode(other) {}

protected:
};

/*!
 *
 */
class IfStatementASTNode : public StatementASTNode
{
public:
	IfStatementASTNode() : StatementASTNode(), m_ifStatements(), m_nextIf(), m_elseStatements() {}
	IfStatementASTNode(const IfStatementASTNode & other);
	
	virtual ASTNode * clone() const { return new IfStatementASTNode(*this); }
	
	void setConditionExpr(ExprASTNode * expr) { m_conditionExpr = expr; }
	ExprASTNode * getConditionExpr() { return m_conditionExpr; }
	
	void setIfStatements(ListASTNode * statements) { m_ifStatements = statements; }
	ListASTNode * getIfStatements() { return m_ifStatements; }
	
	void setNextIf(IfStatementASTNode * nextIf) { m_nextIf = nextIf; }
	IfStatementASTNode * getNextIf() { return m_nextIf; }
	
	void setElseStatements(ListASTNode * statements) { m_elseStatements = statements; }
	ListASTNode * getElseStatements() { return m_elseStatements; }

protected:
	smart_ptr<ExprASTNode> m_conditionExpr;	//!< Boolean expression.
	smart_ptr<ListASTNode> m_ifStatements;	//!< List of "if" section statements.
	smart_ptr<IfStatementASTNode> m_nextIf;	//!< Link to next "else if". If this is non-NULL then #m_elseStatements must be NULL and vice-versa.
	smart_ptr<ListASTNode> m_elseStatements;	//!< Statements for the "else" part of the statements.
};

/*!
 * \brief Statement to insert a ROM_MODE_CMD command.
 */
class ModeStatementASTNode : public StatementASTNode
{
public:
	ModeStatementASTNode() : StatementASTNode(), m_modeExpr() {}
	ModeStatementASTNode(ExprASTNode * modeExpr) : StatementASTNode(), m_modeExpr(modeExpr) {}
	ModeStatementASTNode(const ModeStatementASTNode & other);
	
	virtual ASTNode * clone() const { return new ModeStatementASTNode(*this); }
	
	virtual std::string nodeName() const { return "ModeStatementASTNode"; }
	
	virtual void printTree(int indent) const;
	
	inline void setModeExpr(ExprASTNode * modeExpr) { m_modeExpr = modeExpr; }
	inline ExprASTNode * getModeExpr() { return m_modeExpr; }

protected:
	smart_ptr<ExprASTNode> m_modeExpr;	//!< Expression that evaluates to the new boot mode.
};

/*!
 * \brief Statement to print a message to the elftosb user.
 */
class MessageStatementASTNode : public StatementASTNode
{
public:
	enum _message_type
	{
		kInfo,	//!< Prints an informational messag to the user.
		kWarning,	//!< Prints a warning to the user.
		kError	//!< Throws an error exception.
	};
	
	typedef enum _message_type message_type_t;
	
public:
	MessageStatementASTNode(message_type_t messageType, std::string * message) : StatementASTNode(), m_type(messageType), m_message(message) {}
	MessageStatementASTNode(const MessageStatementASTNode & other);
	
	virtual ASTNode * clone() const { return new MessageStatementASTNode(*this); }
	
	virtual std::string nodeName() const { return "MessageStatementASTNode"; }
	
	virtual void printTree(int indent) const;
	
	inline message_type_t getType() { return m_type; }
	inline std::string * getMessage() { return m_message; }
	
	const char * getTypeName() const;

protected:
	message_type_t m_type;
	smart_ptr<std::string> m_message;	//!< Message to report.
};

/*!
 * \brief AST node for a load statement.
 */
class LoadStatementASTNode : public StatementASTNode
{
public:
	LoadStatementASTNode()
	:	StatementASTNode(), m_data(), m_target(), m_isDCDLoad(false)
	{
	}
	
	LoadStatementASTNode(ASTNode * data, ASTNode * target)
	:	StatementASTNode(), m_data(data), m_target(), m_isDCDLoad(false)
	{
	}
	
	LoadStatementASTNode(const LoadStatementASTNode & other);
	
	virtual ASTNode * clone() const { return new LoadStatementASTNode(*this); }

	virtual std::string nodeName() const { return "LoadStatementASTNode"; }
	
	virtual void printTree(int indent) const;
	
	inline void setData(ASTNode * data) { m_data = data; }
	inline ASTNode * getData() { return m_data; }
	
	inline void setTarget(ASTNode * target) { m_target = target; }
	inline ASTNode * getTarget() { return m_target; }
	
	inline void setDCDLoad(bool isDCD) { m_isDCDLoad = isDCD; }
	inline bool isDCDLoad() const { return m_isDCDLoad; }
	
protected:
	smart_ptr<ASTNode> m_data;
	smart_ptr<ASTNode> m_target;
	bool m_isDCDLoad;
};

/*!
 * \brief AST node for a call statement.
 */
class CallStatementASTNode : public StatementASTNode
{
public:
	//! Possible sub-types of call statements.
	typedef enum {
		kCallType,
		kJumpType
	} call_type_t;
	
public:
	CallStatementASTNode(call_type_t callType=kCallType)
	:	StatementASTNode(), m_type(callType), m_target(), m_arg(), m_isHAB(false)
	{
	}
	
	CallStatementASTNode(call_type_t callType, ASTNode * target, ASTNode * arg)
	:	StatementASTNode(), m_type(callType), m_target(target), m_arg(arg), m_isHAB(false)
	{
	}
	
	CallStatementASTNode(const CallStatementASTNode & other);
	
	virtual ASTNode * clone() const { return new CallStatementASTNode(*this); }

	virtual std::string nodeName() const { return "CallStatementASTNode"; }
	
	virtual void printTree(int indent) const;
	
	inline void setCallType(call_type_t callType) { m_type = callType; }
	inline call_type_t getCallType() { return m_type; }
	
	inline void setTarget(ASTNode * target) { m_target = target; }
	inline ASTNode * getTarget() { return m_target; }
	
	inline void setArgument(ASTNode * arg) { m_arg = arg; }
	inline ASTNode * getArgument() { return m_arg; }
	
	inline void setIsHAB(bool isHAB) { m_isHAB = isHAB; }
	inline bool isHAB() const { return m_isHAB; }
	
protected:
	call_type_t m_type;
	smart_ptr<ASTNode> m_target;	//!< This becomes the IVT address in HAB mode.
	smart_ptr<ASTNode> m_arg;
	bool m_isHAB;
};

/*!
 *
 */
class SourceASTNode : public ASTNode
{
public:
	SourceASTNode(std::string * name) : ASTNode(), m_name(name) {}
	SourceASTNode(const SourceASTNode & other);
	
	virtual ASTNode * clone() const { return new SourceASTNode(*this); }

	virtual std::string nodeName() const { return "SourceASTNode"; }
	
	virtual void printTree(int indent) const;
	
	inline std::string * getSourceName() { return m_name; }
	
protected:
	smart_ptr<std::string> m_name;
};

/*!
 * \brief List of section matches for a particular source name.
 */
class SectionMatchListASTNode : public ASTNode
{
public:
	SectionMatchListASTNode(ListASTNode * sections)
	:	ASTNode(), m_sections(sections), m_source()
	{
	}
	
	SectionMatchListASTNode(ListASTNode * sections, std::string * source)
	:	ASTNode(), m_sections(sections), m_source(source)
	{
	}
	
	SectionMatchListASTNode(const SectionMatchListASTNode & other);
	
	virtual ASTNode * clone() const { return new SectionMatchListASTNode(*this); }

	virtual std::string nodeName() const { return "SectionMatchListASTNode"; }
	
	virtual void printTree(int indent) const;
	
	inline ListASTNode * getSections() { return m_sections; }
	inline std::string * getSourceName() { return m_source; }
	
protected:
	smart_ptr<ListASTNode> m_sections;
	smart_ptr<std::string> m_source;
};

/*!
 * \brief AST node for a section glob.
 *
 * Can be assigned an include or exclude action for when this node is part of a
 * SectionMatchListASTNode.
 */
class SectionASTNode : public ASTNode
{
public:
	//! Possible actions for a section match list.
	typedef enum
	{
		kInclude,	//!< Include sections matched by this node.
		kExclude	//!< Exclude sections matched by this node.
	} match_action_t;
	
public:
	SectionASTNode(std::string * name)
	:	ASTNode(), m_action(kInclude), m_name(name), m_source()
	{
	}
	
	SectionASTNode(std::string * name, match_action_t action)
	:	ASTNode(), m_action(action), m_name(name), m_source()
	{
	}
	
	SectionASTNode(std::string * name, std::string * source)
	:	ASTNode(), m_action(kInclude), m_name(name), m_source(source)
	{
	}
	
	SectionASTNode(const SectionASTNode & other);
	
	virtual ASTNode * clone() const { return new SectionASTNode(*this); }

	virtual std::string nodeName() const { return "SectionASTNode"; }
	
	virtual void printTree(int indent) const;
	
	inline match_action_t getAction() { return m_action; }
	inline std::string * getSectionName() { return m_name; }
	inline std::string * getSourceName() { return m_source; }
	
protected:
	match_action_t m_action;
	smart_ptr<std::string> m_name;
	smart_ptr<std::string> m_source;
};

/*!
 *
 */
class SymbolASTNode : public ASTNode
{
public:
	SymbolASTNode()
	:	ASTNode(), m_symbol(), m_source()
	{
	}

	SymbolASTNode(std::string * symbol, std::string * source=0)
	:	ASTNode(), m_symbol(symbol), m_source(source)
	{
	}
	
	SymbolASTNode(const SymbolASTNode & other);
	
	virtual ASTNode * clone() const { return new SymbolASTNode(*this); }

	virtual std::string nodeName() const { return "SymbolASTNode"; }
	
	virtual void printTree(int indent) const;
	
	inline void setSymbolName(std::string * symbol) { m_symbol = symbol; }
	inline std::string * getSymbolName() { return m_symbol; }
	
	inline void setSource(std::string * source) { m_source = source; }
	inline std::string * getSource() { return m_source; }

protected:
	smart_ptr<std::string> m_symbol;	//!< Required.
	smart_ptr<std::string> m_source;	//!< Optional, may be NULL;
};

/*!
 * If the end of the range is NULL, then only a single address was specified.
 */
class AddressRangeASTNode : public ASTNode
{
public:
	AddressRangeASTNode()
	:	ASTNode(), m_begin(), m_end()
	{
	}
	
	AddressRangeASTNode(ASTNode * begin, ASTNode * end)
	:	ASTNode(), m_begin(begin), m_end(end)
	{
	}
	
	AddressRangeASTNode(const AddressRangeASTNode & other);
	
	virtual ASTNode * clone() const { return new AddressRangeASTNode(*this); }

	virtual std::string nodeName() const { return "AddressRangeASTNode"; }
	
	virtual void printTree(int indent) const;
	
	inline void setBegin(ASTNode * begin) { m_begin = begin; }
	inline ASTNode * getBegin() { return m_begin; }
	
	inline void setEnd(ASTNode * end) { m_end = end; }
	inline ASTNode * getEnd() { return m_end; }
	
protected:
	smart_ptr<ASTNode> m_begin;
	smart_ptr<ASTNode> m_end;
};

/*!
 *
 */
class NaturalLocationASTNode : public ASTNode
{
public:
	NaturalLocationASTNode()
	:	ASTNode()
	{
	}
	
	NaturalLocationASTNode(const NaturalLocationASTNode & other)
	:	ASTNode(other)
	{
	}
	
	virtual ASTNode * clone() const { return new NaturalLocationASTNode(*this); }

	virtual std::string nodeName() const { return "NaturalLocationASTNode"; }
};

/*!
 *
 */
class FromStatementASTNode : public StatementASTNode
{
public:
	FromStatementASTNode() : StatementASTNode() {}
	FromStatementASTNode(std::string * source, ListASTNode * statements);
	FromStatementASTNode(const FromStatementASTNode & other);
	
	virtual ASTNode * clone() const { return new FromStatementASTNode(*this); }

	virtual std::string nodeName() const { return "FromStatementASTNode"; }
	
	virtual void printTree(int indent) const;
	
	inline void setSourceName(std::string * source) { m_source = source; }
	inline std::string * getSourceName() { return m_source; }
	
	inline void setStatements(ListASTNode * statements) { m_statements = statements; }
	inline ListASTNode * getStatements() { return m_statements; }

protected:
	smart_ptr<std::string> m_source;
	smart_ptr<ListASTNode> m_statements;
};

}; // namespace elftosb

#endif // _ElftosbAST_h_